Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dd82aad

Browse files
committedJan 13, 2016
Revise TestExtensionRegistry and related classes
- Revised visibility of methods in TestExtensionRegistry. - Renamed addExtension() to registerExtension() in TestExtensionRegistry. - Renamed LocalExtensionRegistry to DelegatingExtensionRegistry, documented its purpose, and avoided duplicate instantiation. - Removed superfluous use of generics in TestExtensionRegistry. - Overhauled documentation TestExtensionRegistry. - Removed extensionInstance in RegisteredExtensionPoint since it effectively duplicated extensionPoint. Issue: #112
1 parent 83c7e7b commit dd82aad

File tree

6 files changed

+141
-123
lines changed

6 files changed

+141
-123
lines changed
 

‎junit-tests/src/test/java/org/junit/gen5/engine/junit5/execution/ExtensionPointSortingTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
package org.junit.gen5.engine.junit5.execution;
1212

13-
import static org.junit.gen5.api.Assertions.*;
13+
import static org.junit.gen5.api.Assertions.assertEquals;
14+
import static org.junit.gen5.api.Assertions.assertTrue;
15+
import static org.junit.gen5.api.Assertions.expectThrows;
1416

1517
import java.util.ArrayList;
1618
import java.util.Arrays;
@@ -32,7 +34,6 @@ public class ExtensionPointSortingTests {
3234

3335
private ExtensionPointSorter sorter;
3436
private List<RegisteredExtensionPoint<LocalExtensionPoint>> pointsToSort;
35-
private int pointIndex = 0;
3637

3738
@BeforeEach
3839
public void init() {
@@ -148,7 +149,7 @@ protected void assertSorting(RegisteredExtensionPoint... points) {
148149

149150
protected RegisteredExtensionPoint<LocalExtensionPoint> createExtensionPoint(Position position) {
150151
return new RegisteredExtensionPoint<>(() -> {
151-
}, position, "" + ++pointIndex);
152+
}, position);
152153
}
153154

154155
@FunctionalInterface

‎junit-tests/src/test/java/org/junit/gen5/engine/junit5/execution/TestExtensionRegistryTests.java

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import org.junit.gen5.engine.junit5.extension.DisabledCondition;
3131
import org.junit.gen5.engine.junit5.extension.TestInfoParameterResolver;
3232

33+
/**
34+
* @since 5.0
35+
*/
3336
public class TestExtensionRegistryTests {
3437

3538
private TestExtensionRegistry registry;
@@ -41,17 +44,17 @@ public void initRegistry() {
4144

4245
@Test
4346
public void checkDefaultExtensions() {
44-
assertEquals(2, TestExtensionRegistry.getDefaultExtensionClasses().size());
47+
assertEquals(2, TestExtensionRegistry.getDefaultExtensionTypes().size());
4548

4649
assertDefaultExtensionType(DisabledCondition.class);
4750
assertDefaultExtensionType(TestInfoParameterResolver.class);
4851
}
4952

5053
@Test
5154
public void newRegistryWithoutParentHasDefaultExtensions() throws Exception {
52-
Set<Class<? extends TestExtension>> extensions = registry.getRegisteredExtensionClasses();
55+
Set<Class<? extends TestExtension>> extensions = registry.getRegisteredExtensionTypes();
5356

54-
assertEquals(TestExtensionRegistry.getDefaultExtensionClasses().size(), extensions.size());
57+
assertEquals(TestExtensionRegistry.getDefaultExtensionTypes().size(), extensions.size());
5558
assertExtensionRegistered(registry, DisabledCondition.class);
5659
assertExtensionRegistered(registry, TestInfoParameterResolver.class);
5760

@@ -63,28 +66,28 @@ public void newRegistryWithoutParentHasDefaultExtensions() throws Exception {
6366
@Test
6467
public void registerExtensionByImplementingClass() throws Exception {
6568

66-
registry.addExtension(MyExtension.class);
69+
registry.registerExtension(MyExtension.class);
6770

6871
assertExtensionRegistered(registry, MyExtension.class);
6972

70-
int rememberSize = registry.getRegisteredExtensionClasses().size();
71-
registry.addExtension(MyExtension.class);
72-
registry.addExtension(MyExtension.class);
73-
registry.addExtension(MyExtension.class);
73+
int rememberSize = registry.getRegisteredExtensionTypes().size();
74+
registry.registerExtension(MyExtension.class);
75+
registry.registerExtension(MyExtension.class);
76+
registry.registerExtension(MyExtension.class);
7477

75-
assertEquals(rememberSize, registry.getRegisteredExtensionClasses().size());
78+
assertEquals(rememberSize, registry.getRegisteredExtensionTypes().size());
7679
assertExtensionRegistered(registry, MyExtension.class);
7780
assertEquals(1, countExtensionPoints(MyExtensionPoint.class));
7881

79-
registry.addExtension(YourExtension.class);
82+
registry.registerExtension(YourExtension.class);
8083
assertExtensionRegistered(registry, YourExtension.class);
8184
assertEquals(2, countExtensionPoints(MyExtensionPoint.class));
8285
}
8386

8487
@Test
8588
public void registerTestExtensionThatImplementsMultipleExtensionPoints() throws Exception {
8689

87-
registry.addExtension(MultipleExtension.class);
90+
registry.registerExtension(MultipleExtension.class);
8891

8992
assertExtensionRegistered(registry, MultipleExtension.class);
9093

@@ -96,10 +99,10 @@ public void registerTestExtensionThatImplementsMultipleExtensionPoints() throws
9699
public void extensionsAreInheritedFromParent() throws Exception {
97100

98101
TestExtensionRegistry parent = new TestExtensionRegistry();
99-
parent.addExtension(MyExtension.class);
102+
parent.registerExtension(MyExtension.class);
100103

101104
registry = new TestExtensionRegistry(parent);
102-
registry.addExtension(YourExtension.class);
105+
registry.registerExtension(YourExtension.class);
103106

104107
assertExtensionRegistered(registry, MyExtension.class);
105108
assertExtensionRegistered(registry, YourExtension.class);
@@ -114,7 +117,7 @@ public void extensionsAreInheritedFromParent() throws Exception {
114117
@Test
115118
public void registerExtensionPointsByExtensionRegistrar() throws Exception {
116119

117-
registry.addExtension(MyExtensionRegistrar.class);
120+
registry.registerExtension(MyExtensionRegistrar.class);
118121

119122
assertExtensionRegistered(registry, MyExtensionRegistrar.class);
120123
assertEquals(1, countExtensionPoints(MyExtensionPoint.class));
@@ -124,15 +127,15 @@ public void registerExtensionPointsByExtensionRegistrar() throws Exception {
124127
@Test
125128
public void canStreamOverRegisteredExceptionPoint() throws Exception {
126129

127-
registry.addExtension(MyExtension.class);
130+
registry.registerExtension(MyExtension.class);
128131

129132
AtomicBoolean hasRun = new AtomicBoolean(false);
130133

131134
registry.stream(MyExtensionPoint.class, TestExtensionRegistry.ApplicationOrder.FORWARD).forEach(
132135
registeredExtensionPoint -> {
133-
assertEquals(MyExtension.class, registeredExtensionPoint.getExtensionInstance().getClass());
134-
assertEquals(Position.DEFAULT, registeredExtensionPoint.getPosition());
135136
assertTrue(registeredExtensionPoint.getExtensionPoint() instanceof MyExtensionPoint);
137+
assertEquals(MyExtension.class.getName(), registeredExtensionPoint.getExtensionName());
138+
assertEquals(Position.DEFAULT, registeredExtensionPoint.getPosition());
136139
hasRun.set(true);
137140
});
138141

@@ -143,16 +146,17 @@ public void canStreamOverRegisteredExceptionPoint() throws Exception {
143146
@Test
144147
public void registerExtensionPointDirectly() throws Exception {
145148

146-
registry.registerExtension((MyExtensionPoint) test -> {
147-
}, Position.INNERMOST, "anonymous extension");
149+
registry.registerExtensionPoint((MyExtensionPoint) test -> {
150+
}, Position.INNERMOST);
148151

149152
AtomicBoolean hasRun = new AtomicBoolean(false);
150153

151154
registry.stream(MyExtensionPoint.class, TestExtensionRegistry.ApplicationOrder.FORWARD).forEach(
152155
registeredExtensionPoint -> {
153-
assertEquals("anonymous extension", registeredExtensionPoint.getExtensionName());
154-
assertEquals(Position.INNERMOST, registeredExtensionPoint.getPosition());
155156
assertTrue(registeredExtensionPoint.getExtensionPoint() instanceof MyExtensionPoint);
157+
assertTrue(registeredExtensionPoint.getExtensionName().startsWith(getClass().getName() + "$$Lambda$"));
158+
assertTrue(registeredExtensionPoint.getExtensionName().contains("$Lambda$"));
159+
assertEquals(Position.INNERMOST, registeredExtensionPoint.getPosition());
156160
hasRun.set(true);
157161
});
158162

@@ -169,12 +173,12 @@ private int countExtensionPoints(Class<? extends ExtensionPoint> extensionPointT
169173

170174
private void assertExtensionRegistered(TestExtensionRegistry registry,
171175
Class<? extends TestExtension> extensionClass) {
172-
assertTrue(registry.getRegisteredExtensionClasses().contains(extensionClass),
176+
assertTrue(registry.getRegisteredExtensionTypes().contains(extensionClass),
173177
() -> extensionClass.getSimpleName() + " should be present");
174178
}
175179

176180
private void assertDefaultExtensionType(Class<?> extensionType) {
177-
assertTrue(TestExtensionRegistry.getDefaultExtensionClasses().contains(extensionType),
181+
assertTrue(TestExtensionRegistry.getDefaultExtensionTypes().contains(extensionType),
178182
() -> extensionType.getName() + " should be a default extension");
179183
}
180184

‎junit5-engine/src/main/java/org/junit/gen5/engine/junit5/descriptor/ClassTestDescriptor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ private void registerAnnotatedMethodsAsExtensions(TestExtensionRegistry extensio
212212
findAnnotatedMethods(testClass, annotationType, MethodSortOrder.HierarchyDown).stream()
213213
.peek(method -> methodValidator.accept(extensionType, method))
214214
.forEach(method ->
215-
extensionRegistry.registerExtension(extensionPointSynthesizer.apply(extensionRegistry, method),
216-
ExtensionPoint.Position.DEFAULT, method));
215+
extensionRegistry.registerExtensionPoint(extensionPointSynthesizer.apply(extensionRegistry, method)));
217216
// @formatter:on
218217
}
219218

‎junit5-engine/src/main/java/org/junit/gen5/engine/junit5/execution/ExtensionPointSorter.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@
1919
import org.junit.gen5.api.extension.ExtensionPoint.Position;
2020

2121
/**
22-
* Class for sorting {@linkplain RegisteredExtensionPoint points} according to their {@linkplain Position}:
23-
* {@code OUTERMOST -> OUTSIDE_DEFAULT -> DEFAULT -> INSIDE_DEFAULT -> INNERMOST}
22+
* Utility for sorting {@linkplain RegisteredExtensionPoint extension points}
23+
* according to their {@linkplain Position}:
24+
* {@link Position#OUTERMOST OUTERMOST} &raquo;
25+
* {@link Position#OUTSIDE_DEFAULT OUTSIDE_DEFAULT} &raquo;
26+
* {@link Position#DEFAULT DEFAULT} &raquo;
27+
* {@link Position#INSIDE_DEFAULT INSIDE_DEFAULT} &raquo;
28+
* {@link Position#INNERMOST INNERMOST}.
2429
*
2530
* @since 5.0
2631
*/
@@ -29,13 +34,12 @@ public class ExtensionPointSorter {
2934
/**
3035
* Sort the list of extension points according to their specified {@linkplain Position}.
3136
*
32-
* <p>The list instance will be resorted.
37+
* <p>Note: the supplied list instance will be resorted.
3338
*
34-
* @param registeredExtensionPoints List of extension points in order of registration
39+
* @param registeredExtensionPoints list of extension points in order of registration
3540
* @param <T> concrete subtype of {@link ExtensionPoint}
3641
*/
3742
public <T extends ExtensionPoint> void sort(List<RegisteredExtensionPoint<T>> registeredExtensionPoints) {
38-
3943
checkPositionUnique(registeredExtensionPoints, Position.INNERMOST);
4044
checkPositionUnique(registeredExtensionPoints, Position.OUTERMOST);
4145
registeredExtensionPoints.sort(new DefaultComparator());

‎junit5-engine/src/main/java/org/junit/gen5/engine/junit5/execution/RegisteredExtensionPoint.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,38 @@
1818
* Represents an {@linkplain ExtensionPoint extension} registered in a
1919
* {@link TestExtensionRegistry}.
2020
*
21-
* @param <T> the concrete subtype of {@link ExtensionPoint} to be registered
21+
* @param <E> the type of registered {@link ExtensionPoint}
2222
* @since 5.0
2323
*/
24-
public class RegisteredExtensionPoint<T extends ExtensionPoint> {
24+
public class RegisteredExtensionPoint<E extends ExtensionPoint> {
2525

26-
private final T extensionPoint;
26+
private final E extensionPoint;
2727

2828
private final Position position;
2929

30-
private final Object extensionInstance;
31-
32-
public RegisteredExtensionPoint(T extensionPoint, Position position, Object extensionInstance) {
30+
public RegisteredExtensionPoint(E extensionPoint, Position position) {
3331
this.extensionPoint = extensionPoint;
3432
this.position = position;
35-
this.extensionInstance = extensionInstance;
3633
}
3734

38-
public T getExtensionPoint() {
39-
return extensionPoint;
35+
public E getExtensionPoint() {
36+
return this.extensionPoint;
4037
}
4138

4239
public Position getPosition() {
43-
return position;
44-
}
45-
46-
public Object getExtensionInstance() {
47-
return extensionInstance;
40+
return this.position;
4841
}
4942

5043
public String getExtensionName() {
51-
return extensionInstance.toString();
44+
return this.extensionPoint.getClass().getName();
5245
}
5346

5447
@Override
5548
public String toString() {
5649
// @formatter:off
5750
return new ToStringBuilder(this)
5851
.append("position", this.position)
59-
.append("extensionInstance", this.extensionInstance)
52+
.append("extensionPoint", this.extensionPoint)
6053
.toString();
6154
// @formatter:on
6255
}

‎junit5-engine/src/main/java/org/junit/gen5/engine/junit5/execution/TestExtensionRegistry.java

Lines changed: 91 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@
2929
import org.junit.gen5.engine.junit5.extension.TestInfoParameterResolver;
3030

3131
/**
32-
* A {@code TestExtensionRegistry registry} serves to hold all registered extensions (i.e. instances of
33-
* {@linkplain ExtensionPoint}) for a given {@linkplain org.junit.gen5.engine.Container} or
34-
* {@linkplain org.junit.gen5.engine.Leaf}. A registry has a reference to a parent registry and all lookups are done in
35-
* itself and in its parent and thereby all its ancestors. Do not confuse with {@linkplain ExtensionRegistry} which is
36-
* an interface used by {@linkplain ExtensionRegistrar}
32+
* A {@code TestExtensionRegistry} holds all registered extensions (i.e.
33+
* instances of {@link ExtensionPoint}) for a given
34+
* {@link org.junit.gen5.engine.Container} or {@link org.junit.gen5.engine.Leaf}.
35+
*
36+
* <p>A registry has a reference to its parent registry, and all lookups are
37+
* performed first in the current registry itself and then in its parent and
38+
* thereby all its ancestors.
39+
*
40+
* <p>Do not confuse this engine-specific {@code TestExtensionRegistry} with
41+
* the {@link ExtensionRegistry} which is used by an {@link ExtensionRegistrar}.
3742
*
3843
* @since 5.0
3944
*/
@@ -44,88 +49,94 @@ public enum ApplicationOrder {
4449
}
4550

4651
/**
47-
* Used to create and populate a new registry from a list of extensions and a parent.
52+
* Factory for creating and populating a new registry from a list of
53+
* extension types and a parent registry.
4854
*
49-
* @param parentRegistry The parent registry to be used.
50-
* @param extensionClasses The extensions to be registered in the new registry
51-
* @return a new TestExtensionRegistry
55+
* @param parentRegistry the parent registry to be used
56+
* @param extensionTypes the types of extensions to be registered in
57+
* the new registry
58+
* @return a new {@code TestExtensionRegistry}
5259
*/
5360
public static TestExtensionRegistry newRegistryFrom(TestExtensionRegistry parentRegistry,
54-
List<Class<? extends TestExtension>> extensionClasses) {
61+
List<Class<? extends TestExtension>> extensionTypes) {
62+
5563
TestExtensionRegistry newTestExtensionRegistry = new TestExtensionRegistry(parentRegistry);
56-
extensionClasses.forEach(newTestExtensionRegistry::addExtension);
64+
extensionTypes.forEach(newTestExtensionRegistry::registerExtension);
5765
return newTestExtensionRegistry;
5866
}
5967

60-
private static final List<Class<? extends TestExtension>> defaultExtensionClasses = Collections.unmodifiableList(
68+
private static final List<Class<? extends TestExtension>> defaultExtensionTypes = Collections.unmodifiableList(
6169
Arrays.asList(DisabledCondition.class, TestInfoParameterResolver.class));
6270

6371
/**
64-
* @return all extension classes that are added by default to all root registries
72+
* @return the list of all extension types that are added by default to all root registries
6573
*/
66-
public static List<Class<? extends TestExtension>> getDefaultExtensionClasses() {
67-
return defaultExtensionClasses;
74+
static List<Class<? extends TestExtension>> getDefaultExtensionTypes() {
75+
return defaultExtensionTypes;
6876
}
6977

70-
private final Set<Class<? extends TestExtension>> registeredExtensionClasses = new LinkedHashSet<>();
78+
private final Set<Class<? extends TestExtension>> registeredExtensionTypes = new LinkedHashSet<>();
7179

7280
private final List<RegisteredExtensionPoint<?>> registeredExtensionPoints = new ArrayList<>();
7381

82+
private final ExtensionRegistry delegatingExtensionRegistry = new DelegatingExtensionRegistry();
83+
7484
private final Optional<TestExtensionRegistry> parent;
7585

7686
public TestExtensionRegistry() {
7787
this(null);
7888
}
7989

80-
public TestExtensionRegistry(TestExtensionRegistry parent) {
90+
TestExtensionRegistry(TestExtensionRegistry parent) {
8191
this.parent = Optional.ofNullable(parent);
8292
if (!this.parent.isPresent()) {
8393
addDefaultExtensions();
8494
}
8595
}
8696

8797
private void addDefaultExtensions() {
88-
getDefaultExtensionClasses().stream().forEach(this::addExtension);
98+
getDefaultExtensionTypes().stream().forEach(this::registerExtension);
8999
}
90100

91101
/**
92-
* @return all extension classes registered in this registry or one of its ancestors
102+
* @return all extension types registered in this registry or one of its ancestors
93103
*/
94-
public Set<Class<? extends TestExtension>> getRegisteredExtensionClasses() {
95-
Set<Class<? extends TestExtension>> allRegisteredExtensionClasses = new LinkedHashSet<>();
104+
Set<Class<? extends TestExtension>> getRegisteredExtensionTypes() {
105+
Set<Class<? extends TestExtension>> allRegisteredExtensionTypes = new LinkedHashSet<>();
96106
this.parent.ifPresent(
97-
parentRegistry -> allRegisteredExtensionClasses.addAll(parentRegistry.getRegisteredExtensionClasses()));
98-
allRegisteredExtensionClasses.addAll(this.registeredExtensionClasses);
99-
return Collections.unmodifiableSet(allRegisteredExtensionClasses);
107+
parentRegistry -> allRegisteredExtensionTypes.addAll(parentRegistry.getRegisteredExtensionTypes()));
108+
allRegisteredExtensionTypes.addAll(this.registeredExtensionTypes);
109+
return Collections.unmodifiableSet(allRegisteredExtensionTypes);
100110
}
101111

102112
@SuppressWarnings("unchecked")
103-
private <T extends ExtensionPoint> List<RegisteredExtensionPoint<T>> getRegisteredExtensionPoints(
104-
Class<T> extensionClass) {
113+
private <E extends ExtensionPoint> List<RegisteredExtensionPoint<E>> getRegisteredExtensionPoints(
114+
Class<E> extensionType) {
105115

106-
List<RegisteredExtensionPoint<T>> allExtensionPoints = new ArrayList<>();
116+
List<RegisteredExtensionPoint<E>> allExtensionPoints = new ArrayList<>();
107117
this.parent.ifPresent(
108-
parentRegistry -> allExtensionPoints.addAll(parentRegistry.getRegisteredExtensionPoints(extensionClass)));
118+
parentRegistry -> allExtensionPoints.addAll(parentRegistry.getRegisteredExtensionPoints(extensionType)));
109119

110-
//@formatter:off
111-
registeredExtensionPoints.stream()
112-
.filter(registeredExtensionPoint -> extensionClass.isAssignableFrom(registeredExtensionPoint.getExtensionPoint().getClass()))
113-
.forEach(extensionPoint -> allExtensionPoints.add((RegisteredExtensionPoint<T>) extensionPoint));
114-
//@formatter:on
120+
// @formatter:off
121+
this.registeredExtensionPoints.stream()
122+
.filter(registeredExtensionPoint -> extensionType.isAssignableFrom(registeredExtensionPoint.getExtensionPoint().getClass()))
123+
.forEach(extensionPoint -> allExtensionPoints.add((RegisteredExtensionPoint<E>) extensionPoint));
124+
// @formatter:on
115125

116126
return allExtensionPoints;
117127
}
118128

119129
/**
120-
* Return a stream for iterating all registered extension points.
130+
* Return a stream for iterating over all registered extension points
131+
* of the specified type.
121132
*
122-
* @param <T> The exact {@link ExtensionPoint} for which to find all extensions
123-
* @param extensionClass The {@link ExtensionPoint} class
124-
* @param order The order in which to apply the extension points after sorting. FORWARD or BACKWARD.
133+
* @param extensionType the type of {@link ExtensionPoint} to stream
134+
* @param order the order in which to apply the extension points after sorting
125135
*/
126-
public <T extends ExtensionPoint> Stream<RegisteredExtensionPoint<T>> stream(Class<T> extensionClass,
136+
public <E extends ExtensionPoint> Stream<RegisteredExtensionPoint<E>> stream(Class<E> extensionType,
127137
ApplicationOrder order) {
128-
List<RegisteredExtensionPoint<T>> registeredExtensionPoints = getRegisteredExtensionPoints(extensionClass);
138+
139+
List<RegisteredExtensionPoint<E>> registeredExtensionPoints = getRegisteredExtensionPoints(extensionType);
129140
new ExtensionPointSorter().sort(registeredExtensionPoints);
130141
if (order == ApplicationOrder.BACKWARD) {
131142
Collections.reverse(registeredExtensionPoints);
@@ -134,58 +145,64 @@ public <T extends ExtensionPoint> Stream<RegisteredExtensionPoint<T>> stream(Cla
134145
}
135146

136147
/**
137-
* Register an extension class which can be either an {@linkplain ExtensionPoint} implementatio or an
138-
* {@linkplain ExtensionRegistrar}
148+
* Instantiate an extension of the given type using its default constructor,
149+
* and register the extension in this registry.
150+
*
151+
* <p>If an extension of the given type already exists in this registry,
152+
* a new extension will not be registered.
139153
*
140-
* @param extensionClass The test extension class to be registered
154+
* <p>The extension type can be either an {@link ExtensionPoint} or an
155+
* {@link ExtensionRegistrar}.
156+
*
157+
* @param extensionType the type extension to register
141158
*/
142-
public void addExtension(Class<? extends TestExtension> extensionClass) {
143-
boolean extensionExists = getRegisteredExtensionClasses().stream().anyMatch(
144-
registeredClass -> registeredClass.equals(extensionClass));
145-
if (!extensionExists) {
146-
TestExtension testExtension = ReflectionUtils.newInstance(extensionClass);
147-
registerExtensionPointImplementors(testExtension);
148-
registerFromExtensionRegistrar(testExtension);
149-
this.registeredExtensionClasses.add(extensionClass);
150-
}
151-
}
159+
void registerExtension(Class<? extends TestExtension> extensionType) {
152160

153-
private void registerFromExtensionRegistrar(TestExtension testExtension) {
154-
if (testExtension instanceof ExtensionRegistrar) {
155-
ExtensionRegistrar extensionRegistrar = (ExtensionRegistrar) testExtension;
156-
ExtensionRegistry extensionRegistry = createExtensionRegistry(extensionRegistrar);
157-
extensionRegistrar.registerExtensions(extensionRegistry);
158-
}
159-
}
161+
boolean extensionAlreadyRegistered = getRegisteredExtensionTypes().stream().anyMatch(
162+
registeredType -> registeredType.equals(extensionType));
160163

161-
private TestExtensionRegistry.LocalExtensionRegistry createExtensionRegistry(Object extensionInstance) {
162-
return new TestExtensionRegistry.LocalExtensionRegistry(extensionInstance);
164+
if (!extensionAlreadyRegistered) {
165+
TestExtension testExtension = ReflectionUtils.newInstance(extensionType);
166+
registerExtensionPoint(testExtension);
167+
registerExtensionPointsFromRegistrar(testExtension);
168+
this.registeredExtensionTypes.add(extensionType);
169+
}
163170
}
164171

165-
private void registerExtensionPointImplementors(TestExtension testExtension) {
172+
private void registerExtensionPoint(TestExtension testExtension) {
173+
// TODO Determine why TestExtensions are not supported.
166174
if (testExtension instanceof ExtensionPoint) {
167-
ExtensionPoint extension = (ExtensionPoint) testExtension;
168-
registerExtension(extension, Position.DEFAULT, testExtension);
175+
registerExtensionPoint((ExtensionPoint) testExtension);
169176
}
170177
}
171178

172-
public <E extends ExtensionPoint> void registerExtension(E extension, Position position, Object extensionInstance) {
173-
RegisteredExtensionPoint<E> registeredExtensionPoint = new RegisteredExtensionPoint<>(extension, position,
174-
extensionInstance);
175-
registeredExtensionPoints.add(registeredExtensionPoint);
179+
public void registerExtensionPoint(ExtensionPoint extension) {
180+
registerExtensionPoint(extension, Position.DEFAULT);
176181
}
177182

178-
private class LocalExtensionRegistry implements ExtensionRegistry {
179-
180-
private Object extensionInstance;
183+
void registerExtensionPoint(ExtensionPoint extension, Position position) {
184+
this.registeredExtensionPoints.add(new RegisteredExtensionPoint<>(extension, position));
185+
}
181186

182-
private LocalExtensionRegistry(Object extensionInstance) {
183-
this.extensionInstance = extensionInstance;
187+
private void registerExtensionPointsFromRegistrar(TestExtension testExtension) {
188+
if (testExtension instanceof ExtensionRegistrar) {
189+
ExtensionRegistrar extensionRegistrar = (ExtensionRegistrar) testExtension;
190+
extensionRegistrar.registerExtensions(this.delegatingExtensionRegistry);
184191
}
192+
}
193+
194+
/**
195+
* {@link ExtensionRegistry} which internally delegates to the enclosing
196+
* {@link TestExtensionRegistry}: used in order to allow an
197+
* {@link ExtensionRegistrar} to populate a {@link TestExtensionRegistry}
198+
* via the {@code ExtensionRegistry} API.
199+
*/
200+
private class DelegatingExtensionRegistry implements ExtensionRegistry {
185201

186202
@Override
187203
public <E extends ExtensionPoint> void register(E extension, Class<E> extensionPointType, Position position) {
188-
registerExtension(extension, position, extensionInstance);
204+
registerExtensionPoint(extension, position);
189205
}
190206
}
207+
191208
}

0 commit comments

Comments
 (0)
Please sign in to comment.