Commits

Anonymous committed c6550e3

Simplifying test configuration

Comments (0)

Files changed (4)

chempound-app/src/main/java/net/chempound/config/StubChempoundConfiguration.java

 package net.chempound.config;
 
-import org.apache.commons.io.IOUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.*;
+import java.io.File;
 import java.net.URI;
 
 /**
  */
 public class StubChempoundConfiguration extends AbstractChempoundConfiguration {
 
-    private static final Logger LOG = LoggerFactory.getLogger(StubChempoundConfiguration.class);
-
     private transient File workspace;
 
     private URI baseUri;
-    private File configDirectory;
-
-    public StubChempoundConfiguration(final File workspace) throws IOException {
-        setWorkspace(workspace);
-        init();
-    }
-
-    private void init() throws IOException {
-        final File configFile = getConfigurationFile();
-        if (configFile.exists()) {
-            loadConfiguration();
-        } else {
-            loadDefaults();
-            saveConfiguration();
-        }
-    }
-
-    private void loadDefaults() throws FileNotFoundException {
-        final InputStream in = getClass().getResourceAsStream("default-config.xml");
-        if (in == null) {
-            throw new FileNotFoundException("Default configuration missing");
-        }
-        try {
-            LOG.info("Loading default configuration");
-            ConfigurationIO.load(this, in);
-        } finally {
-            IOUtils.closeQuietly(in);
-        }
-    }
-
 
     public StubChempoundConfiguration(final URI baseUri, final File workspace) {
-        setBaseUri(baseUri);
-        setWorkspace(workspace);
-    }
-
-    public void setBaseUri(final URI baseUri) {
-        if (baseUri.toString().endsWith("/")) {
-            this.baseUri = baseUri;
-        } else {
-            this.baseUri = URI.create(baseUri.toString() + '/');
-        }
+        this.baseUri = createBaseUri(baseUri);
+        this.workspace = workspace;
     }
 
     @Override
         return workspace;
     }
 
-    void setWorkspace(final File workspace) {
-        this.workspace = workspace;
-    }
-
-    public File getConfigurationDirectory() {
-        if (configDirectory == null) {
-            configDirectory = new File(getWorkspace(), "config");
-        }
-        if (!configDirectory.exists()) {
-            configDirectory.mkdirs();
-        }
-        return configDirectory;
-    }
-
-
-    public File getConfigurationFile() {
-        final File configDirectory = getConfigurationDirectory();
-        final File configFile = new File(configDirectory, "config.xml");
-        return configFile;
-    }
-
-
-
-    public synchronized void loadConfiguration() throws IOException {
-        final File configFile = getConfigurationFile();
-        if (configFile.exists()) {
-            LOG.info("Loading configuration file: " + configFile);
-            final BufferedInputStream in = new BufferedInputStream(new FileInputStream(configFile));
-            try {
-                ConfigurationIO.load(this, in);
-            } finally {
-                IOUtils.closeQuietly(in);
-            }
+    private static URI createBaseUri(final URI baseUri) {
+        if (baseUri.toString().endsWith("/")) {
+            return baseUri;
+        } else {
+            return URI.create(baseUri.toString() + '/');
         }
     }
 
-    public synchronized void saveConfiguration() throws IOException {
-        final File configFile = getConfigurationFile();
-        final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(configFile));
-        try {
-            ConfigurationIO.save(this, out);
-        } finally {
-            IOUtils.closeQuietly(out);
-        }
-    }
-
-    public static ChempoundConfiguration load(final URI uri, final File workspaceDir) throws IOException {
-        final StubChempoundConfiguration config = new StubChempoundConfiguration(uri, workspaceDir);
-        config.init();
-        return config;
-    }
-
 }

chempound-app/src/test/java/net/chempound/config/ChempoundConfigurationTest.java

-package net.chempound.config;
-
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.UUID;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-
-/**
- * @author sea36
- */
-public class ChempoundConfigurationTest {
-
-    private File workspace;
-
-    @Before
-    public void setupWorkspace() throws IOException {
-        File workspace;
-        do {
-            workspace = new File("target/tmp/" + UUID.randomUUID());
-        } while (workspace.exists());
-        FileUtils.forceMkdir(workspace);
-        this.workspace = workspace;
-    }
-
-    @After
-    public void cleanupWorkspace() throws IOException {
-        FileUtils.forceDelete(this.workspace);
-    }
-
-    private void writeConfigurationFile(String name) throws IOException {
-        InputStream in = getClass().getResourceAsStream(name);
-        if (in == null) {
-            throw new FileNotFoundException("File not found: "+name);
-        }
-        try {
-            File file = new File(workspace, "config/config.xml");
-            FileUtils.copyInputStreamToFile(in, file);
-        } finally {
-            IOUtils.closeQuietly(in);
-        }
-    }
-
-    @Test
-    public void testLoadDefaults() throws IOException {
-        ChempoundConfiguration config = new StubChempoundConfiguration(workspace);
-        assertNotNull(config);
-    }
-
-    @Test
-    public void testDefaultFeed() throws IOException {
-        ChempoundConfiguration config = new StubChempoundConfiguration(workspace);
-        assertEquals(1, config.getFeeds().size());
-        assertEquals("all", config.getFeeds().get(0).getId());
-    }
-
-    @Test
-    public void testLoadFile() throws IOException {
-        writeConfigurationFile("config1.xml");
-        ChempoundConfiguration config = new StubChempoundConfiguration(workspace);
-//        assertEquals(URI.create("http://repo.example.com/"), config.getBaseUri());
-    }
-
-
-}

chempound-webapp/src/test/java/net/chempound/webapp/ChempoundWebAppIntegrationTest.java

     }
 
     protected ChempoundConfiguration initConfiguration() throws IOException {
-        URI uri = URI.create("http://localhost:8711/");
-        return StubChempoundConfiguration.load(uri, workspace);
+        final URI uri = URI.create("http://localhost:8711/");
+        return new StubChempoundConfiguration(uri, workspace);
     }
 
 

chempound-webapp/src/test/java/net/chempound/webapp/pingback/PingbackIntegrationTest.java

 
 
     protected ChempoundConfiguration initConfiguration() throws IOException {
-        URI uri = URI.create("http://localhost:8717/repo/");
-        return StubChempoundConfiguration.load(uri, workspace);
+        final URI uri = URI.create("http://localhost:8717/repo/");
+        return new StubChempoundConfiguration(uri, workspace);
     }
 
     @Test