Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -15,6 +16,11 @@ void resetImplementation() {
TestImplementationProvider.installImplementation(true);
}

@AfterEach
void tearDown() {
TestImplementationProvider.resetImplementation();
}

@Test
void buildRequestBodyIncludesArgumentsAndBinaryData() throws IOException {
MultipartRequest request = new MultipartRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Enumeration;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -28,6 +29,17 @@ void setUp() throws Exception {
bootstrapDisplayThread();
}

@AfterEach
void tearDown() throws Exception {
Field edtField = Display.class.getDeclaredField("edt");
edtField.setAccessible(true);
edtField.set(Display.getInstance(), null);
Field runningField = Display.class.getDeclaredField("codenameOneRunning");
runningField.setAccessible(true);
runningField.set(Display.getInstance(), Boolean.FALSE);
TestImplementationProvider.resetImplementation();
}

@Test
void autoDetectUrlMutators() {
NetworkManager.setAutoDetectURL("https://example.com/ping");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.codename1.io;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -13,14 +12,14 @@
class PreferencesTest {

@BeforeEach
void setUp() throws Exception {
void setUp() {
TestImplementationProvider.resetImplementation();
TestImplementationProvider.installImplementation(true);
resetPreferencesState();
}

@AfterEach
void tearDown() throws Exception {
resetPreferencesState();
void tearDown() {
TestImplementationProvider.resetImplementation();
}

@Test
Expand Down Expand Up @@ -99,20 +98,4 @@ void preferenceLocationIsolation() {
assertEquals("original", Preferences.get("shared", ""));
}

@SuppressWarnings("unchecked")
private void resetPreferencesState() throws Exception {
Field pField = Preferences.class.getDeclaredField("p");
pField.setAccessible(true);
pField.set(null, null);

Field listenerField = Preferences.class.getDeclaredField("listenerMap");
listenerField.setAccessible(true);
((Map) listenerField.get(null)).clear();

Field locationField = Preferences.class.getDeclaredField("preferencesLocation");
locationField.setAccessible(true);
locationField.set(null, "CN1Preferences");

Storage.setStorageInstance(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.codename1.impl.CodenameOneImplementation;
import com.codename1.ui.Display;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -34,6 +35,11 @@ void setUp() {
Display.getInstance();
}

@AfterEach
void tearDown() {
TestImplementationProvider.resetImplementation();
}

@Test
void supportedFlagsDelegateToImplementation() {
when(implementation.isSocketAvailable()).thenReturn(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.codename1.io;

import com.codename1.impl.CodenameOneImplementation;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -25,6 +26,11 @@ void setUp() {
storage.setNormalizeNames(true);
}

@AfterEach
void tearDown() {
TestImplementationProvider.resetImplementation();
}

@Test
void writeObjectCachesAndPersistsEntries() {
Vector<String> payload = new Vector<String>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.codename1.io;

import com.codename1.impl.CodenameOneImplementation;
import com.codename1.io.Preferences;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

import static org.mockito.ArgumentMatchers.any;
Expand All @@ -23,6 +27,9 @@ public final class TestImplementationProvider {
private TestImplementationProvider() {
}

private static final String DEFAULT_AUTO_DETECT_URL = NetworkManager.getAutoDetectURL();
private static final String DEFAULT_PREFERENCES_LOCATION = Preferences.getPreferencesLocation();

public static CodenameOneImplementation installImplementation(boolean timeoutSupported) {
Storage.setStorageInstance(null);
Map<String, byte[]> storage = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -102,4 +109,82 @@ public void close() throws IOException {
Util.setImplementation(impl);
return impl;
}

public static void resetImplementation() {
Util.setImplementation(null);
Storage.setStorageInstance(null);
resetNetworkManager();
resetPreferences();
}

@SuppressWarnings("unchecked")
private static void resetNetworkManager() {
NetworkManager manager = NetworkManager.getInstance();
try {
Field runningField = NetworkManager.class.getDeclaredField("running");
runningField.setAccessible(true);
runningField.setBoolean(manager, false);

Field threadCountField = NetworkManager.class.getDeclaredField("threadCount");
threadCountField.setAccessible(true);
threadCountField.setInt(manager, 1);

Field networkThreadsField = NetworkManager.class.getDeclaredField("networkThreads");
networkThreadsField.setAccessible(true);
networkThreadsField.set(manager, null);

Field errorField = NetworkManager.class.getDeclaredField("errorListeners");
errorField.setAccessible(true);
errorField.set(manager, null);

Field progressField = NetworkManager.class.getDeclaredField("progressListeners");
progressField.setAccessible(true);
progressField.set(manager, null);

Field pendingField = NetworkManager.class.getDeclaredField("pending");
pendingField.setAccessible(true);
((Vector) pendingField.get(manager)).clear();

Field threadAssignmentsField = NetworkManager.class.getDeclaredField("threadAssignements");
threadAssignmentsField.setAccessible(true);
((Hashtable) threadAssignmentsField.get(manager)).clear();

Field userHeadersField = NetworkManager.class.getDeclaredField("userHeaders");
userHeadersField.setAccessible(true);
userHeadersField.set(manager, null);

Field timeoutField = NetworkManager.class.getDeclaredField("timeout");
timeoutField.setAccessible(true);
timeoutField.setInt(manager, 300000);

Field autoDetectedField = NetworkManager.class.getDeclaredField("autoDetected");
autoDetectedField.setAccessible(true);
autoDetectedField.setBoolean(manager, false);

Field nextConnectionIdField = NetworkManager.class.getDeclaredField("nextConnectionId");
nextConnectionIdField.setAccessible(true);
nextConnectionIdField.setInt(manager, 1);
} catch (ReflectiveOperationException e) {
throw new AssertionError("Unable to reset NetworkManager state", e);
}

NetworkManager.setAutoDetectURL(DEFAULT_AUTO_DETECT_URL);
}

@SuppressWarnings("unchecked")
private static void resetPreferences() {
try {
Field pField = Preferences.class.getDeclaredField("p");
pField.setAccessible(true);
pField.set(null, null);

Field listenerField = Preferences.class.getDeclaredField("listenerMap");
listenerField.setAccessible(true);
((Map<?, ?>) listenerField.get(null)).clear();
} catch (ReflectiveOperationException e) {
throw new AssertionError("Unable to reset Preferences state", e);
}

Preferences.setPreferencesLocation(DEFAULT_PREFERENCES_LOCATION);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.codename1.io;

import com.codename1.impl.CodenameOneImplementation;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -65,6 +66,11 @@ public void close() throws IOException {
}
}

@AfterEach
void tearDown() {
TestImplementationProvider.resetImplementation();
}

@Test
void parsesUrlComponents() throws URISyntaxException {
URL url = new URL("https://user:[email protected]:8443/path/resource?x=1");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.codename1.io;

import com.codename1.impl.CodenameOneImplementation;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -27,6 +28,11 @@ void setUp() {
implementation = TestImplementationProvider.installImplementation(true);
}

@AfterEach
void tearDown() {
TestImplementationProvider.resetImplementation();
}

@Test
void copyClosesStreamsAndInvokesCleanup() throws IOException {
byte[] source = "payload".getBytes(StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -22,6 +23,11 @@ void setUp() {
TestImplementationProvider.installImplementation(true);
}

@AfterEach
void tearDown() {
TestImplementationProvider.resetImplementation();
}

@Test
void createRequestPopulatesConnection() throws Exception {
RequestBuilder builder = new RequestBuilder("POST", "https://example.com/items/{id}");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.codename1.properties;

import com.codename1.io.Preferences;
import com.codename1.io.Storage;
import com.codename1.io.TestImplementationProvider;
import com.codename1.xml.Element;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -20,10 +19,9 @@

class PropertiesPackageTest {

private String originalPreferencesLocation;

@BeforeEach
void setup() throws Exception {
TestImplementationProvider.resetImplementation();
TestImplementationProvider.installImplementation(true);
resetPreferencesState();
resetMetadata();
Expand All @@ -35,11 +33,7 @@ void setup() throws Exception {
void tearDown() throws Exception {
PropertyBase.bindGlobalGetListener(null);
PropertyBase.bindGlobalSetListener(null);
resetPreferencesState();
if (originalPreferencesLocation != null) {
Preferences.setPreferencesLocation(originalPreferencesLocation);
}
Storage.setStorageInstance(null);
TestImplementationProvider.resetImplementation();
resetMetadata();
}

Expand Down Expand Up @@ -246,14 +240,8 @@ void preferencesObjectSynchronizesValues() {
assertEquals(7, Preferences.get("prefs.total", 0));
}

private void resetPreferencesState() throws Exception {
FieldAccessor.resetStaticField(Preferences.class, "p", null);
FieldAccessor.resetStaticMap(Preferences.class, "listenerMap");
if (originalPreferencesLocation == null) {
originalPreferencesLocation = Preferences.getPreferencesLocation();
}
private void resetPreferencesState() {
Preferences.setPreferencesLocation("PropertiesTest-" + System.nanoTime());
Storage.setStorageInstance(null);
}

private void resetMetadata() throws Exception {
Expand All @@ -264,12 +252,6 @@ private static class FieldAccessor {
private FieldAccessor() {
}

static void resetStaticField(Class<?> type, String name, Object value) throws Exception {
java.lang.reflect.Field field = type.getDeclaredField(name);
field.setAccessible(true);
field.set(null, value);
}

@SuppressWarnings("unchecked")
static void resetStaticMap(Class<?> type, String name) throws Exception {
java.lang.reflect.Field field = type.getDeclaredField(name);
Expand Down