converted test data to map, use new serializer

pull/477/head
Justin Richer 2013-07-26 12:55:22 -04:00
parent 8edd6da136
commit fb448a246a
1 changed files with 68 additions and 220 deletions

View File

@ -21,7 +21,9 @@ import static org.junit.Assert.*;
import java.util.List;
import org.junit.Test;
import org.springframework.web.util.UriComponents;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
/**
@ -32,134 +34,65 @@ public class TestWebfingerURLNormalizer {
// Test fixture:
private ImmutableMap<String, String> inputToNormalized = new ImmutableMap.Builder<String, String>()
.put("example.com", "https://example.com")
.put("example.com:8080", "https://example.com:8080")
.put("example.com/path", "https://example.com/path")
.put("example.com?query", "https://example.com?query")
.put("example.com#fragment", "https://example.com")
.put("example.com:8080/path?query#fragment", "https://example.com:8080/path?query")
.put("http://example.com", "http://example.com")
.put("http://example.com:8080", "http://example.com:8080")
.put("http://example.com/path", "http://example.com/path")
.put("http://example.com?query", "http://example.com?query")
.put("http://example.com#fragment", "http://example.com")
.put("http://example.com:8080/path?query#fragment", "http://example.com:8080/path?query")
.put("nov@example.com", "acct:nov@example.com")
.put("nov@example.com:8080", "https://nov@example.com:8080")
.put("nov@example.com/path", "https://nov@example.com/path")
.put("nov@example.com?query", "https://nov@example.com?query")
.put("nov@example.com#fragment", "acct:nov@example.com")
.put("nov@example.com:8080/path?query#fragment", "https://nov@example.com:8080/path?query")
.put("acct:nov@matake.jp", "acct:nov@matake.jp")
.put("acct:nov@example.com:8080", "acct:nov@example.com:8080")
.put("acct:nov@example.com/path", "acct:nov@example.com/path")
.put("acct:nov@example.com?query", "acct:nov@example.com?query")
.put("acct:nov@example.com#fragment", "acct:nov@example.com")
.put("acct:nov@example.com:8080/path?query#fragment", "acct:nov@example.com:8080/path?query")
.put("mailto:nov@matake.jp", "mailto:nov@matake.jp")
.put("mailto:nov@example.com:8080", "mailto:nov@example.com:8080")
.put("mailto:nov@example.com/path", "mailto:nov@example.com/path")
.put("mailto:nov@example.com?query", "mailto:nov@example.com?query")
.put("mailto:nov@example.com#fragment", "mailto:nov@example.com")
.put("mailto:nov@example.com:8080/path?query#fragment", "mailto:nov@example.com:8080/path?query")
.put("localhost", "https://localhost")
.put("localhost:8080", "https://localhost:8080")
.put("localhost/path", "https://localhost/path")
.put("localhost?query", "https://localhost?query")
.put("localhost#fragment", "https://localhost")
.put("localhost/path?query#fragment", "https://localhost/path?query")
.put("nov@localhost", "acct:nov@localhost")
.put("nov@localhost:8080", "https://nov@localhost:8080")
.put("nov@localhost/path", "https://nov@localhost/path")
.put("nov@localhost?query", "https://nov@localhost?query")
.put("nov@localhost#fragment", "acct:nov@localhost")
.put("nov@localhost/path?query#fragment", "https://nov@localhost/path?query")
.put("tel:+810312345678", "tel:+810312345678")
.put("device:192.168.2.1", "device:192.168.2.1")
.put("device:192.168.2.1:8080", "device:192.168.2.1:8080")
.put("device:192.168.2.1/path", "device:192.168.2.1/path")
.put("device:192.168.2.1?query", "device:192.168.2.1?query")
.put("device:192.168.2.1#fragment", "device:192.168.2.1")
.put("device:192.168.2.1/path?query#fragment", "device:192.168.2.1/path?query")
.build();
private List<String> identifiers_noScheme = Lists.newArrayList(
"example.com",
"example.com:8080",
"example.com/path",
"example.com?query",
"example.com#fragment",
"example.com:8080/path?query#fragment");
private List<String> identifiers_http = Lists.newArrayList(
"http://example.com",
"http://example.com:8080",
"http://example.com/path",
"http://example.com?query",
"http://example.com#fragment",
"http://example.com:8080/path?query#fragment");
private List<String> identifiers_noSchemeWithUser = Lists.newArrayList(
"nov@example.com",
"nov@example.com:8080",
"nov@example.com/path",
"nov@example.com?query",
"nov@example.com#fragment",
"nov@example.com:8080/path?query#fragment");
private List<String> identifiers_acct = Lists.newArrayList(
"acct:nov@matake.jp",
"acct:nov@example.com:8080",
"acct:nov@example.com/path",
"acct:nov@example.com?query",
"acct:nov@example.com#fragment",
"acct:nov@example.com:8080/path?query#fragment");
private List<String> identifiers_mailto = Lists.newArrayList(
"mailto:nov@matake.jp",
"mailto:nov@example.com:8080",
"mailto:nov@example.com/path",
"mailto:nov@example.com?query",
"mailto:nov@example.com#fragment",
"mailto:nov@example.com:8080/path?query#fragment");
private List<String> identifiers_localhost = Lists.newArrayList(
"localhost",
"localhost:8080",
"localhost/path",
"localhost?query",
"localhost#fragment",
"localhost/path?query#fragment",
"nov@localhost",
"nov@localhost:8080",
"nov@localhost/path",
"nov@localhost?query",
"nov@localhost#fragmentx",
"nov@localhost/path?query#fragment");
private List<String> identifiers_numbers = Lists.newArrayList(
"tel:+810312345678",
"device:192.168.2.1",
"device:192.168.2.1:8080",
"device:192.168.2.1/path",
"device:192.168.2.1?query",
"device:192.168.2.1#fragment",
"device:192.168.2.1/path?query#fragment");
private List<String> expected_noScheme = Lists.newArrayList(
"https://example.com",
"https://example.com:8080",
"https://example.com/path",
"https://example.com?query",
"https://example.com",
"https://example.com:8080/path?query");
private List<String> expected_http = Lists.newArrayList(
"http://example.com",
"http://example.com:8080",
"http://example.com/path",
"http://example.com?query",
"http://example.com",
"http://example.com:8080/path?query");
private List<String> expected_noSchemeWithUser = Lists.newArrayList(
"acct:nov@example.com",
"https://nov@example.com:8080",
"https://nov@example.com/path",
"https://nov@example.com?query",
"acct:nov@example.com",
"https://nov@example.com:8080/path?query");
private List<String> expected_acct = Lists.newArrayList(
"acct:nov@matake.jp",
"acct:nov@example.com:8080",
"acct:nov@example.com/path",
"acct:nov@example.com?query",
"acct:nov@example.com",
"acct:nov@example.com:8080/path?query");
private List<String> expected_mailto = Lists.newArrayList(
"mailto:nov@matake.jp",
"mailto:nov@example.com:8080",
"mailto:nov@example.com/path",
"mailto:nov@example.com?query",
"mailto:nov@example.com",
"mailto:nov@example.com:8080/path?query");
private List<String> expected_localhost = Lists.newArrayList(
"https://localhost",
"https://localhost:8080",
"https://localhost/path",
"https://localhost?query",
"https://localhost",
"https://localhost/path?query",
"acct:nov@localhost",
"https://nov@localhost:8080",
"https://nov@localhost/path",
"https://nov@localhost?query",
"https://nov@localhost?query",
"https://nov@localhost/path?query");
private List<String> expected_numbers = Lists.newArrayList(
"tel:+810312345678",
"device:192.168.2.1",
"device:192.168.2.1:8080",
"device:192.168.2.1/path",
"device:192.168.2.1?query",
"device:192.168.2.1",
"device:192.168.2.1/path?query");
/*
Adapted from Nov Matake's Ruby normalizer implementation.
@ -220,106 +153,21 @@ public class TestWebfingerURLNormalizer {
# device:192.168.2.1?query => device:192.168.2.1?query
# device:192.168.2.1#fragment => device:192.168.2.1
# device:192.168.2.1/path?query#fragment => device:192.168.2.1/path?query
*
*/
@Test
public void normalizeResource_noScheme() {
public void normalizeResource_novTest() {
for (String input : inputToNormalized.keySet()) {
for (int i = 0; i < identifiers_noScheme.size(); i++) {
UriComponents actualNormalized = WebfingerURLNormalizer.normalizeResource(input);
String identifier = identifiers_noScheme.get(i);
String expectedNormalized = expected_noScheme.get(i);
String expectedNormalized = inputToNormalized.get(input);
String actualNormalized = WebfingerURLNormalizer.normalizeResource(identifier).toUriString();
assertEquals("Identifer/Normalized pair #" + i + " failed.", expectedNormalized, actualNormalized);
}
}
@Test
public void normalizeResource_http() {
for (int i = 0; i < identifiers_http.size(); i++) {
String identifier = identifiers_http.get(i);
String expectedNormalized = expected_http.get(i);
String actualNormalized = WebfingerURLNormalizer.normalizeResource(identifier).toUriString();
assertEquals("Identifer/Normalized pair #" + i + " failed.", expectedNormalized, actualNormalized);
}
}
@Test
public void normalizeResource_noSchemeWithUser() {
for (int i = 0; i < identifiers_noSchemeWithUser.size(); i++) {
String identifier = identifiers_noSchemeWithUser.get(i);
String expectedNormalized = expected_noSchemeWithUser.get(i);
String actualNormalized = WebfingerURLNormalizer.normalizeResource(identifier).toUriString();
assertEquals("Identifer/Normalized pair #" + i + " failed.", expectedNormalized, actualNormalized);
}
}
@Test
public void normalizeResource_acct() {
for (int i = 0; i < identifiers_acct.size(); i++) {
String identifier = identifiers_acct.get(i);
String expectedNormalized = expected_acct.get(i);
String actualNormalized = WebfingerURLNormalizer.normalizeResource(identifier).toUriString();
assertEquals("Identifer/Normalized pair #" + i + " failed.", expectedNormalized, actualNormalized);
}
}
@Test
public void normalizeResource_mailto() {
for (int i = 0; i < identifiers_mailto.size(); i++) {
String identifier = identifiers_mailto.get(i);
String expectedNormalized = expected_mailto.get(i);
String actualNormalized = WebfingerURLNormalizer.normalizeResource(identifier).toUriString();
assertEquals("Identifer/Normalized pair #" + i + " failed.", expectedNormalized, actualNormalized);
}
}
@Test
public void normalizeResource_localhost() {
for (int i = 0; i < identifiers_localhost.size(); i++) {
String identifier = identifiers_localhost.get(i);
String expectedNormalized = expected_localhost.get(i);
String actualNormalized = WebfingerURLNormalizer.normalizeResource(identifier).toUriString();
assertEquals("Identifer/Normalized pair #" + i + " failed.", expectedNormalized, actualNormalized);
}
}
@Test
public void normalizeResource_numbers() {
for (int i = 0; i < identifiers_numbers.size(); i++) {
String identifier = identifiers_numbers.get(i);
String expectedNormalized = expected_numbers.get(i);
String actualNormalized = WebfingerURLNormalizer.normalizeResource(identifier).toUriString();
assertEquals("Identifer/Normalized pair #" + i + " failed.", expectedNormalized, actualNormalized);
assertEquals("Identifer/Normalized failed.", expectedNormalized, WebfingerURLNormalizer.serializeURL(actualNormalized));
}
}
}