LineNumberInputStreamTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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 */ 17package tests.api.java.io; 18 19import java.io.BufferedInputStream; 20import java.io.ByteArrayInputStream; 21import java.io.IOException; 22import java.io.LineNumberInputStream; 23 24import dalvik.annotation.TestLevel; 25import dalvik.annotation.TestTargetClass; 26import dalvik.annotation.TestTargetNew; 27 28@SuppressWarnings("deprecation") 29@TestTargetClass( 30 value = LineNumberInputStream.class, 31 untestedMethods = { 32 @TestTargetNew( 33 method = "LineNumberInputStream", 34 args = {java.io.InputStream.class}, 35 level = TestLevel.NOT_NECESSARY, 36 notes = "Simply calls super(in). If this fails, " + 37 "none of the other tests would work." 38 ) 39 } 40) 41public class LineNumberInputStreamTest extends junit.framework.TestCase { 42 43 final String text = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n48\n49\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59\n60\n61\n62\n63\n64\n65\n66\n67\n68\n69\n70\n71\n72\n73\n74\n75\n76\n77\n78\n79\n80\n81\n82\n83\n84\n85\n86\n87\n88\n89\n90\n91\n92\n93\n94\n95\n96\n97\n98\n99\n100\n101\n102\n103\n104\n105\n106\n107\n108\n109\n110\n111\n112\n113\n114\n115\n116\n117\n118\n119\n120\n121\n122\n123\n124\n125\n126\n127\n128\n129\n130\n131\n132\n133\n134\n135\n136\n137\n138\n139\n140\n141\n142\n143\n144\n145\n146\n147\n148\n149\n150\n151\n152\n153\n154\n155\n156\n157\n158\n159\n160\n161\n162\n163\n164\n165\n166\n167\n168\n169\n170\n171\n172\n173\n174\n175\n176\n177\n178\n179\n180\n181\n182\n183\n184\n185\n186\n187\n188\n189\n190\n191\n192\n193\n194\n195\n196\n197\n198\n199\n200\n201\n202\n203\n204\n205\n206\n207\n208\n209\n210\n211\n212\n213\n214\n215\n216\n217\n218\n219\n220\n221\n222\n223\n224\n225\n226\n227\n228\n229\n230\n231\n232\n233\n234\n235\n236\n237\n238\n239\n240\n241\n242\n243\n244\n245\n246\n247\n248\n249\n250\n251\n252\n253\n254\n255\n256\n257\n258\n259\n260\n261\n262\n263\n264\n265\n266\n267\n268\n269\n270\n271\n272\n273\n274\n275\n276\n277\n278\n279\n280\n281\n282\n283\n284\n285\n286\n287\n288\n289\n290\n291\n292\n293\n294\n295\n296\n297\n298\n299\n300\n301\n302\n303\n304\n305\n306\n307\n308\n309\n310\n311\n312\n313\n314\n315\n316\n317\n318\n319\n320\n321\n322\n323\n324\n325\n326\n327\n328\n329\n330\n331\n332\n333\n334\n335\n336\n337\n338\n339\n340\n341\n342\n343\n344\n345\n346\n347\n348\n349\n350\n351\n352\n353\n354\n355\n356\n357\n358\n359\n360\n361\n362\n363\n364\n365\n366\n367\n368\n369\n370\n371\n372\n373\n374\n375\n376\n377\n378\n379\n380\n381\n382\n383\n384\n385\n386\n387\n388\n389\n390\n391\n392\n393\n394\n395\n396\n397\n398\n399\n400\n401\n402\n403\n404\n405\n406\n407\n408\n409\n410\n411\n412\n413\n414\n415\n416\n417\n418\n419\n420\n421\n422\n423\n424\n425\n426\n427\n428\n429\n430\n431\n432\n433\n434\n435\n436\n437\n438\n439\n440\n441\n442\n443\n444\n445\n446\n447\n448\n449\n450\n451\n452\n453\n454\n455\n456\n457\n458\n459\n460\n461\n462\n463\n464\n465\n466\n467\n468\n469\n470\n471\n472\n473\n474\n475\n476\n477\n478\n479\n480\n481\n482\n483\n484\n485\n486\n487\n488\n489\n490\n491\n492\n493\n494\n495\n496\n497\n498\n499\n500\n501"; 44 45 String dosText = "0\r\n1\r\n2"; 46 47 LineNumberInputStream lnis; 48 49 LineNumberInputStream lnis2; 50 51 /** 52 * @tests java.io.LineNumberInputStream#available() 53 */ 54 @TestTargetNew( 55 level = TestLevel.COMPLETE, 56 method = "available", 57 args = {} 58 ) 59 public void test_available() throws IOException { 60 assertEquals("Test 1: Returned incorrect number of available bytes;", 61 text.length() / 2, lnis.available()); 62 63 lnis.close(); 64 try { 65 lnis.available(); 66 fail("Test 2: IOException expected."); 67 } catch (IOException e) { 68 // Expected. 69 } 70 } 71 72 /** 73 * @tests java.io.LineNumberInputStream#getLineNumber() 74 */ 75 @TestTargetNew( 76 level = TestLevel.COMPLETE, 77 notes = "", 78 method = "getLineNumber", 79 args = {} 80 ) 81 public void test_getLineNumber() { 82 // Test for method int java.io.LineNumberInputStream.getLineNumber() 83 assertEquals("New stream returned line number other than zero", 0, lnis 84 .getLineNumber()); 85 try { 86 lnis.read(); 87 lnis.read(); 88 } catch (IOException e) { 89 fail("Exception during getLineNumber test : " + e.getMessage()); 90 } 91 assertEquals("stream returned incorrect line number after read", 1, lnis 92 .getLineNumber()); 93 lnis.setLineNumber(89); 94 assertEquals("stream returned incorrect line number after set", 89, lnis 95 .getLineNumber()); 96 } 97 98 /** 99 * @tests java.io.LineNumberInputStream#mark(int) 100 */ 101 @TestTargetNew( 102 level = TestLevel.COMPLETE, 103 notes = "", 104 method = "mark", 105 args = {int.class} 106 ) 107 public void test_markI() { 108 // Test for method void java.io.LineNumberInputStream.mark(int) 109 try { 110 lnis.mark(40); 111 lnis.skip(4); 112 lnis.reset(); 113 assertEquals("Failed to mark", 0, lnis.getLineNumber()); 114 assertEquals("Failed to mark", '0', lnis.read()); 115 } catch (IOException e) { 116 fail("Exception during mark test : " + e.getMessage()); 117 } 118 } 119 120 /** 121 * @tests java.io.LineNumberInputStream#read() 122 */ 123 @TestTargetNew( 124 level = TestLevel.COMPLETE, 125 method = "read", 126 args = {} 127 ) 128 public void test_read() throws IOException { 129 assertEquals("Test 1: Failed to read correct byte;", 130 '0', lnis.read()); 131 assertEquals("Test 2: Failed to read correct byte on dos text;", 132 '0', lnis2.read()); 133 assertEquals("Test 3: Failed to read correct byte on dos text;", 134 '\n', lnis2.read()); 135 assertEquals("Test 4: Failed to read correct byte on dos text;", 136 '1', lnis2.read()); 137 assertEquals("Test 5: Failed to read correct byte on dos text;", 138 '\n', lnis2.read()); 139 assertEquals("Test 6: Failed to read correct byte on dos text;", 140 '2', lnis2.read()); 141 142 lnis.close(); 143 try { 144 lnis.read(); 145 fail("Test 7: IOException expected."); 146 } catch (IOException e) { 147 // Expected. 148 } 149 } 150 151 /** 152 * @tests java.io.LineNumberInputStream#read(byte[], int, int) 153 */ 154 @TestTargetNew( 155 level = TestLevel.COMPLETE, 156 method = "read", 157 args = {byte[].class, int.class, int.class} 158 ) 159 public void test_read$BII() throws IOException { 160 byte[] buf = new byte[100]; 161 lnis.read(buf, 0, 100); 162 assertTrue("Test 1: Incorrect bytes read.", 163 new String(buf, 0, 100).equals(text.substring(0, 100))); 164 165 lnis.close(); 166 try { 167 lnis.read(buf, 0, 100); 168 fail("Test 2: IOException expected."); 169 } catch (IOException e) { 170 // Expected. 171 } 172 } 173 174 /** 175 * @tests java.io.LineNumberInputStream#reset() 176 */ 177 @TestTargetNew( 178 level = TestLevel.COMPLETE, 179 notes = "IOException checking missed.", 180 method = "reset", 181 args = {} 182 ) 183 public void test_reset() throws IOException { 184 lnis.mark(40); 185 lnis.skip(4); 186 lnis.reset(); 187 assertEquals("Test 1: Failed to reset", 0, lnis.getLineNumber()); 188 assertEquals("Test 2: Failed to reset", '0', lnis.read()); 189 lnis.reset(); 190 191 try { 192 lnis.mark(5); 193 lnis.skip(100); 194 lnis.reset(); 195 fail("Test 3: Failed to invalidate mark."); 196 } catch (IOException e) { 197 // Correct; mark has been invalidated. 198 } 199 200 lnis.mark(5); 201 lnis.close(); 202 try { 203 lnis.reset(); 204 fail("Test 4: IOException expected."); 205 } catch (IOException e) { 206 // Expected. 207 } 208 } 209 210 /** 211 * @tests java.io.LineNumberInputStream#setLineNumber(int) 212 */ 213 @TestTargetNew( 214 level = TestLevel.COMPLETE, 215 notes = "", 216 method = "setLineNumber", 217 args = {int.class} 218 ) 219 public void test_setLineNumberI() { 220 lnis.setLineNumber(42); 221 assertEquals("Test 1: Failed to set line number;", 222 42, lnis.getLineNumber()); 223 // Repeat the test with a different number to make sure that the 224 // line number is really set. 225 lnis.setLineNumber(89); 226 assertEquals("Test 2: Failed to set line number;", 227 89, lnis.getLineNumber()); 228 } 229 230 /** 231 * @tests java.io.LineNumberInputStream#skip(long) 232 */ 233 @TestTargetNew( 234 level = TestLevel.COMPLETE, 235 method = "skip", 236 args = {long.class} 237 ) 238 public void test_skipJ() throws IOException { 239 long skipped = lnis.skip(4); 240 assertEquals("Test 1: Incorrect number of characters skipped;", 241 4, skipped); 242 assertEquals("Test 2: Skip failed to increment line number;", 243 2, lnis.getLineNumber()); 244 245 lnis.close(); 246 try { 247 lnis.skip(4); 248 fail("Test 3: IOException expected."); 249 } catch (IOException e) { 250 // Expected. 251 } 252 } 253 254 protected void setUp() { 255 /* 256 * In order for IOException to be thrown in reset(),the inputStream to 257 * the constructor cannot be a byteArrayInputstream because the reset() 258 * in byteArrayInputStream does not throw IOException. When 259 * BufferedInputStream is used, the size of the buffer must be smaller 260 * than the readlimit in mark inorder for IOException to be thrown 261 */ 262 BufferedInputStream buftemp = new BufferedInputStream( 263 new ByteArrayInputStream(text.getBytes()), 4); 264 lnis = new LineNumberInputStream(buftemp); 265 lnis2 = new LineNumberInputStream(new ByteArrayInputStream(dosText 266 .getBytes())); 267 } 268 269 /** 270 * Tears down the fixture, for example, close a network connection. This 271 * method is called after a test is executed. 272 */ 273 protected void tearDown() { 274 } 275} 276