Skip to content

Commit

Permalink
Namespace can be created with several parts.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Jan 19, 2016
1 parent 73347b3 commit affc4be
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,40 @@ void valueFromParentCanBeOverriddenInChild() {
}
}

@Nested
class CompositNamespaceTests {

@Test
void additionNamespacePartMakesADifferenc() {

Namespace ns1 = Namespace.of("part1", "part2");
Namespace ns2 = Namespace.of("part1");
Namespace ns3 = Namespace.of("part1", "part2");

Object value2 = createObject("value2");

parentStore.put(key, value, ns1);
parentStore.put(key, value2, ns2);

assertEquals(value, store.get(key, ns1));
assertEquals(value, store.get(key, ns3));
assertEquals(value2, store.get(key, ns2));
}

@Test
void orderOfNamespacePartsDoesNotMatter() {

Namespace ns1 = Namespace.of("part1", "part2");
Namespace ns2 = Namespace.of("part2", "part1");

parentStore.put(key, value, ns1);

assertEquals(value, store.get(key, ns1));
assertEquals(value, store.get(key, ns2));
}

}

private Object createObject(final String display) {
return new Object() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
package org.junit.gen5.api.extension;

import java.lang.reflect.AnnotatedElement;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;

import org.junit.gen5.commons.util.Preconditions;
Expand Down Expand Up @@ -112,16 +115,17 @@ public static class Namespace {

public static Namespace DEFAULT = Namespace.of(new Object());

public static Namespace of(Object ref) {
Preconditions.notNull(ref, "A local must not be null");
public static Namespace of(Object... parts) {
Preconditions.notEmpty(Arrays.asList(parts),
"There must be at least one reference object to create a namespace");

return new Namespace(ref);
return new Namespace(parts);
}

private final Object local;
private final Set<?> parts;

private Namespace(Object local) {
this.local = local;
private Namespace(Object... parts) {
this.parts = new HashSet<>(Arrays.asList(parts));
}

@Override
Expand All @@ -131,12 +135,12 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass())
return false;
Namespace namespace = (Namespace) o;
return local != null ? local.equals(namespace.local) : namespace.local == null;
return parts.equals(namespace.parts);
}

@Override
public int hashCode() {
return local != null ? local.hashCode() : 0;
return parts.hashCode();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ private static class TestMethodInvocationWrapper implements BeforeEachExtensionP

@Override
public void beforeEach(TestExtensionContext context) throws Exception {
ExtensionContext.Store times = context.getStore(namespace);
ExtensionContext.Store times = context.getStore(getNamespace(context));
times.put(context.getTestMethod(), System.currentTimeMillis());
}

private Namespace getNamespace(TestExtensionContext context) {
return Namespace.of(getClass(), context);
}

@Override
public void afterEach(TestExtensionContext context) throws Exception {
ExtensionContext.Store times = context.getStore(namespace);
ExtensionContext.Store times = context.getStore(getNamespace(context));
Method testMethod = context.getTestMethod();
long start = (long) times.remove(testMethod);
long duration = System.currentTimeMillis() - start;
Expand Down

0 comments on commit affc4be

Please sign in to comment.