Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2009 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Portions Copyrighted 2009 Sun Microsystems, Inc. |
38 |
*/ |
39 |
|
40 |
package org.openide.nodes; |
41 |
|
42 |
import java.util.HashMap; |
43 |
import javax.swing.Action; |
44 |
import org.junit.After; |
45 |
import org.junit.AfterClass; |
46 |
import org.junit.Before; |
47 |
import org.junit.BeforeClass; |
48 |
import org.junit.Test; |
49 |
import org.openide.util.HelpCtx; |
50 |
import org.openide.util.actions.CallbackSystemAction; |
51 |
import org.openide.util.actions.SystemAction; |
52 |
import static org.junit.Assert.*; |
53 |
|
54 |
/** |
55 |
* |
56 |
* @author Jaroslav Tulach <jtulach@netbeans.org> |
57 |
*/ |
58 |
public class LazyNodeTest { |
59 |
|
60 |
public LazyNodeTest() { |
61 |
} |
62 |
|
63 |
@BeforeClass |
64 |
public static void setUpClass() throws Exception { |
65 |
} |
66 |
|
67 |
@AfterClass |
68 |
public static void tearDownClass() throws Exception { |
69 |
} |
70 |
|
71 |
@Before |
72 |
public void setUp() { |
73 |
} |
74 |
|
75 |
@After |
76 |
public void tearDown() { |
77 |
} |
78 |
|
79 |
@Test |
80 |
public void testCreateOriginalAfterNodeExpansion() { |
81 |
doCreateOriginal(true); |
82 |
} |
83 |
|
84 |
@Test |
85 |
public void testCreateOriginalAfterGetActions() { |
86 |
doCreateOriginal(false); |
87 |
} |
88 |
|
89 |
private void doCreateOriginal(boolean askForChildren) { |
90 |
AbstractNode realNode = new AbstractNode(new Children.Array()) { |
91 |
@Override |
92 |
public Action[] getActions(boolean context) { |
93 |
return getActions(); |
94 |
} |
95 |
|
96 |
@SuppressWarnings("deprecation") |
97 |
@Override |
98 |
public SystemAction[] getActions() { |
99 |
return new SystemAction[] { |
100 |
A1.get(A1.class), |
101 |
A2.get(A2.class), |
102 |
A3.get(A3.class) |
103 |
}; |
104 |
} |
105 |
}; |
106 |
realNode.setName("RealNode"); |
107 |
realNode.setDisplayName("Real Node"); |
108 |
realNode.setShortDescription("Real Node for Test"); |
109 |
realNode.getChildren().add(new Node[] { |
110 |
new AbstractNode(Children.LEAF), |
111 |
new AbstractNode(Children.LEAF), |
112 |
new AbstractNode(Children.LEAF), |
113 |
}); |
114 |
|
115 |
CntHashMap chm = new CntHashMap("original"); |
116 |
chm.put("name", "ANode"); |
117 |
chm.put("displayName", "A Node"); |
118 |
chm.put("shortDescription", "A Node for Test"); |
119 |
chm.put("iconResource", "org/openide/nodes/beans.gif"); |
120 |
chm.put("original", realNode); |
121 |
|
122 |
Node instance = NodeOp.factory(chm); |
123 |
assertEquals("ANode", instance.getName()); |
124 |
assertEquals("A Node", instance.getDisplayName()); |
125 |
assertEquals("A Node for Test", instance.getShortDescription()); |
126 |
assertEquals("No real node queried yet", 0, chm.cnt); |
127 |
|
128 |
if (askForChildren) { |
129 |
Node[] arr = instance.getChildren().getNodes(true); |
130 |
assertEquals("Three children", 3, arr.length); |
131 |
} else { |
132 |
Action[] arr = instance.getActions(true); |
133 |
assertEquals("Three actions", 3, arr.length); |
134 |
} |
135 |
|
136 |
assertEquals("Real node queried now", 1, chm.cnt); |
137 |
|
138 |
assertEquals("RealNode", instance.getName()); |
139 |
assertEquals("Real Node", instance.getDisplayName()); |
140 |
assertEquals("Real Node for Test", instance.getShortDescription()); |
141 |
} |
142 |
|
143 |
private static class CntHashMap extends HashMap<Object,Object> { |
144 |
private final Object keyToWatch; |
145 |
int cnt; |
146 |
|
147 |
public CntHashMap(Object keyToWatch) { |
148 |
this.keyToWatch = keyToWatch; |
149 |
} |
150 |
|
151 |
@Override |
152 |
public Object get(Object key) { |
153 |
if (keyToWatch.equals(key)) { |
154 |
cnt++; |
155 |
} |
156 |
return super.get(key); |
157 |
} |
158 |
|
159 |
|
160 |
} |
161 |
|
162 |
public static class A1 extends CallbackSystemAction { |
163 |
@Override |
164 |
public String getName() { |
165 |
return "A1"; |
166 |
} |
167 |
|
168 |
@Override |
169 |
public HelpCtx getHelpCtx() { |
170 |
throw new UnsupportedOperationException("Not supported yet."); |
171 |
} |
172 |
|
173 |
} |
174 |
public static final class A2 extends A1 { |
175 |
@Override |
176 |
public String getName() { |
177 |
return "A2"; |
178 |
} |
179 |
} |
180 |
public static final class A3 extends A1 { |
181 |
@Override |
182 |
public String getName() { |
183 |
return "A3"; |
184 |
} |
185 |
} |
186 |
} |