From 68f4602c495212a4fb2e5d0cd5378011bc3b493e Mon Sep 17 00:00:00 2001 From: Christian Wulf <chw@informatik.uni-kiel.de> Date: Mon, 3 Nov 2014 08:05:38 +0100 Subject: [PATCH] corrected some structure and PMD issues --- .settings/edu.umd.cs.findbugs.core.prefs | 2 +- .../java/teetime/framework/FileSearcher.java | 15 ++- src/main/java/teetime/util/ArrayWrapper.java | 6 +- .../teetime/framework/FileSearcherTest.java | 96 +------------------ .../framework/pipe/PipeFactoryLoaderTest.java | 96 +++++++++++++++++++ 5 files changed, 110 insertions(+), 105 deletions(-) create mode 100644 src/test/java/teetime/framework/pipe/PipeFactoryLoaderTest.java diff --git a/.settings/edu.umd.cs.findbugs.core.prefs b/.settings/edu.umd.cs.findbugs.core.prefs index 527228eb..2f9ba13d 100644 --- a/.settings/edu.umd.cs.findbugs.core.prefs +++ b/.settings/edu.umd.cs.findbugs.core.prefs @@ -1,5 +1,5 @@ #FindBugs User Preferences -#Wed Oct 22 13:08:20 CEST 2014 +#Mon Nov 03 07:44:13 CET 2014 detector_threshold=3 effort=max excludefilter0=.fbExcludeFilterFile|true diff --git a/src/main/java/teetime/framework/FileSearcher.java b/src/main/java/teetime/framework/FileSearcher.java index 016cdf2f..5902ba89 100644 --- a/src/main/java/teetime/framework/FileSearcher.java +++ b/src/main/java/teetime/framework/FileSearcher.java @@ -8,19 +8,18 @@ import java.util.List; 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 { - final List<URL> list = new ArrayList<URL>(); - final Enumeration<URL> systemRes = ClassLoader.getSystemClassLoader().getResources(name); - while (systemRes.hasMoreElements()) { - list.add(systemRes.nextElement()); + final Enumeration<URL> systemRes = CLASS_LOADER.getResources(name); + while (systemRes.hasMoreElements()) { // NOPMD + list.add(systemRes.nextElement()); // NOPMD } return list; } diff --git a/src/main/java/teetime/util/ArrayWrapper.java b/src/main/java/teetime/util/ArrayWrapper.java index 96e17fb5..d3f84db4 100644 --- a/src/main/java/teetime/util/ArrayWrapper.java +++ b/src/main/java/teetime/util/ArrayWrapper.java @@ -12,15 +12,15 @@ public final class ArrayWrapper<T> { this.elements = (T[]) new Object[initialCapacity]; } - public final T get(final int index) { + public T get(final int 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; } - public final int getCapacity() { + public int getCapacity() { return this.elements.length; } diff --git a/src/test/java/teetime/framework/FileSearcherTest.java b/src/test/java/teetime/framework/FileSearcherTest.java index 4dd6a9b2..69e43e9b 100644 --- a/src/test/java/teetime/framework/FileSearcherTest.java +++ b/src/test/java/teetime/framework/FileSearcherTest.java @@ -1,120 +1,30 @@ package teetime.framework; -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; -import teetime.framework.pipe.IPipeFactory; -import teetime.framework.pipe.PipeFactoryLoader; -import teetime.framework.pipe.PipeFactoryRegistry; - public class FileSearcherTest { @Test public void fileInClasspath() throws IOException { List<URL> list = FileSearcher.loadResources("pipe-factories.conf"); - Assert.assertEquals(false, list.isEmpty()); + Assert.assertEquals(false, list.isEmpty());// NOPMD } @Test public void multipleFiles() throws IOException { List<URL> list = FileSearcher.loadResources("LICENSE.txt"); - Assert.assertEquals(true, list.size() > 1); + Assert.assertEquals(true, list.size() > 1);// NOPMD } @Test public void missingFile() throws IOException { List<URL> list = FileSearcher.loadResources("filethatdoesnotexistinanyproject.nope"); - Assert.assertEquals(true, list.isEmpty()); - } - - @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()); + Assert.assertEquals(true, list.isEmpty());// NOPMD } - @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; - } } diff --git a/src/test/java/teetime/framework/pipe/PipeFactoryLoaderTest.java b/src/test/java/teetime/framework/pipe/PipeFactoryLoaderTest.java new file mode 100644 index 00000000..aa289d37 --- /dev/null +++ b/src/test/java/teetime/framework/pipe/PipeFactoryLoaderTest.java @@ -0,0 +1,96 @@ +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; + } +} -- GitLab