Author: bfoster
Date: Thu Jun 2 22:06:12 2011
New Revision: 1130847
URL: http://svn.apache.org/viewvc?rev=1130847&view=rev
Log:
- refactoring to better support unit-testing
-----------------
OODT-194
Added:
oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/util/
oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/util/HttpUtils.java
(with props)
oodt/branches/protocol/protocol-http/src/test/
- copied from r1128514,
oodt/branches/protocol/protocol-http/src/main/test/
oodt/branches/protocol/protocol-http/src/test/org/
oodt/branches/protocol/protocol-http/src/test/org/apache/
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/TestHttpFile.java
(with props)
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/TestHttpProtocol.java
(with props)
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/util/
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/util/TestHttpUtils.java
(with props)
Removed:
oodt/branches/protocol/protocol-http/src/main/test/
Modified:
oodt/branches/protocol/protocol-http/pom.xml
oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/HttpFile.java
oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/HttpProtocol.java
Modified: oodt/branches/protocol/protocol-http/pom.xml
URL:
http://svn.apache.org/viewvc/oodt/branches/protocol/protocol-http/pom.xml?rev=1130847&r1=1130846&r2=1130847&view=diff
==============================================================================
--- oodt/branches/protocol/protocol-http/pom.xml (original)
+++ oodt/branches/protocol/protocol-http/pom.xml Thu Jun 2 22:06:12 2011
@@ -50,28 +50,6 @@
</build>
</profile>
</profiles>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-assembly-plugin</artifactId>
- <version>2.2-beta-2</version>
- <configuration>
- <descriptors>
-
<descriptor>src/main/assembly/assembly.xml</descriptor>
- </descriptors>
- </configuration>
- <executions>
- <execution>
- <phase>package</phase>
- <goals>
- <goal>single</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
<dependencies>
<dependency>
<groupId>org.apache.oodt</groupId>
Modified:
oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/HttpFile.java
URL:
http://svn.apache.org/viewvc/oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/HttpFile.java?rev=1130847&r1=1130846&r2=1130847&view=diff
==============================================================================
---
oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/HttpFile.java
(original)
+++
oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/HttpFile.java
Thu Jun 2 22:06:12 2011
@@ -17,6 +17,7 @@
package org.apache.oodt.cas.protocol.http;
//OODT imports
+import org.apache.commons.lang.Validate;
import org.apache.oodt.cas.protocol.ProtocolFile;
//JDK imports
@@ -35,13 +36,14 @@ public class HttpFile extends ProtocolFi
private URL link;
private HttpFile parent;
- protected HttpFile(String virtualPath, boolean isDir, URL link, HttpFile
parent) {
+ public HttpFile(String virtualPath, boolean isDir, URL link, HttpFile
parent) {
super(virtualPath, isDir);
+ Validate.notNull(link, "URL link must not be NULL");
this.link = link;
this.parent = parent;
}
- protected URL getLink() {
+ public URL getLink() {
return this.link;
}
Modified:
oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/HttpProtocol.java
URL:
http://svn.apache.org/viewvc/oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/HttpProtocol.java?rev=1130847&r1=1130846&r2=1130847&view=diff
==============================================================================
---
oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/HttpProtocol.java
(original)
+++
oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/HttpProtocol.java
Thu Jun 2 22:06:12 2011
@@ -22,9 +22,11 @@ import org.apache.oodt.cas.protocol.Prot
import org.apache.oodt.cas.protocol.ProtocolFile;
import org.apache.oodt.cas.protocol.auth.Authentication;
import org.apache.oodt.cas.protocol.exceptions.ProtocolException;
+import org.apache.oodt.cas.protocol.http.util.HttpUtils;
//TIKA imports
import org.apache.tika.metadata.Metadata;
+import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.html.HtmlParser;
import org.apache.tika.sax.Link;
import org.apache.tika.sax.LinkContentHandler;
@@ -38,11 +40,11 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
+import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
-import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
@@ -77,36 +79,30 @@ public class HttpProtocol implements Pro
boolean isConnected;
- MimeTypeUtils mimeTypes;
-
URL currentURL;
- public HttpProtocol() throws InstantiationException {
- try {
- mimeTypes = new MimeTypeUtils();
- } catch (Exception e) {
- e.printStackTrace();
- throw new InstantiationException(
- "Failed to load tika configuration file : " + e.getMessage());
- }
+ public HttpProtocol() {
isConnected = false;
}
public void cd(ProtocolFile file) throws ProtocolException {
- if (!(file instanceof HttpFile))
- throw new ProtocolException(
- "HttpClient must receive a HttpPath - failed to cd");
-
- HttpFile httpFile = (HttpFile) file;
try {
- if (!this
- .isDirectory(httpFile.getLink().toString(), file.getPath()))
+ HttpFile httpFile = null;
+ if (!(file instanceof HttpFile)) {
+ URL link = HttpUtils.resolveUri(currentURL.toURI(),
file.getPath()).toURL();
+ httpFile = new HttpFile(file.getPath(), file.isDir(),
link, null);
+ } else {
+ httpFile = (HttpFile) file;
+ }
+
+ if (!HttpUtils
+ .isDirectory(httpFile.getLink(), file.getPath()))
throw new ProtocolException(file
+ " is not a directory (mime type must be text/html)");
this.currentFile = httpFile;
} catch (Exception e) {
throw new ProtocolException("Failed to cd to " + file + " : "
- + e.getMessage());
+ + e.getMessage(), e);
}
}
@@ -127,15 +123,19 @@ public class HttpProtocol implements Pro
currentFile = parentFile = null;
}
- public void get(ProtocolFile file, File toLocalFile)
+ public void get(ProtocolFile fromFile, File toFile)
throws ProtocolException {
OutputStream out = null;
InputStream in = null;
try {
this.abort = false;
- out = new BufferedOutputStream(new FileOutputStream(toLocalFile));
- in = ((HttpFile) file).getLink().openStream();
+ out = new BufferedOutputStream(new FileOutputStream(toFile));
+ if (fromFile instanceof HttpFile) {
+ in = ((HttpFile) fromFile).getLink().openStream();
+ } else {
+ in = currentURL.toURI().relativize(new
URI(fromFile.getPath())).toURL().openStream();
+ }
byte[] buffer = new byte[1024];
int numRead;
@@ -147,8 +147,8 @@ public class HttpProtocol implements Pro
in.close();
out.close();
} catch (Exception e) {
- throw new ProtocolException("Failed to get file " + file + " : "
- + e.getMessage());
+ throw new ProtocolException("Failed to get file '" + fromFile + "' : "
+ + e.getMessage(), e);
} finally {
if (in != null)
try {
@@ -193,47 +193,29 @@ public class HttpProtocol implements Pro
if (file.isDir() && children == null) {
try {
- // Open link
- HttpURLConnection con = (HttpURLConnection) file.getLink()
- .openConnection();
- con.connect();
- con.getResponseMessage();
-
- // if redirection took place, then change the ProtocolFile's URL
- if (!file.getLink().toString().equals(con.getURL().toString()))
- file = new HttpFile(file.getPath(), file.isDir(), con
- .getURL(), file);
-
- // create URL source reader
- Scanner scanner = new Scanner(con.getInputStream());
-
- // Read in link
- StringBuffer sb = new StringBuffer("");
- while (scanner.hasNext())
- sb.append(scanner.nextLine());
-
- HtmlParser parser = new HtmlParser();
- Metadata met = new Metadata();
- LinkContentHandler handler = new LinkContentHandler();
-
- parser.parse(new ByteArrayInputStream(sb.toString().getBytes()),
- handler, met);
- List<Link> links = handler.getLinks();
+ // Open link.
+ HttpURLConnection conn = HttpUtils.connect(file.getLink());
+
+ // If redirection took place, then change the ProtocolFile's URL.
+ if (HttpUtils.checkForRedirection(file.getLink(), conn.getURL())) {
+ file = new HttpFile(file.getPath(), file.isDir(), conn.getURL(),
file);
+ }
+
+ // Find links in URL.
+ List<Link> links = HttpUtils.findLinks(conn);
+
+ // Convert links to HttpFiles.
children = new LinkedList<ProtocolFile>();
for (Link link : links) {
- String href = link.getUri();
- String linkName = link.getTitle();
- String curPath = this.pwd().getPath();
- String linkPath = curPath + (curPath.endsWith("/") ? "" : "/")
- + linkName;
- children.add(new HttpFile(linkPath, isDirectory(href, linkPath), new
URL(href), file));
+ children.add(HttpUtils.toHttpFile(link, file));
}
+
+ // Save children links found.
linkChildren.put(file.getLink().toString(), children);
} catch (Exception e) {
- e.printStackTrace();
throw new ProtocolException("Failed to get children links for " + file
- + " : " + e.getMessage());
+ + " : " + e.getMessage(), e);
}
}
return children;
@@ -276,18 +258,6 @@ public class HttpProtocol implements Pro
return find;
}
- public boolean isDirectory(String link, String virtualPath)
- throws ProtocolException, IOException {
- // connect URL and get content type
- try {
- String mime = this.mimeTypes.autoResolveContentType(link, MimeTypeUtils
- .readMagicHeader(new URL(link).openStream()));
- return (mime.equals("text/html") && !virtualPath.endsWith(".html"));
- } catch (Exception e) {
- throw new IOException("URL does not exist " + link);
- }
- }
-
public static String createLinkFromHref(HttpFile parent, String href) {
if (!href.startsWith("http")) {
String link = parent.getLink().toExternalForm();
Added:
oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/util/HttpUtils.java
URL:
http://svn.apache.org/viewvc/oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/util/HttpUtils.java?rev=1130847&view=auto
==============================================================================
---
oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/util/HttpUtils.java
(added)
+++
oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/util/HttpUtils.java
Thu Jun 2 22:06:12 2011
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.protocol.http.util;
+
+//JDK imports
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.List;
+import java.util.Scanner;
+
+//APACHE imports
+import org.apache.commons.lang.Validate;
+import org.apache.oodt.cas.metadata.util.MimeTypeUtils;
+import org.apache.oodt.cas.protocol.http.HttpFile;
+import org.apache.tika.exception.TikaException;
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.parser.ParseContext;
+import org.apache.tika.parser.html.HtmlParser;
+import org.apache.tika.sax.Link;
+import org.apache.tika.sax.LinkContentHandler;
+import org.xml.sax.SAXException;
+
+/**
+ * Utility methods for HTTP Protocol related tasks.
+ *
+ * @author bfoster
+ */
+public class HttpUtils {
+
+ private static MimeTypeUtils mimeTypes = new MimeTypeUtils();
+
+ private HttpUtils() {}
+
+ /**
+ * Resolves a path against given {@link URI} and creates the resolved
{@link URI}.
+ * (i.e. base = "http://localhost" ; path = "/path/to/file" ; resolved
= "http://localhost/path/to/file")
+ * Handles all cases: if base already has a path, if path is relative,
if path is absolute.
+ *
+ * @param base The base {@link URI} which the given path will be
resolved against.
+ * @param path The path to be resolved against the given {@link URI}
+ * @return resolved {@link URI}.
+ * @throws URISyntaxException
+ */
+ public static URI resolveUri(URI base, String path) throws
URISyntaxException {
+ Validate.notNull(base, "base URI must not be NULL");
+ Validate.notNull(path, "resolve path must not be NULL");
+ if (path.startsWith("/")) {
+ return new URI(base.getScheme() + "://" +
base.getHost() + path);
+ } else {
+ if (base.toString().endsWith("/")) {
+ return new URI(base.toString() + path);
+ } else {
+ return new URI(base.toString() + "/" + path);
+ }
+ }
+ }
+
+ public static HttpURLConnection connect(URL url) throws IOException {
+ HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
+ conn.connect();
+ conn.getResponseMessage();
+ return conn;
+ }
+
+ public static boolean checkForRedirection(URL beforeConnUrl, URL
afterConnUrl) {
+ return !beforeConnUrl.toString().equals(afterConnUrl.toString());
+ }
+
+ public static String readUrl(HttpURLConnection conn) throws IOException {
+ // create URL source reader
+ Scanner scanner = new Scanner(conn.getInputStream());
+
+ // Read in link
+ StringBuffer sb = new StringBuffer("");
+ while (scanner.hasNext())
+ sb.append(scanner.nextLine());
+
+ return sb.toString();
+ }
+
+ public static List<Link> findLinks(HttpURLConnection conn) throws
IOException, SAXException, TikaException {
+ LinkContentHandler handler = new LinkContentHandler();
+
+ new HtmlParser().parse(new
ByteArrayInputStream(HttpUtils.readUrl(conn).getBytes()),
+ handler, new Metadata(), new ParseContext());
+
+ return handler.getLinks();
+ }
+
+ public static HttpFile toHttpFile(Link link, HttpFile parent) throws
IOException {
+ URL href = new URL(link.getUri());
+ String linkPath = parent.getPath() + "/" + link.getTitle();
+ return new HttpFile(linkPath, isDirectory(href, linkPath), href, parent);
+ }
+
+ public static boolean isDirectory(URL url, String virtualPath) throws
IOException {
+ try {
+ String mime =
mimeTypes.autoResolveContentType(url.toString(),
+
MimeTypeUtils.readMagicHeader(url.openStream()));
+ return (mime.equals("text/html") &&
!virtualPath.endsWith(".html"));
+ } catch (Exception e) {
+ throw new IOException("URL does not exist '" + url +
"'", e);
+ }
+ }
+}
Propchange:
oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/util/HttpUtils.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/TestHttpFile.java
URL:
http://svn.apache.org/viewvc/oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/TestHttpFile.java?rev=1130847&view=auto
==============================================================================
---
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/TestHttpFile.java
(added)
+++
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/TestHttpFile.java
Thu Jun 2 22:06:12 2011
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.protocol.http;
+
+//JUnit imports
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import junit.framework.TestCase;
+
+/**
+ * Test class for {@link HttpFile}.
+ *
+ * @author bfoster
+ */
+public class TestHttpFile extends TestCase {
+
+ public void testInitialState() throws MalformedURLException {
+ HttpFile parent = new HttpFile("/path/to", false, new
URL("http://some-site"), null);
+ HttpFile file = new HttpFile("/path/to/file", false, new
URL("http://some-site"), parent);
+ assertNotNull(file.getLink());
+ assertEquals("http://some-site", file.getLink().toString());
+ assertFalse(file.isDir());
+ assertFalse(file.isRelative());
+ assertNotNull(file.getParent());
+ assertEquals(parent, file.getParent());
+ }
+
+ public void testNullCase() throws MalformedURLException {
+ try {
+ new HttpFile(null, false, new URL("http://some-site"),
null);
+ fail("Should have thrown an IllegalArgumentException");
+ } catch (IllegalArgumentException e) {}
+ try {
+ new HttpFile("/path/to/file", false, null, null);
+ fail("Should have thrown an IllegalArgumentException");
+ } catch (IllegalArgumentException e) {}
+ }
+}
Propchange:
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/TestHttpFile.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/TestHttpProtocol.java
URL:
http://svn.apache.org/viewvc/oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/TestHttpProtocol.java?rev=1130847&view=auto
==============================================================================
---
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/TestHttpProtocol.java
(added)
+++
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/TestHttpProtocol.java
Thu Jun 2 22:06:12 2011
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.protocol.http;
+
+//JUnit imports
+import java.util.List;
+
+import org.apache.oodt.cas.protocol.ProtocolFile;
+import org.apache.oodt.cas.protocol.auth.NoAuthentication;
+import org.apache.oodt.cas.protocol.exceptions.ProtocolException;
+
+import junit.framework.TestCase;
+
+/**
+ * Test class for {@link HttpProtocol}.
+ *
+ * @author bfoster
+ */
+public class TestHttpProtocol extends TestCase {
+
+ public void testConnection() throws InstantiationException,
ProtocolException {
+ HttpProtocol httpProtocol = new HttpProtocol();
+ assertFalse(httpProtocol.connected());
+ httpProtocol.connect("svn.apache.org", new NoAuthentication());
+ assertTrue(httpProtocol.connected());
+ }
+
+ public void testLS() throws ProtocolException, InstantiationException {
+ HttpProtocol httpProtocol = new HttpProtocol();
+ httpProtocol.connect("svn.apache.org", new NoAuthentication());
+ assertTrue(httpProtocol.connected());
+ httpProtocol.cd(new
ProtocolFile("repos/asf/oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http",
true));
+ List<ProtocolFile> files = httpProtocol.ls();
+ boolean foundFile = false;
+ for (ProtocolFile file : files) {
+ if (file.getName().equals("HttpProtocol.java")) {
+ foundFile = true;
+ break;
+ }
+ }
+ assertTrue(foundFile);
+ }
+}
Propchange:
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/TestHttpProtocol.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/util/TestHttpUtils.java
URL:
http://svn.apache.org/viewvc/oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/util/TestHttpUtils.java?rev=1130847&view=auto
==============================================================================
---
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/util/TestHttpUtils.java
(added)
+++
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/util/TestHttpUtils.java
Thu Jun 2 22:06:12 2011
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.protocol.http.util;
+
+//JDK imports
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+//JUnits imports
+import junit.framework.TestCase;
+
+/**
+ * Test class for {@link HttpUtils}.
+ *
+ * @author bfoster
+ */
+public class TestHttpUtils extends TestCase {
+
+ private static final String URL_OF_THIS_TEST =
"http://svn.apache.org/repos/asf/oodt/branches/protocol/protocol-http/src/main/java/org/apache/oodt/cas/protocol/http/util/TestHttpUtils.java";
+
+ public void testResolveUri() throws URISyntaxException {
+ URI baseUri = new URI("http://localhost/base/directory/");
+
+ // Test absolute resolve.
+ URI resolvedAbsoluteUri = HttpUtils.resolveUri(baseUri,
"/path/to/file");
+ assertEquals("http://localhost/path/to/file",
resolvedAbsoluteUri.toString());
+
+ // Test relative resolve.
+ URI resolvedRelativeUri = HttpUtils.resolveUri(baseUri,
"path/to/file");
+ assertEquals("http://localhost/base/directory/path/to/file",
resolvedRelativeUri.toString());
+
+ // Test relative with base not ending in /
+ baseUri = new URI("http://localhost/base/directory");
+ assertEquals("http://localhost/base/directory/path/to/file",
resolvedRelativeUri.toString());
+ }
+
+ public void testConnectUrl() throws MalformedURLException, IOException {
+ HttpURLConnection conn = HttpUtils.connect(new
URL(URL_OF_THIS_TEST));
+ assertNotSame(0, conn.getDate());
+ String urlText = HttpUtils.readUrl(conn);
+ assertTrue(urlText.contains("public class TestHttpUtils extends
TestCase {"));
+ conn.disconnect();
+ }
+
+}
Propchange:
oodt/branches/protocol/protocol-http/src/test/org/apache/oodt/cas/protocol/http/util/TestHttpUtils.java
------------------------------------------------------------------------------
svn:mime-type = text/plain