2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
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
10 * http://www.apache.org/licenses/LICENSE-2.0.
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.
19 package gov.nasa.jpf.test.java.io;
21 import gov.nasa.jpf.util.test.TestJPF;
23 import java.io.BufferedReader;
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;
35 import org.junit.Test;
38 * raw test for Writers, Readers, FileOutputStream and FileInputStream
40 public class FileIOTest extends TestJPF {
42 public static final String fname = "_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"};
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);
58 for (int i = 0; i < lines.length; i++) {
63 System.out.println("## write got here: " + a);
68 os.close(); // without this, Windows/Cygwin doesn't delete the file
70 System.out.println("##---- checking file system attributes");
72 assert file.exists() : "File.exits() failed on " + fname;
74 assert file.isFile() : "File.isFile() failed on " + fname;
76 assert !file.isDirectory() : "!File.isDirectory() failed on " + fname;
78 assert isInCurrentDirList(fname) : "dir list test failed on " + fname;
82 System.out.println("##---- reading: " + file.getName());
83 ArrayList<String> contents = new ArrayList<String>();
85 FileInputStream is = new FileInputStream(file);
86 InputStreamReader ir = new InputStreamReader(is);
87 BufferedReader br = new BufferedReader(ir);
89 for (int i = 0; (line = br.readLine()) != null; i++) {
92 System.out.println("## read got here: " + b);
98 is.close(); // without this, Windows/Cygwin doesn't delete the file
101 System.out.println("##---- comparing");
102 assert lines.length == contents.size() : "file length differs: " + lines.length + " / " + contents.size();
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) + "\"";
110 assert !file.exists() : "File.delete() failed (supposedly deleted but file exists) on " + fname;
112 assert false : "File.delete() failed to delete file (can happen on Windows/Cygwin)";
115 System.out.println("##---- done");
119 private boolean isInCurrentDirList(String fn) {
120 for (String s : new File(".").list()) {