1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package tests.api.java.io; 17 18import java.io.IOException; 19import java.io.Writer; 20 21import junit.framework.TestCase; 22import tests.support.Support_ASimpleWriter; 23import dalvik.annotation.TestLevel; 24import dalvik.annotation.TestTargetClass; 25import dalvik.annotation.TestTargetNew; 26 27@TestTargetClass(Writer.class) 28public class WriterTest extends TestCase { 29 30 /** 31 * @tests java.io.Writer#append(char) 32 */ 33 @TestTargetNew( 34 level = TestLevel.COMPLETE, 35 notes = "", 36 method = "append", 37 args = {char.class} 38 ) 39 public void test_appendChar() throws IOException { 40 char testChar = ' '; 41 MockWriter writer = new MockWriter(20); 42 writer.append(testChar); 43 assertEquals(String.valueOf(testChar), String.valueOf(writer 44 .getContents())); 45 writer.close(); 46 47 Writer tobj = new Support_ASimpleWriter(2); 48 tobj.append('a'); 49 tobj.append('b'); 50 assertEquals("Wrong stuff written!", "ab", tobj.toString()); 51 try { 52 tobj.append('c'); 53 fail("IOException not thrown!"); 54 } catch (IOException e) { 55 // expected 56 } 57 } 58 59 /** 60 * @tests java.io.Writer#append(CharSequence) 61 */ 62 @TestTargetNew( 63 level = TestLevel.COMPLETE, 64 notes = "", 65 method = "append", 66 args = {java.lang.CharSequence.class} 67 ) 68 public void test_appendCharSequence() throws IOException { 69 String testString = "My Test String"; 70 MockWriter writer = new MockWriter(20); 71 writer.append(testString); 72 assertEquals(testString, String.valueOf(writer.getContents())); 73 writer.close(); 74 75 Writer tobj = new Support_ASimpleWriter(20); 76 tobj.append(testString); 77 assertEquals("Wrong stuff written!", testString, tobj.toString()); 78 try { 79 tobj.append(testString); 80 fail("IOException not thrown!"); 81 } catch (IOException e) { 82 // expected 83 } 84 } 85 86 /** 87 * @tests java.io.Writer#append(CharSequence, int, int) 88 */ 89 @TestTargetNew( 90 level = TestLevel.PARTIAL_COMPLETE, 91 notes = "", 92 method = "append", 93 args = {CharSequence.class, int.class, int.class} 94 ) 95 public void test_appendCharSequenceIntInt() throws IOException { 96 String testString = "My Test String"; 97 MockWriter writer = new MockWriter(20); 98 writer.append(testString, 1, 3); 99 assertEquals(testString.substring(1, 3), String.valueOf(writer 100 .getContents())); 101 writer.close(); 102 103 Writer tobj = new Support_ASimpleWriter(21); 104 testString = "0123456789abcdefghijABCDEFGHIJ"; 105 tobj.append(testString, 0, 5); 106 assertEquals("Wrong stuff written!", "01234", tobj.toString()); 107 tobj.append(testString, 10, 15); 108 assertEquals("Wrong stuff written!", "01234abcde", tobj.toString()); 109 tobj.append(testString, 20, 30); 110 assertEquals("Wrong stuff written!", "01234abcdeABCDEFGHIJ", tobj.toString()); 111 try { 112 tobj.append(testString, 30, 31); 113 fail("IndexOutOfBoundsException not thrown!"); 114 } catch (IndexOutOfBoundsException e) { 115 // expected 116 } 117 tobj.append(testString, 20, 21); // Just fill the writer to its limit! 118 try { 119 tobj.append(testString, 29, 30); 120 fail("IOException not thrown!"); 121 } catch (IOException e) { 122 // expected 123 } 124 } 125 126 /** 127 * @tests java.io.Writer#append(CharSequence, int, int) 128 */ 129 @TestTargetNew( 130 level = TestLevel.PARTIAL_COMPLETE, 131 notes = "", 132 method = "append", 133 args = {CharSequence.class, int.class, int.class} 134 ) 135 public void test_appendCharSequenceIntInt_Exception() throws IOException { 136 String testString = "My Test String"; 137 Writer tobj = new Support_ASimpleWriter(21); 138 try { 139 tobj.append(testString, 30, 31); 140 fail("IndexOutOfBoundsException not thrown!"); 141 } catch (IndexOutOfBoundsException e) { 142 // expected 143 } 144 try { 145 tobj.append(testString, -1, 1); 146 fail("IndexOutOfBoundsException not thrown!"); 147 } catch (IndexOutOfBoundsException e) { 148 // expected 149 } 150 try { 151 tobj.append(testString, 0, -1); 152 fail("IndexOutOfBoundsException not thrown!"); 153 } catch (IndexOutOfBoundsException e) { 154 // expected 155 } 156 } 157 158 159 @TestTargetNew( 160 level = TestLevel.COMPLETE, 161 notes = "", 162 method = "write", 163 args = {char[].class} 164 ) 165 public void test_write$C() throws IOException { 166 Writer tobj = new Support_ASimpleWriter(21); 167 tobj.write("01234".toCharArray()); 168 assertEquals("Wrong stuff written!", "01234", tobj.toString()); 169 tobj.write("abcde".toCharArray()); 170 assertEquals("Wrong stuff written!", "01234abcde", tobj.toString()); 171 tobj.write("ABCDEFGHIJ".toCharArray()); 172 assertEquals("Wrong stuff written!", "01234abcdeABCDEFGHIJ", tobj.toString()); 173 tobj.write("z".toCharArray()); // Just fill the writer to its limit! 174 try { 175 tobj.write("LES JEUX SONT FAITS".toCharArray()); 176 fail("IOException not thrown!"); 177 } catch (IOException e) { 178 // expected 179 } 180 } 181 182 @TestTargetNew( 183 level = TestLevel.COMPLETE, 184 notes = "", 185 method = "write", 186 args = {int.class} 187 ) 188 public void test_writeI() throws IOException { 189 Writer tobj = new Support_ASimpleWriter(2); 190 tobj.write('a'); 191 tobj.write('b'); 192 assertEquals("Wrong stuff written!", "ab", tobj.toString()); 193 try { 194 tobj.write('c'); 195 fail("IOException not thrown!"); 196 } catch (IOException e) { 197 // expected 198 } 199 } 200 201 @TestTargetNew( 202 level = TestLevel.COMPLETE, 203 notes = "", 204 method = "write", 205 args = {java.lang.String.class} 206 ) 207 public void test_writeLjava_lang_String() throws IOException { 208 Writer tobj = new Support_ASimpleWriter(21); 209 tobj.write("01234"); 210 assertEquals("Wrong stuff written!", "01234", tobj.toString()); 211 tobj.write("abcde"); 212 assertEquals("Wrong stuff written!", "01234abcde", tobj.toString()); 213 tobj.write("ABCDEFGHIJ"); 214 assertEquals("Wrong stuff written!", "01234abcdeABCDEFGHIJ", tobj.toString()); 215 tobj.write("z"); // Just fill the writer to its limit! 216 try { 217 tobj.write("LES JEUX SONT FAITS"); 218 fail("IOException not thrown!"); 219 } catch (IOException e) { 220 // expected 221 } 222 } 223 224 /** 225 * @tests java.io.PrintWriter#write(java.lang.String, int, int) 226 */ 227 @TestTargetNew( 228 level = TestLevel.PARTIAL_COMPLETE, 229 notes = "", 230 method = "write", 231 args = {String.class, int.class, int.class} 232 ) 233 public void test_writeLjava_lang_StringII() throws IOException { 234 String testString; 235 Writer tobj = new Support_ASimpleWriter(21); 236 testString = "0123456789abcdefghijABCDEFGHIJ"; 237 tobj.write(testString, 0, 5); 238 assertEquals("Wrong stuff written!", "01234", tobj.toString()); 239 tobj.write(testString, 10, 5); 240 assertEquals("Wrong stuff written!", "01234abcde", tobj.toString()); 241 tobj.write(testString, 20, 10); 242 assertEquals("Wrong stuff written!", "01234abcdeABCDEFGHIJ", tobj.toString()); 243 try { 244 tobj.write(testString, 30, 1); 245 fail("IndexOutOfBoundsException not thrown!"); 246 } catch (IndexOutOfBoundsException e) { 247 // expected 248 } 249 tobj.write(testString, 20, 1); // Just fill the writer to its limit! 250 try { 251 tobj.write(testString, 29, 1); 252 fail("IOException not thrown!"); 253 } catch (IOException e) { 254 // expected 255 } 256 } 257 258 /** 259 * @tests java.io.Writer#append(CharSequence, int, int) 260 */ 261 @TestTargetNew( 262 level = TestLevel.PARTIAL_COMPLETE, 263 notes = "", 264 method = "write", 265 args = {String.class, int.class, int.class} 266 ) 267 public void test_writeLjava_lang_StringII_Exception() throws IOException { 268 String testString = "My Test String"; 269 Writer tobj = new Support_ASimpleWriter(21); 270 try { 271 tobj.write(testString, 30, 31); 272 fail("IndexOutOfBoundsException not thrown!"); 273 } catch (IndexOutOfBoundsException e) { 274 // expected 275 } 276 try { 277 tobj.write(testString, -1, 1); 278 fail("IndexOutOfBoundsException not thrown!"); 279 } catch (IndexOutOfBoundsException e) { 280 // expected 281 } 282 try { 283 tobj.write(testString, 0, -1); 284 fail("IndexOutOfBoundsException not thrown!"); 285 } catch (IndexOutOfBoundsException e) { 286 // expected 287 } 288 } 289 290 class MockWriter extends Writer { 291 private char[] contents; 292 293 private int length; 294 295 private int offset; 296 297 MockWriter(int capacity) { 298 contents = new char[capacity]; 299 length = capacity; 300 offset = 0; 301 } 302 303 public synchronized void close() throws IOException { 304 flush(); 305 contents = null; 306 } 307 308 public synchronized void flush() throws IOException { 309 // do nothing 310 } 311 312 public void write(char[] buffer, int offset, int count) 313 throws IOException { 314 if (null == contents) { 315 throw new IOException(); 316 } 317 if (offset < 0 || count < 0 || offset >= buffer.length) { 318 throw new IndexOutOfBoundsException(); 319 } 320 count = Math.min(count, buffer.length - offset); 321 count = Math.min(count, this.length - this.offset); 322 for (int i = 0; i < count; i++) { 323 contents[this.offset + i] = buffer[offset + i]; 324 } 325 this.offset += count; 326 327 } 328 329 public char[] getContents() { 330 char[] result = new char[offset]; 331 for (int i = 0; i < offset; i++) { 332 result[i] = contents[i]; 333 } 334 return result; 335 } 336 } 337 338} 339