UtilTest.java revision 1472
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* See LICENSE.txt included in this distribution for the specific
* language governing permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at LICENSE.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright 2012 Jens Elkner.
*/
package org.opensolaris.opengrok.web;
import java.io.ByteArrayInputStream;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Locale;
import javax.xml.parsers.DocumentBuilderFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Test of the methods in <code>org.opensolaris.opengrok.web.Util</code>.
*/
public class UtilTest {
private static Locale savedLocale;
@SuppressWarnings("javadoc")
@BeforeClass
public static void setUpClass() {
// Some of the methods have different results in different locales.
// Set locale to en_US for these tests.
savedLocale = Locale.getDefault();
Locale.setDefault(Locale.US);
}
@SuppressWarnings("javadoc")
@AfterClass
public static void tearDownClass() {
Locale.setDefault(savedLocale);
savedLocale = null;
}
@SuppressWarnings({ "javadoc", "static-method" })
@Test
public void htmlize() {
String[][] input_output = {
{"Newline\nshould become <br/>",
"Newline<br/>should become &lt;br/&gt;" },
{"This is a test", "This is a test" },
{"Open & Grok", "Open &amp; Grok" },
{"&amp;&lt;&gt;", "&amp;amp;&amp;lt;&amp;gt;" },
};
for (int i=0; i < input_output.length; i++) {
String[] in_out = input_output[i];
if (i == 0) {
assertEquals(in_out[1].replace("<br/>", "\n"), Util.htmlize(in_out[0]));
} else {
assertEquals(in_out[1], Util.htmlize(in_out[0]));
}
// 2 args
assertEquals(in_out[1], Util.htmlize(in_out[0], "<br/>"));
}
}
@SuppressWarnings({ "javadoc", "static-method" })
@Test
public void breadcrumbPath() {
assertEquals(null, Util.breadcrumbPath("/root/", null));
assertEquals("", Util.breadcrumbPath("/root/", ""));
assertEquals("<a href=\"/root/x\">x</a>",
Util.breadcrumbPath("/root/", "x"));
assertEquals("<a href=\"/root/xx\">xx</a>",
Util.breadcrumbPath("/root/", "xx"));
// parent directories have a trailing slash in href
assertEquals("<a href=\"/r/a/\">a</a>/<a href=\"/r/a/b\">b</a>",
Util.breadcrumbPath("/r/", "a/b"));
// if basename is a dir (ends with file seperator), href link also
// ends with a '/'
assertEquals("<a href=\"/r/a/\">a</a>/<a href=\"/r/a/b/\">b</a>/",
Util.breadcrumbPath("/r/", "a/b/"));
// should work the same way with a '.' as file separator
assertEquals("<a href=\"/r/java/\">java</a>." +
"<a href=\"/r/java/lang/\">lang</a>." +
"<a href=\"/r/java/lang/String\">String</a>",
Util.breadcrumbPath("/r/", "java.lang.String", '.'));
// suffix added to the link?
assertEquals("<a href=\"/root/xx&project=y\">xx</a>",
Util.breadcrumbPath("/root/", "xx", '/', "&project=y", false));
// compact: path needs to be resolved to /xx and no link is added
// for the virtual root directory (parent) but emitted as plain text.
// Prefix gets just prefixed as is and not mangled wrt. path -> "//"
assertEquals("/<a href=\"/root//xx&project=y\">xx</a>",
Util.breadcrumbPath("/root/", "../xx", '/', "&project=y", true));
// relative pathes are resolved wrt. / , so path resolves to /a/c/d
assertEquals("/<a href=\"/r//a/\">a</a>/" +
"<a href=\"/r//a/c/\">c</a>/" +
"<a href=\"/r//a/c/d\">d</a>",
Util.breadcrumbPath("/r/", "../a/b/../c//d", '/', "", true));
}
@SuppressWarnings({ "javadoc", "static-method" })
@Test
public void redableSize() {
assertEquals("0 ", Util.readableSize(0));
assertEquals("1 ", Util.readableSize(1));
assertEquals("-1 ", Util.readableSize(-1));
assertEquals("1,000 ", Util.readableSize(1000));
assertEquals("1 KiB", Util.readableSize(1024));
assertEquals("2.4 KiB", Util.readableSize(2500));
assertEquals("<b>1.4 MiB</b>", Util.readableSize(1474560));
assertEquals("<b>3,584.4 MiB</b>", Util.readableSize(3758489600L));
assertEquals("<b>8,796,093,022,208 MiB</b>",
Util.readableSize(Long.MAX_VALUE));
}
@SuppressWarnings({ "javadoc", "static-method" })
@Test
public void path2uid() {
assertEquals("\u0000etc\u0000passwd\u0000date",
Util.path2uid("/etc/passwd", "date"));
}
@SuppressWarnings({ "javadoc", "static-method" })
@Test
public void uid2url() {
assertEquals("/etc/passwd", Util.uid2url(
Util.path2uid("/etc/passwd", "date")));
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void uriEncodeURL() {
assertEquals("http://www.heise.de",
Util.uriEncodeURL("http://www.heise.de"));
assertEquals("http://www.heise.de/",
Util.uriEncodeURL("http://www.heise.de/"));
// important to escape ", since it may terminate an attr value ...
assertEquals("http://www.hei%22se.de/",
Util.uriEncodeURL("http://www.hei\"se.de/"));
// & handling
assertEquals("http://www.heise.de/bla&amp;alpha",
Util.uriEncodeURL("http://www.heise.de/bla&alpha"));
assertEquals("http://www.heise.de/bla?x=1&amp;alpha",
Util.uriEncodeURL("http://www.heise.de/bla?x=1&alpha"));
assertEquals("http://www.heise.de/bla?x=1&amp;alpha",
Util.uriEncodeURL("http://www.heise.de/bla?x=1&amp;alpha"));
// as intended: encoder is not perfect - stops at 1st entity encountered
assertEquals("http://www.heise.de/bla?x=1&amp;alpha&bla",
Util.uriEncodeURL("http://www.heise.de/bla?x=1&amp;alpha&bla"));
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void URIEncodePath() throws Exception {
String[] testPath = new String[] {
"", "/", "a", "\t", "a+b", "a b", "/a//x/yz/##/ / ?",
"foo::bar::test.js", "bl\u00E5b\u00E6rsyltet\u00F8y"
};
// Assuming this test will not be run in a sandbox and URI's internal
// decoder does the right thing. URI assumes UTF-8 encoding, what we
// use as well.
Class<?> clazz = URI.class;
Method decoder = clazz.getDeclaredMethod("decode", String.class);
decoder.setAccessible(true);
for (int i=testPath.length-1; i >= 0; i--) {
String encoded = Util.uriEncodePath(testPath[i]);
Object decoded = decoder.invoke(clazz, encoded);
assertEquals(testPath[i], decoded);
}
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void formQuoteEscape() {
assertEquals("", Util.formQuoteEscape(null));
assertEquals("abc&amp;", Util.formQuoteEscape("abc&"));
assertEquals("&quot;abc&quot;", Util.formQuoteEscape("\"abc\""));
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void diffline() {
String[][] tests = {
{
"\"(ses_id, mer_id, pass_id, \" + refCol +\" , mer_ref, amnt, "
+ "cur, ps_id, ret_url, d_req_time, d_req_mil, h_resp_time, "
+ "h_resp_mil) \"",
"\"(ses_id, mer_id, pass_id, \" + refCol +\" , mer_ref, amnt, "
+ "cur, ps_id, ret_url, exp_url, d_req_time, d_req_mil, "
+ "h_resp_time, h_resp_mil) \"",
"\"(ses_id, mer_id, pass_id, \" + refCol +\" , mer_ref, amnt, "
+ "cur, ps_id, ret_url, <span class=\"p\">exp_url, "
+ "</span>d_req_time, d_req_mil, h_resp_time, h_resp_mil) \""
},
{
"\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\", values);",
"\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\", values);",
"\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?<span "
+ "class=\"p\">, ?</span>)\", values);"
},
{
"char *config_list = NULL;",
"char **config_list = NULL;",
"char *<span class=\"p\">*</span>config_list = NULL;"
},
{
"* An error occured or there is non-numeric stuff at the end",
"* An error occurred or there is non-numeric stuff at the end",
"* An error occur<span class=\"p\">r</span>ed or there is "
+ "non-numeric stuff at the end"
}
};
for (int i=0; i < tests.length; i++) {
String[] strings=Util.diffline(tests[i][0], tests[i][1]);
assertEquals(""+ i + "," + 0, strings[0], tests[i][0]);
assertEquals(""+ i + "," + 1, strings[1], tests[i][2]);
}
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void dumpConfiguration() throws Exception {
StringBuilder out = new StringBuilder();
Util.dumpConfiguration(out);
String s = out.toString();
// Verify that we got a table.
assertTrue(s.startsWith("<table"));
// Verify that the output is well-formed.
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + s;
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
new ByteArrayInputStream(xml.getBytes("UTF-8")));
}
@SuppressWarnings({ "static-method", "javadoc" })
@Test
public void jsStringLiteral() {
assertEquals("\"abc\\n\\r\\\"'\\\\\"",
Util.jsStringLiteral("abc\n\r\"'\\"));
}
}