Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / vm / multiProcess / TypeSeparationTest.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.vm.multiProcess;
20
21 import java.lang.annotation.Inherited;
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.RetentionPolicy;
24 import java.util.List;
25
26 import gov.nasa.jpf.util.test.TestMultiProcessJPF;
27 import gov.nasa.jpf.vm.ClassInfo;
28 import gov.nasa.jpf.vm.ClassLoaderInfo;
29
30 import org.junit.Test;
31
32 /**
33  * @author Nastaran Shafiei <nastaran.shafiei@gmail.com>
34  */
35 public class TypeSeparationTest extends TestMultiProcessJPF {
36
37   private static int counter = 0;
38
39   private static void incCounter() {
40     counter++;
41   }
42
43   // To make sure that our multiProcess VM keeps the static attributes separated
44   // This also checks for type safe clone for the InvokeStatic instruction.
45   @Test
46   public void staticCounterTest() {
47     // Note that this code is executed 4 times. Since every time this is executed its
48     // state is restored, the value of counter should be always 1
49     if (mpVerifyNoPropertyViolation(2)) {
50       int id = getProcessId();
51       switch(id) {
52       case 0:
53         incCounter();
54         break;
55       case 1:
56         incCounter();
57         break;
58       default:
59         fail("invalid process number!");
60       }
61
62       assertEquals(counter,1);
63     }
64   }
65
66   @Retention(RetentionPolicy.RUNTIME)
67   @Inherited
68   public @interface A0 {
69   }
70
71   private native void keepAnnotationClass(Class annCls, int prcId);
72
73   @Test
74   public void annotationsTest () {
75     if(!isJPFRun()) {
76       JPF_gov_nasa_jpf_vm_multiProcess_TypeSeparationTest.resetPrcIds();
77     }
78
79     if (mpVerifyNoPropertyViolation(2)) {
80       int prcId = getProcessId();
81       keepAnnotationClass(A0.class, prcId);
82     }
83
84     if(!isJPFRun()) {
85       List<ClassInfo> annClassList = JPF_gov_nasa_jpf_vm_multiProcess_TypeSeparationTest.getAnnotationClasses();
86       assertEquals(annClassList.size(), 2);
87       assertTrue(annClassList.get(0)!=annClassList.get(1));
88     }
89   }
90
91
92   private native void keepClassLoader(ClassLoader thd, int prcId);
93
94   // to make sure that each process accesses the classes loaded by the right
95   // system class loader
96   @Test
97   public void systemClassLoaderTest() {
98     if(!isJPFRun()) {
99       JPF_gov_nasa_jpf_vm_multiProcess_TypeSeparationTest.resetPrcIds();
100     }
101
102     if (mpVerifyNoPropertyViolation(2)) {
103       ClassLoader cl = Object.class.getClassLoader();
104
105       // in our implementation this goes through the class hierarchy of the 
106       // current thread and it returns the class loader of the Thread class
107       ClassLoader sysLoader = ClassLoader.getSystemClassLoader();
108       assertEquals(cl, sysLoader);
109
110       int prcId = getProcessId();
111       keepClassLoader(cl, prcId);
112     }
113
114     if(!isJPFRun()) {
115       List<ClassLoaderInfo> classLoaders = JPF_gov_nasa_jpf_vm_multiProcess_TypeSeparationTest.getClassLoaders();
116       assertEquals(classLoaders.size(), 2);
117       assertTrue(classLoaders.get(0)!=classLoaders.get(1));
118     }
119   }
120 }