KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > awt > ValidateLayerMenuTest


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 2002 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.windows.awt;
21
22 import java.io.InputStream JavaDoc;
23 import junit.framework.*;
24 import org.netbeans.junit.*;
25 import org.openide.cookies.InstanceCookie;
26
27 import org.openide.filesystems.*;
28 import org.openide.loaders.*;
29
30 /** Checks the consistence of Menu folder.
31  *
32  * @author Jaroslav Tulach
33  */

34 public class ValidateLayerMenuTest extends NbTestCase {
35
36     /** Creates a new instance of SFSTest */
37     public ValidateLayerMenuTest(String JavaDoc name) {
38         super (name);
39     }
40
41     /**
42      * @param args the command line arguments
43      */

44     public static void main(java.lang.String JavaDoc[] args) {
45         junit.textui.TestRunner.run(suite());
46     }
47     
48     public static Test suite() {
49         TestSuite suite = new NbTestSuite(ValidateLayerMenuTest.class);
50         
51         return suite;
52     }
53     
54     //
55
// override in subclasses
56
//
57

58     protected String JavaDoc rootName () {
59         return "Menu";
60     }
61     
62     /** Allowes to skip filest that are know to be broken */
63     protected boolean skipFile (FileObject fo) {
64         // ignore these files, there are helper for implementation of
65
// View/filesystems, View/Runtime, View/Projects, etc.
66
return fo.getPath().startsWith ("Menu/Window/oldRoots") && fo.hasExt ("txt");
67     }
68     
69     protected boolean correctInstance (Object JavaDoc obj) {
70         if (obj instanceof javax.swing.Action JavaDoc) {
71             return true;
72         }
73         if (obj instanceof org.openide.util.actions.Presenter.Menu) {
74             return true;
75         }
76         if (obj instanceof javax.swing.JSeparator JavaDoc) {
77             return true;
78         }
79         if (obj instanceof javax.swing.JMenuItem JavaDoc) {
80             return true;
81         }
82         
83         return false;
84     }
85     
86     
87     //
88
// the test
89
//
90

91     public void testContentCorrect () throws Exception JavaDoc {
92         java.util.ArrayList JavaDoc errors = new java.util.ArrayList JavaDoc ();
93         
94         DataFolder df = DataFolder.findFolder (Repository.getDefault().getDefaultFileSystem().findResource (rootName ()));
95         verifyMenu (df, errors);
96         
97         if (!errors.isEmpty()) {
98             fail ("Some files do not provide valid menu elements" + errors);
99         }
100     }
101     
102     private void verifyMenu (DataFolder f, java.util.ArrayList JavaDoc errors) throws Exception JavaDoc {
103         DataObject[] arr = f.getChildren();
104         for (int i = 0; i < arr.length; i++) {
105             if (arr[i] instanceof DataFolder) {
106                 verifyMenu ((DataFolder)arr[i], errors);
107                 continue;
108             }
109             FileObject file = arr[i].getPrimaryFile ();
110             
111             if (skipFile (file)) {
112                 continue;
113             }
114             
115             Object JavaDoc url = file.getURL();
116             
117             InstanceCookie ic = (InstanceCookie)arr[i].getCookie(InstanceCookie.class);
118             if (ic == null) {
119                 errors.add ("\n File " + file + " does not have instance cookie, url: " + url);
120                 continue;
121             }
122             
123             try {
124                 Object JavaDoc obj = ic.instanceCreate();
125                 if (correctInstance (obj)) {
126                     continue;
127                 }
128                 errors.add ("\n File " + arr[i].getPrimaryFile () + " does not provide correct instance: " + obj + " url: " + url);
129             } catch (Exception JavaDoc ex) {
130                 errors.add ("\n File " + arr[i].getPrimaryFile () + " cannot be read " + ex + " url: " + url);
131             }
132         }
133     }
134     
135 }
136
137
Popular Tags