From 058ffd068e38c57e7da2daf395e279e85abeb036 Mon Sep 17 00:00:00 2001
From: Thorsten Vitt <thorsten.vitt@uni-wuerzburg.de>
Date: Mon, 27 May 2013 11:29:49 +0000
Subject: [PATCH] Refactored common parts of the integration tests

git-svn-id: https://develop.sub.uni-goettingen.de/repos/textgrid/trunk/services/aggregator@14080 7c539038-3410-0410-b1ec-0f2a7bf1c452
---
 .../aggregator/AbstractIntegrationTest.java   | 47 +++++++++++++++++++
 .../textgrid/services/aggregator/EpubIT.java  | 34 ++------------
 .../services/aggregator/html/HtmlIT.java      | 18 +++----
 3 files changed, 58 insertions(+), 41 deletions(-)
 create mode 100644 src/test/java/info/textgrid/services/aggregator/AbstractIntegrationTest.java

diff --git a/src/test/java/info/textgrid/services/aggregator/AbstractIntegrationTest.java b/src/test/java/info/textgrid/services/aggregator/AbstractIntegrationTest.java
new file mode 100644
index 0000000..f86baaf
--- /dev/null
+++ b/src/test/java/info/textgrid/services/aggregator/AbstractIntegrationTest.java
@@ -0,0 +1,47 @@
+package info.textgrid.services.aggregator;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+
+import com.google.common.io.ByteStreams;
+import com.google.common.io.NullOutputStream;
+
+public abstract class AbstractIntegrationTest {
+
+	/**
+	 * Creates a request in the form of a (not yet connected)
+	 * {@link URLConnection}.
+	 * 
+	 * @param request
+	 *            Path and query string (everything after the
+	 *            <code>aggregator/</code>
+	 * @throws MalformedURLException
+	 * @throws IOException
+	 */
+	protected URLConnection createRequest(final String request) throws MalformedURLException,
+			IOException {
+				final URL url = new URL(System.getProperty("service.url") + request);
+				final URLConnection connection = url.openConnection();
+				connection.setConnectTimeout(360000);
+				return connection;
+			}
+
+	/**
+	 * Reads the given connection's content (and throws it away).
+	 * 
+	 * @param connection
+	 *            must be open.
+	 * @return number of bytes read
+	 * @throws IOException
+	 */
+	protected long readContents(final URLConnection connection) throws IOException {
+		final InputStream content = connection.getInputStream();
+		final long realLength = ByteStreams.copy(content,
+				new NullOutputStream());
+		return realLength;
+	}
+
+}
\ No newline at end of file
diff --git a/src/test/java/info/textgrid/services/aggregator/EpubIT.java b/src/test/java/info/textgrid/services/aggregator/EpubIT.java
index e54f455..e4acafe 100644
--- a/src/test/java/info/textgrid/services/aggregator/EpubIT.java
+++ b/src/test/java/info/textgrid/services/aggregator/EpubIT.java
@@ -1,46 +1,22 @@
 package info.textgrid.services.aggregator;
 
-import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.AuthFault;
-import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.IoFault;
-import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.MetadataParseFault;
-import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.ObjectNotFoundFault;
-import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.ProtocolNotImplementedFault;
-
 import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
+import java.net.MalformedURLException;
 import java.net.URLConnection;
 
-import javax.ws.rs.WebApplicationException;
-
-import net.sf.saxon.s9api.SaxonApiException;
-
 import org.junit.Assert;
 import org.junit.Test;
 
-import com.google.common.io.ByteStreams;
-import com.google.common.io.NullOutputStream;
-
-public class EpubIT {
+public class EpubIT extends AbstractIntegrationTest {
 
 	@Test
-	public void testGet() throws ObjectNotFoundFault, MetadataParseFault,
-	IoFault, AuthFault, ProtocolNotImplementedFault, WebApplicationException, IOException, SaxonApiException {
+	public void testGet() throws MalformedURLException, IOException {
 		
-		final URL url = new URL(System.getProperty("service.url")
-				+ "/epub/textgrid:jfst.0");
-		final URLConnection connection = url.openConnection();
-		connection.setConnectTimeout(360000);
+		final URLConnection connection = createRequest("/epub/textgrid:jfst.0");
 		connection.connect();
 		Assert.assertEquals("application/epub+zip", connection.getContentType());
-		// final int expectedLength = connection.getContentLength(); // -1 for
-		// now
-		final InputStream content = connection.getInputStream();
-		final long realLength = ByteStreams.copy(content,
-				new NullOutputStream());
+		final long realLength = readContents(connection);
 		Assert.assertTrue("Content empty", realLength > 0);
-		// Assert.assertEquals("Reported and delivered content lengths differ",
-		// expectedLength, realLength);
 	}
 
 }
diff --git a/src/test/java/info/textgrid/services/aggregator/html/HtmlIT.java b/src/test/java/info/textgrid/services/aggregator/html/HtmlIT.java
index a43c553..af594c2 100644
--- a/src/test/java/info/textgrid/services/aggregator/html/HtmlIT.java
+++ b/src/test/java/info/textgrid/services/aggregator/html/HtmlIT.java
@@ -1,31 +1,25 @@
 package info.textgrid.services.aggregator.html;
 
+import info.textgrid.services.aggregator.AbstractIntegrationTest;
+
 import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
 import java.net.URLConnection;
 
 import junit.framework.Assert;
 
 import org.junit.Test;
 
-import com.google.common.io.ByteStreams;
-import com.google.common.io.NullOutputStream;
 
-public class HtmlIT {
+public class HtmlIT extends AbstractIntegrationTest {
 
 	@Test
 	public void testGet() throws IOException {
-		final URL url = new URL(System.getProperty("service.url")
-				+ "/html/textgrid:jfst.0");
-		final URLConnection connection = url.openConnection();
-		connection.setConnectTimeout(360000);
+		final URLConnection connection = createRequest("/html/textgrid:jfst.0");
 		connection.connect();
 		Assert.assertEquals("text/html", connection.getContentType());
-		final InputStream content = connection.getInputStream();
-		final long realLength = ByteStreams.copy(content,
-				new NullOutputStream());
+		final long realLength = readContents(connection);
 		Assert.assertTrue("Content length > 0", realLength > 0);
 	}
 
+
 }
-- 
GitLab