1313
1414package org .codehaus .plexus .components .secdispatcher .internal ;
1515
16- import java .io .OutputStream ;
16+ import java .io .IOException ;
1717import java .nio .charset .StandardCharsets ;
1818import java .nio .file .Files ;
1919import java .nio .file .Path ;
2020import java .nio .file .Paths ;
21- import java .util .Map ;
2221
23- import org .codehaus .plexus .components .secdispatcher .SecDispatcher ;
24- import org .codehaus .plexus .components .secdispatcher .SecDispatcherException ;
2522import org .codehaus .plexus .components .secdispatcher .model .Config ;
2623import org .codehaus .plexus .components .secdispatcher .model .ConfigProperty ;
2724import org .codehaus .plexus .components .secdispatcher .model .SettingsSecurity ;
28- import org .codehaus .plexus .components .secdispatcher .model .io .stax .SecurityConfigurationStaxWriter ;
2925import org .junit .jupiter .api .BeforeEach ;
3026import org .junit .jupiter .api .Test ;
3127
3228import static org .junit .jupiter .api .Assertions .assertEquals ;
3329import static org .junit .jupiter .api .Assertions .assertNotNull ;
34- import static org .junit .jupiter .api .Assertions .assertThrows ;
3530import static org .junit .jupiter .api .Assertions .assertTrue ;
3631
3732/**
@@ -46,90 +41,49 @@ public class SecUtilTest {
4641 String _propName = "pname" ;
4742 String _propVal = "pval" ;
4843
49- private void saveSec (String masterSource ) throws Exception {
50- saveSec ("./target/sec1 .xml" , masterSource );
44+ private void saveSec (String masterSource ) throws IOException {
45+ saveSec ("./target/sec .xml" , masterSource );
5146 }
5247
53- private void saveSec (String path , String masterSource ) throws Exception {
48+ private void saveSec (String path , String masterSource ) throws IOException {
5449 SettingsSecurity sec = new SettingsSecurity ();
55-
56- sec .setModelEncoding (StandardCharsets .UTF_8 .name ());
57- sec .setModelVersion (SecDispatcher .class .getPackage ().getSpecificationVersion ());
58- sec .setRelocation (null );
5950 sec .setMasterSource (masterSource );
60-
6151 ConfigProperty cp = new ConfigProperty ();
6252 cp .setName (_propName );
6353 cp .setValue (_propVal );
64-
6554 Config conf = new Config ();
6655 conf .setName (_confName );
6756 conf .addProperty (cp );
68-
6957 sec .addConfiguration (conf );
70-
71- try (OutputStream fos = Files .newOutputStream (Paths .get (path ))) {
72- new SecurityConfigurationStaxWriter ().write (fos , sec );
73- }
58+ SecUtil .write (Paths .get (path ), sec );
7459 }
7560
7661 @ BeforeEach
77- public void prepare () throws Exception {
78- System .setProperty (DefaultSecDispatcher .SYSTEM_PROPERTY_CONFIGURATION_LOCATION , "./target/sec.xml" );
79- SettingsSecurity sec = new SettingsSecurity ();
80- sec .setModelEncoding (StandardCharsets .UTF_8 .name ());
81- sec .setModelVersion (SecDispatcher .class .getPackage ().getSpecificationVersion ());
82- sec .setRelocation ("sec1.xml" );
83- try (OutputStream fos = Files .newOutputStream (Paths .get ("./target/sec.xml" ))) {
84- new SecurityConfigurationStaxWriter ().write (fos , sec );
85- }
62+ void prepare () throws IOException {
8663 saveSec ("magic:mighty" );
8764 }
8865
8966 @ Test
90- void testReadWithRelocation () throws Exception {
91- SettingsSecurity sec = SecUtil .read (Paths .get ("./target/sec.xml" ), true );
92- assertNotNull (sec );
93- assertEquals ("magic:mighty" , sec .getMasterSource ());
94- Map <String , String > conf = SecUtil .getConfig (sec , _confName );
95- assertNotNull (conf );
96- assertNotNull (conf .get (_propName ));
97- assertEquals (_propVal , conf .get (_propName ));
98- }
99-
100- @ Test
101- void testReadWithRelocationCycleSelf () throws Exception {
102- Path sec1 = Paths .get ("./target/sec-cycle-1.xml" );
103- SettingsSecurity s1 = new SettingsSecurity ();
104- s1 .setModelEncoding (StandardCharsets .UTF_8 .name ());
105- s1 .setModelVersion (SecDispatcher .class .getPackage ().getSpecificationVersion ());
106- s1 .setRelocation ("sec-cycle-1.xml" );
107- try (OutputStream fos = Files .newOutputStream (sec1 )) {
108- new SecurityConfigurationStaxWriter ().write (fos , s1 );
109- }
110- SecDispatcherException ex = assertThrows (SecDispatcherException .class , () -> SecUtil .read (sec1 , true ));
111- assertTrue (ex .getMessage ().contains ("cycle" ));
67+ void readWrite () throws IOException {
68+ Path path = Path .of ("./target/sec.xml" );
69+ SettingsSecurity config = SecUtil .read (path );
70+ assertNotNull (config );
71+ assertEquals (SettingsSecurity .class .getPackage ().getSpecificationVersion (), config .getModelVersion ());
72+ assertEquals (StandardCharsets .UTF_8 .name (), config .getModelEncoding ());
73+ assertEquals ("magic:mighty" , config .getMasterSource ());
74+ SecUtil .write (path , config );
11275 }
11376
11477 @ Test
115- void testReadWithRelocationCycle () throws Exception {
116- Path sec1 = Paths .get ("./target/sec-cycle-1.xml" );
117- Path sec2 = Paths .get ("./target/sec-cycle-2.xml" );
118- SettingsSecurity s1 = new SettingsSecurity ();
119- s1 .setModelEncoding (StandardCharsets .UTF_8 .name ());
120- s1 .setModelVersion (SecDispatcher .class .getPackage ().getSpecificationVersion ());
121- s1 .setRelocation ("sec-cycle-2.xml" );
122- try (OutputStream fos = Files .newOutputStream (sec1 )) {
123- new SecurityConfigurationStaxWriter ().write (fos , s1 );
124- }
125- SettingsSecurity s2 = new SettingsSecurity ();
126- s2 .setModelEncoding (StandardCharsets .UTF_8 .name ());
127- s2 .setModelVersion (SecDispatcher .class .getPackage ().getSpecificationVersion ());
128- s2 .setRelocation ("sec-cycle-1.xml" );
129- try (OutputStream fos = Files .newOutputStream (sec1 )) {
130- new SecurityConfigurationStaxWriter ().write (fos , s2 );
131- }
132- SecDispatcherException ex = assertThrows (SecDispatcherException .class , () -> SecUtil .read (sec1 , true ));
133- assertTrue (ex .getMessage ().contains ("cycle" ));
78+ void readWriteWithBackup () throws IOException {
79+ Path path = Path .of ("./target/sec.xml" );
80+ SettingsSecurity config = SecUtil .read (path );
81+ assertNotNull (config );
82+ assertEquals (SettingsSecurity .class .getPackage ().getSpecificationVersion (), config .getModelVersion ());
83+ assertEquals (StandardCharsets .UTF_8 .name (), config .getModelEncoding ());
84+ assertEquals ("magic:mighty" , config .getMasterSource ());
85+ SecUtil .writeWithBackup (path , config );
86+ assertTrue (Files .exists (path ));
87+ assertTrue (Files .exists (path .getParent ().resolve (path .getFileName () + ".bak" )));
13488 }
13589}
0 commit comments