Author: tpalsulich
Date: Mon Dec 29 20:46:56 2014
New Revision: 1648425
URL: http://svn.apache.org/r1648425
Log:
OODT-762. Migrate protocol/http test resources.
Added:
oodt/trunk/protocol/http/src/test/java/
oodt/trunk/protocol/http/src/test/java/org/
oodt/trunk/protocol/http/src/test/java/org/apache/
oodt/trunk/protocol/http/src/test/java/org/apache/oodt/
oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/
oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/
oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/http/
oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/http/TestHttpFile.java
oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/http/TestHttpProtocol.java
oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/http/util/
oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/http/util/TestHttpUtils.java
Removed:
oodt/trunk/protocol/http/src/test/org/
Added:
oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/http/TestHttpFile.java
URL:
http://svn.apache.org/viewvc/oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/http/TestHttpFile.java?rev=1648425&view=auto
==============================================================================
---
oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/http/TestHttpFile.java
(added)
+++
oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/http/TestHttpFile.java
Mon Dec 29 20:46:56 2014
@@ -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"));
+ HttpFile file = new HttpFile(parent, "/path/to/file", false,
new URL("http://some-site"));
+ 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"));
+ fail("Should have thrown an IllegalArgumentException");
+ } catch (IllegalArgumentException e) {}
+ try {
+ new HttpFile("/path/to/file", false, null);
+ fail("Should have thrown an IllegalArgumentException");
+ } catch (IllegalArgumentException e) {}
+ }
+}
Added:
oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/http/TestHttpProtocol.java
URL:
http://svn.apache.org/viewvc/oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/http/TestHttpProtocol.java?rev=1648425&view=auto
==============================================================================
---
oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/http/TestHttpProtocol.java
(added)
+++
oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/http/TestHttpProtocol.java
Mon Dec 29 20:46:56 2014
@@ -0,0 +1,98 @@
+/*
+ * 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.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.util.List;
+import java.util.Scanner;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.oodt.cas.protocol.ProtocolFile;
+import org.apache.oodt.cas.protocol.auth.NoAuthentication;
+import org.apache.oodt.cas.protocol.exceptions.ProtocolException;
+import org.apache.oodt.cas.protocol.http.util.HttpUtils;
+
+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 testLSandCD() throws ProtocolException,
InstantiationException {
+ HttpProtocol httpProtocol = new HttpProtocol();
+ httpProtocol.connect("svn.apache.org", new NoAuthentication());
+ assertTrue(httpProtocol.connected());
+ httpProtocol.cd(new
ProtocolFile("repos/asf/oodt/trunk/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);
+ }
+
+ public void testPWD() throws ProtocolException {
+ HttpProtocol httpProtocol = new HttpProtocol();
+ httpProtocol.connect("svn.apache.org", new NoAuthentication());
+ assertTrue(httpProtocol.connected());
+ ProtocolFile gotoDir = new ProtocolFile(httpProtocol.pwd(),
"repos/asf/oodt/trunk/protocol/http/src/test/org/apache/oodt/cas/protocol/http",
true);
+ httpProtocol.cd(gotoDir);
+ ProtocolFile currentDir = httpProtocol.pwd();
+ System.out.println(gotoDir.getAbsoluteFile());
+ System.out.println(currentDir.getAbsoluteFile());
+ assertEquals(gotoDir, currentDir);
+ }
+
+ public void testGET() throws ProtocolException, IOException {
+ HttpProtocol httpProtocol = new HttpProtocol();
+ httpProtocol.connect("svn.apache.org", new NoAuthentication());
+ assertTrue(httpProtocol.connected());
+ httpProtocol.cd(new
ProtocolFile("repos/asf/oodt/trunk/protocol/http/src/test/org/apache/oodt/cas/protocol/http",
true));
+ File bogus = File.createTempFile("bogus", "bogus");
+ File tmpDir = new File(bogus.getParentFile(),
"TestHttpProtocol");
+ bogus.delete();
+ tmpDir.mkdirs();
+ File toFile = new File(tmpDir, "TestHttpProtocol.java");
+ httpProtocol.get(new ProtocolFile("TestHttpProtocol.java",
false), toFile);
+ assertTrue(toFile.exists());
+ assertNotSame(0, toFile.length());
+
+ String fileContent = "";
+ Scanner scanner = new Scanner(toFile);
+ while(scanner.hasNextLine()) {
+ fileContent += scanner.nextLine();
+ }
+ assertEquals(fileContent,
HttpUtils.readUrl(HttpUtils.connect(new
URL("http://svn.apache.org/repos/asf/oodt/trunk/protocol/http/src/test/org/apache/oodt/cas/protocol/http/TestHttpProtocol.java"))));
+ FileUtils.forceDelete(tmpDir);
+ }
+}
Added:
oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/http/util/TestHttpUtils.java
URL:
http://svn.apache.org/viewvc/oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/http/util/TestHttpUtils.java?rev=1648425&view=auto
==============================================================================
---
oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/http/util/TestHttpUtils.java
(added)
+++
oodt/trunk/protocol/http/src/test/java/org/apache/oodt/cas/protocol/http/util/TestHttpUtils.java
Mon Dec 29 20:46:56 2014
@@ -0,0 +1,172 @@
+/*
+ * 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;
+import java.util.List;
+import java.util.regex.Matcher;
+
+//OODT imports
+import org.apache.oodt.cas.protocol.http.HttpFile;
+
+//JUnits imports
+import junit.framework.TestCase;
+
+/**
+ * Test class for {@link HttpUtils}.
+ *
+ * @author bfoster
+ */
+public class TestHttpUtils extends TestCase {
+
+ private static final String APACHE_SVN_SITE = "http://svn.apache.org";
+
+ private static final String PROTOCOL_HTTP_SVN_LOC =
"/repos/asf/oodt/trunk/protocol/http";
+ private static final String PARENT_URL_OF_THIS_TEST =
PROTOCOL_HTTP_SVN_LOC + "/src/test/org/apache/oodt/cas/protocol/http/util";
+ private static final String URL_OF_THIS_TEST = PARENT_URL_OF_THIS_TEST
+ "/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(APACHE_SVN_SITE + URL_OF_THIS_TEST));
+ assertNotSame(0, conn.getDate());
+ String urlText = HttpUtils.readUrl(conn);
+ assertTrue(urlText.contains("public class TestHttpUtils extends
TestCase {"));
+ conn.disconnect();
+ }
+
+ public void testRedirector() throws MalformedURLException {
+ URL url = new URL("http://localhost:80");
+ URL redirectedURL = new URL("http://localhost:8080");
+ assertFalse(HttpUtils.checkForRedirection(url, url));
+ assertTrue(HttpUtils.checkForRedirection(url, redirectedURL));
+ }
+
+ public void testXhtmlLinkPattern() {
+ // SUCCESS cases
+ Matcher matcher = HttpUtils.XHTML_LINK_PATTERN.matcher("<a
href=\"http://localhost\">localhost</a>");
+ assertTrue(matcher.find());
+ assertEquals("\"", matcher.group(1).trim());
+ assertEquals("http://localhost", matcher.group(2).trim());
+ assertEquals("localhost", matcher.group(3).trim());
+
+ matcher = HttpUtils.XHTML_LINK_PATTERN.matcher("<a
href='http://localhost'>localhost</a>");
+ assertTrue(matcher.find());
+ assertEquals("'", matcher.group(1).trim());
+ assertEquals("http://localhost", matcher.group(2).trim());
+ assertEquals("localhost", matcher.group(3).trim());
+
+ matcher = HttpUtils.XHTML_LINK_PATTERN.matcher("< a href = \"
http://localhost \" > localhost < / a >");
+ assertTrue(matcher.find());
+ assertEquals("\"", matcher.group(1).trim());
+ assertEquals("http://localhost", matcher.group(2).trim());
+ assertEquals("localhost", matcher.group(3).trim());
+
+ matcher = HttpUtils.XHTML_LINK_PATTERN.matcher("< a href = '
http://localhost ' > localhost < / a >");
+ assertTrue(matcher.find());
+ assertEquals("'", matcher.group(1).trim());
+ assertEquals("http://localhost", matcher.group(2).trim());
+ assertEquals("localhost", matcher.group(3).trim());
+
+ //Should not find case: open with " end with '
+ matcher = HttpUtils.XHTML_LINK_PATTERN.matcher("<a
href=\"http://localhost\'>localhost</a>");
+ assertFalse(matcher.find());
+
+ //Should not find case: open with ' end with "
+ matcher = HttpUtils.XHTML_LINK_PATTERN.matcher("<a
href=\'http://localhost\">localhost</a>");
+ assertFalse(matcher.find());
+
+ //Should not find case: lazy link pattern
+ matcher = HttpUtils.XHTML_LINK_PATTERN.matcher("<a
href=\"http://localhost\"/>");
+ assertFalse(matcher.find());
+ }
+
+ public void testLazyLinkPattern() {
+ Matcher matcher = HttpUtils.LAZY_LINK_PATTERN.matcher("<a
href=\"http://localhost\"/>");
+ assertTrue(matcher.find());
+ assertEquals("\"", matcher.group(1).trim());
+ assertEquals("http://localhost", matcher.group(2).trim());
+
+ matcher = HttpUtils.LAZY_LINK_PATTERN.matcher("<a
href='http://localhost'/>");
+ assertTrue(matcher.find());
+ assertEquals("'", matcher.group(1).trim());
+ assertEquals("http://localhost", matcher.group(2).trim());
+
+ matcher = HttpUtils.LAZY_LINK_PATTERN.matcher("< a href = \"
http://localhost \" / >");
+ assertTrue(matcher.find());
+ assertEquals("\"", matcher.group(1).trim());
+ assertEquals("http://localhost", matcher.group(2).trim());
+
+ matcher = HttpUtils.LAZY_LINK_PATTERN.matcher("< a href = '
http://localhost ' / >");
+ assertTrue(matcher.find());
+ assertEquals("'", matcher.group(1).trim());
+ assertEquals("http://localhost", matcher.group(2).trim());
+
+ //Should not find case: open with " end with '
+ matcher = HttpUtils.LAZY_LINK_PATTERN.matcher("<a
href=\"http://localhost\'/>");
+ assertFalse(matcher.find());
+
+ //Should not find case: open with ' end with "
+ matcher = HttpUtils.LAZY_LINK_PATTERN.matcher("<a
href=\'http://localhost\"/>");
+ assertFalse(matcher.find());
+
+ //Should not find case: xhtml link pattern
+ matcher = HttpUtils.LAZY_LINK_PATTERN.matcher("<a
href='http://localhost'>localhost</a>");
+ assertFalse(matcher.find());
+ }
+
+ public void testFindLinks() throws MalformedURLException, IOException,
URISyntaxException {
+ URL url = new URL(APACHE_SVN_SITE + PARENT_URL_OF_THIS_TEST);
+ HttpFile parent = new HttpFile(PARENT_URL_OF_THIS_TEST, true,
url);
+ HttpURLConnection conn = HttpUtils.connect(url);
+ List<HttpFile> httpFiles = HttpUtils.findLinks(parent);
+ boolean foundThisTest = false;
+ for (HttpFile httpFile : httpFiles) {
+ if (httpFile.getName().equals("TestHttpUtils.java")) {
+ foundThisTest = true;
+ break;
+ }
+ }
+ assertTrue(foundThisTest);
+ }
+
+ public void testIsDirectory() throws MalformedURLException, IOException
{
+ assertTrue(HttpUtils.isDirectory(new URL(APACHE_SVN_SITE +
PARENT_URL_OF_THIS_TEST), ""));
+ assertFalse(HttpUtils.isDirectory(new URL(APACHE_SVN_SITE +
URL_OF_THIS_TEST), ""));
+ assertTrue(HttpUtils.isDirectory(new URL(APACHE_SVN_SITE), ""));
+ }
+}