KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > TestNativePeer


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.jvm;
20
21 /**
22  * model class of MJI test
23  */

24 public class TestNativePeer {
25   static int sdata;
26
27   static {
28     // only here to be intercepted
29
sdata = 0; // dummy insn required for the Eclipse compiler (skips empty methods)
30
}
31
32   int idata;
33
34   TestNativePeer () {
35   }
36
37   TestNativePeer (int data) {
38     // only here to be intercepted
39
}
40
41   public static void main (String JavaDoc[] args) {
42     TestNativePeer t = new TestNativePeer();
43
44     if (args.length > 0) {
45       // just run the specified tests
46
for (int i = 0; i < args.length; i++) {
47         String JavaDoc func = args[i];
48
49         // note that we don't use reflection here because this would
50
// blow up execution/test scope under JPF
51
if ("testClInit".equals(func)) {
52           t.testClInit();
53         } else if ("testInit".equals(func)) {
54           t.testInit();
55         } else if ("testNativeInstanceMethod".equals(func)) {
56           t.testNativeInstanceMethod();
57         } else if ("testNativeStaticMethod".equals(func)) {
58           t.testNativeStaticMethod();
59         } else if ("testNativeCreateStringArray".equals(func)) {
60           t.testNativeCreateStringArray();
61         } else if ("testNativeCreateIntArray".equals(func)) {
62           t.testNativeCreateIntArray();
63         } else if ("testNativeCreate2DimIntArray".equals(func)) {
64           t.testNativeCreate2DimIntArray();
65         } else if ("testNativeException".equals(func)) {
66           t.testNativeException();
67         } else if ("testNativeCrash".equals(func)) {
68           t.testNativeCrash();
69         } else {
70           throw new IllegalArgumentException JavaDoc("unknown test function");
71         }
72       }
73     } else {
74       // that's mainly for our standalone test verification
75
t.testClInit();
76       t.testInit();
77       t.testNativeInstanceMethod();
78       t.testNativeStaticMethod();
79       t.testNativeCreateStringArray();
80       t.testNativeCreateIntArray();
81       t.testNativeCreate2DimIntArray();
82       t.testNativeException();
83       t.testNativeCrash();
84     }
85   }
86
87   public void testClInit () {
88     if (sdata != 42) {
89       throw new RuntimeException JavaDoc("native '<clinit>' failed");
90     }
91   }
92
93   public void testInit () {
94     TestNativePeer t = new TestNativePeer(42);
95
96     if (t.idata != 42) {
97       throw new RuntimeException JavaDoc("native '<init>' failed");
98     }
99   }
100
101   public void testNativeCreate2DimIntArray () {
102     int[][] a = nativeCreate2DimIntArray(2, 3);
103
104     if (a == null) {
105       throw new RuntimeException JavaDoc("native int[][] creation failed: null");
106     }
107
108     if (!a.getClass().isArray()) {
109       throw new RuntimeException JavaDoc("native int[][] creation failed: not an array");
110     }
111
112     if (!a.getClass().getComponentType().getName().equals("[I")) {
113       throw new RuntimeException JavaDoc(
114             "native int[][] creation failed: wrong component type");
115     }
116
117     if (!(a[1][1] == 42)) {
118       throw new RuntimeException JavaDoc("native int[][] element init failed");
119     }
120   }
121
122   public void testNativeCreateIntArray () {
123     int[] a = nativeCreateIntArray(3);
124
125     if (a == null) {
126       throw new RuntimeException JavaDoc("native int array creation failed: null");
127     }
128
129     if (!a.getClass().isArray()) {
130       throw new RuntimeException JavaDoc(
131             "native int array creation failed: not an array");
132     }
133
134     if (a.getClass().getComponentType() != int.class) {
135       throw new RuntimeException JavaDoc(
136             "native int array creation failed: wrong component type");
137     }
138
139     if (!(a[1] == 1)) {
140       throw new RuntimeException JavaDoc("native int array element init failed");
141     }
142   }
143
144   public void testNativeCreateStringArray () {
145     String JavaDoc[] a = nativeCreateStringArray(3);
146
147     if (a == null) {
148       throw new RuntimeException JavaDoc("native String array creation failed: null");
149     }
150
151     if (!a.getClass().isArray()) {
152       throw new RuntimeException JavaDoc(
153             "native String array creation failed: not an array");
154     }
155
156     if (a.getClass().getComponentType() != String JavaDoc.class) {
157       throw new RuntimeException JavaDoc(
158             "native String array creation failed: wrong component type");
159     }
160
161     if (!"one".equals(a[1])) {
162       throw new RuntimeException JavaDoc("native String array element init failed");
163     }
164   }
165
166   public void testNativeException () {
167     try {
168       nativeException();
169     } catch (UnsupportedOperationException JavaDoc ux) {
170       String JavaDoc details = ux.getMessage();
171
172       if ("caught me".equals(details)) {
173         ux.printStackTrace();
174         return;
175       } else {
176         throw new RuntimeException JavaDoc("wrong native exception details: " +
177                                    details);
178       }
179     } catch (Throwable JavaDoc t) {
180       throw new RuntimeException JavaDoc("wrong native exception type: " +
181                                  t.getClass());
182     }
183
184     throw new RuntimeException JavaDoc("no native exception thrown");
185   }
186   
187   public void testNativeCrash () {
188     nativeCrash();
189   }
190
191   public void testNativeInstanceMethod () {
192     int res = nativeInstanceMethod(2.0, '?', true, 40);
193
194     if (res != 42) {
195       throw new RuntimeException JavaDoc("native instance method failed");
196     }
197   }
198
199   public void testNativeStaticMethod () {
200     long res = nativeStaticMethod(40, "Blah");
201
202     if (res != 42) {
203       throw new RuntimeException JavaDoc("native instance method failed");
204     }
205   }
206
207   native int[][] nativeCreate2DimIntArray (int s1, int s2);
208
209   native int[] nativeCreateIntArray (int size);
210
211   native String JavaDoc[] nativeCreateStringArray (int size);
212
213   native void nativeException ();
214   
215   native int nativeCrash ();
216
217   native int nativeInstanceMethod (double d, char c, boolean b, int i);
218
219   native long nativeStaticMethod (long l, String JavaDoc s);
220 }
Popular Tags