KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > nodes2looks > NodeLookValuesTest


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.api.nodes2looks;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import org.openide.nodes.*;
28 import org.netbeans.junit.*;
29 import org.openide.util.Lookup;
30
31 import org.netbeans.spi.looks.*;
32
33 /** Tests whether all vales returned from a Node are identical with
34  * the values server by associated look
35  */

36 public class NodeLookValuesTest extends TestBaseValues {
37
38     Node delegate;
39
40     // Methods of testCase -----------------------------------------------------
41

42     public NodeLookValuesTest(java.lang.String JavaDoc testName) {
43         super(testName);
44     }
45     
46     public static void main(java.lang.String JavaDoc[] args) {
47         junit.textui.TestRunner.run(suite());
48     }
49     
50     public static NbTest suite() {
51         NbTestSuite suite = new NbTestSuite( NodeLookValuesTest.class );
52         return suite;
53     }
54     
55     protected void setUp() throws Exception JavaDoc {
56         
57         Look look = new SampleLook( "NodeLookValuesTestLook" );
58         LookSelector selector = Selectors.selector( new SampleProvider( look ) );
59         GoldenValue[] goldenValues = GoldenValue.createGoldenValues();
60         SampleRepObject ro = new SampleRepObject( goldenValues );
61         delegate = Nodes.node( ro, look, selector );
62         Node node = Nodes.node( delegate, Nodes.nodeLook(), selector );
63
64         setTarget( node, ro, 1 );
65         setGoldenValues( goldenValues );
66
67         super.setUp();
68     }
69     
70     protected void tearDown() throws Exception JavaDoc {
71         super.tearDown();
72     }
73     
74     
75     // Test methods ------------------------------------------------------------
76

77     ///////////////////////////////////////////////////////
78
// //
79
// Most methods are inherited from: TestBaseValues //
80
// //
81
///////////////////////////////////////////////////////
82

83     // Overriden test methods --------------------------------------------------
84

85     public void testGetLookupValues() {
86         
87         Lookup lookup = node.getLookup();
88         Lookup.Result result = lookup.lookup( new Lookup.Template( Object JavaDoc.class ) );
89         Collection JavaDoc items = new ArrayList JavaDoc( result.allItems() ); // Make it modifyable
90

91         // We need to remove the node itself TWICE
92

93         for( int i = 0; i < 2; i++ ) {
94             Object JavaDoc nodeItself = null;
95
96             for( Iterator JavaDoc it = items.iterator(); it.hasNext(); ) {
97                 Lookup.Item item = (Lookup.Item)it.next();
98                 
99                 if ( item.getInstance() == ( i == 0 ? node : delegate ) ) {
100                     nodeItself = item;
101                     break;
102                 }
103             }
104
105             assertNotNull( "Lookup should contain the node itself. Run " + i +".", nodeItself );
106             items.remove( nodeItself );
107         }
108         
109         assertTrue ( MSSG_UNEXPECTED_VALUE_RETURNED,
110                         GoldenValue.isOK(
111                             ProxyLook.GET_LOOKUP_ITEMS,
112                             items,
113                             goldenValues ) );
114     }
115     
116     public void testGetChildObjects() {
117         Node[] nodes = node.getChildren().getNodes();
118         List JavaDoc gv = (List JavaDoc)GoldenValue.get( ProxyLook.GET_CHILD_OBJECTS, goldenValues );
119
120         if ( gv == null ) {
121             fail( "Golden value is invalid" );
122         }
123         if ( gv.size() != nodes.length ) {
124             fail( MSSG_UNEXPECTED_VALUE_RETURNED );
125         }
126
127         for( int i = 0; i < nodes.length; i++ ) {
128
129             Node n = nodes[i];
130                       
131             Object JavaDoc o = TestUtil.getRepresentedObject( n );
132             assertTrue("o is a Node: " + o.getClass().getName(), o instanceof Node);
133             Node delegate = (Node)o;
134             
135             if ( TestUtil.getRepresentedObject( delegate ) != gv.get(i) &&
136                 !TestUtil.getRepresentedObject( delegate ).equals( gv.get(i) ) ) {
137                 fail( MSSG_UNEXPECTED_VALUE_RETURNED + "on index : " + i );
138             }
139         }
140     }
141     
142     // Additional test methods -------------------------------------------------
143

144     public void testGetRepresentedObject() {
145         Object JavaDoc ro = TestUtil.getRepresentedObject( node );
146         if ( ro != delegate ) {
147             fail("Bad represented object.");
148         }
149         if ( TestUtil.getRepresentedObject( (Node)ro ) != representedObject ) {
150             fail("Bad represented delegate.");
151         }
152     }
153     
154     public void testGetLook() {
155         Look look = ((LookNode)node).getLook();
156         if ( look != Nodes.nodeLook() ) {
157             fail("Bad or no look on node");
158         }
159     }
160     
161     public void testGetCookie() {
162         Class JavaDoc c = org.openide.cookies.SaveCookie.class;
163         Node.Cookie cookie = node.getCookie( c );
164         
165         Collection JavaDoc items = (Collection JavaDoc)representedObject.getValue( ProxyLook.GET_LOOKUP_ITEMS );
166         Node.Cookie gc = null;
167         
168         for( Iterator JavaDoc it = items.iterator(); it.hasNext(); ) {
169             Lookup.Item item = (Lookup.Item)it.next();
170             if ( c.isInstance( item.getInstance() ) ) {
171                 gc = (Node.Cookie)item.getInstance();
172             }
173         }
174         
175         if ( cookie != gc ) {
176             fail("Bad cookie." + cookie + " instad of " + gc );
177         }
178     }
179     
180 }
181
182
183
Popular Tags