Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / test / mc / data / ObjectStreamTest.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.mc.data;
19
20 import gov.nasa.jpf.util.test.TestJPF;
21 import gov.nasa.jpf.vm.Verify;
22
23 import java.io.File;
24 import java.io.Serializable;
25 import java.util.ArrayList;
26
27 import org.junit.After;
28 import org.junit.Test;
29
30 /**
31  *
32  * @author Ivan Mushketik
33  */
34 public class ObjectStreamTest extends TestJPF {
35   static String osFileName = "file";
36
37   @After
38   public void deleteFile(){
39     File osFile = new File(osFileName);
40
41     if (osFile.exists()) {
42       osFile.delete();
43     }
44   }
45
46
47   @Test
48   public void testWriteReadInteger() {
49     if (!isJPFRun()) {
50       Verify.writeObjectToFile(new Integer(123), osFileName);
51     }
52
53     if (verifyNoPropertyViolation()) {
54       Integer i = Verify.readObjectFromFile(Integer.class, osFileName);
55
56       assert i == 123;
57     }
58   }
59
60   @Test
61   public void testWriteReadString() {
62     if (!isJPFRun()) {
63       Verify.writeObjectToFile(new String("hello"), osFileName);
64     }
65
66     if (verifyNoPropertyViolation()) {
67       String s = Verify.readObjectFromFile(String.class, osFileName);
68       assert s.equals("hello");
69     }
70   }
71   
72   static class Sup implements Serializable {
73     int s;
74   }
75   
76   static class Inherited extends Sup{
77     int i;
78   }
79   
80   @Test
81   public void testWriteReadInheritedClass() {
82     if (!isJPFRun()) {
83       Inherited inh = new Inherited();
84       inh.s = 1;
85       inh.i = 2;
86
87       Verify.writeObjectToFile(inh, osFileName);
88     }
89
90     if (verifyNoPropertyViolation("+jpf-core.native_classpath+=;${jpf-core}/build/tests")) {
91       Inherited inh = Verify.readObjectFromFile(Inherited.class, osFileName);
92
93       assert inh.s == 1;
94       assert inh.i == 2;
95     }
96
97   }
98
99   static class WithTransient implements Serializable {
100     int i;
101     transient int t;
102   }
103
104   @Test
105   public void testWriteReadTransientField() {
106     if (!isJPFRun()) {
107       WithTransient wt = new WithTransient();
108       wt.i = 10;
109       wt.t = 10;
110       Verify.writeObjectToFile(wt, osFileName);
111     }
112
113     if (verifyNoPropertyViolation("+jpf-core.native_classpath+=;${jpf-core}/build/tests")) {
114       WithTransient wt = Verify.readObjectFromFile(WithTransient.class, osFileName);
115
116       assert wt.i == 10;
117       // t is transient
118       assert wt.t == 0;
119     }
120   }
121
122   class SerializableArrayList<T> extends ArrayList<T> implements Serializable {}
123
124   @Test
125   public void testWriteReadArrayList() {
126     if (!isJPFRun()) {
127       ArrayList<Integer> al = new ArrayList<Integer>();
128       al.add(1);
129       al.add(2);
130       al.add(3);
131       Verify.writeObjectToFile(al, osFileName);
132     }
133
134     if (verifyNoPropertyViolation()) {
135       ArrayList al = Verify.readObjectFromFile(ArrayList.class, osFileName);
136
137       assert al.size() == 3;
138       assert al.get(0).equals(1);
139       assert al.get(1).equals(2);
140       assert al.get(2).equals(3);
141     }
142   }
143
144   static class MultiDimArr implements Serializable {
145     int arr [][];
146   }
147
148   @Test
149   public void tsetWriteReadObjectWithMultiDimArray() {
150     if (!isJPFRun()) {
151       MultiDimArr mda = new MultiDimArr();
152       mda.arr = new int[2][];
153       mda.arr[0] = new int[3];
154       mda.arr[1] = new int[3];
155
156       mda.arr[0][0] = 1;
157       mda.arr[0][1] = 2;
158       mda.arr[0][2] = 3;
159
160       mda.arr[1][0] = 4;
161       mda.arr[1][1] = 5;
162       mda.arr[1][2] = 6;
163
164       Verify.writeObjectToFile(mda, osFileName);
165     }
166
167     if (verifyNoPropertyViolation("+jpf-core.native_classpath+=;${jpf-core}/build/tests")) {
168       MultiDimArr mda = Verify.readObjectFromFile(MultiDimArr.class, osFileName);
169
170       assert mda.arr[0][0] == 1;
171       assert mda.arr[0][1] == 2;
172       assert mda.arr[0][2] == 3;
173
174       assert mda.arr[1][0] == 4;
175       assert mda.arr[1][1] == 5;
176       assert mda.arr[1][2] == 6;
177     }
178   }
179
180   static class Inner implements Serializable {
181     int i;
182   }
183
184   static class Outer implements Serializable {
185     Inner inner;
186     int o;
187   }
188
189
190   @Test
191   public void testReadWriteObjectWithReference() {
192     if (!isJPFRun()) {
193       Outer out = new Outer();
194       out.o = 1;
195       out.inner = new Inner();
196       out.inner.i = 2;
197
198       Verify.writeObjectToFile(out, osFileName);
199     }
200
201     if (verifyNoPropertyViolation("+jpf-core.native_classpath+=;${jpf-core}/build/tests")) {
202       Outer out = Verify.readObjectFromFile(Outer.class, osFileName);
203
204       assert out.o == 1;
205       assert out.inner.i == 2;
206     }
207   }
208 }