org.deephacks.tools4j.config.test.FeatureTestsBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tools4j-config-tck Show documentation
Show all versions of tools4j-config-tck Show documentation
Functional Tests for Tools4j Config
/**
* 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 org.deephacks.tools4j.config.test;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import org.junit.runners.Parameterized.Parameters;
import org.scannotation.AnnotationDB;
import org.scannotation.ClasspathUrlFinder;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* {@link FeatureTestsBuilder} is to be used by {@link FeatureTests} to build a set of
* feature tests.
*/
public abstract class FeatureTestsBuilder {
/** name of the test suite as shown in junit test execution */
protected String name;
/** Run before every test */
private Map, Object> setUp = new LinkedHashMap, Object>();
/** Run after every test */
private Map, Object> tearDown = new LinkedHashMap, Object>();
/** TestSetupTeardown classes found on classpath */
private static final Map> setupTeardownRegistry = new HashMap>();
/** TestSetupTeardown to run for each test */
private static final LinkedHashMap> setupTeardowns = new LinkedHashMap>();
/** TestSetupTeardown that are parameterized to run for each test */
private static final Map, Method> parameterizedMethods = new HashMap, Method>();
static {
// search classpath for @TestSetupTeardown annotated classes
findSetupTeardownClasses();
}
/**
* Set implementation for a specific service interface. Will automatically
* enforce any setup/teardown requirements that the implementation might have
* by searching classpath for TestSetupTeardown annotated classes.
*/
public FeatureTestsBuilder using(Class> service, Object impl) {
// make sure Lookup.get().lookup() find the the right implementation.
LookupProxy.register(service, impl);
Class> setupTeardown = setupTeardownRegistry.get(service.getName());
if (setupTeardown != null) {
// make sure implementation setup/teardown is run.
Method m = getParameterizedMethod(setupTeardown);
if (m != null) {
parameterizedMethods.put(setupTeardown, m);
} else {
setupTeardowns.put(setupTeardown.getName(), setupTeardown);
}
}
return this;
}
/**
* Add setup classes to be run @Before
*/
public FeatureTestsBuilder withSetUp(Object setUp) {
this.setUp.put(setUp.getClass(), setUp);
return this;
}
protected Map, Object> getSetUp() {
return setUp;
}
/**
* Add teardown classes to be run @After
*/
public FeatureTestsBuilder withTearDown(Object tearDown) {
this.tearDown.put(tearDown.getClass(), setUp);
return this;
}
protected Map, Object> getTearDown() {
return tearDown;
}
protected abstract List> getTests();
public List build() {
final List> testClasses = getTests();
List rounds = new ArrayList<>();
for (Class> test : testClasses) {
ArrayList