UtilTest.java revision 1185
0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/A * Common Development and Distribution License (the "License").
0N/A * You may not use this file except in compliance with the License.
0N/A *
0N/A * See LICENSE.txt included in this distribution for the specific
0N/A * language governing permissions and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at LICENSE.txt.
0N/A * If applicable, add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your own identifying
0N/A * information: Portions Copyright [yyyy] [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A */
0N/A
0N/A/*
119N/A * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A */
0N/Apackage org.opensolaris.opengrok.web;
0N/A
65N/Aimport java.io.ByteArrayInputStream;
125N/Aimport java.io.StringWriter;
125N/Aimport java.util.Locale;
125N/Aimport javax.xml.parsers.DocumentBuilderFactory;
125N/Aimport org.junit.AfterClass;
58N/Aimport org.junit.BeforeClass;
77N/Aimport org.junit.Test;
125N/Aimport static org.junit.Assert.*;
125N/A
125N/A/**
125N/A * Test of the methods in <code>org.opensolaris.opengrok.web.Util</code>.
261N/A */
261N/Apublic class UtilTest {
312N/A private static Locale savedLocale;
312N/A
428N/A @BeforeClass
126N/A public static void setUpClass() {
58N/A // Some of the methods have different results in different locales.
394N/A // Set locale to en_US for these tests.
8N/A savedLocale = Locale.getDefault();
77N/A Locale.setDefault(Locale.US);
0N/A }
0N/A
0N/A @AfterClass
0N/A public static void tearDownClass() {
0N/A Locale.setDefault(savedLocale);
0N/A savedLocale = null;
434N/A }
0N/A
312N/A @Test
312N/A public void htmlize() {
320N/A String[][] input_output = {
312N/A {"This is a test", "This is a test" },
312N/A {"Newline\nshould become <br/>",
312N/A "Newline<br/>should become &lt;br/&gt;" },
312N/A {"Open & Grok", "Open &amp; Grok" },
65N/A {"&amp;&lt;&gt;", "&amp;amp;&amp;lt;&amp;gt;" },
65N/A };
65N/A for (String[] in_out : input_output) {
65N/A // 1 arg
0N/A assertEquals(in_out[1], Util.htmlize(in_out[0]));
30N/A // 2 args
58N/A StringBuilder sb = new StringBuilder();
312N/A Util.htmlize(in_out[0], sb);
312N/A assertEquals(in_out[1], sb.toString());
260N/A }
112N/A }
428N/A
376N/A @Test
376N/A public void breadcrumbPath() {
0N/A assertEquals(null, Util.breadcrumbPath("/root/", null));
11N/A
0N/A assertEquals("", Util.breadcrumbPath("/root/", ""));
240N/A
58N/A assertEquals("<a href=\"/root/x\">x</a>",
58N/A Util.breadcrumbPath("/root/", "x"));
58N/A assertEquals("<a href=\"/root/xx\">xx</a>",
58N/A Util.breadcrumbPath("/root/", "xx"));
77N/A
207N/A // parent directories have a trailing slash in href
207N/A assertEquals("<a href=\"/r/a/\">a</a>/<a href=\"/r/a/b\">b</a>",
261N/A Util.breadcrumbPath("/r/", "a/b"));
260N/A // if basename is a dir (ends with file seperator), href link also
77N/A // ends with a '/'
260N/A assertEquals("<a href=\"/r/a/\">a</a>/<a href=\"/r/a/b/\">b</a>/",
112N/A Util.breadcrumbPath("/r/", "a/b/"));
77N/A // should work the same way with a '.' as file separator
77N/A assertEquals("<a href=\"/r/java/\">java</a>." +
77N/A "<a href=\"/r/java/lang/\">lang</a>." +
77N/A "<a href=\"/r/java/lang/String\">String</a>",
260N/A Util.breadcrumbPath("/r/", "java.lang.String", '.'));
77N/A // suffix added to the link?
77N/A assertEquals("<a href=\"/root/xx&project=y\">xx</a>",
77N/A Util.breadcrumbPath("/root/", "xx", '/', "&project=y", false));
0N/A // compact: path needs to be resolved to /xx and no link is added
77N/A // for the virtual root directory (parent) but emitted as plain text.
111N/A // Prefix gets just prefixed as is and not mangled wrt. path -> "//"
111N/A assertEquals("/<a href=\"/root//xx&project=y\">xx</a>",
111N/A Util.breadcrumbPath("/root/", "../xx", '/', "&project=y", true));
111N/A // relative pathes are resolved wrt. / , so path resolves to /a/c/d
111N/A assertEquals("/<a href=\"/r//a/\">a</a>/" +
111N/A "<a href=\"/r//a/c/\">c</a>/" +
111N/A "<a href=\"/r//a/c/d\">d</a>",
111N/A Util.breadcrumbPath("/r/", "../a/b/../c//d", '/', "", true));
111N/A }
111N/A
111N/A @Test
111N/A public void redableSize() {
77N/A assertEquals("0 ", Util.readableSize(0));
77N/A assertEquals("1 ", Util.readableSize(1));
77N/A assertEquals("-1 ", Util.readableSize(-1));
207N/A assertEquals("1,000 ", Util.readableSize(1000));
207N/A assertEquals("1 KiB", Util.readableSize(1024));
77N/A assertEquals("2.4 KiB", Util.readableSize(2500));
77N/A assertEquals("<b>1.4 MiB</b>", Util.readableSize(1474560));
99N/A assertEquals("<b>3,584.4 MiB</b>", Util.readableSize(3758489600L));
77N/A assertEquals("<b>8,796,093,022,208 MiB</b>",
77N/A Util.readableSize(Long.MAX_VALUE));
77N/A }
77N/A
77N/A @Test
77N/A public void readableLine() throws Exception {
77N/A StringWriter out = new StringWriter();
77N/A // hmmm - where do meaningful test start?
0N/A Util.readableLine(42, out, null, null, null);
77N/A assertEquals("\n<a class=\"l\" name=\"42\" href=\"#42\">42</a>",
77N/A out.toString());
77N/A
77N/A out.getBuffer().setLength(0); // clear buffer
77N/A Util.readableLine(110, out, null, null, null);
77N/A assertEquals("\n<a class=\"hl\" name=\"110\" href=\"#110\">110</a>",
77N/A out.toString());
77N/A }
77N/A
77N/A @Test
77N/A public void path2uid() {
111N/A assertEquals("\u0000etc\u0000passwd\u0000date",
111N/A Util.path2uid("/etc/passwd", "date"));
77N/A }
77N/A
77N/A @Test
240N/A public void uid2url() {
173N/A assertEquals("/etc/passwd", Util.uid2url(
173N/A Util.path2uid("/etc/passwd", "date")));
173N/A }
173N/A
254N/A @Test
173N/A public void URIEncode() {
173N/A assertEquals("", Util.URIEncode(""));
173N/A assertEquals("a+b", Util.URIEncode("a b"));
254N/A assertEquals("a%23b", Util.URIEncode("a#b"));
173N/A assertEquals("a%2Fb", Util.URIEncode("a/b"));
173N/A assertEquals("README.txt", Util.URIEncode("README.txt"));
173N/A }
253N/A
312N/A @Test
253N/A public void URIEncodePath() {
253N/A assertEquals("", Util.URIEncodePath(""));
253N/A assertEquals("/", Util.URIEncodePath("/"));
253N/A assertEquals("a", Util.URIEncodePath("a"));
253N/A assertEquals("a+b", Util.URIEncodePath("a+b"));
253N/A assertEquals("a%20b", Util.URIEncodePath("a b"));
253N/A assertEquals("/a//x/yz/%23%23/%20/%20%3F",
253N/A Util.URIEncodePath("/a//x/yz/##/ / ?"));
253N/A }
312N/A
312N/A @Test
312N/A public void formQuoteEscape() {
253N/A assertEquals("", Util.formQuoteEscape(null));
253N/A assertEquals("abc", Util.formQuoteEscape("abc"));
99N/A assertEquals("&quot;abc&quot;", Util.formQuoteEscape("\"abc\""));
77N/A }
77N/A
77N/A @Test
77N/A public void diffline() {
77N/A String[][] tests = {
0N/A {
0N/A "\"(ses_id, mer_id, pass_id, \" + refCol +\" , mer_ref, amnt, "
77N/A + "cur, ps_id, ret_url, d_req_time, d_req_mil, h_resp_time, "
125N/A + "h_resp_mil) \"",
77N/A "\"(ses_id, mer_id, pass_id, \" + refCol +\" , mer_ref, amnt, "
77N/A + "cur, ps_id, ret_url, exp_url, d_req_time, d_req_mil, "
205N/A + "h_resp_time, h_resp_mil) \"",
205N/A
205N/A "\"(ses_id, mer_id, pass_id, \" + refCol +\" , mer_ref, amnt, "
112N/A + "cur, ps_id, ret_url, <span class=\"a\">exp_url, "
112N/A + "</span>d_req_time, d_req_mil, h_resp_time, h_resp_mil) \""
112N/A },
77N/A {
106N/A "\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\", values);",
119N/A "\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\", values);",
106N/A
119N/A "\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?<span "
106N/A + "class=\"a\">, ?</span>)\", values);"
119N/A },
119N/A {
119N/A "char *config_list = NULL;",
119N/A "char **config_list = NULL;",
106N/A
106N/A "char *<span class=\"a\">*</span>config_list = NULL;"
106N/A },
99N/A {
99N/A "* An error occured or there is non-numeric stuff at the end",
99N/A "* An error occurred or there is non-numeric stuff at the end",
99N/A
99N/A "* An error occur<span class=\"a\">r</span>ed or there is "
99N/A + "non-numeric stuff at the end"
99N/A }
99N/A };
99N/A for (int i=0; i < tests.length; i++) {
125N/A String[] strings=Util.diffline(new StringBuilder(tests[i][0]),
125N/A new StringBuilder(tests[i][1]));
125N/A assertEquals(""+ i + "," + 0, strings[0], tests[i][0]);
125N/A assertEquals(""+ i + "," + 1, strings[1], tests[i][2]);
125N/A }
125N/A }
125N/A
125N/A @Test
125N/A public void dumpConfiguration() throws Exception {
126N/A StringBuilder out = new StringBuilder();
125N/A Util.dumpConfiguration(out);
125N/A String s = out.toString();
125N/A
126N/A // Verify that we got a table.
126N/A assertTrue(s.startsWith("<table"));
126N/A
126N/A // Verify that the output is well-formed.
126N/A String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + s;
126N/A DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
126N/A new ByteArrayInputStream(xml.getBytes("UTF-8")));
126N/A }
126N/A
126N/A @Test
126N/A public void jsStringLiteral() {
126N/A assertEquals("\"abc\\n\\r\\\"\\\\\"",
126N/A Util.jsStringLiteral("abc\n\r\"\\"));
126N/A }
126N/A}
126N/A
126N/A