📄 stringstest.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.wicket.util.string;import java.io.UnsupportedEncodingException;import junit.framework.Assert;import junit.framework.TestCase;import org.apache.wicket.WicketRuntimeException;/** * Test cases for the <code>Strings</code> class. * * @author Jonathan Locke * @author Martijn Dashorst */public final class StringsTest extends TestCase{ /** * */ public void test() { Assert.assertEquals("foo", Strings.lastPathComponent("bar.garply.foo", '.')); Assert.assertEquals("foo", Strings.lastPathComponent("foo", '.')); Assert.assertEquals("bar", Strings.firstPathComponent("bar.garply.foo", '.')); Assert.assertEquals("foo", Strings.lastPathComponent("foo", '.')); Assert.assertEquals("garply.foo", Strings.afterFirstPathComponent("bar.garply.foo", '.')); Assert.assertEquals("", Strings.afterFirstPathComponent("foo", '.')); Assert.assertEquals("bar.baz", Strings.beforeLast("bar.baz.foo", '.')); Assert.assertEquals("", Strings.beforeLast("bar", '.')); Assert.assertEquals("bar", Strings.beforeFirst("bar.baz.foo", '.')); Assert.assertEquals("", Strings.beforeFirst("bar", '.')); Assert.assertEquals("baz.foo", Strings.afterFirst("bar.baz.foo", '.')); Assert.assertEquals("", Strings.afterFirst("bar", '.')); Assert.assertEquals("foo", Strings.afterLast("bar.baz.foo", '.')); Assert.assertEquals("", Strings.afterLast("bar", '.')); Assert.assertEquals("foo", Strings.replaceAll("afaooaaa", "a", "").toString()); Assert.assertEquals("fuzzyffuzzyoofuzzyfuzzyfuzzy", Strings.replaceAll("afaooaaa", "a", "fuzzy").toString()); } /** * Tests the <code>beforeFirst</code> method. */ public void testBeforeFirst() { assertNull(Strings.beforeFirst(null, '.')); assertEquals("", Strings.beforeFirst("", '.')); assertEquals("", Strings.beforeFirst("", ' ')); assertEquals("", Strings.beforeFirst(".", '.')); assertEquals("", Strings.beforeFirst("..", '.')); assertEquals("com", Strings.beforeFirst("com.foo.bar", '.')); assertEquals("com", Strings.beforeFirst("com foo bar", ' ')); assertEquals("com foo", Strings.beforeFirst("com foo.bar", '.')); } /** * Tests the <code>afterFirst</code> method. */ public void testAfterFirst() { assertNull(Strings.afterFirst(null, '.')); assertEquals("", Strings.afterFirst("", '.')); assertEquals("", Strings.afterFirst("", ' ')); assertEquals("", Strings.afterFirst(".", '.')); assertEquals(".", Strings.afterFirst("..", '.')); assertEquals("foo.bar", Strings.afterFirst("com.foo.bar", '.')); assertEquals("foo bar", Strings.afterFirst("com foo bar", ' ')); assertEquals("bar", Strings.afterFirst("com.foo bar", ' ')); } /** * Tests the <code>afterLast</code> method. */ public void testAfterLast() { assertNull(Strings.afterLast(null, '.')); assertEquals("", Strings.afterLast("", '.')); assertEquals("", Strings.afterLast("", ' ')); assertEquals("", Strings.afterLast(".", '.')); assertEquals("", Strings.afterLast("..", '.')); assertEquals("bar", Strings.afterLast("com.foo.bar", '.')); assertEquals("bar", Strings.afterLast("com foo bar", ' ')); assertEquals("bar", Strings.afterLast("com foo.bar", '.')); } /** * Tests the beforeLastPathComponent method */ public void testBeforeLastPathComponent() { assertNull(Strings.beforeLastPathComponent(null, '.')); assertEquals("", Strings.beforeLastPathComponent("", '.')); assertEquals("", Strings.beforeLastPathComponent("", ' ')); assertEquals("", Strings.beforeLastPathComponent(".", '.')); assertEquals(".", Strings.beforeLastPathComponent("..", '.')); assertEquals("foo", Strings.beforeLastPathComponent("foo.bar", '.')); assertEquals("", Strings.beforeLastPathComponent("foo.bar", ' ')); assertEquals("foo.ba", Strings.beforeLastPathComponent("foo.bar", 'r')); assertEquals("com.foo", Strings.beforeLastPathComponent("com.foo.bar", '.')); } /** * Tests the capitalize method. */ public void testCapitalize() { assertEquals("Lorem ipsum dolor sit amet", Strings.capitalize("lorem ipsum dolor sit amet")); assertEquals("Lorem ipsum dolor sit amet", Strings.capitalize("Lorem ipsum dolor sit amet")); assertEquals(" abcdefghijklm", Strings.capitalize(" abcdefghijklm")); assertEquals("", Strings.capitalize("")); assertNull(Strings.capitalize(null)); } /** * Tests the escapeMarkup method. */ public void testEscapeMarkup() { assertNull(Strings.escapeMarkup(null)); assertEquals("", Strings.escapeMarkup("").toString()); assertEquals("&", Strings.escapeMarkup("&").toString()); assertEquals("&#", Strings.escapeMarkup("&#").toString()); assertEquals("�", Strings.escapeMarkup("�").toString()); assertEquals("&amp;", Strings.escapeMarkup("&").toString()); assertEquals("< >&"'?:;{}[]-_+=()*^%$#@!~`", Strings.escapeMarkup( "< >&\"'?:;{}[]-_+=()*^%$#@!~`").toString()); assertEquals("< >&"'?:;{}[]-_+=()*^%$#@!~`", Strings.escapeMarkup( "< >&\"'?:;{}[]-_+=()*^%$#@!~`", true).toString()); } /** * Tests the escapeMarkup method with whitespace. */ public void testEscapeMarkupWhiteSpace() { assertNull(Strings.escapeMarkup(null, true)); assertEquals("", Strings.escapeMarkup("", true).toString()); assertEquals("\n \t", Strings.escapeMarkup("\n \t", false).toString()); assertEquals("\n ", Strings.escapeMarkup("\n \t", true) .toString()); assertEquals(" ", Strings.escapeMarkup(" ", false).toString()); assertEquals(" ", Strings.escapeMarkup(" ", true).toString()); } /** * Tests the escapeMarkup method with unicode escapes. * * @throws UnsupportedEncodingException */ public void testEscapeMarkupUnicode() throws UnsupportedEncodingException { assertNull(Strings.escapeMarkup(null, true, true)); assertEquals("", Strings.escapeMarkup("", true, true).toString()); // The escaped unicode is ����������" assertEquals("Çüéâäàåçêë", Strings .escapeMarkup("\u00c7\u00fc\u00e9\u00e2\u00e4\u00e0\u00e5\u00e7\u00ea\u00eb", false, true).toString()); assertEquals("\n \té", Strings.escapeMarkup(convertNonASCIIString("\n \té"), false, true).toString()); assertEquals(convertNonASCIIString("\n \té"), Strings.escapeMarkup( convertNonASCIIString("\n \té"), false, false).toString()); } /** * Tests the <code>replaceHtmlEscapeNumber</code> method. * * @throws UnsupportedEncodingException */ public void testReplaceHtmlEscapeNumber() throws UnsupportedEncodingException { assertNull(Strings.replaceHtmlEscapeNumber(null)); assertEquals("", Strings.replaceHtmlEscapeNumber("")); assertEquals("abcdefghijklm�", Strings.replaceHtmlEscapeNumber("abcdefghijklm�")); assertEquals("a &#", Strings.replaceHtmlEscapeNumber("a &#")); assertEquals( "\u00c7\u00fc\u00e9\u00e2\u00e4\u00e0\u00e5\u00e7\u00ea\u00eb", Strings .replaceHtmlEscapeNumber("Çüéâäàåçêë")); } private String convertNonASCIIString(String str) throws UnsupportedEncodingException { return new String(str.getBytes(), "iso-8859-1"); } /** * Tests the <code>firstPathComponent</code> method. */ public void testFirstPathComponent() { assertNull(Strings.firstPathComponent(null, '.')); assertEquals("", Strings.firstPathComponent("", '.')); assertEquals("foo", Strings.firstPathComponent("foo", '.')); assertEquals("foo", Strings.firstPathComponent("foo.bar", '.')); assertEquals("foo bar", Strings.firstPathComponent("foo bar", '.')); } /** * Tests the <code>isEmpty</code> method. */ public void testIsEmpty() { assertTrue(Strings.isEmpty(null)); assertTrue(Strings.isEmpty("")); assertTrue(Strings.isEmpty(" ")); assertTrue(Strings.isEmpty(" ")); assertTrue(Strings.isEmpty(" \n\t")); assertFalse(Strings.isEmpty("a")); assertFalse(Strings.isEmpty(" a")); } /** * Tests the <code>isTrue</code> method. * * @throws StringValueConversionException */ public void testIsTrue() throws StringValueConversionException { assertFalse(Strings.isTrue(null)); assertFalse(Strings.isTrue("")); assertFalse(Strings.isTrue(" \n \t")); assertFalse(Strings.isTrue("no")); assertFalse(Strings.isTrue("n")); assertFalse(Strings.isTrue("false")); assertFalse(Strings.isTrue("nO")); assertFalse(Strings.isTrue("N"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -