1 38 package com.gargoylesoftware.htmlunit; 39 40 import java.io.BufferedReader; 41 import java.io.InputStream; 42 import java.io.InputStreamReader; 43 44 50 public final class TextUtilTest extends WebTestCase { 51 55 public TextUtilTest( final String name ) { 56 super(name); 57 } 58 59 60 63 public void testStartsWithIgnoreCase_nulls() { 64 try { 65 TextUtil.startsWithIgnoreCase(null, "foo"); 66 fail("Expected null pointer exception"); 67 } 68 catch( final NullPointerException e ) { 69 } 71 72 try { 73 TextUtil.startsWithIgnoreCase("foo", null); 74 fail("Expected null pointer exception"); 75 } 76 catch( final NullPointerException e ) { 77 } 79 } 80 81 82 85 public void testStartsWithIgnoreCase_emptyPrefix() { 86 try { 87 TextUtil.startsWithIgnoreCase("foo", ""); 88 fail("Expected IllegalArgumentException"); 89 } 90 catch( final IllegalArgumentException e ) { 91 } 93 } 94 95 96 99 public void testStartsWithIgnoreCase_ShouldReturnTrue() { 100 final String[][] data = { 101 {"foo","foo"}, 102 {"foo:bar","foo"}, 103 {"FOO:BAR","foo"}, 104 {"foo:bar","FOO"}, 105 }; 106 107 for( int i=0; i<data.length; i++ ) { 108 final String stringToCheck = data[i][0]; 109 final String prefix = data[i][1]; 110 111 assertTrue( 112 "stringToCheck=["+stringToCheck+"] prefix=["+prefix+"]", 113 TextUtil.startsWithIgnoreCase(stringToCheck, prefix)); 114 } 115 } 116 117 118 121 public void testStartsWithIgnoreCase_ShouldReturnFalse() { 122 final String[][] data = { 123 {"","foo"}, 124 {"fobar","foo"}, 125 {"fo","foo"}, 126 }; 127 128 for( int i=0; i<data.length; i++ ) { 129 final String stringToCheck = data[i][0]; 130 final String prefix = data[i][1]; 131 132 assertFalse( 133 "stringToCheck=["+stringToCheck+"] prefix=["+prefix+"]", 134 TextUtil.startsWithIgnoreCase(stringToCheck, prefix)); 135 } 136 } 137 138 141 public void testToInputStream_null() throws Exception { 142 try { 143 TextUtil.toInputStream(null); 144 fail("Expected NullPointerException"); 145 } 146 catch( final NullPointerException e ) { 147 } 149 } 150 151 152 155 public void testToInputStream() throws Exception { 156 final String[][] data = { 157 {"", null}, 158 {"a", "a"}, 159 {"abcdefABCDEF", "abcdefABCDEF"}, 160 }; 161 final String encoding = "ISO-8859-1"; 162 163 for( int i=0; i<data.length; i++ ) { 164 final String input = data[i][0]; 165 final String expectedResult = data[i][1]; 166 167 final InputStream inputStream = TextUtil.toInputStream(input, encoding); 168 final String actualResult 169 = new BufferedReader( new InputStreamReader(inputStream, encoding) ).readLine(); 170 assertEquals( expectedResult, actualResult); 171 } 172 } 173 } 174 | Popular Tags |