UtilTest.java revision 1382
308N/A/*
308N/A * CDDL HEADER START
308N/A *
308N/A * The contents of this file are subject to the terms of the
308N/A * Common Development and Distribution License (the "License").
308N/A * You may not use this file except in compliance with the License.
308N/A *
308N/A * See LICENSE.txt included in this distribution for the specific
308N/A * language governing permissions and limitations under the License.
308N/A *
308N/A * When distributing Covered Code, include this CDDL HEADER in each
308N/A * file and include the License file at LICENSE.txt.
308N/A * If applicable, add the following below this CDDL HEADER, with the
308N/A * fields enclosed by brackets "[]" replaced with your own identifying
308N/A * information: Portions Copyright [yyyy] [name of copyright owner]
308N/A *
308N/A * CDDL HEADER END
308N/A */
308N/A
308N/A/*
1062N/A * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
308N/A * Portions Copyright 2012 Jens Elkner.
308N/A */
308N/Apackage org.opensolaris.opengrok.web;
308N/A
308N/Aimport java.io.ByteArrayInputStream;
308N/Aimport java.lang.reflect.Method;
1178N/Aimport java.net.URI;
308N/Aimport java.util.Locale;
308N/Aimport javax.xml.parsers.DocumentBuilderFactory;
308N/Aimport org.junit.AfterClass;
308N/Aimport org.junit.BeforeClass;
308N/Aimport org.junit.Test;
308N/Aimport static org.junit.Assert.*;
308N/A
308N/A/**
308N/A * Test of the methods in <code>org.opensolaris.opengrok.web.Util</code>.
1190N/A */
308N/Apublic class UtilTest {
308N/A private static Locale savedLocale;
1238N/A
308N/A @BeforeClass
1190N/A public static void setUpClass() {
308N/A // Some of the methods have different results in different locales.
308N/A // Set locale to en_US for these tests.
308N/A savedLocale = Locale.getDefault();
308N/A Locale.setDefault(Locale.US);
308N/A }
308N/A
308N/A @AfterClass
308N/A public static void tearDownClass() {
308N/A Locale.setDefault(savedLocale);
1062N/A savedLocale = null;
308N/A }
1178N/A
308N/A @Test
308N/A public void htmlize() {
308N/A String[][] input_output = {
308N/A {"This is a test", "This is a test" },
308N/A {"Newline\nshould become <br/>",
308N/A "Newline<br/>should become &lt;br/&gt;" },
308N/A {"Open & Grok", "Open &amp; Grok" },
308N/A {"&amp;&lt;&gt;", "&amp;amp;&amp;lt;&amp;gt;" },
308N/A };
308N/A for (String[] in_out : input_output) {
// 1 arg
assertEquals(in_out[1], Util.htmlize(in_out[0]));
// 2 args
StringBuilder sb = new StringBuilder();
Util.htmlize(in_out[0], sb);
assertEquals(in_out[1], sb.toString());
}
}
@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));
}
@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));
}
@Test
public void path2uid() {
assertEquals("\u0000etc\u0000passwd\u0000date",
Util.path2uid("/etc/passwd", "date"));
}
@Test
public void uid2url() {
assertEquals("/etc/passwd", Util.uid2url(
Util.path2uid("/etc/passwd", "date")));
}
@Test
public void URIEncode() {
assertEquals("", Util.URIEncode(""));
assertEquals("a+b", Util.URIEncode("a b"));
assertEquals("a%23b", Util.URIEncode("a#b"));
assertEquals("a%2Fb", Util.URIEncode("a/b"));
assertEquals("README.txt", Util.URIEncode("README.txt"));
}
@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);
}
}
@Test
public void formQuoteEscape() {
assertEquals("", Util.formQuoteEscape(null));
assertEquals("abc", Util.formQuoteEscape("abc"));
assertEquals("&quot;abc&quot;", Util.formQuoteEscape("\"abc\""));
}
@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=\"a\">exp_url, "
+ "</span>d_req_time, d_req_mil, h_resp_time, h_resp_mil) \""
},
{
"\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\", values);",
"\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\", values);",
"\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?<span "
+ "class=\"a\">, ?</span>)\", values);"
},
{
"char *config_list = NULL;",
"char **config_list = NULL;",
"char *<span class=\"a\">*</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=\"a\">r</span>ed or there is "
+ "non-numeric stuff at the end"
}
};
for (int i=0; i < tests.length; i++) {
String[] strings=Util.diffline(new StringBuilder(tests[i][0]),
new StringBuilder(tests[i][1]));
assertEquals(""+ i + "," + 0, strings[0], tests[i][0]);
assertEquals(""+ i + "," + 1, strings[1], tests[i][2]);
}
}
@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")));
}
@Test
public void jsStringLiteral() {
assertEquals("\"abc\\n\\r\\\"\\\\\"",
Util.jsStringLiteral("abc\n\r\"\\"));
}
}