13
13
14
14
package org .codehaus .plexus .components .secdispatcher .internal ;
15
15
16
- import java .io .OutputStream ;
16
+ import java .io .IOException ;
17
17
import java .nio .charset .StandardCharsets ;
18
18
import java .nio .file .Files ;
19
19
import java .nio .file .Path ;
20
20
import java .nio .file .Paths ;
21
- import java .util .Map ;
22
21
23
- import org .codehaus .plexus .components .secdispatcher .SecDispatcher ;
24
- import org .codehaus .plexus .components .secdispatcher .SecDispatcherException ;
25
22
import org .codehaus .plexus .components .secdispatcher .model .Config ;
26
23
import org .codehaus .plexus .components .secdispatcher .model .ConfigProperty ;
27
24
import org .codehaus .plexus .components .secdispatcher .model .SettingsSecurity ;
28
- import org .codehaus .plexus .components .secdispatcher .model .io .stax .SecurityConfigurationStaxWriter ;
29
25
import org .junit .jupiter .api .BeforeEach ;
30
26
import org .junit .jupiter .api .Test ;
31
27
32
28
import static org .junit .jupiter .api .Assertions .assertEquals ;
33
29
import static org .junit .jupiter .api .Assertions .assertNotNull ;
34
- import static org .junit .jupiter .api .Assertions .assertThrows ;
35
30
import static org .junit .jupiter .api .Assertions .assertTrue ;
36
31
37
32
/**
@@ -46,90 +41,49 @@ public class SecUtilTest {
46
41
String _propName = "pname" ;
47
42
String _propVal = "pval" ;
48
43
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 );
51
46
}
52
47
53
- private void saveSec (String path , String masterSource ) throws Exception {
48
+ private void saveSec (String path , String masterSource ) throws IOException {
54
49
SettingsSecurity sec = new SettingsSecurity ();
55
-
56
- sec .setModelEncoding (StandardCharsets .UTF_8 .name ());
57
- sec .setModelVersion (SecDispatcher .class .getPackage ().getSpecificationVersion ());
58
- sec .setRelocation (null );
59
50
sec .setMasterSource (masterSource );
60
-
61
51
ConfigProperty cp = new ConfigProperty ();
62
52
cp .setName (_propName );
63
53
cp .setValue (_propVal );
64
-
65
54
Config conf = new Config ();
66
55
conf .setName (_confName );
67
56
conf .addProperty (cp );
68
-
69
57
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 );
74
59
}
75
60
76
61
@ 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 {
86
63
saveSec ("magic:mighty" );
87
64
}
88
65
89
66
@ 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 );
112
75
}
113
76
114
77
@ 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" )));
134
88
}
135
89
}
0 commit comments