-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sjoerd Talsma <[email protected]>
- Loading branch information
1 parent
1cbaa47
commit e1e60fd
Showing
9 changed files
with
400 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...xt-propagation-api/src/test/java/nl/talsmasoftware/context/api/NoContextManagersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2016-2024 Talsma ICT | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package nl.talsmasoftware.context.api; | ||
|
||
import nl.talsmasoftware.context.dummy.DummyContext; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
|
||
public class NoContextManagersTest { | ||
private static final String SERVICE_LOCATION = "target/test-classes/META-INF/services/"; | ||
private static final File SERVICE_FILE = new File(SERVICE_LOCATION + ContextManager.class.getName()); | ||
private static final File TMP_SERVICE_FILE = new File(SERVICE_LOCATION + "tmp-ContextManager"); | ||
|
||
@BeforeEach | ||
public void avoidContextManagersCache() { | ||
ContextManager.useClassLoader(new ClassLoader(Thread.currentThread().getContextClassLoader()) { | ||
}); | ||
assertThat("Move service file", SERVICE_FILE.renameTo(TMP_SERVICE_FILE), is(true)); | ||
} | ||
|
||
@AfterEach | ||
public void resetDefaultClassLoader() { | ||
ContextManager.useClassLoader(null); | ||
assertThat("Restore service file!", TMP_SERVICE_FILE.renameTo(SERVICE_FILE), is(true)); | ||
} | ||
|
||
@Test | ||
public void testReactivate_withoutContextManagers() { | ||
Context<String> ctx1 = new DummyContext("foo"); | ||
ContextSnapshot snapshot = ContextSnapshot.capture(); | ||
ctx1.close(); | ||
|
||
ContextSnapshot.Reactivation reactivated = snapshot.reactivate(); | ||
reactivated.close(); | ||
} | ||
|
||
@Test | ||
public void testCreateSnapshot_withoutContextManagers() { | ||
ContextSnapshot snapshot = ContextSnapshot.capture(); | ||
assertThat(snapshot, is(notNullValue())); | ||
|
||
ContextSnapshot.Reactivation reactivated = snapshot.reactivate(); | ||
assertThat(reactivated, is(notNullValue())); | ||
reactivated.close(); | ||
} | ||
|
||
@Test | ||
public void testClearManagedContexts_withoutContextManagers() { | ||
Assertions.assertDoesNotThrow(() -> ContextManager.clearAll()); // there should be no exception | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
context-propagation-api/src/test/java/nl/talsmasoftware/context/dummy/DummyContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2016-2024 Talsma ICT | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package nl.talsmasoftware.context.dummy; | ||
|
||
import nl.talsmasoftware.context.api.Context; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* @author Sjoerd Talsma | ||
*/ | ||
public final class DummyContext implements Context<String> { | ||
private static final ThreadLocal<DummyContext> INSTANCE = new ThreadLocal<>(); | ||
|
||
private final DummyContext parent; | ||
private final String value; | ||
private final AtomicBoolean closed; | ||
|
||
public DummyContext(String newValue) { | ||
this.parent = INSTANCE.get(); | ||
this.value = newValue; | ||
this.closed = new AtomicBoolean(false); | ||
INSTANCE.set(this); | ||
} | ||
|
||
// Public for testing! | ||
public boolean isClosed() { | ||
return closed.get(); | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public void close() { | ||
if (closed.compareAndSet(false, true) && INSTANCE.get() == this) { | ||
DummyContext current = INSTANCE.get(); | ||
while (current != null && current.isClosed()) { | ||
current = current.parent; | ||
} | ||
if (current == null) { | ||
INSTANCE.remove(); | ||
} else { | ||
INSTANCE.set(current); | ||
} | ||
} | ||
} | ||
|
||
public static String currentValue() { | ||
final Context<String> currentContext = INSTANCE.get(); | ||
return currentContext != null ? currentContext.getValue() : null; | ||
} | ||
|
||
public static void setCurrentValue(String value) { | ||
new DummyContext(value); | ||
} | ||
|
||
public static void reset() { | ||
INSTANCE.remove(); | ||
} | ||
|
||
} |
Oops, something went wrong.