// Copyright (c) 2010 SuccessFactors, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above
// copyright notice, this list of conditions and the following
// disclaimer.
//
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// * Neither the name of the SuccessFactors, Inc. nor the names of
// its contributors may be used to endorse or promote products
// derived from this software without specific prior written
// permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
package org.owasp.jxt;
import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* JxtUtilsTest
*
* @author Jeffrey Ichnowski
* @version $Revision: 8 $
*/
public class JxtUtilsTest {
@Test
public void testCapitalize() {
assertEquals("Foo", JxtUtils.capitalize("foo"));
assertEquals("_bar", JxtUtils.capitalize("_bar"));
assertEquals("Baz", JxtUtils.capitalize("Baz"));
assertEquals("X", JxtUtils.capitalize("x"));
try {
JxtUtils.capitalize(null);
fail("Expected an exception");
} catch (IllegalArgumentException expected) {}
try {
JxtUtils.capitalize("");
fail("Expected an exception");
} catch (IllegalArgumentException expected) {}
}
@Test
public void testSetterName() {
assertEquals("setFoo", JxtUtils.setterName("foo"));
}
@Test
public void testQuoteAttribute() {
assertEquals("\"abc\"", JxtUtils.quoteAttribute("abc"));
assertEquals("\"'\"", JxtUtils.quoteAttribute("\'"));
assertEquals("'\"'", JxtUtils.quoteAttribute("\""));
assertEquals("\"<>&'"\"", JxtUtils.quoteAttribute("<>&'\""));
}
@Test
public void testUnescapeXml() {
assertEquals("abc", JxtUtils.unescapeXml("abc"));
assertEquals("<>&\'\"", JxtUtils.unescapeXml("<>&'""));
assertEquals("\'\"", JxtUtils.unescapeXml("'""));
// error edge case
assertEquals("&aapos;", JxtUtils.unescapeXml("&aapos;"));
// common bad case
assertEquals("a=b&c=d", JxtUtils.unescapeXml("a=b&c=d"));
// "EOF" in the middle of an entity
assertEquals("&", JxtUtils.unescapeXml("&"));
// hex escapes
assertEquals("\u00a0\u201c\uffef", JxtUtils.unescapeXml(" “￯"));
assertEquals("\u00a0\u201c\uffef", JxtUtils.unescapeXml(" “￯"));
// almost a numeric entity
assertEquals("&#;", JxtUtils.unescapeXml("&#;"));
assertEquals("&#x;", JxtUtils.unescapeXml("&#x;"));
}
@Test
public void testResolveRelative() {
assertEquals("/foo.jxt", JxtUtils.resolveRelative(null, "/foo.jxt"));
assertEquals("/foo.jxt", JxtUtils.resolveRelative(null, "foo.jxt"));
assertEquals("/foo.jxt", JxtUtils.resolveRelative(null, "/./foo.jxt"));
assertEquals("/foo.jxt", JxtUtils.resolveRelative(null, "/bar/../foo.jxt"));
assertEquals("/foo.jxt", JxtUtils.resolveRelative(null, "/../foo.jxt"));
assertEquals("/foo.jxt", JxtUtils.resolveRelative(null, "/./bar/../baz/./../foo.jxt"));
assertEquals("/foo.jxt", JxtUtils.resolveRelative("/bar/baz.jxt", "/foo.jxt"));
assertEquals("/bar/foo.jxt", JxtUtils.resolveRelative("/bar/baz.jxt", "foo.jxt"));
assertEquals("/bar/baz.jxt/foo.jxt", JxtUtils.resolveRelative("/bar/baz.jxt/", "foo.jxt"));
assertEquals("/foo.jxt", JxtUtils.resolveRelative("/bar/baz.jxt", "../foo.jxt"));
}
private void checkAppendAttribute(String expected, String key, Object value, String defaultValue)
throws Exception
{
StringBuilder out = new StringBuilder();
JxtUtils.appendAttribute(out, key, value, defaultValue);
assertEquals(expected, out.toString());
}
@Test
public void testAppendAttribute() throws Exception {
checkAppendAttribute(" key=\"value\"", "key", "value", null);
checkAppendAttribute("", "key", null, null);
checkAppendAttribute("", "type", "text", "text");
checkAppendAttribute("", "foo", null, "bar");
checkAppendAttribute(" key=\"key\"", "key", true, "value");
checkAppendAttribute("", "key", false, "value");
}
} // JxtUtilsTest
|