UtilTest.java revision 1025
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/*
1024N/A * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
1024N/A * Use is subject to license terms.
1024N/A */
160N/Apackage org.opensolaris.opengrok.web;
160N/A
160N/Aimport java.io.StringWriter;
160N/Aimport java.util.Locale;
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 // Are these two correct? Why don't we create links?
160N/A assertEquals("", Util.breadcrumbPath("/root/", ""));
160N/A assertEquals("x", Util.breadcrumbPath("/root/", "x"));
160N/A
160N/A assertEquals("<a href=\"/root/xx\">xx</a>",
160N/A Util.breadcrumbPath("/root/", "xx"));
160N/A
604N/A assertEquals("<a href=\"/r/a\">a</a>/<a href=\"/r/a/b\">b</a>",
160N/A Util.breadcrumbPath("/r/", "a/b"));
604N/A
604N/A assertEquals("<a href=\"/r/a\">a</a>/<a href=\"/r/a/b\">b</a>/",
604N/A Util.breadcrumbPath("/r/", "a/b/"));
605N/A
605N/A assertEquals("<a href=\"/r/java\">java</a>." +
605N/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", '.'));
605N/A
605N/A assertEquals("<a href=\"/root/xx&project=y\">xx</a>",
605N/A Util.breadcrumbPath("/root/", "xx", '/', "&project=y", false));
605N/A
605N/A assertEquals("<a href=\"/root/xx&project=y\">xx</a>",
605N/A Util.breadcrumbPath("/root/", "xx", '/', "&project=y", true));
605N/A
605N/A assertEquals("<a href=\"/r/\">..</a>/" +
605N/A "<a href=\"/r/a\">a</a>/" +
605N/A "<a href=\"/r/a/b\">b</a>/" +
605N/A "<a href=\"/r/a\">..</a>/" +
605N/A "<a href=\"/r/a/c\">c</a>/" +
605N/A "/" +
605N/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() {
160N/A assertEquals("0", Util.redableSize(0));
160N/A assertEquals("1", Util.redableSize(1));
160N/A assertEquals("-1", Util.redableSize(-1));
160N/A assertEquals("1,000", Util.redableSize(1000));
160N/A assertEquals("1K", Util.redableSize(1024));
160N/A assertEquals("2.4K", Util.redableSize(2500));
160N/A assertEquals("<b>1.4M</b>", Util.redableSize(1474560));
160N/A assertEquals("<b>3,584.4M</b>", Util.redableSize(3758489600L));
160N/A assertEquals("<b>8,796,093,022,208M</b>",
160N/A Util.redableSize(Long.MAX_VALUE));
160N/A }
160N/A
160N/A @Test
160N/A public void readableLine() throws Exception {
160N/A StringWriter out = new StringWriter();
160N/A Util.readableLine(42, out, null);
848N/A assertEquals("\n<a class=\"l\" name=\"42\" href=\"#42\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;42&nbsp;</a>",
160N/A out.toString());
160N/A
160N/A out.getBuffer().setLength(0); // clear buffer
160N/A Util.readableLine(110, out, null);
848N/A assertEquals("\n<a class=\"hl\" name=\"110\" href=\"#110\">&nbsp;&nbsp;&nbsp;&nbsp;110&nbsp;</a>",
160N/A out.toString());
160N/A }
160N/A
160N/A @Test
160N/A public void uid() {
160N/A assertEquals("\u0000etc\u0000passwd\u0000date",
160N/A Util.uid("/etc/passwd", "date"));
160N/A }
160N/A
160N/A @Test
160N/A public void uid2url() {
160N/A assertEquals("/etc/passwd", Util.uid2url(
160N/A Util.uid("/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
160N/A public void URIEncodePath() {
160N/A assertEquals("", Util.URIEncodePath(""));
160N/A assertEquals("/", Util.URIEncodePath("/"));
160N/A assertEquals("a", Util.URIEncodePath("a"));
292N/A assertEquals("a+b", Util.URIEncodePath("a+b"));
292N/A assertEquals("a%20b", Util.URIEncodePath("a b"));
292N/A assertEquals("/a//x/yz/%23%23/%20/%20%3F",
292N/A Util.URIEncodePath("/a//x/yz/##/ / ?"));
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() {
1025N/A String[] strings=Util.diffline("\"(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) \"");
1025N/A assertEquals(strings[0],"\"(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) \"");
1025N/A assertEquals(strings[1],"\"(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) \"");
1025N/A strings=Util.diffline("\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\", values);","\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\", values);");
1025N/A assertEquals(strings[0],"\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\", values);");
1025N/A assertEquals(strings[1],"\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?<span class=\"a\">, ?</span>)\", values);");
1025N/A strings=Util.diffline("char *config_list = NULL;","char **config_list = NULL;");
1025N/A assertEquals(strings[0],"char *config_list = NULL;");
1025N/A assertEquals(strings[1],"char *<span class=\"a\">*</span>config_list = NULL;");
1025N/A strings=Util.diffline("* An error occured or there is non-numeric stuff at the end","* An error occurred or there is non-numeric stuff at the end");
1025N/A assertEquals(strings[0],"* An error occured or there is non-numeric stuff at the end");
1025N/A assertEquals(strings[1],"* An error occur<span class=\"a\">r</span>ed or there is non-numeric stuff at the end");
1025N/A }
1024N/A}
1025N/A