Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2013 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2013 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.api.debugger.jpda; |
43 |
|
44 |
import java.awt.Color; |
45 |
import java.awt.Point; |
46 |
import java.io.File; |
47 |
import java.util.Arrays; |
48 |
import java.util.Date; |
49 |
import java.util.HashMap; |
50 |
import java.util.Map; |
51 |
import junit.framework.Test; |
52 |
import org.netbeans.api.debugger.DebuggerManager; |
53 |
import org.netbeans.junit.NbTestCase; |
54 |
|
55 |
/** |
56 |
* Tests {@link Variable#createMirrorObject()} and {@link MutableVariable#setFromMirrorObject(java.lang.Object)}. |
57 |
* |
58 |
* @author Martin Entlicher |
59 |
*/ |
60 |
public class MirrorValuesTest extends NbTestCase { |
61 |
|
62 |
private static final String CLASS_NAME = |
63 |
"org.netbeans.api.debugger.jpda.testapps.MirrorValuesApp"; |
64 |
|
65 |
private JPDASupport support; |
66 |
private DebuggerManager dm = DebuggerManager.getDebuggerManager (); |
67 |
|
68 |
public MirrorValuesTest(String s) { |
69 |
super(s); |
70 |
} |
71 |
|
72 |
public static Test suite() { |
73 |
return JPDASupport.createTestSuite(MirrorValuesTest.class); |
74 |
} |
75 |
|
76 |
public void testMirrors() throws Exception { |
77 |
try { |
78 |
Utils.BreakPositions bp = Utils.getBreakPositions(System.getProperty ("test.dir.src") + |
79 |
"org/netbeans/api/debugger/jpda/testapps/MirrorValuesApp.java"); |
80 |
LineBreakpoint lb = bp.getLineBreakpoints().get(0); |
81 |
dm.addBreakpoint (lb); |
82 |
|
83 |
support = JPDASupport.attach (CLASS_NAME); |
84 |
|
85 |
support.waitState (JPDADebugger.STATE_STOPPED); // breakpoint hit |
86 |
|
87 |
CallStackFrame sf = support.getDebugger ().getCurrentCallStackFrame (); |
88 |
LocalVariable [] vars = sf.getLocalVariables (); |
89 |
Map<String, Variable> variablesByName = getVariablesByName(vars); |
90 |
Map<String, Object> mirrorsByName = getMirrorsByName(); |
91 |
|
92 |
Variable v; |
93 |
Object m; |
94 |
|
95 |
// Test of createMirrorObject(): |
96 |
for (String name : mirrorsByName.keySet()) { |
97 |
v = variablesByName.get(name); |
98 |
m = v.createMirrorObject(); |
99 |
assertNotNull(name, m); |
100 |
Object mm = mirrorsByName.get(name); |
101 |
if (mm.getClass().isArray()) { |
102 |
assertTrue(name+" is array", m.getClass().isArray()); |
103 |
assertTrue(name+" array "+arrayToString(mm)+ |
104 |
" does not equal to "+arrayToString(m), |
105 |
compareArrays(mm, m)); |
106 |
} else { |
107 |
assertEquals(name, mm, m); |
108 |
} |
109 |
} |
110 |
|
111 |
// Test of setFromMirrorObject(): |
112 |
v = variablesByName.get("boo"); |
113 |
assertEquals("boo", "true", v.getValue()); |
114 |
((MutableVariable) v).setFromMirrorObject(Boolean.FALSE); |
115 |
assertEquals("boo", "false", v.getValue()); |
116 |
|
117 |
v = variablesByName.get("i"); |
118 |
assertEquals("i", "10000", v.getValue()); |
119 |
((MutableVariable) v).setFromMirrorObject(12345); |
120 |
assertEquals("i", "12345", v.getValue()); |
121 |
|
122 |
v = variablesByName.get("color"); |
123 |
assertEquals("color", "java.awt.Color[r=255,g=0,b=0]", ((ObjectVariable) v).getToStringValue()); |
124 |
((MutableVariable) v).setFromMirrorObject(Color.GREEN); |
125 |
assertEquals("color", "java.awt.Color[r=0,g=255,b=0]", ((ObjectVariable) v).getToStringValue()); |
126 |
|
127 |
} finally { |
128 |
support.doFinish (); |
129 |
} |
130 |
|
131 |
} |
132 |
|
133 |
private static boolean compareArrays(Object arr1, Object arr2) { |
134 |
if (arr1 instanceof Object[]) { |
135 |
return Arrays.deepEquals((Object[]) arr1, (Object[]) arr2); |
136 |
} else if (arr1 instanceof int[]) { |
137 |
return Arrays.equals((int[]) arr1, (int[]) arr2); |
138 |
} else { |
139 |
throw new IllegalStateException(arr1+", "+arr2); |
140 |
} |
141 |
} |
142 |
|
143 |
private static String arrayToString(Object a) { |
144 |
if (a instanceof Object[]) { |
145 |
return Arrays.deepToString((Object[]) a); |
146 |
} else if (a instanceof int[]) { |
147 |
return Arrays.toString((int[]) a); |
148 |
} else { |
149 |
throw new IllegalStateException(a.toString()); |
150 |
} |
151 |
} |
152 |
|
153 |
private static Map<String, Variable> getVariablesByName(LocalVariable[] vars) { |
154 |
Map<String, Variable> map = new HashMap<String, Variable>(); |
155 |
for (LocalVariable lv : vars) { |
156 |
assertTrue("Not mutable", lv instanceof MutableVariable); |
157 |
map.put(lv.getName(), lv); |
158 |
} |
159 |
return map; |
160 |
} |
161 |
|
162 |
private static Map<String, Object> getMirrorsByName() { |
163 |
String[] names = { "boo", "b", "s", "i", "l", |
164 |
"f", "d", |
165 |
"iarr", |
166 |
"darr", |
167 |
"str", "date", "color", |
168 |
"point", "file" }; |
169 |
Object[] mirrors = { true, (byte) 5, (short) 512, 10000, Long.MAX_VALUE, |
170 |
12.12f, 1e150, |
171 |
new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, |
172 |
new double[][] { { 0.1, 0.2, 0.3 }, { 1.1, 1.2, 1.3 }, { 2.1, 2.2, 2.3 } }, |
173 |
"A String", new Date(1000000000l), Color.RED, |
174 |
new Point(10, 10), new File("/tmp/Foo.txt") }; |
175 |
|
176 |
Map<String, Object> map = new HashMap<String, Object>(); |
177 |
assertEquals(names.length, mirrors.length); |
178 |
for (int i = 0; i < names.length; i++) { |
179 |
map.put(names[i], mirrors[i]); |
180 |
} |
181 |
return map; |
182 |
} |
183 |
} |