Skip to content
Snippets Groups Projects
Commit 77ede1c3 authored by Christian Wulf's avatar Christian Wulf
Browse files

refactored test

parent 1d6b8c35
No related branches found
No related tags found
No related merge requests found
/***************************************************************************
* Copyright 2014 Kieker Project (http://kieker-monitoring.net)
*
* 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 teetime.util.classpath;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* @param <T>
* the type that is used to cast a type that was found in the class path
*
* @author Christian Wulf
* @since 1.11
*/
public class CachedClassForNameResolver<T> {
private final ConcurrentMap<String, Class<? extends T>> cachedClasses = new ConcurrentHashMap<String, Class<? extends T>>(); // NOCS
private final ClassForNameResolver<T> classForNameResolver;
public CachedClassForNameResolver(final ClassForNameResolver<T> classForNameResolver) {
this.classForNameResolver = classForNameResolver;
}
/**
* This method tries to find a class with the given name.
*
* @param classname
* The name of the class.
*
* @return A {@link Class} instance corresponding to the given name, if it exists.
*
* @throws ClassNotFoundException
*/
public final Class<? extends T> classForName(final String classname) throws ClassNotFoundException {
Class<? extends T> clazz = this.cachedClasses.get(classname);
if (clazz == null) {
clazz = this.classForNameResolver.classForName(classname);
final Class<? extends T> previousClass = this.cachedClasses.putIfAbsent(classname, clazz);
if (null != previousClass) {
clazz = previousClass;
}
}
return clazz;
}
}
/***************************************************************************
* Copyright 2014 Kieker Project (http://kieker-monitoring.net)
*
* 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 teetime.util.classpath;
/**
* @param <T>
* the type that is used to cast a type that was found in the class path
*
* @author Christian Wulf
* @since 1.11
*/
public class ClassForNameResolver<T> {
private final Class<T> classToCast;
public ClassForNameResolver(final Class<T> classToCast) {
this.classToCast = classToCast;
}
/**
* This method tries to find a class with the given name.
*
* @param classname
* The name of the class.
*
* @return A {@link Class} instance corresponding to the given name, if it exists.
* @throws ClassNotFoundException
*
*/
public final Class<? extends T> classForName(final String classname) throws ClassNotFoundException {
final Class<?> clazz = Class.forName(classname);
return clazz.asSubclass(this.classToCast);
}
}
...@@ -11,7 +11,10 @@ import java.util.List; ...@@ -11,7 +11,10 @@ import java.util.List;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import teetime.util.classpath.ClassForNameResolver;
public class PipeFactoryLoaderTest { public class PipeFactoryLoaderTest {
@Test @Test
public void emptyConfig() throws IOException { public void emptyConfig() throws IOException {
List<IPipeFactory> list = PipeFactoryLoader.loadPipeFactoriesFromClasspath("data/empty-test.conf"); List<IPipeFactory> list = PipeFactoryLoader.loadPipeFactoriesFromClasspath("data/empty-test.conf");
...@@ -44,16 +47,17 @@ public class PipeFactoryLoaderTest { ...@@ -44,16 +47,17 @@ public class PipeFactoryLoaderTest {
// Second part of the test: PipeFactoryRegistry // Second part of the test: PipeFactoryRegistry
PipeFactoryRegistry pipeRegistry = PipeFactoryRegistry.INSTANCE; PipeFactoryRegistry pipeRegistry = PipeFactoryRegistry.INSTANCE;
ClassForNameResolver<IPipeFactory> classResolver = new ClassForNameResolver<IPipeFactory>(IPipeFactory.class);
// Look for the "normal" pipes // Look for the "normal" pipes
for (String string : readConf(pipeConfig)) { for (String className : readConf(pipeConfig)) {
IPipeFactory pipeFactory = getClassByString(string); IPipeFactory pipeFactory = classResolver.classForName(className).newInstance();
IPipeFactory returnedFactory = pipeRegistry.getPipeFactory(pipeFactory.getThreadCommunication(), pipeFactory.getOrdering(), pipeFactory.isGrowable()); IPipeFactory returnedFactory = pipeRegistry.getPipeFactory(pipeFactory.getThreadCommunication(), pipeFactory.getOrdering(), pipeFactory.isGrowable());
Assert.assertEquals(pipeFactory.getClass().getCanonicalName(), returnedFactory.getClass().getCanonicalName()); Assert.assertEquals(pipeFactory.getClass().getCanonicalName(), returnedFactory.getClass().getCanonicalName());
} }
// Second "and a half" part // Second "and a half" part
for (String string : readConf(testConfig)) { for (String className : readConf(testConfig)) {
IPipeFactory pipeFactory = getClassByString(string); IPipeFactory pipeFactory = classResolver.classForName(className).newInstance();
// Still old factory // Still old factory
IPipeFactory returnedFactory = pipeRegistry.getPipeFactory(pipeFactory.getThreadCommunication(), pipeFactory.getOrdering(), pipeFactory.isGrowable()); IPipeFactory returnedFactory = pipeRegistry.getPipeFactory(pipeFactory.getThreadCommunication(), pipeFactory.getOrdering(), pipeFactory.isGrowable());
Assert.assertNotEquals(pipeFactory.getClass().getCanonicalName(), returnedFactory.getClass().getCanonicalName()); Assert.assertNotEquals(pipeFactory.getClass().getCanonicalName(), returnedFactory.getClass().getCanonicalName());
...@@ -64,12 +68,6 @@ public class PipeFactoryLoaderTest { ...@@ -64,12 +68,6 @@ public class PipeFactoryLoaderTest {
} }
} }
private IPipeFactory getClassByString(final String string) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class<?> clazz = Class.forName(string);
Class<? extends IPipeFactory> pipeFactoryClass = clazz.asSubclass(IPipeFactory.class);
return pipeFactoryClass.newInstance();
}
private int countLines(final File fileName) throws IOException { private int countLines(final File fileName) throws IOException {
BufferedReader fileReader = new BufferedReader(new FileReader(fileName)); BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
int lines = 0;// NOPMD int lines = 0;// NOPMD
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment