Skip to content

Commit

Permalink
SINT: Replace debugger context
Browse files Browse the repository at this point in the history
Based on reviewing feedback, the context added to the last commit in SmackDebugger is now replaced with a static field in SmackIntegrationFramework.
  • Loading branch information
guusdk committed Apr 3, 2024
1 parent 99327d6 commit 0e65a66
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2003-2007 Jive Software, 2017 Florian Schmaus, 2024 Guus der Kinderen
* Copyright 2003-2007 Jive Software, 2017 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,8 +20,6 @@
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.TopLevelStreamElement;
Expand All @@ -43,8 +41,6 @@
*/
public abstract class SmackDebugger {

private static final Map<String, String> CONTEXT = new HashMap<>();

protected final XMPPConnection connection;

private XmppXmlSplitter outgoingStreamSplitterForPrettyPrinting;
Expand Down Expand Up @@ -153,46 +149,4 @@ public final Writer newConnectionWriter(Writer writer) {
*/
public abstract void onOutgoingStreamElement(TopLevelStreamElement streamElement);

/**
* Associates the specified value with the specified key in the debugging context, following the contract as
* specified by {@link Map#put(Object, Object)}.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with key, or null if there was no mapping for key. A null return can
* also indicate that the map previously associated null with key.
*/
public static String putInContext(final String key, final String value) {
return CONTEXT.put(key, value);
}

/**
* Returns the value to which the specified key is mapped, or null if the debugging context contains no mapping for
* the key, following the contract as specified by {@link Map#get(Object)}.
*
* @param key the key whose associated value is to be returned
* @return the value to which the specified key is mapped, or null if this map contains no mapping for the key
*/
public static String getFromContext(final String key) {
return CONTEXT.get(key);
}

/**
* Removes the mapping for a key from the debugging context if it is present, following the contract as
* specified by {@link Map#remove(Object)}.
*
* @param key the key whose associated value is to be returned
* @return the value to which the specified key is mapped, or null if this map contains no mapping for the key
*/
public static String removeFromContext(final String key) {
return CONTEXT.remove(key);
}

/**
* Removes all of the mappings from the debugging context. The debugging context will be empty after this call
* returns.
*/
public static void clearContext() {
CONTEXT.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.debugger.SmackDebugger;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.TLSUtils;
Expand Down Expand Up @@ -88,6 +87,8 @@ public class SmackIntegrationTestFramework {

public static boolean SINTTEST_UNIT_TEST = false;

private static ConcreteTest TEST_UNDER_EXECUTION;

protected final Configuration config;

protected TestRunResult testRunResult;
Expand Down Expand Up @@ -245,6 +246,10 @@ public synchronized TestRunResult run()
return testRunResult;
}

public static ConcreteTest getTestUnderExecution() {
return TEST_UNDER_EXECUTION;
}

@SuppressWarnings({"Finally"})
private void runTests(Set<Class<? extends AbstractSmackIntTest>> classes)
throws InterruptedException, InstantiationException, IllegalAccessException,
Expand Down Expand Up @@ -677,19 +682,20 @@ private PreparedTest(AbstractSmackIntTest test, List<ConcreteTest> concreteTests

public void run() throws InterruptedException, XMPPException, IOException, SmackException {
try {
SmackDebugger.putInContext("sint.test", test.getClass().getSimpleName());

// Run the @BeforeClass methods (if any)
executeSinttestSpecialMethod(beforeClassMethod);

for (ConcreteTest concreteTest : concreteTests) {
SmackDebugger.putInContext("sint.concreteTest", concreteTest.toString());
runConcreteTest(concreteTest);
try {
TEST_UNDER_EXECUTION = concreteTest;
runConcreteTest(concreteTest);
} finally {
TEST_UNDER_EXECUTION = null;
}
}
}
finally {
executeSinttestSpecialMethod(afterClassMethod);
SmackDebugger.clearContext();
}
}

Expand Down Expand Up @@ -734,21 +740,37 @@ else if (specialClassMethods.size() > 1) {
return null;
}

static final class ConcreteTest {
public static final class ConcreteTest {
private final TestType testType;
private final Method method;
private final Executor executor;
private final String[] subdescriptons;
private final List<String> subdescriptons;

private ConcreteTest(TestType testType, Method method, Executor executor, String... subdescriptions) {
this.testType = testType;
this.method = method;
this.executor = executor;
this.subdescriptons = subdescriptions;
if (subdescriptions == null) {
this.subdescriptons = null;
} else {
this.subdescriptons = List.of(subdescriptions);
}
}

private transient String stringCache;

public TestType getTestType() {
return testType;
}

public Method getMethod() {
return method;
}

public List<String> getSubdescriptons() {
return subdescriptons;
}

@Override
public String toString() {
if (stringCache != null) {
Expand All @@ -761,9 +783,9 @@ public String toString() {
.append(method.getName())
.append(" (")
.append(testType.name());
if (subdescriptons != null && subdescriptons.length > 0) {
if (subdescriptons != null && !subdescriptons.isEmpty()) {
sb.append(", ");
StringUtils.appendTo(Arrays.asList(subdescriptons), sb);
StringUtils.appendTo(subdescriptons, sb);
}
sb.append(')');

Expand Down

0 comments on commit 0e65a66

Please sign in to comment.