1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.io; 19 20import dalvik.annotation.TestTargetClass; 21import dalvik.annotation.TestTargets; 22import dalvik.annotation.TestTargetNew; 23import dalvik.annotation.TestLevel; 24 25import java.io.IOException; 26import java.io.StringWriter; 27 28@TestTargetClass(StringWriter.class) 29public class StringWriterTest extends junit.framework.TestCase { 30 31 StringWriter sw; 32 33 /** 34 * @tests java.io.StringWriter#StringWriter() 35 */ 36 @TestTargets({ 37 @TestTargetNew( 38 level = TestLevel.COMPLETE, 39 notes = "", 40 method = "StringWriter", 41 args = {} 42 ), 43 @TestTargetNew( 44 level = TestLevel.COMPLETE, 45 notes = "", 46 method = "StringWriter", 47 args = {int.class} 48 ) 49 }) 50 public void test_Constructor() { 51 new StringWriter(); 52 new StringWriter(10); 53 new StringWriter(10000); 54 } 55 56 /** 57 * @tests java.io.StringWriter#close() 58 */ 59 @TestTargetNew( 60 level = TestLevel.COMPLETE, 61 notes = "There won't be IOException, ever!", 62 method = "close", 63 args = {} 64 ) 65 public void test_close() { 66 // Test for method void java.io.StringWriter.close() 67 try { 68 sw.close(); 69 } catch (IOException e) { 70 fail("IOException closing StringWriter : " + e.getMessage()); 71 } 72 } 73 74 /** 75 * @tests java.io.StringWriter#flush() 76 */ 77 @TestTargetNew( 78 level = TestLevel.COMPLETE, 79 notes = "", 80 method = "flush", 81 args = {} 82 ) 83 public void test_flush() { 84 // Test for method void java.io.StringWriter.flush() 85 sw.flush(); 86 sw.write('c'); 87 assertEquals("Failed to flush char", "c", sw.toString()); 88 } 89 90 /** 91 * @tests java.io.StringWriter#getBuffer() 92 */ 93 @TestTargetNew( 94 level = TestLevel.COMPLETE, 95 notes = "", 96 method = "getBuffer", 97 args = {} 98 ) 99 public void test_getBuffer() { 100 // Test for method java.lang.StringBuffer 101 // java.io.StringWriter.getBuffer() 102 103 sw.write("This is a test string"); 104 StringBuffer sb = sw.getBuffer(); 105 assertEquals("Incorrect buffer returned", 106 "This is a test string", sb.toString()); 107 } 108 109 /** 110 * @tests java.io.StringWriter#toString() 111 */ 112 @TestTargetNew( 113 level = TestLevel.COMPLETE, 114 notes = "", 115 method = "toString", 116 args = {} 117 ) 118 public void test_toString() { 119 // Test for method java.lang.String java.io.StringWriter.toString() 120 sw.write("This is a test string"); 121 assertEquals("Incorrect string returned", 122 "This is a test string", sw.toString()); 123 } 124 125 /** 126 * @tests java.io.StringWriter#write(char[], int, int) 127 */ 128 @TestTargetNew( 129 level = TestLevel.PARTIAL_COMPLETE, 130 notes = "", 131 method = "write", 132 args = {char[].class, int.class, int.class} 133 ) 134 public void test_write$CII() { 135 // Test for method void java.io.StringWriter.write(char [], int, int) 136 char[] c = new char[1000]; 137 "This is a test string".getChars(0, 21, c, 0); 138 sw.write(c, 4, 14); 139 assertEquals("Chars not written properly", 140 " is a test str", sw.toString()); 141 } 142 143 /** 144 * @tests java.io.StringWriter#write(char[], int, int) 145 * Regression for HARMONY-387 146 */ 147 @TestTargetNew( 148 level = TestLevel.PARTIAL_COMPLETE, 149 notes = "", 150 method = "write", 151 args = {char[].class, int.class, int.class} 152 ) 153 public void test_write$CII_Exception() { 154 StringWriter obj = new StringWriter(); 155 try { 156 obj.write(new char[10], 0, -1); 157 fail("IndexOutOfBoundsException expected"); 158 } catch (IndexOutOfBoundsException e) { 159 // Expected 160 } 161 162 try { 163 obj.write(new char[10], -1, 1); 164 fail("IndexOutOfBoundsException expected"); 165 } catch (IndexOutOfBoundsException e) { 166 // Expected 167 } 168 169 try { 170 obj.write(new char[10], 2, 9); 171 fail("IndexOutOfBoundsException expected"); 172 } catch (IndexOutOfBoundsException e) { 173 // Expected 174 } 175 } 176 177 /** 178 * @tests java.io.StringWriter#write(int) 179 */ 180 @TestTargetNew( 181 level = TestLevel.COMPLETE, 182 notes = "", 183 method = "write", 184 args = {int.class} 185 ) 186 public void test_writeI() { 187 // Test for method void java.io.StringWriter.write(int) 188 sw.write('c'); 189 assertEquals("Char not written properly", "c", sw.toString()); 190 } 191 192 /** 193 * @tests java.io.StringWriter#write(java.lang.String) 194 */ 195 @TestTargetNew( 196 level = TestLevel.COMPLETE, 197 notes = "", 198 method = "write", 199 args = {java.lang.String.class} 200 ) 201 public void test_writeLjava_lang_String() { 202 // Test for method void java.io.StringWriter.write(java.lang.String) 203 sw.write("This is a test string"); 204 assertEquals("String not written properly", 205 "This is a test string", sw.toString()); 206 } 207 208 /** 209 * @tests java.io.StringWriter#write(java.lang.String, int, int) 210 */ 211 @TestTargetNew( 212 level = TestLevel.COMPLETE, 213 notes = "", 214 method = "write", 215 args = {java.lang.String.class, int.class, int.class} 216 ) 217 public void test_writeLjava_lang_StringII() { 218 // Test for method void java.io.StringWriter.write(java.lang.String, 219 // int, int) 220 sw.write("This is a test string", 2, 2); 221 assertEquals("String not written properly", "is", sw.toString()); 222 } 223 224 /** 225 * @tests java.io.StringWriter#append(char) 226 */ 227 @TestTargetNew( 228 level = TestLevel.COMPLETE, 229 notes = "", 230 method = "append", 231 args = {char.class} 232 ) 233 public void test_appendChar() throws IOException { 234 char testChar = ' '; 235 StringWriter stringWriter = new StringWriter(20); 236 stringWriter.append(testChar); 237 assertEquals(String.valueOf(testChar), stringWriter.toString()); 238 stringWriter.close(); 239 } 240 241 /** 242 * @tests java.io.PrintWriter#append(CharSequence) 243 */ 244 @TestTargetNew( 245 level = TestLevel.COMPLETE, 246 notes = "", 247 method = "append", 248 args = {java.lang.CharSequence.class} 249 ) 250 public void test_appendCharSequence() throws IOException { 251 252 String testString = "My Test String"; 253 StringWriter stringWriter = new StringWriter(20); 254 stringWriter.append(testString); 255 assertEquals(String.valueOf(testString), stringWriter.toString()); 256 stringWriter.close(); 257 } 258 259 /** 260 * @tests java.io.PrintWriter#append(CharSequence, int, int) 261 */ 262 @TestTargetNew( 263 level = TestLevel.COMPLETE, 264 notes = "", 265 method = "append", 266 args = {java.lang.CharSequence.class, int.class, int.class} 267 ) 268 public void test_appendCharSequenceIntInt() throws IOException { 269 String testString = "My Test String"; 270 StringWriter stringWriter = new StringWriter(20); 271 stringWriter.append(testString, 1, 3); 272 assertEquals(testString.substring(1, 3), stringWriter.toString()); 273 stringWriter.close(); 274 275 try { 276 StringWriter tobj = new StringWriter(9); 277 tobj.append("01234567890123456789", 19, 2); 278 fail("IndexOutOfBoundsException not thrown!"); 279 } catch (IndexOutOfBoundsException e) { 280 // expected 281 } 282 try { 283 StringWriter tobj = new StringWriter(9); 284 tobj.append("01234567890123456789", 29, 2); 285 fail("IndexOutOfBoundsException not thrown!"); 286 } catch (IndexOutOfBoundsException e) { 287 // expected 288 } 289 } 290 291 /** 292 * Sets up the fixture, for example, open a network connection. This method 293 * is called before a test is executed. 294 */ 295 protected void setUp() { 296 297 sw = new StringWriter(); 298 } 299 300 /** 301 * Tears down the fixture, for example, close a network connection. This 302 * method is called after a test is executed. 303 */ 304 protected void tearDown() { 305 } 306} 307