Skip to content
This repository has been archived by the owner on Jul 9, 2022. It is now read-only.

Commit

Permalink
Improve code coverage in ValidatedUri (#14)
Browse files Browse the repository at this point in the history
Adapted from the changes made to the database adaptor's
version of ValidatedUri and its test.
  • Loading branch information
BrettMichaelJohnson authored Jun 13, 2017
1 parent c480e2d commit dff0bf0
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 38 deletions.
6 changes: 1 addition & 5 deletions src/com/google/enterprise/adaptor/opentext/ValidatedUri.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
}
Expand Down
53 changes: 53 additions & 0 deletions test/com/google/enterprise/adaptor/opentext/Logging.java
Original file line number Diff line number Diff line change
@@ -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<? super String> 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();
}
}
63 changes: 30 additions & 33 deletions test/com/google/enterprise/adaptor/opentext/ValidatedUriTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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"),
Expand All @@ -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<? super String> 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());
}
}
});
}
}

0 comments on commit dff0bf0

Please sign in to comment.