// Copyright 2010 Google Inc. All Rights Reserved.
package org.waveprotocol.wave.client.doodad.link;
import junit.framework.TestCase;
import org.waveprotocol.wave.client.doodad.link.Link.InvalidLinkException;
import org.waveprotocol.wave.util.escapers.GwtWaverefEncoder;
import org.waveprotocol.wave.util.escapers.jvm.JavaWaverefEncoder;
/**
* @author danilatos@google.com (Daniel Danilatos)
*/
public class LinkTest extends TestCase {
@Override
protected void setUp() throws Exception {
GwtWaverefEncoder.INSTANCE = JavaWaverefEncoder.INSTANCE;
}
public void testNormalizeRejectsJunk() {
String[] invalid = new String[] {
"asdf", "foo://asdf", "wave://asdf", "waveid://asdf",
"wave://example.com!abcd"
};
for (String s : invalid) {
checkNormalizeRejects(s);
}
}
public void testNormalizeIsGenerousWithWaveIdOrNoPrefix() throws InvalidLinkException {
checkNormalize("wave://example.com/abcd", "example.com!abcd");
checkNormalize("wave://example.com/abcd", "example.com/abcd");
// waveid: can support wave serialized wave ids and wave refs
checkNormalize("wave://example.com/abcd", "waveid://example.com/abcd");
checkNormalize("wave://example.com/abcd", "waveid://example.com!abcd");
}
public void testNormalizeWaveRefs() throws InvalidLinkException {
checkNormalize("wave://example.com/abcd");
checkNormalize("wave://example.com/abcd/~/bar");
checkNormalizeRejects("wave://example.com!abcd");
}
public void testNormalizeAcceptsValidWebSchemes() throws InvalidLinkException {
checkNormalize("http://example.com/abcd");
checkNormalize("https://example.com/abcd");
checkNormalize("ftp://example.com/abcd");
checkNormalize("mailto://example.com/abcd");
checkNormalize("#");
checkNormalize("#example.com/w+N0tgD7rctjA");
checkNormalize("#fragment");
checkNormalize("#fragment/?+-:@!$&'()*+,;=");
checkNormalize("?param=1¶m2");
checkNormalize("?param=1¶m2#");
checkNormalize("?param=1¶m2#fragment/?+-:@!$&'()*+,;=");
checkNormalizeRejects("#fragment<>");
checkNormalizeRejects("?param=1¶m2<>");
}
public void testConvertUri() {
assertNull(Link.toHrefFromUri("asdf"));
assertNull(Link.toHrefFromUri("wave://asdf"));
assertNull(Link.toHrefFromUri("foo://asdf"));
assertEquals("http://example.com/abcd", Link.toHrefFromUri("http://example.com/abcd"));
assertEquals("#example.com/abcd", Link.toHrefFromUri("wave://example.com/abcd"));
assertEquals("#", Link.toHrefFromUri("#"));
assertEquals("#fragment", Link.toHrefFromUri("#fragment"));
assertEquals("#fragment/?+-:@!$&'()*+,;=", Link.toHrefFromUri("#fragment/?+-:@!$&'()*+,;="));
assertEquals("?param=1¶m2", Link.toHrefFromUri("?param=1¶m2"));
assertEquals("?param=1¶m2#", Link.toHrefFromUri("?param=1¶m2#"));
assertEquals("?param=1¶m2#fragment/?+-:@!$&'()*+,;=", Link.toHrefFromUri("?param=1¶m2#fragment/?+-:@!$&'()*+,;="));
}
void checkNormalize(String inputOutput) throws InvalidLinkException {
checkNormalize(inputOutput, inputOutput);
}
void checkNormalize(String expected, String input) throws InvalidLinkException {
assertEquals(expected, Link.normalizeLink(input));
// Check the invariant that passing in the output should always return the same thing.
assertEquals(expected, Link.normalizeLink(expected));
}
void checkNormalizeRejects(String input) {
try {
Link.normalizeLink(input);
fail("'" + input + "' should not be considered a valid link");
} catch (InvalidLinkException e) {
// ok
}
}
}