001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.io;
019:
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.FileOutputStream;
023: import java.io.FilePermission;
024: import java.io.IOException;
025: import java.security.Permission;
026:
027: import junit.framework.TestCase;
028: import tests.support.Support_PlatformFile;
029:
030: public class FileInputStreamTest extends TestCase {
031:
032: public String fileName;
033:
034: private java.io.InputStream is;
035:
036: byte[] ibuf = new byte[4096];
037:
038: public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_FileInputStream\nTest_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_java_io_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n";
039:
040: /**
041: * @tests java.io.FileInputStream#FileInputStream(java.io.File)
042: */
043: public void test_ConstructorLjava_io_File() throws IOException {
044: java.io.File f = new File(fileName);
045: is = new FileInputStream(f);
046: is.close();
047: }
048:
049: /**
050: * @tests java.io.FileInputStream#FileInputStream(java.io.FileDescriptor)
051: */
052: public void test_ConstructorLjava_io_FileDescriptor()
053: throws IOException {
054: FileOutputStream fos = new FileOutputStream(fileName);
055: FileInputStream fis = new FileInputStream(fos.getFD());
056: fos.close();
057: fis.close();
058: }
059:
060: /**
061: * @tests java.io.FileInputStream#FileInputStream(java.lang.String)
062: */
063: public void test_ConstructorLjava_lang_String() throws IOException {
064: is = new FileInputStream(fileName);
065: is.close();
066: }
067:
068: /**
069: * @tests java.io.FileInputStream#available()
070: */
071: public void test_available() throws IOException {
072: try {
073: is = new FileInputStream(fileName);
074: assertTrue("Returned incorrect number of available bytes",
075: is.available() == fileString.length());
076: } finally {
077: try {
078: is.close();
079: } catch (IOException e) {
080: }
081: }
082: }
083:
084: /**
085: * @tests java.io.FileInputStream#close()
086: */
087: public void test_close() throws IOException {
088: is = new FileInputStream(fileName);
089: is.close();
090:
091: try {
092: is.read();
093: fail("Able to read from closed stream");
094: } catch (IOException e) {
095: // Expected
096: }
097: }
098:
099: /**
100: * @tests java.io.FileInputStream#getFD()
101: */
102: public void test_getFD() throws IOException {
103: FileInputStream fis = new FileInputStream(fileName);
104: assertTrue("Returned invalid fd", fis.getFD().valid());
105: fis.close();
106: assertTrue("Returned invalid fd", !fis.getFD().valid());
107: }
108:
109: /**
110: * @tests java.io.FileInputStream#read()
111: */
112: public void test_read() throws IOException {
113: is = new FileInputStream(fileName);
114: int c = is.read();
115: is.close();
116: assertTrue("read returned incorrect char", c == fileString
117: .charAt(0));
118: }
119:
120: /**
121: * @tests java.io.FileInputStream#read(byte[])
122: */
123: public void test_read$B() throws IOException {
124: byte[] buf1 = new byte[100];
125: is = new FileInputStream(fileName);
126: is.skip(3000);
127: is.read(buf1);
128: is.close();
129: assertTrue("Failed to read correct data", new String(buf1, 0,
130: buf1.length).equals(fileString.substring(3000, 3100)));
131: }
132:
133: /**
134: * @tests java.io.FileInputStream#read(byte[], int, int)
135: */
136: public void test_read$BII() throws IOException {
137: byte[] buf1 = new byte[100];
138: is = new FileInputStream(fileName);
139: is.skip(3000);
140: is.read(buf1, 0, buf1.length);
141: is.close();
142: assertTrue("Failed to read correct data", new String(buf1, 0,
143: buf1.length).equals(fileString.substring(3000, 3100)));
144:
145: // Regression test for HARMONY-285
146: File file = new File("FileInputStream.tmp");
147: file.createNewFile();
148: file.deleteOnExit();
149: FileInputStream in = new FileInputStream(file);
150: try {
151: in.read(null, 0, 0);
152: fail("Should throw NullPointerException");
153: } catch (NullPointerException e) {
154: // Expected
155: } finally {
156: in.close();
157: }
158: }
159:
160: /**
161: * @tests java.io.FileInputStream#read(byte[], int, int)
162: */
163: public void test_read_$BII_IOException() throws IOException {
164: byte[] buf = new byte[1000];
165: try {
166: is = new FileInputStream(fileName);
167: is.read(buf, -1, 0);
168: fail("should throw IndexOutOfBoundsException");
169: } catch (IndexOutOfBoundsException e) {
170: // Expected
171: } finally {
172: is.close();
173: }
174:
175: try {
176: is = new FileInputStream(fileName);
177: is.read(buf, 0, -1);
178: fail("should throw IndexOutOfBoundsException");
179: } catch (IndexOutOfBoundsException e) {
180: // Expected
181: } finally {
182: is.close();
183: }
184:
185: try {
186: is = new FileInputStream(fileName);
187: is.read(buf, -1, -1);
188: fail("should throw IndexOutOfBoundsException");
189: } catch (IndexOutOfBoundsException e) {
190: // Expected
191: } finally {
192: is.close();
193: }
194:
195: try {
196: is = new FileInputStream(fileName);
197: is.read(buf, 0, 1001);
198: fail("should throw IndexOutOfBoundsException");
199: } catch (IndexOutOfBoundsException e) {
200: // Expected
201: } finally {
202: is.close();
203: }
204:
205: try {
206: is = new FileInputStream(fileName);
207: is.read(buf, 1001, 0);
208: fail("should throw IndexOutOfBoundsException");
209: } catch (IndexOutOfBoundsException e) {
210: // Expected
211: } finally {
212: is.close();
213: }
214:
215: try {
216: is = new FileInputStream(fileName);
217: is.read(buf, 500, 501);
218: fail("should throw IndexOutOfBoundsException");
219: } catch (IndexOutOfBoundsException e) {
220: // Expected
221: } finally {
222: is.close();
223: }
224:
225: try {
226: is = new FileInputStream(fileName);
227: is.close();
228: is.read(buf, 0, 100);
229: fail("should throw IOException");
230: } catch (IOException e) {
231: // Expected
232: } finally {
233: is.close();
234: }
235:
236: try {
237: is = new FileInputStream(fileName);
238: is.close();
239: is.read(buf, 0, 0);
240: } finally {
241: is.close();
242: }
243: }
244:
245: /**
246: * @tests java.io.FileInputStream#read(byte[], int, int)
247: */
248: public void test_read_$BII_NullPointerException()
249: throws IOException {
250: byte[] buf = null;
251: try {
252: is = new FileInputStream(fileName);
253: is.read(buf, -1, 0);
254: fail("should throw NullPointerException");
255: } catch (NullPointerException e) {
256: // Expected
257: } finally {
258: is.close();
259: }
260: }
261:
262: /**
263: * @tests java.io.FileInputStream#read(byte[], int, int)
264: */
265: public void test_read_$BII_IndexOutOfBoundsException()
266: throws IOException {
267: byte[] buf = new byte[1000];
268: try {
269: is = new FileInputStream(fileName);
270: is.close();
271: is.read(buf, -1, -1);
272: fail("should throw IndexOutOfBoundsException");
273: } catch (IndexOutOfBoundsException e) {
274: // Expected
275: } finally {
276: is.close();
277: }
278: }
279:
280: /**
281: * @tests java.io.FileInputStream#skip(long)
282: */
283: public void test_skipJ() throws IOException {
284: byte[] buf1 = new byte[10];
285: is = new FileInputStream(fileName);
286: is.skip(1000);
287: is.read(buf1, 0, buf1.length);
288: is.close();
289: assertTrue("Failed to skip to correct position", new String(
290: buf1, 0, buf1.length).equals(fileString.substring(1000,
291: 1010)));
292: }
293:
294: /**
295: * @tests java.io.FileInputStream#read(byte[], int, int))
296: */
297: public void test_regressionNNN() throws IOException {
298: // Regression for HARMONY-434
299: FileInputStream fis = new FileInputStream(fileName);
300:
301: try {
302: fis.read(new byte[1], -1, 1);
303: fail("IndexOutOfBoundsException must be thrown if off <0");
304: } catch (IndexOutOfBoundsException e) {
305: // Expected
306: }
307:
308: try {
309: fis.read(new byte[1], 0, -1);
310: fail("IndexOutOfBoundsException must be thrown if len <0");
311: } catch (IndexOutOfBoundsException e) {
312: // Expected
313: }
314:
315: try {
316: fis.read(new byte[1], 0, 5);
317: fail("IndexOutOfBoundsException must be thrown if off+len > b.length");
318: } catch (IndexOutOfBoundsException e) {
319: // Expected
320: }
321:
322: try {
323: fis.read(new byte[10], Integer.MAX_VALUE, 5);
324: fail("IndexOutOfBoundsException expected");
325: } catch (IndexOutOfBoundsException e) {
326: // Expected
327: }
328:
329: try {
330: fis.read(new byte[10], 5, Integer.MAX_VALUE);
331: fail("IndexOutOfBoundsException expected");
332: } catch (IndexOutOfBoundsException e) {
333: // Expected
334: }
335: fis.close();
336: }
337:
338: /**
339: * @tests java.io.FileInputStream#FileInputStream(String)
340: */
341: public void test_Constructor_LString_WithSecurityManager()
342: throws IOException {
343: SecurityManager old = System.getSecurityManager();
344: try {
345: MockSecurityManager msm = new MockSecurityManager();
346: System.setSecurityManager(msm);
347: new FileInputStream((String) null);
348: fail("should throw SecurityException");
349: } catch (SecurityException e) {
350: // expected
351: } finally {
352: System.setSecurityManager(old);
353: }
354: }
355:
356: /**
357: * @tests java.io.FileInputStream#skip(long)
358: */
359: public void test_skipNegativeArgumentJ() throws IOException {
360: FileInputStream fis = new FileInputStream(fileName);
361: try {
362: fis.skip(-5);
363: fail("IOException must be thrown if number of bytes to skip <0");
364: } catch (IOException e) {
365: // Expected IOException
366: } finally {
367: fis.close();
368: }
369: }
370:
371: /**
372: * Sets up the fixture, for example, open a network connection. This method
373: * is called before a test is executed.
374: */
375: protected void setUp() throws IOException {
376: fileName = System.getProperty("user.dir");
377: String separator = System.getProperty("file.separator");
378: if (fileName.charAt(fileName.length() - 1) == separator
379: .charAt(0))
380: fileName = Support_PlatformFile.getNewPlatformFile(
381: fileName, "input.tst");
382: else
383: fileName = Support_PlatformFile.getNewPlatformFile(fileName
384: + separator, "input.tst");
385: java.io.OutputStream fos = new java.io.FileOutputStream(
386: fileName);
387: fos.write(fileString.getBytes());
388: fos.close();
389: }
390:
391: /**
392: * Tears down the fixture, for example, close a network connection. This
393: * method is called after a test is executed.
394: */
395: protected void tearDown() {
396: new File(fileName).delete();
397: }
398: }
399:
400: class MockSecurityManager extends SecurityManager {
401: public void checkPermission(Permission permission) {
402: if (permission instanceof FilePermission) {
403: if (permission.getActions().indexOf("read") == 0)
404: throw new SecurityException();
405: }
406: }
407:
408: public void checkRead(String file) {
409: if (null == file) {
410: file = "";
411: }
412: checkPermission(new FilePermission(file, "read"));
413: }
414: }
|