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

corrected some structure and PMD issues

parent 9d2147f6
No related branches found
No related tags found
No related merge requests found
#FindBugs User Preferences #FindBugs User Preferences
#Wed Oct 22 13:08:20 CEST 2014 #Mon Nov 03 07:44:13 CET 2014
detector_threshold=3 detector_threshold=3
effort=max effort=max
excludefilter0=.fbExcludeFilterFile|true excludefilter0=.fbExcludeFilterFile|true
......
...@@ -8,19 +8,18 @@ import java.util.List; ...@@ -8,19 +8,18 @@ import java.util.List;
public final class FileSearcher { public final class FileSearcher {
private FileSearcher() { private static final ClassLoader CLASS_LOADER = ClassLoader.getSystemClassLoader();
}; private FileSearcher() {
// utility class
}
@SuppressWarnings("PMD.LawOfDemeter")
// LawOfDemeter not solvable in this case
public static List<URL> loadResources(final String name) throws IOException { public static List<URL> loadResources(final String name) throws IOException {
final List<URL> list = new ArrayList<URL>(); final List<URL> list = new ArrayList<URL>();
final Enumeration<URL> systemRes = ClassLoader.getSystemClassLoader().getResources(name); final Enumeration<URL> systemRes = CLASS_LOADER.getResources(name);
while (systemRes.hasMoreElements()) { while (systemRes.hasMoreElements()) { // NOPMD
list.add(systemRes.nextElement()); list.add(systemRes.nextElement()); // NOPMD
} }
return list; return list;
} }
......
...@@ -12,15 +12,15 @@ public final class ArrayWrapper<T> { ...@@ -12,15 +12,15 @@ public final class ArrayWrapper<T> {
this.elements = (T[]) new Object[initialCapacity]; this.elements = (T[]) new Object[initialCapacity];
} }
public final T get(final int index) { public T get(final int index) {
return this.elements[index]; return this.elements[index];
} }
public final void put(final int index, final T element) { public void put(final int index, final T element) {
this.elements[index] = element; this.elements[index] = element;
} }
public final int getCapacity() { public int getCapacity() {
return this.elements.length; return this.elements.length;
} }
......
package teetime.framework; package teetime.framework;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import teetime.framework.pipe.IPipeFactory;
import teetime.framework.pipe.PipeFactoryLoader;
import teetime.framework.pipe.PipeFactoryRegistry;
public class FileSearcherTest { public class FileSearcherTest {
@Test @Test
public void fileInClasspath() throws IOException { public void fileInClasspath() throws IOException {
List<URL> list = FileSearcher.loadResources("pipe-factories.conf"); List<URL> list = FileSearcher.loadResources("pipe-factories.conf");
Assert.assertEquals(false, list.isEmpty()); Assert.assertEquals(false, list.isEmpty());// NOPMD
} }
@Test @Test
public void multipleFiles() throws IOException { public void multipleFiles() throws IOException {
List<URL> list = FileSearcher.loadResources("LICENSE.txt"); List<URL> list = FileSearcher.loadResources("LICENSE.txt");
Assert.assertEquals(true, list.size() > 1); Assert.assertEquals(true, list.size() > 1);// NOPMD
} }
@Test @Test
public void missingFile() throws IOException { public void missingFile() throws IOException {
List<URL> list = FileSearcher.loadResources("filethatdoesnotexistinanyproject.nope"); List<URL> list = FileSearcher.loadResources("filethatdoesnotexistinanyproject.nope");
Assert.assertEquals(true, list.isEmpty()); Assert.assertEquals(true, list.isEmpty());// NOPMD
}
@Test
public void emptyConfig() throws IOException {
List<IPipeFactory> list = PipeFactoryLoader.loadPipeFactoriesFromClasspath("data/empty-test.conf");
Assert.assertEquals(true, list.isEmpty());
}
@Test
public void singleConfig() throws IOException {
List<IPipeFactory> list = PipeFactoryLoader.loadPipeFactoriesFromClasspath("pipe-factories.conf");
int lines = this.countLines(new File("conf/pipe-factories.conf"));
Assert.assertEquals(lines, list.size());
} }
@Test
public void multipleConfigs() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
List<URL> files = new ArrayList<URL>();
File pipeConfig = new File("conf/pipe-factories.conf");
File testConfig = new File("src/test/resources/data/normal-test.conf");
files.add(testConfig.toURI().toURL());
files.add(pipeConfig.toURI().toURL());
List<IPipeFactory> pipeFactories = PipeFactoryLoader.mergeFiles(files);
ArrayList<String> contents = readConf(pipeConfig);
contents.addAll(readConf(testConfig));
// Check if all read factories are contained in one of the files
for (IPipeFactory iPipeFactory : pipeFactories) {
Assert.assertTrue(contents.indexOf(iPipeFactory.getClass().getCanonicalName()) != -1);
}
// Second part of the test: PipeFactoryRegistry
PipeFactoryRegistry pipeRegistry = PipeFactoryRegistry.INSTANCE;
// Look for the "normal" pipes
for (String string : readConf(pipeConfig)) {
IPipeFactory pipeFactory = getClassByString(string);
IPipeFactory returnedFactory = pipeRegistry.getPipeFactory(pipeFactory.getThreadCommunication(), pipeFactory.getOrdering(), pipeFactory.isGrowable());
Assert.assertEquals(pipeFactory.getClass().getCanonicalName(), returnedFactory.getClass().getCanonicalName());
}
// Second "and a half" part
for (String string : readConf(testConfig)) {
IPipeFactory pipeFactory = getClassByString(string);
// Still old factory
IPipeFactory returnedFactory = pipeRegistry.getPipeFactory(pipeFactory.getThreadCommunication(), pipeFactory.getOrdering(), pipeFactory.isGrowable());
Assert.assertNotEquals(pipeFactory.getClass().getCanonicalName(), returnedFactory.getClass().getCanonicalName());
// Overload factory and check for the new one
pipeRegistry.register(pipeFactory);
returnedFactory = pipeRegistry.getPipeFactory(pipeFactory.getThreadCommunication(), pipeFactory.getOrdering(), pipeFactory.isGrowable());
Assert.assertEquals(pipeFactory.getClass().getCanonicalName(), returnedFactory.getClass().getCanonicalName());
}
}
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();
}
@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
private int countLines(final File fileName) throws IOException {
BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
int lines = 0;
try {
while (fileReader.readLine() != null) {
lines = lines + 1;
}
} finally {
fileReader.close();
}
return lines;
}
private ArrayList<String> readConf(final File fileName) throws IOException {
BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
ArrayList<String> list = new ArrayList<String>();
String line;
while ((line = fileReader.readLine()) != null) {
list.add(line);
}
fileReader.close();
return list;
}
} }
package teetime.framework.pipe;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
public class PipeFactoryLoaderTest {
@Test
public void emptyConfig() throws IOException {
List<IPipeFactory> list = PipeFactoryLoader.loadPipeFactoriesFromClasspath("data/empty-test.conf");
Assert.assertEquals(true, list.isEmpty());
}
@Test
public void singleConfig() throws IOException {
List<IPipeFactory> list = PipeFactoryLoader.loadPipeFactoriesFromClasspath("pipe-factories.conf");
int lines = this.countLines(new File("conf/pipe-factories.conf"));
Assert.assertEquals(lines, list.size());
}
@Test
public void multipleConfigs() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
List<URL> files = new ArrayList<URL>();
File pipeConfig = new File("conf/pipe-factories.conf");
File testConfig = new File("src/test/resources/data/normal-test.conf");
files.add(testConfig.toURI().toURL());
files.add(pipeConfig.toURI().toURL());
List<IPipeFactory> pipeFactories = PipeFactoryLoader.mergeFiles(files);
ArrayList<String> contents = readConf(pipeConfig);
contents.addAll(readConf(testConfig));
// Check if all read factories are contained in one of the files
for (IPipeFactory iPipeFactory : pipeFactories) {
Assert.assertTrue(contents.indexOf(iPipeFactory.getClass().getCanonicalName()) != -1);
}
// Second part of the test: PipeFactoryRegistry
PipeFactoryRegistry pipeRegistry = PipeFactoryRegistry.INSTANCE;
// Look for the "normal" pipes
for (String string : readConf(pipeConfig)) {
IPipeFactory pipeFactory = getClassByString(string);
IPipeFactory returnedFactory = pipeRegistry.getPipeFactory(pipeFactory.getThreadCommunication(), pipeFactory.getOrdering(), pipeFactory.isGrowable());
Assert.assertEquals(pipeFactory.getClass().getCanonicalName(), returnedFactory.getClass().getCanonicalName());
}
// Second "and a half" part
for (String string : readConf(testConfig)) {
IPipeFactory pipeFactory = getClassByString(string);
// Still old factory
IPipeFactory returnedFactory = pipeRegistry.getPipeFactory(pipeFactory.getThreadCommunication(), pipeFactory.getOrdering(), pipeFactory.isGrowable());
Assert.assertNotEquals(pipeFactory.getClass().getCanonicalName(), returnedFactory.getClass().getCanonicalName());
// Overload factory and check for the new one
pipeRegistry.register(pipeFactory);
returnedFactory = pipeRegistry.getPipeFactory(pipeFactory.getThreadCommunication(), pipeFactory.getOrdering(), pipeFactory.isGrowable());
Assert.assertEquals(pipeFactory.getClass().getCanonicalName(), returnedFactory.getClass().getCanonicalName());
}
}
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 {
BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
int lines = 0;// NOPMD
try {
while (fileReader.readLine() != null) {// NOPMD
lines = lines + 1;
}
} finally {
fileReader.close();// NOPMD
}
return lines;
}
private ArrayList<String> readConf(final File fileName) throws IOException {
BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
ArrayList<String> list = new ArrayList<String>();
String line;
while ((line = fileReader.readLine()) != null) {
list.add(line);
}
fileReader.close();
return list;
}
}
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