UtilTest.java revision 160
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
160N/A public void Htmlize() {
160N/A String[][] input_output = {
160N/A { "This is a test", "This is a test" },
160N/A { "Newline\nshould become <br/>",
160N/A "Newline<br/>should become &lt;br/&gt;" },
160N/A { "Open & Grok", "Open &amp; Grok" },
160N/A { "&amp;&lt;&gt;", "&amp;amp;&amp;lt;&amp;gt;" },
160N/A };
160N/A for (String[] in_out : input_output) {
160N/A // 1 arg
160N/A assertEquals(in_out[1], Util.Htmlize(in_out[0]));
160N/A // 2 args
160N/A StringBuilder sb = new StringBuilder();
160N/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
160N/A assertEquals("<a href=\"/r/a/\">a</a>/<a href=\"/r/a/b\">b</a>",
160N/A Util.breadcrumbPath("/r/", "a/b"));
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);
160N/A assertEquals("\n<a class=\"l\" name=\"42\"> 42 </a>",
160N/A out.toString());
160N/A
160N/A out.getBuffer().setLength(0); // clear buffer
160N/A Util.readableLine(110, out, null);
160N/A assertEquals("\n<a class=\"hl\" name=\"110\"> 110 </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"));
160N/A assertEquals("//x/yz/%23%23/+/+%3F",
160N/A Util.URIEncodePath("//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 }
160N/A
160N/A}