Commits

Anonymous committed b4c489b

Migrate IntegrationTests to use WebappModule

Comments (0)

Files changed (6)

chempound-webapp/src/main/java/net/chempound/webapp/ChempoundWebappLauncher.java

 package net.chempound.webapp;
 
-import com.google.inject.*;
-import net.chempound.ChempoundConfigurationModule;
-import net.chempound.DefaultChempoundModule;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.Key;
+import com.google.inject.Stage;
 import net.chempound.config.BaseUri;
-import net.chempound.config.DefaultPropertySource;
 import org.restlet.Application;
 import org.restlet.Component;
 import org.restlet.data.Protocol;
 
-import java.io.IOException;
 import java.net.URI;
 
 /**
  */
 public class ChempoundWebappLauncher {
 
-    private final ChempoundWebappModule module;
-    private final Stage stage;
+    private final Component component;
+    private final Injector injector;
 
-    public ChempoundWebappLauncher(final ChempoundWebappModule chempoundWebappModule) {
-        this.module = chempoundWebappModule;
-        this.stage = Stage.PRODUCTION;
+    public ChempoundWebappLauncher(final ChempoundWebappModule module) {
+        this.injector = Guice.createInjector(Stage.PRODUCTION, module);
+        this.component = createComponent(injector);
     }
 
     public static void main(final String[] args) throws Exception {
     }
 
     public void start() throws Exception {
-        final Injector injector = Guice.createInjector(stage, module);
+        component.start();
+    }
 
+    public void stop() throws Exception {
+        component.stop();
+    }
+
+    public Injector getInjector() {
+        return injector;
+    }
+
+    private static Component createComponent(final Injector injector) {
         final URI uri = injector.getInstance(Key.get(URI.class, BaseUri.class));
         final int port = uri.getPort() == -1 ? 80 : uri.getPort();
         final String path = uri.getPath().replaceFirst("(.*)/$", "$1");
         component.getServers().add(Protocol.HTTP, port)
                 .getContext().getParameters().add("maxThreads", "256");
         component.getDefaultHost().attach(path, injector.getInstance(Application.class));
-        component.start();
-    }
 
-    protected Injector getInjector() throws IOException {
-        return Guice.createInjector(stage, module);
-    }
-
-    protected Module getConfigurationModule() throws IOException {
-        return new ChempoundConfigurationModule(new DefaultPropertySource());
-    }
-
-    protected Module getChempoundModule() {
-        return new DefaultChempoundModule();
-    }
-
-    protected Module getPluginsModule() {
-        return new AbstractModule() {
-            @Override
-            protected void configure() {
-
-            }
-        };
+        return component;
     }
 }

chempound-webapp/src/main/java/net/chempound/webapp/DefaultChempoundWebModule.java

                 component.getClients().add(Protocol.CLAP);
                 component.getClients().add(Protocol.FILE);
 
+                bind(Component.class).toInstance(component);
                 bind(Context.class).toInstance(component.getContext().createChildContext());
                 bind(Application.class).to(ChempoundApplication.class);
             }

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

+package net.chempound.webapp;
+
+import com.google.inject.Injector;
+import org.apache.commons.io.FileUtils;
+import org.junit.After;
+import org.junit.Before;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.util.UUID;
+
+public abstract class AbstractChempoundIntegrationTest {
+
+    private File workspace;
+    private ChempoundWebappLauncher webappLauncher;
+    protected Injector injector;
+
+    protected AbstractChempoundIntegrationTest() {
+        final URI baseUri = getBaseUri();
+        workspace = createWorkspace();
+
+        final ChempoundWebappModule module = new StubConfigurationWebAppModule(baseUri, workspace);
+
+        webappLauncher = new ChempoundWebappLauncher(module);
+        injector = webappLauncher.getInjector();
+    }
+
+    @Before
+    public void init() throws Exception {
+        webappLauncher.start();
+    }
+
+    protected abstract URI getBaseUri();
+
+    @After
+    public void close() throws Exception {
+        if (webappLauncher != null) {
+            webappLauncher.stop();
+        }
+        FileUtils.deleteQuietly(workspace);
+    }
+
+    protected static File createWorkspace() {
+        File workspace;
+        do {
+            workspace = new File("target/tmp/" + UUID.randomUUID());
+        } while (workspace.exists());
+        try {
+            FileUtils.forceMkdir(workspace);
+        } catch (IOException e) {
+            throw new RuntimeException("Failed to create workspace", e);
+        }
+        return workspace;
+    }
+
+}

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

 package net.chempound.webapp;
 
-import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.hp.hpl.jena.vocabulary.RDF;
 import net.chempound.Chempound;
-import net.chempound.ChempoundConfigurationModule;
-import net.chempound.DefaultChempoundModule;
-import net.chempound.config.ChempoundConfiguration;
-import net.chempound.config.DefaultPropertySource;
-import net.chempound.config.StubChempoundConfiguration;
 import net.chempound.content.DefaultDepositRequest;
 import net.chempound.content.DepositRequest;
 import net.chempound.rdf.CPTerms;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
-import org.restlet.*;
+import org.restlet.Client;
+import org.restlet.Request;
+import org.restlet.Response;
 import org.restlet.data.*;
 import org.swordapp.client.*;
 
 /**
  * @author sea36
  */
-public class ChempoundWebAppIntegrationTest {
+public class ChempoundWebAppIntegrationTest extends AbstractChempoundIntegrationTest {
 
-    private Component component;
-
-    private File workspace;
-    private Injector injector;
-
-    @Before
-    public void init() throws Exception {
-
-        workspace = createWorkspace();
-
-        injector = Guice.createInjector(
-                new ChempoundConfigurationModule(initConfiguration(), new DefaultPropertySource()),
-                new DefaultChempoundModule(),
-                new DefaultChempoundWebModule()
-            );
-
-        component = injector.getInstance(Component.class);
-        component.getServers().add(Protocol.HTTP, 8711)
-                .getContext().getParameters().add("maxThreads", "256");
-        component.getDefaultHost()
-                .attachDefault(injector.getInstance(Application.class));
-
-        component.start();
-    }
-
-    @After
-    public void close() throws Exception {
-        if (component != null) {
-            component.stop();
-        }
-        FileUtils.deleteQuietly(workspace);
-    }
-
-    protected static File createWorkspace() throws IOException {
-        File workspace;
-        do {
-            workspace = new File("target/tmp/"+ UUID.randomUUID());
-        } while (workspace.exists());
-        FileUtils.forceMkdir(workspace);
-        return workspace;
+    @Override
+    protected URI getBaseUri() {
+        return URI.create("http://localhost:8711/");
     }
 
     protected static void loadTestData(Injector injector) throws IOException {
         chempound.loadContent(request);
     }
 
-    protected ChempoundConfiguration initConfiguration() throws IOException {
-        final URI uri = URI.create("http://localhost:8711/");
-        return new StubChempoundConfiguration(uri, workspace);
-    }
-
-
     @Test
     public void testGetStaticContent() {
         Client client = new Client(Protocol.HTTP);

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

+package net.chempound.webapp;
+
+import com.google.inject.Module;
+import net.chempound.ChempoundConfigurationModule;
+import net.chempound.config.ChempoundConfiguration;
+import net.chempound.config.DefaultPropertySource;
+import net.chempound.config.StubChempoundConfiguration;
+
+import java.io.File;
+import java.net.URI;
+
+class StubConfigurationWebAppModule extends ChempoundWebappModule {
+
+    private final URI baseUri;
+    private final File workspace;
+
+    public StubConfigurationWebAppModule(final URI baseUri, final File workspace) {
+        this.baseUri = baseUri;
+        this.workspace = workspace;
+    }
+
+    @Override
+    protected Module getConfigurationModule() {
+        final ChempoundConfiguration configuration = new StubChempoundConfiguration(baseUri, workspace);
+        return new ChempoundConfigurationModule(configuration, new DefaultPropertySource());
+    }
+}

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

 package net.chempound.webapp.pingback;
 
-import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.hp.hpl.jena.rdf.model.ResourceFactory;
 import net.chempound.Chempound;
-import net.chempound.ChempoundConfigurationModule;
-import net.chempound.DefaultChempoundModule;
-import net.chempound.config.ChempoundConfiguration;
-import net.chempound.config.DefaultPropertySource;
-import net.chempound.config.StubChempoundConfiguration;
 import net.chempound.content.DefaultDepositRequest;
 import net.chempound.content.DepositRequest;
 import net.chempound.datastore.TripleStore;
 import net.chempound.rdf.CPTerms;
 import net.chempound.storage.InMemoryResource;
 import net.chempound.util.MimeType;
-import net.chempound.webapp.DefaultChempoundWebModule;
-import org.apache.commons.io.FileUtils;
+import net.chempound.webapp.AbstractChempoundIntegrationTest;
 import org.apache.commons.io.IOUtils;
 import org.apache.http.Header;
 import org.apache.http.HttpResponse;
 import org.apache.http.entity.BasicHttpEntity;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.util.EntityUtils;
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.restlet.Application;
 import org.restlet.resource.Directory;
 
 import java.io.ByteArrayInputStream;
-import java.io.File;
 import java.io.IOException;
 import java.net.URI;
-import java.util.UUID;
 
 import static org.junit.Assert.*;
 
 /**
  * @author Sam Adams
  */
-public class PingbackIntegrationTest {
-
-    private Component component;
-
-    private File workspace;
-    private Injector injector;
+public class PingbackIntegrationTest extends AbstractChempoundIntegrationTest {
 
     @Before
+    @Override
     public void init() throws Exception {
 
-        workspace = createWorkspace();
-
-        injector = Guice.createInjector(
-                new ChempoundConfigurationModule(initConfiguration(), new DefaultPropertySource()),
-                new DefaultChempoundModule(),
-                new DefaultChempoundWebModule()
-        );
-
-        component = injector.getInstance(Component.class);
-        component.getClients().add(Protocol.CLAP);
-        component.getServers().add(Protocol.HTTP, 8717)
-                .getContext().getParameters().add("maxThreads", "256");
-
-        component.getDefaultHost()
-                .attach("/repo", injector.getInstance(Application.class));
+        final Component component = injector.getInstance(Component.class);
         component.getDefaultHost()
                 .attach("/blog", new BlogApplication(component.getContext().createChildContext()));
 
-        component.start();
+        loadTestData(injector);
+
+        super.init();
     }
 
-    @After
-    public void close() throws Exception {
-        component.stop();
-        FileUtils.deleteQuietly(workspace);
+    @Override
+    protected URI getBaseUri() {
+        return URI.create("http://localhost:8717/repo/");
     }
 
-
-    protected File createWorkspace() throws IOException {
-        File workspace;
-        do {
-            workspace = new File("target/tmp/"+ UUID.randomUUID());
-        } while (workspace.exists());
-        FileUtils.forceMkdir(workspace);
-        return workspace;
-    }
-
-
     protected void loadTestData(Injector injector) throws IOException {
         Chempound chempound = injector.getInstance(Chempound.class);
         DepositRequest request = new DefaultDepositRequest();
         chempound.loadContent(request);
     }
 
-
-    protected ChempoundConfiguration initConfiguration() throws IOException {
-        final URI uri = URI.create("http://localhost:8717/repo/");
-        return new StubChempoundConfiguration(uri, workspace);
-    }
-
     @Test
     public void testPingbackHeader() throws IOException {
-        loadTestData(injector);
 
         HttpClient client = new DefaultHttpClient();
         HttpUriRequest request = new HttpGet("http://localhost:8717/repo/content/foo/");
 
     @Test
     public void testPingback() throws IOException {
-        loadTestData(injector);
 
         HttpClient client = new DefaultHttpClient();
 
 
     @Test
     public void testPingbackBadSourceNotExist() throws IOException {
-        loadTestData(injector);
 
         HttpClient client = new DefaultHttpClient();
 
 
     @Test
     public void testPingbackBadSourceNoLink() throws IOException {
-        loadTestData(injector);
 
         HttpClient client = new DefaultHttpClient();
 
 
     @Test
     public void testPingbackBadTarget() throws IOException {
-        loadTestData(injector);
 
         HttpClient client = new DefaultHttpClient();
 
     }
 
 
-
     static class BlogApplication extends Application {
 
         BlogApplication(Context context) {