2020import java .io .File ;
2121import java .io .FileOutputStream ;
2222import java .io .FileReader ;
23+ import java .io .IOException ;
2324import java .util .Properties ;
2425
25- import org .junit .After ;
26- import org .junit .Before ;
26+ import org .junit .Rule ;
2727import org .junit .Test ;
28+ import org .junit .rules .TemporaryFolder ;
29+ import org .junit .rules .TestName ;
2830
2931
3032/**
@@ -45,32 +47,26 @@ public class EncryptedPropertiesUtilsTest {
4547 private static final String KEY4 = "sally sue" ;
4648 private static final String VALUE4 = "betty mae" ;
4749
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+
6462 /**
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.
6666 */
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 ++));
7269 }
73-
7470 /**
7571 * Test of creating and storing a new EncryptedProperties from scratch,
7672 * as if calling:
@@ -81,7 +77,8 @@ public class EncryptedPropertiesUtilsTest {
8177 * @throws Exception Any exception that occurs
8278 */
8379 @ Test public void testCreateNew () throws Exception {
84-
80+ File encryptedFile = getTempPropertiesFile ();
81+
8582 //create a new properties with no input
8683 Properties props = EncryptedPropertiesUtils .loadProperties (null , null );
8784
@@ -93,11 +90,11 @@ public class EncryptedPropertiesUtilsTest {
9390 assertNull ("Expected null but returned: " + prop2 , prop2 );
9491
9592 //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" );
9794
9895 //try reading in the resulting file
9996 ReferenceEncryptedProperties loadedProps = new ReferenceEncryptedProperties ();
100- loadedProps .load (new FileReader (ENCRYPTED_FILENAME_1 ));
97+ loadedProps .load (new FileReader (encryptedFile . getAbsolutePath () ));
10198
10299 assertEquals (VALUE1 , loadedProps .getProperty (KEY1 ));
103100 assertEquals (VALUE2 , loadedProps .getProperty (KEY2 ));
@@ -113,20 +110,29 @@ public class EncryptedPropertiesUtilsTest {
113110 * @throws Exception Any exception that occurs
114111 */
115112 @ 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+
117123 //load the plaintext properties file
118- Properties props = EncryptedPropertiesUtils .loadProperties (PLAINTEXT_FILENAME , false );
124+ props = EncryptedPropertiesUtils .loadProperties (plainTextFile . getAbsolutePath () , false );
119125
120126 //test some properties using getProperty
121127 assertEquals (VALUE3 , props .getProperty (KEY3 ));
122128 assertEquals (VALUE4 , props .getProperty (KEY4 ));
123129
124130 //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" );
126132
127133 //try reading in the resulting file
128134 ReferenceEncryptedProperties loadedProps = new ReferenceEncryptedProperties ();
129- loadedProps .load (new FileReader (ENCRYPTED_FILENAME_1 ));
135+ loadedProps .load (new FileReader (encryptedFile . getAbsolutePath () ));
130136
131137 assertEquals (VALUE3 , loadedProps .getProperty (KEY3 ));
132138 assertEquals (VALUE4 , loadedProps .getProperty (KEY4 ));
@@ -142,13 +148,10 @@ public class EncryptedPropertiesUtilsTest {
142148 * @throws Exception Any exception that occurs
143149 */
144150 @ Test public void testLoadEncryptedAndAdd () throws Exception {
145-
151+ File encryptedFile = getTempPropertiesFile ();
152+ File encryptedFile2 = getTempPropertiesFile ();
146153 //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 );
152155
153156 //add some new properties
154157 EncryptedPropertiesUtils .addProperty (props , KEY1 , VALUE1 );
@@ -159,17 +162,15 @@ public class EncryptedPropertiesUtilsTest {
159162 assertEquals (VALUE2 , props .getProperty (KEY2 ));
160163
161164 //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" );
163166
164167 //try reading in the resulting file
165168 ReferenceEncryptedProperties loadedProps = new ReferenceEncryptedProperties ();
166- loadedProps .load (new FileReader (ENCRYPTED_FILENAME_2 ));
169+ loadedProps .load (new FileReader (encryptedFile2 . getAbsolutePath () ));
167170
168171 //test the values read in
169172 assertEquals (VALUE1 , loadedProps .getProperty (KEY1 ));
170173 assertEquals (VALUE2 , loadedProps .getProperty (KEY2 ));
171- assertEquals (VALUE3 , loadedProps .getProperty (KEY3 ));
172- assertEquals (VALUE4 , loadedProps .getProperty (KEY4 ));
173174 }
174175
175176}
0 commit comments