Skip to content

Commit 851359c

Browse files
authored
Isolate core unit tests per classloader (#4156)
* Isolate core unit tests per classloader * Stabilize core unit tests initialization
1 parent 23d486b commit 851359c

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

maven/core-unittests/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
<groupId>org.apache.maven.plugins</groupId>
3434
<artifactId>maven-surefire-plugin</artifactId>
3535
<version>3.2.1</version>
36+
<configuration>
37+
<!-- Run each test in its own forked JVM to ensure an isolated classloader. -->
38+
<forkCount>1</forkCount>
39+
<reuseForks>false</reuseForks>
40+
</configuration>
3641
</plugin>
3742
<plugin>
3843
<groupId>org.jacoco</groupId>

maven/core-unittests/src/test/java/com/codename1/io/CacheMapTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.codename1.io;
22

3+
import com.codename1.impl.ImplementationFactory;
34
import com.codename1.junit.EdtTest;
45
import com.codename1.testing.TestCodenameOneImplementation;
56
import com.codename1.ui.Display;
@@ -17,6 +18,15 @@ class CacheMapTest {
1718
@BeforeEach
1819
void setUp() {
1920
implementation = new TestCodenameOneImplementation(true);
21+
ImplementationFactory.setInstance(new ImplementationFactory() {
22+
@Override
23+
public Object createImplementation() {
24+
return implementation;
25+
}
26+
});
27+
if (!Display.isInitialized()) {
28+
Display.init(null);
29+
}
2030
Util.setImplementation(implementation);
2131
Storage.setStorageInstance(null);
2232
}
@@ -25,6 +35,7 @@ void setUp() {
2535
void tearDown() {
2636
Storage.setStorageInstance(null);
2737
Util.setImplementation(null);
38+
Display.deinitialize();
2839
}
2940

3041
@Test

maven/core-unittests/src/test/java/com/codename1/properties/PropertiesPackageTest.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class PropertiesPackageTest extends UITestBase {
2525

2626
@BeforeEach
2727
void setup() throws Exception {
28-
CN.callSeriallyAndWait(new Runnable() {
28+
Runnable setupTask = new Runnable() {
2929
public void run() {
3030
originalPreferencesLocation = Preferences.getPreferencesLocation();
3131
Preferences.setPreferencesLocation("PropertiesTest-" + System.nanoTime());
@@ -36,12 +36,17 @@ public void run() {
3636
PropertyBase.bindGlobalGetListener(null);
3737
PropertyBase.bindGlobalSetListener(null);
3838
}
39-
});
39+
};
40+
if (CN.isEdt()) {
41+
setupTask.run();
42+
} else {
43+
CN.callSeriallyAndWait(setupTask);
44+
}
4045
}
4146

4247
@AfterEach
4348
void tearDown() throws Exception {
44-
CN.callSeriallyAndWait(new Runnable() {
49+
Runnable teardownTask = new Runnable() {
4550
public void run() {
4651
PropertyBase.bindGlobalGetListener(null);
4752
PropertyBase.bindGlobalSetListener(null);
@@ -52,7 +57,12 @@ public void run() {
5257
Storage.getInstance().clearCache();
5358
implementation.clearStorage();
5459
}
55-
});
60+
};
61+
if (CN.isEdt()) {
62+
teardownTask.run();
63+
} else {
64+
CN.callSeriallyAndWait(teardownTask);
65+
}
5666
}
5767

5868
@EdtTest

0 commit comments

Comments
 (0)