Skip to content

Commit ca153ba

Browse files
committed
added support for junit5
- made the value of @name annotation optional, it defaults to "" - added collaborator provider provided instances to the DefaultServiceInstance - refactored the location of parameter resolvers - added the ability to resolve @real and @name parameters - added more test cases - fixed @systemtest annotation (missing SystemTestExtension)
1 parent a0dffd5 commit ca153ba

31 files changed

+586
-80
lines changed

modules/api/src/main/java/org/testifyproject/annotation/Name.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@
4040
*
4141
* @return the name
4242
*/
43-
String value();
43+
String value() default "";
4444
}

modules/api/src/test/resources/org/testifyproject/fixture/DiscoverableExplicitService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2017 Testify Project.
2+
* Copyright 2016-2018 Testify Project.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,8 @@
2323
* @author saden
2424
*/
2525
@Discoverable(DiscoverableExplicitContract.class)
26-
public class DiscoverableExplicitService implements DiscoverableContract, DiscoverableExplicitContract {
26+
public class DiscoverableExplicitService
27+
implements DiscoverableContract, DiscoverableExplicitContract {
2728

2829
@Override
2930
public String sayHello() {

modules/api/src/test/resources/org/testifyproject/fixture/DiscoverableMultiContractService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2017 Testify Project.
2+
* Copyright 2016-2018 Testify Project.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,8 @@
2323
* @author saden
2424
*/
2525
@Discoverable
26-
public class DiscoverableMultiContractService implements DiscoverableMultiContract1, DiscoverableMultiContract2 {
26+
public class DiscoverableMultiContractService
27+
implements DiscoverableMultiContract1, DiscoverableMultiContract2 {
2728

2829
@Override
2930
public String sayHello() {

modules/core/src/main/java/org/testifyproject/core/DefaultServiceInstance.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2017 Testify Project.
2+
* Copyright 2016-2018 Testify Project.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -87,8 +87,14 @@ public <T> T getService(Type type, Annotation... qualifiers) {
8787
}
8888

8989
} else if (Name.class.equals(qualifiers[0].annotationType())) {
90-
Name name = (Name) qualifiers[0];
91-
instance = serviceContext.get(ServiceKey.of(type, name.value()));
90+
Name annotation = (Name) qualifiers[0];
91+
String name = annotation.value();
92+
93+
if ("".equals(name)) {
94+
instance = serviceContext.get(ServiceKey.of(type));
95+
} else {
96+
instance = serviceContext.get(ServiceKey.of(type, name));
97+
}
9298
}
9399

94100
return (T) instance;

modules/core/src/main/java/org/testifyproject/core/DefaultServiceProvider.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2017 Testify Project.
2+
* Copyright 2016-2018 Testify Project.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,12 +16,16 @@
1616
package org.testifyproject.core;
1717

1818
import java.util.Map;
19+
import java.util.Objects;
1920
import java.util.concurrent.ConcurrentHashMap;
2021

2122
import org.testifyproject.ServiceInstance;
2223
import org.testifyproject.ServiceProvider;
2324
import org.testifyproject.TestContext;
25+
import org.testifyproject.TestDescriptor;
2426
import org.testifyproject.annotation.Discoverable;
27+
import org.testifyproject.annotation.Name;
28+
import org.testifyproject.core.extension.FindCollaborators;
2529
import org.testifyproject.core.util.ServiceLocatorUtil;
2630
import org.testifyproject.extension.InstanceProvider;
2731
import org.testifyproject.extension.annotation.UnitCategory;
@@ -45,7 +49,6 @@ public Map<ServiceKey, Object> create(TestContext testContext) {
4549
@Override
4650
public ServiceInstance configure(TestContext testContext,
4751
Map<ServiceKey, Object> serviceContext) {
48-
4952
//add instances
5053
ServiceLocatorUtil.INSTANCE
5154
.findAllWithFilter(InstanceProvider.class)
@@ -58,6 +61,29 @@ public ServiceInstance configure(TestContext testContext,
5861
);
5962
});
6063

64+
Object testInstance = testContext.getTestInstance();
65+
TestDescriptor testDescriptor = testContext.getTestDescriptor();
66+
67+
testDescriptor.getCollaboratorProviders().stream().forEach(methodDescriptor -> {
68+
new FindCollaborators(testDescriptor, methodDescriptor, testInstance)
69+
.execute()
70+
.filter(Objects::nonNull)
71+
.ifPresent(value -> {
72+
String name = methodDescriptor.getAnnotation(Name.class)
73+
.map(Name::value)
74+
.filter(p -> !p.isEmpty())
75+
.orElseGet(methodDescriptor::getDeclaredName);
76+
77+
Class<?> contract = value.getClass();
78+
ServiceKey contractAndNameKey = ServiceKey.of(contract, name);
79+
ServiceKey contractKey = ServiceKey.of(contract, name);
80+
81+
serviceContext.computeIfAbsent(contractAndNameKey, t -> value);
82+
serviceContext.computeIfAbsent(contractKey, t -> value);
83+
84+
});
85+
});
86+
6187
return new DefaultServiceInstance(serviceContext);
6288
}
6389

modules/core/src/main/java/org/testifyproject/core/extension/FindCollaborators.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.testifyproject.extension.Query;
2424

2525
/**
26-
* TODO.
26+
* A query to find collaborators for a method.
2727
*
2828
* @author saden
2929
*/
@@ -43,12 +43,14 @@ public FindCollaborators(TestDescriptor testDescriptor,
4343

4444
@Override
4545
public Optional<Object> execute() {
46+
//get the collaborators for the method
4647
Object[] collaborators = new GetCollaborators(
4748
testDescriptor,
4849
methodDescriptor,
4950
testInstance
5051
).execute();
5152

53+
//invoke the method and get the acutal collaborator
5254
return methodDescriptor.getInstance()
5355
.map(instance -> {
5456
return methodDescriptor.invoke(instance, convertToArray(collaborators));

modules/core/src/main/java/org/testifyproject/core/extension/GetCollaborator.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public class GetCollaborator implements Operation<Object> {
3434
private final Parameter parameter;
3535
private final Object testInstance;
3636

37-
public GetCollaborator(TestDescriptor testDescriptor, Parameter parameter,
37+
public GetCollaborator(TestDescriptor testDescriptor,
38+
Parameter parameter,
3839
Object testInstance) {
3940
this.testDescriptor = testDescriptor;
4041
this.parameter = parameter;
@@ -52,10 +53,10 @@ public Object execute() {
5253
parameter.getType().getSimpleName(),
5354
parameter.getName());
5455

55-
return foundMethodDescriptor.map(parameterMethodDescriptor -> {
56+
return foundMethodDescriptor.map(methodDescriptor -> {
5657
FindCollaborators query = new FindCollaborators(
5758
testDescriptor,
58-
parameterMethodDescriptor,
59+
methodDescriptor,
5960
testInstance);
6061
return query.execute().orElse(null);
6162
}).orElse(null);

modules/core/src/main/java/org/testifyproject/core/extension/GetCollaborators.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ public GetCollaborators(TestDescriptor testDescriptor,
4040

4141
@Override
4242
public Object[] execute() {
43-
return methodDescriptor.getParameters().stream()
44-
.map(parameter -> {
45-
return new GetCollaborator(testDescriptor, parameter, testInstance)
46-
.execute();
47-
}).toArray();
43+
return methodDescriptor.getParameters().stream().map(parameter -> {
44+
return new GetCollaborator(testDescriptor, parameter, testInstance).execute();
45+
}).toArray();
4846
}
4947

5048
}

modules/core/src/main/java/org/testifyproject/core/extension/GetMetaAnnotations.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import org.testifyproject.extension.Operation;
2525

2626
/**
27-
* TODO.
27+
* Get annotations with meta annotations for a parameter. This operation can discover
28+
* annotations on a parameter with the specified meta annotations. This is useful for
29+
* discovering custom qualifiers.
2830
*
2931
* @author saden
3032
*/
@@ -41,7 +43,6 @@ public GetMetaAnnotations(Parameter parameter,
4143

4244
@Override
4345
public Annotation[] execute() {
44-
//XXX: dont execute on this stream in parallel
4546
return Stream.of(metaAnnotationTypes).parallel()
4647
.flatMap(Collection::parallelStream)
4748
.distinct()

modules/core/src/main/java/org/testifyproject/core/util/logger/SimpleLoggerConfiguration.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,16 @@ void init() {
121121
}
122122

123123
private void loadProperties() {
124-
// Add props from the resource simplelogger.properties
125-
InputStream in = AccessController.doPrivileged(new PrivilegedAction<InputStream>() {
126-
@Override
127-
public InputStream run() {
128-
ClassLoader threadCL = Thread.currentThread().getContextClassLoader();
129-
if (threadCL != null) {
130-
return threadCL.getResourceAsStream(CONFIGURATION_FILE);
131-
} else {
132-
return ClassLoader.getSystemResourceAsStream(CONFIGURATION_FILE);
133-
}
124+
// add props from the resource simplelogger.properties
125+
InputStream in = AccessController.doPrivileged((PrivilegedAction<InputStream>) () -> {
126+
ClassLoader threadCL = Thread.currentThread().getContextClassLoader();
127+
if (threadCL != null) {
128+
return threadCL.getResourceAsStream(CONFIGURATION_FILE);
129+
} else {
130+
return ClassLoader.getSystemResourceAsStream(CONFIGURATION_FILE);
134131
}
135132
});
133+
136134
if (null != in) {
137135
try {
138136
properties.load(in);

0 commit comments

Comments
 (0)