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