Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / test / java / io / FileIOTest.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
19 package gov.nasa.jpf.test.java.io;
20
21 import gov.nasa.jpf.util.test.TestJPF;
22
23 import java.io.BufferedReader;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
30 import java.io.OutputStreamWriter;
31 import java.io.PrintWriter;
32 import java.util.ArrayList;
33 import java.util.Random;
34
35 import org.junit.Test;
36
37 /**
38  * raw test for Writers, Readers, FileOutputStream and FileInputStream
39  */
40 public class FileIOTest extends TestJPF {
41
42   public static final String fname = "_test_";
43
44   @Test
45   public void testRoundtrip() throws IOException, FileNotFoundException {
46     if (verifyNoPropertyViolation()) {
47       Random r = new Random(42);
48       File file = new File(fname);
49       String[] lines = {"one", "two", "three", "four", "five"};
50
51       //--- write part
52       System.out.println("##---- writing: " + file.getName());
53       FileOutputStream os = new FileOutputStream(file);
54       OutputStreamWriter ow = new OutputStreamWriter(os);
55       PrintWriter pw = new PrintWriter(ow);
56       int a, b;
57
58       for (int i = 0; i < lines.length; i++) {
59         pw.println(lines[i]);
60         if (i == 2) {
61           // add a CG here
62           a = r.nextInt(1);
63           System.out.println("## write got here: " + a);
64         }
65       }
66
67       pw.close();
68       os.close(); // without this, Windows/Cygwin doesn't delete the file
69
70       System.out.println("##---- checking file system attributes");
71
72       assert file.exists() : "File.exits() failed on " + fname;
73
74       assert file.isFile() : "File.isFile() failed on " + fname;
75
76       assert !file.isDirectory() : "!File.isDirectory() failed on " + fname;
77
78       assert isInCurrentDirList(fname) : "dir list test failed on " + fname;
79
80
81       //--- read part
82       System.out.println("##---- reading: " + file.getName());
83       ArrayList<String> contents = new ArrayList<String>();
84       String line;
85       FileInputStream is = new FileInputStream(file);
86       InputStreamReader ir = new InputStreamReader(is);
87       BufferedReader br = new BufferedReader(ir);
88
89       for (int i = 0; (line = br.readLine()) != null; i++) {
90         if (i == 2) {
91           b = r.nextInt(1);
92           System.out.println("## read got here: " + b);
93         }
94         contents.add(line);
95       }
96
97       br.close();
98       is.close(); // without this, Windows/Cygwin doesn't delete the file
99
100       //--- check part
101       System.out.println("##---- comparing");
102       assert lines.length == contents.size() : "file length differs: " + lines.length + " / " + contents.size();
103
104       for (int i = 0; i < lines.length; i++) {
105         assert lines[i].equals(contents.get(i)) : "line " + i + " differs, expected: \"" + lines[i] + "\", got: \"" + contents.get(i) + "\"";
106       }
107
108
109       if (file.delete()) {
110         assert !file.exists() : "File.delete() failed (supposedly deleted but file exists) on " + fname;
111       } else {
112         assert false : "File.delete() failed to delete file (can happen on Windows/Cygwin)";
113       }
114
115       System.out.println("##---- done");
116     }
117   }
118
119   private boolean isInCurrentDirList(String fn) {
120     for (String s : new File(".").list()) {
121       if (fn.equals(s)) {
122         return true;
123       }
124     }
125
126     return false;
127   }
128 }