Mercurial > hg > Members > kono > jpf-core
comparison src/tests/gov/nasa/jpf/test/mc/basic/NullTrackerTest.java @ 0:61d41facf527
initial v8 import (history reset)
author | Peter Mehlitz <Peter.C.Mehlitz@nasa.gov> |
---|---|
date | Fri, 23 Jan 2015 10:14:01 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:61d41facf527 |
---|---|
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.mc.basic; | |
20 | |
21 import gov.nasa.jpf.util.test.TestJPF; | |
22 import java.util.HashMap; | |
23 import org.junit.Test; | |
24 | |
25 /** | |
26 * regression test for NullTracker. | |
27 * | |
28 * Well, not really a regression test since NullTracker only prints out reports, but at least | |
29 * we can see if it has errors while running | |
30 */ | |
31 public class NullTrackerTest extends TestJPF { | |
32 | |
33 static class TestObject { | |
34 String d; | |
35 | |
36 TestObject(){ | |
37 // nothing, we forget to init d; | |
38 } | |
39 | |
40 TestObject (String d){ | |
41 this.d = d; | |
42 } | |
43 | |
44 int getDLength(){ | |
45 return d.length(); | |
46 } | |
47 | |
48 void foo(){ | |
49 // nothing | |
50 } | |
51 } | |
52 | |
53 TestObject o; | |
54 | |
55 TestObject getTestObject (){ | |
56 return null; | |
57 } | |
58 | |
59 void accessReturnedObject (){ | |
60 TestObject o = getTestObject(); | |
61 System.out.println("now accessing testObject"); | |
62 String d = o.d; // that will NPE | |
63 } | |
64 | |
65 void accessObject (TestObject o){ | |
66 System.out.println("now accessing testObject"); | |
67 String d = o.d; // that will NPE | |
68 } | |
69 | |
70 void createAndAccessObject(){ | |
71 TestObject o = getTestObject(); | |
72 accessObject(o); | |
73 } | |
74 | |
75 | |
76 @Test | |
77 public void testGetAfterIntraMethodReturn (){ | |
78 if (verifyUnhandledException("java.lang.NullPointerException", "+listener=.listener.NullTracker")){ | |
79 accessReturnedObject(); | |
80 } | |
81 } | |
82 | |
83 @Test | |
84 public void testGetAfterInterMethodReturn (){ | |
85 if (verifyUnhandledException("java.lang.NullPointerException", "+listener=.listener.NullTracker")){ | |
86 createAndAccessObject(); | |
87 } | |
88 } | |
89 | |
90 @Test | |
91 public void testGetAfterIntraPut (){ | |
92 if (verifyUnhandledException("java.lang.NullPointerException", "+listener=.listener.NullTracker")){ | |
93 o = null; // the null source | |
94 | |
95 String d = o.d; // causes the NPE | |
96 } | |
97 } | |
98 | |
99 @Test | |
100 public void testCallAfterIntraPut (){ | |
101 if (verifyUnhandledException("java.lang.NullPointerException", "+listener=.listener.NullTracker")){ | |
102 o = null; // the null source | |
103 | |
104 o.foo(); // causes the NPE | |
105 } | |
106 } | |
107 | |
108 @Test | |
109 public void testGetAfterASTORE (){ | |
110 if (verifyUnhandledException("java.lang.NullPointerException", "+listener=.listener.NullTracker")){ | |
111 TestObject myObj = null; // the null source | |
112 | |
113 myObj.foo(); // causes the NPE | |
114 } | |
115 } | |
116 | |
117 | |
118 HashMap<String,TestObject> map = new HashMap<String,TestObject>(); | |
119 | |
120 TestObject lookupTestObject (String name){ | |
121 return map.get(name); | |
122 } | |
123 | |
124 @Test | |
125 public void testHashMapGet (){ | |
126 if (verifyUnhandledException("java.lang.NullPointerException", "+listener=.listener.NullTracker")){ | |
127 TestObject o = lookupTestObject("FooBar"); | |
128 o.foo(); | |
129 } | |
130 } | |
131 | |
132 //------------------------------------------------------------------ | |
133 | |
134 TestObject createTestObject (){ | |
135 return new TestObject(); | |
136 } | |
137 | |
138 | |
139 TestObject createTestObject (String d){ | |
140 return new TestObject(d); | |
141 } | |
142 | |
143 @Test | |
144 public void testMissingCtorInit (){ | |
145 if (verifyUnhandledException("java.lang.NullPointerException", "+listener=.listener.NullTracker")){ | |
146 TestObject o = createTestObject("blah"); | |
147 int len = o.getDLength(); // that should be fine | |
148 | |
149 o = createTestObject(); | |
150 len = o.getDLength(); // that should NPE and report the default ctor as culprit | |
151 } | |
152 } | |
153 } |