Skip to content
Snippets Groups Projects
Commit 2a296e20 authored by Nelson Tavares de Sousa's avatar Nelson Tavares de Sousa
Browse files

First version of multipleConfigs()-test

parent fe696b6f
No related branches found
No related tags found
No related merge requests found
......@@ -6,8 +6,14 @@ import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
public class FileSearcher {
public final class FileSearcher {
private FileSearcher() {
};
@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>();
......
......@@ -5,6 +5,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
......@@ -51,9 +52,8 @@ public class PipeFactoryLoader {
return pipeFactories;
}
public static List<IPipeFactory> mergeConfigFiles(final String configFileName) {
public static List<IPipeFactory> loadPipefactoriesFromClasspath(final String configFileName) {
List<IPipeFactory> pipeFactories = new LinkedList<IPipeFactory>();
List<URL> files = null;
try {
......@@ -61,16 +61,21 @@ public class PipeFactoryLoader {
} catch (IOException e) {
throw new IllegalStateException(e);
}
return mergeFiles(files);
}
public static List<IPipeFactory> mergeFiles(final List<URL> files) {
ArrayList<IPipeFactory> list = new ArrayList<IPipeFactory>();
for (URL url : files) {
try {
InputStream is = url.openStream();
pipeFactories.addAll(loadFromStream(is));
list.addAll(loadFromStream(is));
is.close();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
return pipeFactories;
return list;
}
}
......@@ -14,7 +14,7 @@ import org.slf4j.LoggerFactory;
* To get a PipeFactory instance, call {@link #getPipeFactory(ThreadCommunication, PipeOrdering, boolean)}.
*
*/
public class PipeFactoryRegistry {
public final class PipeFactoryRegistry {
private static final Logger LOGGER = LoggerFactory.getLogger(PipeFactoryRegistry.class);
......@@ -45,10 +45,10 @@ public class PipeFactoryRegistry {
/**
* The singleton instance of PipeFactoryRegistry
*/
public static PipeFactoryRegistry INSTANCE = new PipeFactoryRegistry();
public static PipeFactoryRegistry INSTANCE = new PipeFactoryRegistry("pipe-factories.conf");
private PipeFactoryRegistry() {
List<IPipeFactory> pipeFactories = PipeFactoryLoader.mergeConfigFiles("pipe-factories.conf");
private PipeFactoryRegistry(final String configFileName) {
List<IPipeFactory> pipeFactories = PipeFactoryLoader.loadPipefactoriesFromClasspath(configFileName);
for (IPipeFactory pipeFactory : pipeFactories) {
this.register(pipeFactory);
}
......
......@@ -5,6 +5,7 @@ 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;
......@@ -12,6 +13,7 @@ import org.junit.Test;
import teetime.framework.pipe.IPipeFactory;
import teetime.framework.pipe.PipeFactoryLoader;
import teetime.framework.pipe.PipeFactoryRegistry;
public class FileSearcherTest {
......@@ -35,24 +37,56 @@ public class FileSearcherTest {
@Test
public void emptyConfig() throws IOException {
List<IPipeFactory> list = PipeFactoryLoader.mergeConfigFiles("data/empty-test.conf");
List<IPipeFactory> list = PipeFactoryLoader.loadPipefactoriesFromClasspath("data/empty-test.conf");
Assert.assertEquals(true, list.isEmpty());
}
@Test
public void singleConfig() throws IOException {
List<IPipeFactory> list = PipeFactoryLoader.mergeConfigFiles("pipe-factories.conf");
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 {
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);
}
PipeFactoryRegistry pipeRegistry = PipeFactoryRegistry.INSTANCE;
}
@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
private int countLines(final File fileName) throws IOException {
BufferedReader r = new BufferedReader(new FileReader(fileName));
BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
int lines = 0;
while (r.readLine() != null) {
lines++;
while (fileReader.readLine() != null) { // TODO: Finally
lines = lines + 1;
}
r.close();
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;
}
}
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