Skip to content

Commit dee6b52

Browse files
Intermittent Test Failure Cleanup
The property manager and property loader tests were altering system properties used to initialize common resources. The original values were not restored, and I was having test failures from the system being unable to resolve the values configured for the tests. I have added static BeforeAll and AfterAll blocks to the three tests to capture the original property state before the test runs, and to put it back after all tests are finished.
1 parent 87c5310 commit dee6b52

File tree

3 files changed

+76
-21
lines changed

3 files changed

+76
-21
lines changed

src/test/java/org/owasp/esapi/configuration/EsapiPropertyManagerTest.java

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package org.owasp.esapi.configuration;
22

3+
import static junit.framework.Assert.assertEquals;
4+
import static junit.framework.Assert.assertNotNull;
5+
import static junit.framework.Assert.assertNotSame;
6+
import static junit.framework.Assert.fail;
7+
8+
import java.io.File;
9+
import java.io.FileNotFoundException;
10+
import java.io.IOException;
11+
12+
import org.junit.AfterClass;
313
import org.junit.Before;
14+
import org.junit.BeforeClass;
415
import org.junit.Test;
516
import org.owasp.esapi.ESAPI;
617
import org.owasp.esapi.configuration.consts.EsapiConfiguration;
718
import org.owasp.esapi.errors.ConfigurationException;
819

9-
import java.io.File;
10-
import java.io.IOException;
11-
import java.io.FileNotFoundException;
12-
13-
import static junit.framework.Assert.*;
14-
1520
public class EsapiPropertyManagerTest {
1621

1722
private static String propFilename1;
@@ -21,7 +26,22 @@ public class EsapiPropertyManagerTest {
2126
private static final String noSuchFile = "/invalidDir/noSubDir/nosuchFile.xml";
2227

2328
private EsapiPropertyManager testPropertyManager;
29+
private static String DEVTEAM_CFG = "";
30+
private static String OPSTEAM_CFG = "";
2431

32+
@BeforeClass
33+
public static void captureEsapiConfigurations() {
34+
DEVTEAM_CFG = System.getProperty(EsapiConfiguration.DEVTEAM_ESAPI_CFG.getConfigName(),"");
35+
OPSTEAM_CFG = System.getProperty(EsapiConfiguration.OPSTEAM_ESAPI_CFG.getConfigName(),"");
36+
}
37+
38+
@AfterClass
39+
public static void restoreEsapiConfigurations() {
40+
System.setProperty(EsapiConfiguration.DEVTEAM_ESAPI_CFG.getConfigName(), DEVTEAM_CFG);
41+
System.setProperty(EsapiConfiguration.OPSTEAM_ESAPI_CFG.getConfigName(), OPSTEAM_CFG);
42+
}
43+
44+
2545
@Before
2646
public void init() {
2747
System.setProperty(EsapiConfiguration.DEVTEAM_ESAPI_CFG.getConfigName(), "");
@@ -402,20 +422,20 @@ public void testByteArrayPropertyNotFoundByLoaderAndThrowException() {
402422

403423
@Test
404424
public void testExpectFileNotFoundException() {
405-
// given
406-
System.setProperty(EsapiConfiguration.DEVTEAM_ESAPI_CFG.getConfigName(), noSuchFile);
407-
408-
// when
409-
try {
410-
testPropertyManager = new EsapiPropertyManager();
411-
} catch (IOException e) {
412-
if ( e instanceof FileNotFoundException ) {
413-
return;
414-
} else {
415-
fail("testExpectFileNotFoundException(): Was expecting FileNotFoundException for IOException. Exception:" + e);
416-
}
417-
}
418-
419-
fail("Did not throw expected IOException for property file " + noSuchFile);
425+
// given
426+
System.setProperty(EsapiConfiguration.DEVTEAM_ESAPI_CFG.getConfigName(), noSuchFile);
427+
428+
// when
429+
try {
430+
testPropertyManager = new EsapiPropertyManager();
431+
} catch (IOException e) {
432+
if ( e instanceof FileNotFoundException ) {
433+
return;
434+
} else {
435+
fail("testExpectFileNotFoundException(): Was expecting FileNotFoundException for IOException. Exception:" + e);
436+
}
437+
}
438+
439+
fail("Did not throw expected IOException for property file " + noSuchFile);
420440
}
421441
}

src/test/java/org/owasp/esapi/configuration/StandardEsapiPropertyLoaderTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.owasp.esapi.configuration;
22

33

4+
import org.junit.AfterClass;
45
import org.junit.Before;
6+
import org.junit.BeforeClass;
57
import org.junit.Test;
68
import org.owasp.esapi.ESAPI;
79
import org.owasp.esapi.configuration.consts.EsapiConfiguration;
@@ -19,6 +21,22 @@ public class StandardEsapiPropertyLoaderTest {
1921

2022
private StandardEsapiPropertyLoader testPropertyLoader;
2123

24+
private static String DEVTEAM_CFG = "";
25+
private static String OPSTEAM_CFG = "";
26+
27+
@BeforeClass
28+
public static void captureEsapiConfigurations() {
29+
DEVTEAM_CFG = System.getProperty(EsapiConfiguration.DEVTEAM_ESAPI_CFG.getConfigName(),"");
30+
OPSTEAM_CFG = System.getProperty(EsapiConfiguration.OPSTEAM_ESAPI_CFG.getConfigName(),"");
31+
}
32+
33+
@AfterClass
34+
public static void restoreEsapiConfigurations() {
35+
System.setProperty(EsapiConfiguration.DEVTEAM_ESAPI_CFG.getConfigName(), DEVTEAM_CFG);
36+
System.setProperty(EsapiConfiguration.OPSTEAM_ESAPI_CFG.getConfigName(), OPSTEAM_CFG);
37+
}
38+
39+
2240
@Before
2341
public void init() {
2442
System.setProperty(EsapiConfiguration.DEVTEAM_ESAPI_CFG.getConfigName(), "");

src/test/java/org/owasp/esapi/configuration/XmlEsapiPropertyLoaderTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.owasp.esapi.configuration;
22

3+
import org.junit.AfterClass;
34
import org.junit.Before;
5+
import org.junit.BeforeClass;
46
import org.junit.Test;
57
import org.owasp.esapi.ESAPI;
68
import org.owasp.esapi.configuration.consts.EsapiConfiguration;
@@ -18,6 +20,21 @@ public class XmlEsapiPropertyLoaderTest {
1820

1921
private XmlEsapiPropertyLoader testPropertyLoader;
2022

23+
private static String DEVTEAM_CFG = "";
24+
private static String OPSTEAM_CFG = "";
25+
26+
@BeforeClass
27+
public static void captureEsapiConfigurations() {
28+
DEVTEAM_CFG = System.getProperty(EsapiConfiguration.DEVTEAM_ESAPI_CFG.getConfigName(),"");
29+
OPSTEAM_CFG = System.getProperty(EsapiConfiguration.OPSTEAM_ESAPI_CFG.getConfigName(),"");
30+
}
31+
32+
@AfterClass
33+
public static void restoreEsapiConfigurations() {
34+
System.setProperty(EsapiConfiguration.DEVTEAM_ESAPI_CFG.getConfigName(), DEVTEAM_CFG);
35+
System.setProperty(EsapiConfiguration.OPSTEAM_ESAPI_CFG.getConfigName(), OPSTEAM_CFG);
36+
}
37+
2138
@Before
2239
public void init() {
2340
System.setProperty(EsapiConfiguration.DEVTEAM_ESAPI_CFG.getConfigName(), "");

0 commit comments

Comments
 (0)