PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/luni/src/test/java/tests/api/java/io/CharArrayWriterTest.java

https://github.com/MIPS/libcore
Java | 436 lines | 304 code | 32 blank | 100 comment | 2 complexity | dd999630840bf1a945c78105b09b45cd MD5 | raw file
  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. package tests.api.java.io;
  18. import java.io.CharArrayReader;
  19. import java.io.CharArrayWriter;
  20. import java.io.IOException;
  21. import java.io.StringWriter;
  22. import tests.support.Support_ASimpleWriter;
  23. import dalvik.annotation.TestLevel;
  24. import dalvik.annotation.TestTargetClass;
  25. import dalvik.annotation.TestTargetNew;
  26. @TestTargetClass(CharArrayWriter.class)
  27. public class CharArrayWriterTest extends junit.framework.TestCase {
  28. char[] hw = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
  29. CharArrayWriter cw;
  30. CharArrayReader cr;
  31. /**
  32. * @tests java.io.CharArrayWriter#CharArrayWriter(int)
  33. */
  34. @TestTargetNew(
  35. level = TestLevel.COMPLETE,
  36. method = "CharArrayWriter",
  37. args = {int.class}
  38. )
  39. public void test_ConstructorI() {
  40. // Test for method java.io.CharArrayWriter(int)
  41. cw = new CharArrayWriter(90);
  42. assertEquals("Test 1: Incorrect writer created.", 0, cw.size());
  43. try {
  44. cw = new CharArrayWriter(-1);
  45. fail("IllegalArgumentException expected.");
  46. } catch (IllegalArgumentException e) {
  47. // Expected.
  48. }
  49. }
  50. /**
  51. * @tests java.io.CharArrayWriter#CharArrayWriter()
  52. */
  53. @TestTargetNew(
  54. level = TestLevel.COMPLETE,
  55. notes = "Verifies CharArrayWriter() method.",
  56. method = "CharArrayWriter",
  57. args = {}
  58. )
  59. public void test_Constructor() {
  60. // Test for method java.io.CharArrayWriter()
  61. cw = new CharArrayWriter();
  62. assertEquals("Created incorrect writer", 0, cw.size());
  63. }
  64. /**
  65. * @tests java.io.CharArrayWriter#close()
  66. */
  67. @TestTargetNew(
  68. level = TestLevel.COMPLETE,
  69. notes = "Verifies close() method.",
  70. method = "close",
  71. args = {}
  72. )
  73. public void test_close() {
  74. // Test for method void java.io.CharArrayWriter.close()
  75. cw.close();
  76. }
  77. /**
  78. * @tests java.io.CharArrayWriter#flush()
  79. */
  80. @TestTargetNew(
  81. level = TestLevel.COMPLETE,
  82. notes = "Verifies flush() method.",
  83. method = "flush",
  84. args = {}
  85. )
  86. public void test_flush() {
  87. // Test for method void java.io.CharArrayWriter.flush()
  88. cw.flush();
  89. }
  90. /**
  91. * @tests java.io.CharArrayWriter#reset()
  92. */
  93. @TestTargetNew(
  94. level = TestLevel.COMPLETE,
  95. notes = "Verifies reset() method.",
  96. method = "reset",
  97. args = {}
  98. )
  99. public void test_reset() {
  100. // Test for method void java.io.CharArrayWriter.reset()
  101. cw.write("HelloWorld", 5, 5);
  102. cw.reset();
  103. cw.write("HelloWorld", 0, 5);
  104. cr = new CharArrayReader(cw.toCharArray());
  105. try {
  106. char[] c = new char[100];
  107. cr.read(c, 0, 5);
  108. assertEquals("Reset failed to reset buffer",
  109. "Hello", new String(c, 0, 5));
  110. } catch (IOException e) {
  111. fail("Exception during reset test : " + e.getMessage());
  112. }
  113. }
  114. /**
  115. * @tests java.io.CharArrayWriter#size()
  116. */
  117. @TestTargetNew(
  118. level = TestLevel.COMPLETE,
  119. notes = "Verifies size() method.",
  120. method = "size",
  121. args = {}
  122. )
  123. public void test_size() {
  124. // Test for method int java.io.CharArrayWriter.size()
  125. assertEquals("Returned incorrect size", 0, cw.size());
  126. cw.write(hw, 5, 5);
  127. assertEquals("Returned incorrect size", 5, cw.size());
  128. }
  129. /**
  130. * @tests java.io.CharArrayWriter#toCharArray()
  131. */
  132. @TestTargetNew(
  133. level = TestLevel.COMPLETE,
  134. notes = "Verifies toCharArray() method.",
  135. method = "toCharArray",
  136. args = {}
  137. )
  138. public void test_toCharArray() {
  139. // Test for method char [] java.io.CharArrayWriter.toCharArray()
  140. cw.write("HelloWorld", 0, 10);
  141. cr = new CharArrayReader(cw.toCharArray());
  142. try {
  143. char[] c = new char[100];
  144. cr.read(c, 0, 10);
  145. assertEquals("toCharArray failed to return correct array",
  146. "HelloWorld", new String(c, 0, 10));
  147. } catch (IOException e) {
  148. fail("Exception during toCharArray test : " + e.getMessage());
  149. }
  150. }
  151. /**
  152. * @tests java.io.CharArrayWriter#toString()
  153. */
  154. @TestTargetNew(
  155. level = TestLevel.COMPLETE,
  156. notes = "Verifies toString() method.",
  157. method = "toString",
  158. args = {}
  159. )
  160. public void test_toString() {
  161. // Test for method java.lang.String java.io.CharArrayWriter.toString()
  162. cw.write("HelloWorld", 5, 5);
  163. cr = new CharArrayReader(cw.toCharArray());
  164. assertEquals("Returned incorrect string",
  165. "World", cw.toString());
  166. }
  167. /**
  168. * @tests java.io.CharArrayWriter#write(char[], int, int)
  169. */
  170. @TestTargetNew(
  171. level = TestLevel.PARTIAL_COMPLETE,
  172. method = "write",
  173. args = {char[].class, int.class, int.class}
  174. )
  175. public void test_write$CII() {
  176. // Test for method void java.io.CharArrayWriter.write(char [], int, int)
  177. cw.write(hw, 5, 5);
  178. cr = new CharArrayReader(cw.toCharArray());
  179. try {
  180. char[] c = new char[100];
  181. cr.read(c, 0, 5);
  182. assertEquals("Writer failed to write correct chars",
  183. "World", new String(c, 0, 5));
  184. } catch (IOException e) {
  185. fail("Exception during write test : " + e.getMessage());
  186. }
  187. }
  188. /**
  189. * @tests java.io.CharArrayWriter#write(char[], int, int)
  190. * Regression for HARMONY-387
  191. */
  192. @TestTargetNew(
  193. level = TestLevel.PARTIAL_COMPLETE,
  194. notes = "Illegal argument checks.",
  195. method = "write",
  196. args = {char[].class, int.class, int.class}
  197. )
  198. public void test_write$CII_Exception() {
  199. char[] target = new char[10];
  200. cw = new CharArrayWriter();
  201. try {
  202. cw.write(target, -1, 1);
  203. fail("Test 1: IndexOutOfBoundsException expected.");
  204. } catch (IndexOutOfBoundsException e) {
  205. // Expected
  206. }
  207. try {
  208. cw.write(target, 0, -1);
  209. fail("Test 2: IndexOutOfBoundsException expected.");
  210. } catch (IndexOutOfBoundsException e) {
  211. // Expected
  212. }
  213. try {
  214. cw.write(target, 1, target.length);
  215. fail("Test 3: IndexOutOfBoundsException expected.");
  216. } catch (IndexOutOfBoundsException e) {
  217. // Expected
  218. }
  219. try {
  220. cw.write((char[]) null, 1, 1);
  221. fail("Test 4: NullPointerException expected.");
  222. } catch (NullPointerException e) {
  223. // Expected.
  224. }
  225. }
  226. /**
  227. * @tests java.io.CharArrayWriter#write(int)
  228. */
  229. @TestTargetNew(
  230. level = TestLevel.COMPLETE,
  231. notes = "Verifies write(int) method.",
  232. method = "write",
  233. args = {int.class}
  234. )
  235. public void test_writeI() {
  236. // Test for method void java.io.CharArrayWriter.write(int)
  237. cw.write('T');
  238. cr = new CharArrayReader(cw.toCharArray());
  239. try {
  240. assertEquals("Writer failed to write char", 'T', cr.read());
  241. } catch (IOException e) {
  242. fail("Exception during write test : " + e.getMessage());
  243. }
  244. }
  245. /**
  246. * @tests java.io.CharArrayWriter#write(java.lang.String, int, int)
  247. */
  248. @TestTargetNew(
  249. level = TestLevel.COMPLETE,
  250. notes = "Verifies write(java.lang.String, int, int) method. [Need to check different strings?]",
  251. method = "write",
  252. args = {java.lang.String.class, int.class, int.class}
  253. )
  254. public void test_writeLjava_lang_StringII() {
  255. // Test for method void java.io.CharArrayWriter.write(java.lang.String,
  256. // int, int)
  257. cw.write("HelloWorld", 5, 5);
  258. cr = new CharArrayReader(cw.toCharArray());
  259. try {
  260. char[] c = new char[100];
  261. cr.read(c, 0, 5);
  262. assertEquals("Writer failed to write correct chars",
  263. "World", new String(c, 0, 5));
  264. } catch (IOException e) {
  265. fail("Exception during write test : " + e.getMessage());
  266. }
  267. }
  268. /**
  269. * @tests java.io.CharArrayWriter#write(java.lang.String, int, int)
  270. * Regression for HARMONY-387
  271. */
  272. @TestTargetNew(
  273. level = TestLevel.PARTIAL,
  274. notes = "Regression for write(java.lang.String, int, int) method.",
  275. method = "write",
  276. args = {java.lang.String.class, int.class, int.class}
  277. )
  278. public void test_writeLjava_lang_StringII_2() throws StringIndexOutOfBoundsException {
  279. CharArrayWriter obj = new CharArrayWriter();
  280. try {
  281. obj.write((String) null, -1, 0);
  282. fail("NullPointerException expected");
  283. } catch (NullPointerException t) {
  284. }
  285. }
  286. /**
  287. * @tests java.io.CharArrayWriter#writeTo(java.io.Writer)
  288. */
  289. @TestTargetNew(
  290. level = TestLevel.COMPLETE,
  291. method = "writeTo",
  292. args = {java.io.Writer.class}
  293. )
  294. public void test_writeToLjava_io_Writer() {
  295. Support_ASimpleWriter ssw = new Support_ASimpleWriter(true);
  296. cw.write("HelloWorld", 0, 10);
  297. StringWriter sw = new StringWriter();
  298. try {
  299. cw.writeTo(sw);
  300. assertEquals("Test 1: Writer failed to write correct chars;",
  301. "HelloWorld", sw.toString());
  302. } catch (IOException e) {
  303. fail("Exception during writeTo test : " + e.getMessage());
  304. }
  305. try {
  306. cw.writeTo(ssw);
  307. fail("Test 2: IOException expected.");
  308. } catch (IOException e) {
  309. // Expected.
  310. }
  311. }
  312. /**
  313. * Sets up the fixture, for example, open a network connection. This method
  314. * is called before a test is executed.
  315. */
  316. protected void setUp() {
  317. cw = new CharArrayWriter();
  318. }
  319. /**
  320. * Tears down the fixture, for example, close a network connection. This
  321. * method is called after a test is executed.
  322. */
  323. protected void tearDown() {
  324. if (cr != null)
  325. cr.close();
  326. cw.close();
  327. }
  328. /**
  329. * @tests java.io.CharArrayWriter#append(char)
  330. */
  331. @TestTargetNew(
  332. level = TestLevel.COMPLETE,
  333. notes = "Verifies append(char c) method.",
  334. method = "append",
  335. args = {char.class}
  336. )
  337. public void test_appendChar() throws IOException {
  338. char testChar = ' ';
  339. CharArrayWriter writer = new CharArrayWriter(10);
  340. writer.append(testChar);
  341. writer.flush();
  342. assertEquals(String.valueOf(testChar), writer.toString());
  343. writer.close();
  344. }
  345. /**
  346. * @tests java.io.CharArrayWriter#append(CharSequence)
  347. */
  348. @TestTargetNew(
  349. level = TestLevel.COMPLETE,
  350. notes = "Verifies append(CharSequence csq) method.",
  351. method = "append",
  352. args = {java.lang.CharSequence.class}
  353. )
  354. public void test_appendCharSequence() {
  355. String testString = "My Test String";
  356. CharArrayWriter writer = new CharArrayWriter(10);
  357. writer.append(testString);
  358. writer.flush();
  359. assertEquals(testString, writer.toString());
  360. writer.close();
  361. }
  362. /**
  363. * @tests java.io.CharArrayWriter#append(CharSequence, int, int)
  364. */
  365. @TestTargetNew(
  366. level = TestLevel.COMPLETE,
  367. method = "append",
  368. args = {java.lang.CharSequence.class, int.class, int.class}
  369. )
  370. public void test_appendLjava_langCharSequenceII() {
  371. String testString = "My Test String";
  372. CharArrayWriter writer = new CharArrayWriter(10);
  373. // Illegal argument checks.
  374. try {
  375. writer.append(testString, -1, 0);
  376. fail("Test 1: IndexOutOfBoundsException expected.");
  377. } catch (IndexOutOfBoundsException e) {
  378. // Expected.
  379. }
  380. try {
  381. writer.append(testString, 0, -1);
  382. fail("Test 2: IndexOutOfBoundsException expected.");
  383. } catch (IndexOutOfBoundsException e) {
  384. // Expected.
  385. }
  386. try {
  387. writer.append(testString, 1, 0);
  388. fail("Test 3: IndexOutOfBoundsException expected.");
  389. } catch (IndexOutOfBoundsException e) {
  390. // Expected.
  391. }
  392. try {
  393. writer.append(testString, 1, testString.length() + 1);
  394. fail("Test 4: IndexOutOfBoundsException expected.");
  395. } catch (IndexOutOfBoundsException e) {
  396. // Expected.
  397. }
  398. writer.append(testString, 1, 3);
  399. writer.flush();
  400. assertEquals("Test 5: Appending failed;",
  401. testString.substring(1, 3), writer.toString());
  402. writer.close();
  403. }
  404. }