diff --git a/src/com/google/enterprise/adaptor/opentext/ValidatedUri.java b/src/com/google/enterprise/adaptor/opentext/ValidatedUri.java index bf3c3ed..8119bf0 100644 --- a/src/com/google/enterprise/adaptor/opentext/ValidatedUri.java +++ b/src/com/google/enterprise/adaptor/opentext/ValidatedUri.java @@ -53,7 +53,7 @@ public ValidatedUri(String uriString) throws URISyntaxException { } try { // Basic syntax checking, with more understandable error messages. - // Also ensures the URI is a URL, not a URN. + // Also ensures the URI is a URL, not a URN, and is absolute. new URL(uriString); // Advanced syntax checking, with more cryptic error messages. uri = new URI(uriString); @@ -65,10 +65,6 @@ public ValidatedUri(String uriString) throws URISyntaxException { throw new URISyntaxException(uriString, reason); } - if (!uri.isAbsolute()) { - throw new URISyntaxException(uriString, "relative URIs are not allowed"); - } - if (Strings.isNullOrEmpty(uri.getHost())) { throw new URISyntaxException(uriString, "no host"); } diff --git a/test/com/google/enterprise/adaptor/opentext/Logging.java b/test/com/google/enterprise/adaptor/opentext/Logging.java new file mode 100644 index 0000000..9846faa --- /dev/null +++ b/test/com/google/enterprise/adaptor/opentext/Logging.java @@ -0,0 +1,53 @@ +// Copyright 2014 Google Inc. All Rights Reserved. +// +// Licensed 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 com.google.enterprise.adaptor.opentext; + +import java.util.Collection; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +/** Tools to test logging output. */ +public class Logging { + /** + * Enables logging and captures matching log messages. The messages + * will not be localized or formatted. + * + * @param clazz the class to enable logging for + * @param substring capture log messages containing this substring + * @param output captured messages will be added to this collection + */ + public static void captureLogMessages(Class clazz, + final String substring, final Collection output) { + Logger logger = Logger.getLogger(clazz.getName()); + logger.setLevel(Level.ALL); + + logger.addHandler(new Handler() { + @Override public void close() {} + @Override public void flush() {} + + @Override public void publish(LogRecord record) { + if (record.getMessage().contains(substring)) { + output.add(record.getMessage()); + } + } + }); + } + + private Logging() { + throw new AssertionError(); + } +} diff --git a/test/com/google/enterprise/adaptor/opentext/ValidatedUriTest.java b/test/com/google/enterprise/adaptor/opentext/ValidatedUriTest.java index cdca8fe..7ae9a56 100644 --- a/test/com/google/enterprise/adaptor/opentext/ValidatedUriTest.java +++ b/test/com/google/enterprise/adaptor/opentext/ValidatedUriTest.java @@ -14,17 +14,13 @@ package com.google.enterprise.adaptor.opentext; +import static com.google.enterprise.adaptor.opentext.Logging.captureLogMessages; import static org.junit.Assert.assertEquals; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; -import java.util.Collection; import java.util.List; -import java.util.logging.Handler; -import java.util.logging.Level; -import java.util.logging.LogRecord; -import java.util.logging.Logger; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -50,9 +46,17 @@ public void testEmptyUrl() throws Exception { @Test public void testNoProtocol() throws Exception { thrown.expect(URISyntaxException.class); + thrown.expectMessage("no protocol"); new ValidatedUri("//foo/bar"); } + @Test + public void testRelativeUri() throws Exception { + thrown.expect(URISyntaxException.class); + thrown.expectMessage("no protocol"); + new ValidatedUri("foo/bar"); + } + @Test public void testUnknownProtocol() throws Exception { thrown.expect(URISyntaxException.class); @@ -66,15 +70,24 @@ public void testBadProtocol() throws Exception { } @Test - public void testNoHost() throws Exception { + public void testNoHostOrPath() throws Exception { thrown.expect(URISyntaxException.class); + thrown.expectMessage("Expected authority"); new ValidatedUri("http://"); } @Test - public void testRelativeUri() throws Exception { + public void testNoHost() throws Exception { thrown.expect(URISyntaxException.class); - new ValidatedUri("foo/bar"); + thrown.expectMessage("no host"); + new ValidatedUri("http:///foo/bar"); + } + + @Test + public void testNoAuthority() throws Exception { + thrown.expect(URISyntaxException.class); + thrown.expectMessage("no host"); + new ValidatedUri("file:/foo/bar"); } @Test @@ -107,6 +120,15 @@ public void testRootPath() throws Exception { new ValidatedUri("http://foo:80/").getUri()); } + @Test + public void testUnderscoreInHostName() throws Exception { + // new URI("http://foo_bar/baz") parses incorrectly, but does not throw a + // URISyntaxException. The host part, however, is discarded. So our check + // for missing host should catch this. + thrown.expect(URISyntaxException.class); + new ValidatedUri("http://foo_bar/baz").getUri(); + } + @Test public void testGetUri() throws Exception { assertEquals(new URI("http://example.com/foo/bar"), @@ -128,29 +150,4 @@ public void testUnreachableHost() throws Exception { new ValidatedUri("http://unknown-host/foo/bar").logUnreachableHost(); assertEquals(messages.toString(), 1, messages.size()); } - - /** - * Enables logging and captures matching log messages. The messages - * will not be localized or formatted. - * - * @param clazz the class to enable logging for - * @param substring capture log messages containing this substring - * @param output captured messages will be added to this collection - */ - public static void captureLogMessages(Class clazz, - final String substring, final Collection output) { - Logger logger = Logger.getLogger(clazz.getName()); - logger.setLevel(Level.ALL); - - logger.addHandler(new Handler() { - @Override public void close() {} - @Override public void flush() {} - - @Override public void publish(LogRecord record) { - if (record.getMessage().contains(substring)) { - output.add(record.getMessage()); - } - } - }); - } }