Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / test / java / io / BufferedInputStreamTest.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package gov.nasa.jpf.test.java.io;
19
20 import gov.nasa.jpf.util.test.TestJPF;
21
22 import java.io.BufferedInputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30
31 /**
32  * raw test for java.io.BufferedInputStream
33  */
34 public class BufferedInputStreamTest extends TestJPF {
35
36   @Before
37   public void setUp() {
38     System.out.println("setUp() creating test file");
39     createTestFile();
40   }
41
42   @After
43   public void tearDown() {
44     System.out.println("setUp() deleting test file");
45     deleteTestFile();
46   }
47
48   public static void createTestFile() {
49     try {
50       FileOutputStream fo = new FileOutputStream(testFile);
51       fo.write(TEST_DATA);
52       fo.close();
53     } catch (Throwable t) {
54       throw new RuntimeException("failed to create test file", t);
55     }
56   }
57
58   public static void deleteTestFile() {
59     if (testFile.exists()) {
60       testFile.delete();
61     }
62   }
63
64   //--- the tests
65   static File testFile = new File("__test__");
66   static final byte[] TEST_DATA = {42, 42, 42};
67
68   @Test
69   public void testSimpleRead() {
70     if (verifyNoPropertyViolation()) {
71       try {
72         FileInputStream fis = new FileInputStream(testFile);
73         BufferedInputStream bis = new BufferedInputStream(fis);
74         int n = bis.available();
75
76         assert n == TEST_DATA.length : "wrong available count: " + n;
77
78         for (int i = 0; i < n; i++) {
79           int d = bis.read();
80           System.out.print(d);
81           System.out.print(',');
82           assert d == TEST_DATA[i] : "wrong read data";
83         }
84         System.out.println();
85
86         bis.close();
87
88       } catch (Throwable t) {
89         assert false : "BufferedInputStream test failed: " + t;
90       }
91     }
92   }
93 }