/* * 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 org.opensolaris.opengrok.web.Util. */ 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
", "Newline
should become <br/>" }, {"This is a test", "This is a test" }, {"Open & Grok", "Open & Grok" }, {"&<>", "&amp;&lt;&gt;" }, }; for (int i=0; i < input_output.length; i++) { String[] in_out = input_output[i]; if (i == 0) { assertEquals(in_out[1].replace("
", "\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], "
")); } } @SuppressWarnings({ "javadoc", "static-method" }) @Test public void breadcrumbPath() { assertEquals(null, Util.breadcrumbPath("/root/", null)); assertEquals("", Util.breadcrumbPath("/root/", "")); assertEquals("x", Util.breadcrumbPath("/root/", "x")); assertEquals("xx", Util.breadcrumbPath("/root/", "xx")); // parent directories have a trailing slash in href assertEquals("a/b", Util.breadcrumbPath("/r/", "a/b")); // if basename is a dir (ends with file seperator), href link also // ends with a '/' assertEquals("a/b/", Util.breadcrumbPath("/r/", "a/b/")); // should work the same way with a '.' as file separator assertEquals("java." + "lang." + "String", Util.breadcrumbPath("/r/", "java.lang.String", '.')); // suffix added to the link? assertEquals("xx", 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("/xx", Util.breadcrumbPath("/root/", "../xx", '/', "&project=y", true)); // relative pathes are resolved wrt. / , so path resolves to /a/c/d assertEquals("/a/" + "c/" + "d", 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("1.4 MiB", Util.readableSize(1474560)); assertEquals("3,584.4 MiB", Util.readableSize(3758489600L)); assertEquals("8,796,093,022,208 MiB", 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&alpha", Util.uriEncodeURL("http://www.heise.de/bla&alpha")); assertEquals("http://www.heise.de/bla?x=1&alpha", Util.uriEncodeURL("http://www.heise.de/bla?x=1&alpha")); assertEquals("http://www.heise.de/bla?x=1&alpha", Util.uriEncodeURL("http://www.heise.de/bla?x=1&alpha")); // as intended: encoder is not perfect - stops at 1st entity encountered assertEquals("http://www.heise.de/bla?x=1&alpha&bla", Util.uriEncodeURL("http://www.heise.de/bla?x=1&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&", Util.formQuoteEscape("abc&")); assertEquals(""abc"", 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, exp_url, " + "d_req_time, d_req_mil, h_resp_time, h_resp_mil) \"" }, { "\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\", values);", "\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\", values);", "\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\", values);" }, { "char *config_list = NULL;", "char **config_list = NULL;", "char **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 occurred 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("\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\"'\\")); } }