KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > looks > TestBaseValues


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.spi.looks;
21
22 import java.util.*;
23 import java.io.IOException JavaDoc;
24 import java.beans.BeanInfo JavaDoc;
25
26
27
28 import org.openide.nodes.*;
29 import org.openide.util.Lookup;
30
31 import org.netbeans.spi.looks.*;
32
33 import org.netbeans.api.nodes2looks.TestUtil;
34 import org.netbeans.junit.*;
35
36 /** This class is a test base for all test for returning right
37  * values from a look
38  */

39
40 public class TestBaseValues extends NbTestCase {
41
42     // The look to be tested
43
protected Look look;
44
45     // The node to be tested
46
protected Node node;
47
48     // If testing on Node the representedObject has to be set
49
protected SampleRepObject representedObject;
50     
51     // Array with golden items
52
protected GoldenValue[] goldenValues;
53     
54     // Nuber of expected calls to attachTo, rename etc.;
55
protected int expectedCallsCount;
56     
57     // Message for unexpected value
58
protected static final String JavaDoc MSSG_UNEXPECTED_VALUE_RETURNED =
59         "Unexpected value returned.";
60     
61     // Message for unexpected value
62
protected static final String JavaDoc MSSG_METHOD_NOT_CALLED =
63         "Method not called.";
64     
65     // Message for the case when an exception is thrown
66
protected static final String JavaDoc MSSG_EXCEPTION_THROWN =
67         "Exception thrown.";
68
69     protected static final Lookup NO_LOOKUP = null; // PENDING
70

71     
72     
73
74     // Methods of testCase -----------------------------------------------------
75

76     public TestBaseValues(java.lang.String JavaDoc testName) {
77         super(testName);
78     }
79     
80     public static void main(java.lang.String JavaDoc[] args) {
81         junit.textui.TestRunner.run(suite());
82     }
83     
84     public static NbTest suite() {
85         NbTestSuite suite = new NbTestSuite( TestBaseValues.class );
86         
87         return suite;
88     }
89     
90     protected void setUp() throws Exception JavaDoc {
91         super.setUp();
92     }
93     
94     protected void tearDown() throws Exception JavaDoc {
95         look = null;
96         super.tearDown();
97     }
98     
99     // Methods for setting up the test case ------------------------------------
100

101     protected void setTarget( Look look, SampleRepObject representedObject, int expectedCallsCount ) {
102         this.look = look;
103         this.node = null;
104         this.representedObject = representedObject;
105         this.expectedCallsCount = expectedCallsCount;
106         org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( look, representedObject, null );
107     }
108     
109     protected void setTarget( Node node, SampleRepObject representedObject, int expectedCallsCount ) {
110         this.look = null;
111         this.node = node;
112         this.representedObject = representedObject;
113         this.expectedCallsCount = expectedCallsCount;
114     }
115     
116     protected void setGoldenValues( GoldenValue[] goldenValues ) {
117         this.goldenValues = goldenValues;
118     }
119     
120     protected boolean onNode() {
121         return node != null;
122     }
123     
124     // Test methods ------------------------------------------------------------
125

126     public void testAttachTo() {
127         if ( onNode() ) {
128             assertEquals ( MSSG_METHOD_NOT_CALLED, expectedCallsCount,
129                 representedObject.getAttachCalled() );
130         }
131         else {
132             assertEquals ( MSSG_METHOD_NOT_CALLED, expectedCallsCount,
133                 ((SampleRepObject)representedObject).getAttachCalled() );
134         }
135     }
136     
137     // Methods for FUNCTIONALITY EXTENSIONS ------------------------------------
138

139     public void testGetLookupValues() {
140         if ( onNode() ) {
141             Lookup lookup = node.getLookup();
142             Lookup.Result result = lookup.lookup( new Lookup.Template( Object JavaDoc.class ) );
143             Collection items = new ArrayList( result.allItems() ); // Make it modifyable
144

145             // We need to remove the node itself
146

147             Object JavaDoc nodeItself = null;
148             
149             for( Iterator it = items.iterator(); it.hasNext(); ) {
150                 Lookup.Item item = (Lookup.Item)it.next();
151                 if ( item.getInstance() == node ) {
152                     nodeItself = item;
153                     break;
154                 }
155             }
156             
157             assertNotNull( "Lookup should contain the node itself ", nodeItself );
158             items.remove( nodeItself );
159             
160             assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
161                             GoldenValue.isOK(
162                                 ProxyLook.GET_LOOKUP_ITEMS,
163                                 items,
164                                 goldenValues ) );
165         }
166         else {
167             assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
168                             GoldenValue.isOK(
169                                 ProxyLook.GET_LOOKUP_ITEMS,
170                                 look.getLookupItems( representedObject, Lookup.EMPTY ),
171                                 goldenValues ) );
172         }
173     }
174     
175     // Methods for STYLE -------------------------------------------------------
176

177     public void testGetDisplayName() {
178         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
179                         GoldenValue.isOK(
180                             ProxyLook.GET_DISPLAY_NAME,
181                             onNode() ? node.getDisplayName() :
182                                        look.getDisplayName( representedObject, NO_LOOKUP ),
183                             goldenValues ) );
184         
185     }
186     
187     public void testGetName() {
188         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
189                         GoldenValue.isOK(
190                             ProxyLook.GET_NAME,
191                             onNode() ? node.getName() :
192                                        look.getName( representedObject, NO_LOOKUP ),
193                             goldenValues ) );
194     }
195     
196     public void testSetName() throws IOException JavaDoc {
197         if ( onNode() ) {
198            node.setName( "New name" );
199            if ( onNode() ) {
200             assertEquals( MSSG_METHOD_NOT_CALLED, expectedCallsCount,
201                 representedObject.getSetNameCalled() );
202         }
203         }
204         else {
205            look.rename( representedObject, "New name", NO_LOOKUP );
206            assertEquals ( MSSG_METHOD_NOT_CALLED, expectedCallsCount,
207                ((SampleRepObject)representedObject).getSetNameCalled() );
208         }
209         
210     }
211
212     public void testGetShortDescription() {
213         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
214                         GoldenValue.isOK(
215                             ProxyLook.GET_SHORT_DESCRIPTION,
216                             onNode() ? node.getShortDescription() :
217                                        look.getShortDescription( representedObject, NO_LOOKUP ),
218                             goldenValues ) );
219     }
220     
221     public void testGetIcon() {
222         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
223                         GoldenValue.isOK(
224                             ProxyLook.GET_ICON,
225                             onNode() ? node.getIcon( BeanInfo.ICON_COLOR_16x16 ) :
226                                        look.getIcon( representedObject, BeanInfo.ICON_COLOR_16x16, NO_LOOKUP ),
227                             goldenValues ) );
228     
229     }
230     
231     public void testGetOpenedIcon() {
232         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
233                         GoldenValue.isOK(
234                             ProxyLook.GET_OPENED_ICON,
235                             onNode() ? node.getOpenedIcon( BeanInfo.ICON_COLOR_16x16 ) :
236                                        look.getOpenedIcon( representedObject, BeanInfo.ICON_COLOR_16x16, NO_LOOKUP ),
237                             goldenValues ) );
238     }
239     
240     public void testGetHelpCtx() {
241         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
242                         GoldenValue.isOK(
243                             ProxyLook.GET_HELP_CTX,
244                             onNode() ? node.getHelpCtx() :
245                                        look.getHelpCtx( representedObject, NO_LOOKUP ),
246                             goldenValues ) );
247     }
248     
249     // Methods for CHILDREN ----------------------------------------------------
250

251     public void testGetChildObjects() {
252         
253         if ( onNode() ) {
254
255             Node[] nodes = node.getChildren().getNodes();
256             List gv = (List)GoldenValue.get( ProxyLook.GET_CHILD_OBJECTS, goldenValues );
257             
258             if ( gv == null ) {
259                 fail( "Golden value is invalid" );
260             }
261             if ( gv.size() != nodes.length ) {
262                 fail( MSSG_UNEXPECTED_VALUE_RETURNED );
263             }
264             
265             
266             for( int i = 0; i < nodes.length; i++ ) {
267                 
268                 Node n = nodes[i];
269                 if ( TestUtil.getRepresentedObject( n ) != gv.get(i) &&
270                     !TestUtil.getRepresentedObject( n ).equals( gv.get(i) ) ) {
271                     fail( MSSG_UNEXPECTED_VALUE_RETURNED + "on index : " + i );
272                 }
273             }
274             
275         }
276         else {
277             assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
278                             GoldenValue.isOK(
279                                 ProxyLook.GET_CHILD_OBJECTS,
280                                 look.getChildObjects( representedObject, NO_LOOKUP ),
281                                 goldenValues ) );
282         }
283     }
284     
285     // Methods for ACTIONS & NEW TYPES -----------------------------------------
286

287     public void testGetNewTypes() {
288         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
289                         GoldenValue.isOK(
290                             ProxyLook.GET_NEW_TYPES,
291                             onNode() ? node.getNewTypes() :
292                                        look.getNewTypes( representedObject, NO_LOOKUP ),
293                             goldenValues ) );
294     }
295     
296     public void testGetActions() throws Exception JavaDoc {
297         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
298                         GoldenValue.isOK(
299                             ProxyLook.GET_ACTIONS,
300                             onNode() ? node.getActions() :
301                                        look.getActions( representedObject, NO_LOOKUP ),
302                             goldenValues ) );
303     }
304     
305     public void testGetContextActions() {
306         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
307                         GoldenValue.isOK(
308                             ProxyLook.GET_CONTEXT_ACTIONS,
309                             onNode() ? node.getContextActions() :
310                                        look.getContextActions( representedObject, NO_LOOKUP ),
311                             goldenValues ) );
312     }
313     
314     public void testGetDefaultAction() {
315         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
316                         GoldenValue.isOK(
317                             ProxyLook.GET_DEFAULT_ACTION,
318                             onNode() ? node.getDefaultAction() :
319                                        look.getDefaultAction( representedObject, NO_LOOKUP ),
320                             goldenValues ) );
321     }
322     
323     // Methods for PROPERTIES AND CUSTOMIZER -----------------------------------
324

325     public void testGetPropertySets() {
326         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
327                         GoldenValue.isOK(
328                             ProxyLook.GET_PROPERTY_SETS,
329                             onNode() ? node.getPropertySets() :
330                                        look.getPropertySets( representedObject, NO_LOOKUP ),
331                             goldenValues ) );
332     }
333     
334     public void testGetCustomizer() {
335         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
336                         GoldenValue.isOK(
337                             ProxyLook.GET_CUSTOMIZER,
338                             onNode() ? node.getCustomizer() :
339                                        look.getCustomizer( representedObject, NO_LOOKUP ),
340                             goldenValues ) );
341     }
342     
343     public void testHasCustomizer() {
344         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
345                         GoldenValue.isOK(
346                             ProxyLook.HAS_CUSTOMIZER,
347                             onNode() ? new Boolean JavaDoc( node.hasCustomizer() ) :
348                                        new Boolean JavaDoc( look.hasCustomizer( representedObject, NO_LOOKUP ) ),
349                             goldenValues ) );
350     }
351     
352     
353     // Methods for CLIPBOARD OPERATIONS ----------------------------------------
354

355     public void testCanRename() {
356         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
357                         GoldenValue.isOK(
358                             ProxyLook.CAN_RENAME,
359                             onNode() ? new Boolean JavaDoc( node.canRename() ) :
360                                        new Boolean JavaDoc( look.canRename( representedObject, NO_LOOKUP ) ),
361                             goldenValues ) );
362     }
363     
364     public void testCanDestroy() {
365         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
366                         GoldenValue.isOK(
367                             ProxyLook.CAN_DESTROY,
368                             onNode() ? new Boolean JavaDoc( node.canDestroy() ) :
369                                        new Boolean JavaDoc( look.canDestroy( representedObject, NO_LOOKUP ) ),
370                             goldenValues ) );
371     }
372     
373     public void testCanCopy() {
374         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
375                         GoldenValue.isOK(
376                             ProxyLook.CAN_COPY,
377                             onNode() ? new Boolean JavaDoc( node.canCopy() ) :
378                                        new Boolean JavaDoc( look.canCopy( representedObject, NO_LOOKUP ) ),
379                             goldenValues ) );
380     }
381     
382     public void testCanCut() {
383         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
384                         GoldenValue.isOK(
385                             ProxyLook.CAN_CUT,
386                             onNode() ? new Boolean JavaDoc( node.canCut() ) :
387                                        new Boolean JavaDoc( look.canCut( representedObject, NO_LOOKUP ) ),
388                             goldenValues ) );
389     }
390     
391     public void testGetPasteTypes() {
392         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
393                         GoldenValue.isOK(
394                             ProxyLook.GET_PASTE_TYPES,
395                             onNode() ? node.getPasteTypes( null ) :
396                                        look.getPasteTypes( representedObject, null, NO_LOOKUP ),
397                             goldenValues ) );
398     }
399     
400     public void testGetDropType() {
401         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
402                         GoldenValue.isOK(
403                             ProxyLook.GET_DROP_TYPE,
404                             onNode() ? node.getDropType( null, 0, 0 ) :
405                                        look.getDropType( representedObject, null, 0, 0, NO_LOOKUP ),
406                             goldenValues ) );
407     }
408     
409     public void testClipboardCopy() {
410         try {
411             assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
412                         GoldenValue.isOK(
413                             ProxyLook.CLIPBOARD_COPY,
414                             onNode() ? node.clipboardCopy() :
415                                        look.clipboardCopy( representedObject, NO_LOOKUP ),
416                             goldenValues ) );
417         }
418         catch ( java.io.IOException JavaDoc e ) {
419             fail ( MSSG_EXCEPTION_THROWN );
420         }
421     }
422     
423     public void testClipboardCut() {
424         try {
425             assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
426                         GoldenValue.isOK(
427                             ProxyLook.CLIPBOARD_CUT,
428                             onNode() ? node.clipboardCut() :
429                                        look.clipboardCut( representedObject, NO_LOOKUP ),
430                             goldenValues ) );
431         }
432         catch ( java.io.IOException JavaDoc e ) {
433             fail ( MSSG_EXCEPTION_THROWN );
434         }
435         
436     }
437     
438     public void testDrag() {
439         try {
440             assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
441                         GoldenValue.isOK(
442                             ProxyLook.DRAG,
443                             onNode() ? node.drag() :
444                                        look.drag( representedObject, NO_LOOKUP ),
445                             goldenValues ) );
446         }
447         catch ( java.io.IOException JavaDoc e ) {
448             fail ( MSSG_EXCEPTION_THROWN );
449         }
450     }
451     
452     public void testDestroy() {
453         try {
454             if ( onNode() ) {
455                 node.destroy();
456                 assertEquals ( MSSG_METHOD_NOT_CALLED, expectedCallsCount,
457                     representedObject.getDestroyCalled() );
458             }
459             else {
460                 look.destroy( representedObject, NO_LOOKUP );
461                 assertEquals ( MSSG_METHOD_NOT_CALLED, expectedCallsCount,
462                     ((SampleRepObject)representedObject).getDestroyCalled() );
463             }
464                         
465         }
466         catch ( java.io.IOException JavaDoc e ) {
467             fail ( MSSG_EXCEPTION_THROWN );
468         }
469     }
470     
471     
472     
473 }
474
Popular Tags