20
20
import java .io .File ;
21
21
import java .io .FileOutputStream ;
22
22
import java .io .FileReader ;
23
+ import java .io .IOException ;
23
24
import java .util .Properties ;
24
25
25
- import org .junit .After ;
26
- import org .junit .Before ;
26
+ import org .junit .Rule ;
27
27
import org .junit .Test ;
28
+ import org .junit .rules .TemporaryFolder ;
29
+ import org .junit .rules .TestName ;
28
30
29
31
30
32
/**
@@ -45,32 +47,26 @@ public class EncryptedPropertiesUtilsTest {
45
47
private static final String KEY4 = "sally sue" ;
46
48
private static final String VALUE4 = "betty mae" ;
47
49
48
- private static final String PLAINTEXT_FILENAME = "plaintext.properties" ;
49
- private static final String ENCRYPTED_FILENAME_1 = "encrypted.properties" ;
50
- private static final String ENCRYPTED_FILENAME_2 = "encrypted.2.properties" ;
51
-
52
- /**
53
- * {@inheritDoc}
54
- */
55
- @ Before public void setUp () throws Exception {
56
- //write an initial plaintext properties file
57
- Properties props = new Properties ();
58
- props .setProperty (KEY3 , VALUE3 );
59
- props .setProperty (KEY4 , VALUE4 );
60
-
61
- props .store (new FileOutputStream (PLAINTEXT_FILENAME ), "Plaintext test file created by EncryptedPropertiesUtilsTest" );
62
- }
63
-
50
+
51
+ /** Rule to acquire the running test's information at runtime.*/
52
+ @ Rule
53
+ public TestName testName = new TestName ();
54
+
55
+ /** File management component for IO targets during test executions.*/
56
+ @ Rule
57
+ public TemporaryFolder tempFolder = new TemporaryFolder ();
58
+
59
+ /** Counter used to track the number of files created for a single test to assist with creating unique file references.*/
60
+ private int fileIndex = 0 ;
61
+
64
62
/**
65
- * {@inheritDoc}
63
+ * Creates a new properties file reference for the active test which will be cleaned up upon test completion.
64
+ * @return File New unique file reference for the active test.
65
+ * @throws IOException If a new file could not be generated.
66
66
*/
67
- @ After public void tearDown () throws Exception {
68
- File [] delFiles = new File [] { new File (PLAINTEXT_FILENAME ), new File (ENCRYPTED_FILENAME_1 ), new File (ENCRYPTED_FILENAME_2 ) };
69
- for ( File f : delFiles ) {
70
- f .deleteOnExit ();
71
- }
67
+ private final File getTempPropertiesFile () throws IOException {
68
+ return tempFolder .newFile (String .format ("%s_%2d.properties" , testName .getMethodName (), fileIndex ++));
72
69
}
73
-
74
70
/**
75
71
* Test of creating and storing a new EncryptedProperties from scratch,
76
72
* as if calling:
@@ -81,7 +77,8 @@ public class EncryptedPropertiesUtilsTest {
81
77
* @throws Exception Any exception that occurs
82
78
*/
83
79
@ Test public void testCreateNew () throws Exception {
84
-
80
+ File encryptedFile = getTempPropertiesFile ();
81
+
85
82
//create a new properties with no input
86
83
Properties props = EncryptedPropertiesUtils .loadProperties (null , null );
87
84
@@ -93,11 +90,11 @@ public class EncryptedPropertiesUtilsTest {
93
90
assertNull ("Expected null but returned: " + prop2 , prop2 );
94
91
95
92
//store the file
96
- EncryptedPropertiesUtils .storeProperties (ENCRYPTED_FILENAME_1 , props , "Encrypted Properties File generated by EncryptedPropertiesUtilsTest" );
93
+ EncryptedPropertiesUtils .storeProperties (encryptedFile . getAbsolutePath () , props , "Encrypted Properties File generated by EncryptedPropertiesUtilsTest" );
97
94
98
95
//try reading in the resulting file
99
96
ReferenceEncryptedProperties loadedProps = new ReferenceEncryptedProperties ();
100
- loadedProps .load (new FileReader (ENCRYPTED_FILENAME_1 ));
97
+ loadedProps .load (new FileReader (encryptedFile . getAbsolutePath () ));
101
98
102
99
assertEquals (VALUE1 , loadedProps .getProperty (KEY1 ));
103
100
assertEquals (VALUE2 , loadedProps .getProperty (KEY2 ));
@@ -113,20 +110,29 @@ public class EncryptedPropertiesUtilsTest {
113
110
* @throws Exception Any exception that occurs
114
111
*/
115
112
@ Test public void testLoadPlaintextAndEncrypt () throws Exception {
116
-
113
+ File encryptedFile = getTempPropertiesFile ();
114
+ File plainTextFile = getTempPropertiesFile ();
115
+
116
+ //write an initial plaintext properties file
117
+ Properties props = new Properties ();
118
+ props .setProperty (KEY3 , VALUE3 );
119
+ props .setProperty (KEY4 , VALUE4 );
120
+
121
+ props .store (new FileOutputStream (plainTextFile .getAbsolutePath ()), "Plaintext test file created by EncryptedPropertiesUtilsTest" );
122
+
117
123
//load the plaintext properties file
118
- Properties props = EncryptedPropertiesUtils .loadProperties (PLAINTEXT_FILENAME , false );
124
+ props = EncryptedPropertiesUtils .loadProperties (plainTextFile . getAbsolutePath () , false );
119
125
120
126
//test some properties using getProperty
121
127
assertEquals (VALUE3 , props .getProperty (KEY3 ));
122
128
assertEquals (VALUE4 , props .getProperty (KEY4 ));
123
129
124
130
//store the file
125
- EncryptedPropertiesUtils .storeProperties (ENCRYPTED_FILENAME_1 , props , "Encrypted Properties File generated by EncryptedPropertiesUtilsTest" );
131
+ EncryptedPropertiesUtils .storeProperties (encryptedFile . getAbsolutePath () , props , "Encrypted Properties File generated by EncryptedPropertiesUtilsTest" );
126
132
127
133
//try reading in the resulting file
128
134
ReferenceEncryptedProperties loadedProps = new ReferenceEncryptedProperties ();
129
- loadedProps .load (new FileReader (ENCRYPTED_FILENAME_1 ));
135
+ loadedProps .load (new FileReader (encryptedFile . getAbsolutePath () ));
130
136
131
137
assertEquals (VALUE3 , loadedProps .getProperty (KEY3 ));
132
138
assertEquals (VALUE4 , loadedProps .getProperty (KEY4 ));
@@ -142,13 +148,10 @@ public class EncryptedPropertiesUtilsTest {
142
148
* @throws Exception Any exception that occurs
143
149
*/
144
150
@ Test public void testLoadEncryptedAndAdd () throws Exception {
145
-
151
+ File encryptedFile = getTempPropertiesFile ();
152
+ File encryptedFile2 = getTempPropertiesFile ();
146
153
//load the plaintext properties file
147
- Properties props = EncryptedPropertiesUtils .loadProperties (ENCRYPTED_FILENAME_1 , true );
148
-
149
- //test some properties
150
- assertEquals (VALUE3 , props .getProperty (KEY3 ));
151
- assertEquals (VALUE4 , props .getProperty (KEY4 ));
154
+ Properties props = EncryptedPropertiesUtils .loadProperties (encryptedFile .getAbsolutePath (), true );
152
155
153
156
//add some new properties
154
157
EncryptedPropertiesUtils .addProperty (props , KEY1 , VALUE1 );
@@ -159,17 +162,15 @@ public class EncryptedPropertiesUtilsTest {
159
162
assertEquals (VALUE2 , props .getProperty (KEY2 ));
160
163
161
164
//store the file
162
- EncryptedPropertiesUtils .storeProperties (ENCRYPTED_FILENAME_2 , props , "Encrypted Properties File generated by EncryptedPropertiesUtilsTest" );
165
+ EncryptedPropertiesUtils .storeProperties (encryptedFile2 . getAbsolutePath () , props , "Encrypted Properties File generated by EncryptedPropertiesUtilsTest" );
163
166
164
167
//try reading in the resulting file
165
168
ReferenceEncryptedProperties loadedProps = new ReferenceEncryptedProperties ();
166
- loadedProps .load (new FileReader (ENCRYPTED_FILENAME_2 ));
169
+ loadedProps .load (new FileReader (encryptedFile2 . getAbsolutePath () ));
167
170
168
171
//test the values read in
169
172
assertEquals (VALUE1 , loadedProps .getProperty (KEY1 ));
170
173
assertEquals (VALUE2 , loadedProps .getProperty (KEY2 ));
171
- assertEquals (VALUE3 , loadedProps .getProperty (KEY3 ));
172
- assertEquals (VALUE4 , loadedProps .getProperty (KEY4 ));
173
174
}
174
175
175
176
}
0 commit comments