KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > actions > TestSupport


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.modules.project.ui.actions;
21
22 import java.io.IOException JavaDoc;
23 import org.netbeans.api.project.Project;
24 import org.netbeans.spi.project.AuxiliaryConfiguration;
25 import org.netbeans.spi.project.ProjectFactory;
26 import org.netbeans.spi.project.ProjectState;
27 import org.openide.filesystems.FileObject;
28 import org.openide.util.Lookup;
29 import org.openide.util.lookup.Lookups;
30 import org.openide.util.lookup.ProxyLookup;
31 import org.openide.xml.XMLUtil;
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36
37 /**
38  * Help set up org.netbeans.api.project.*Test.
39  * @author Jesse Glick
40  */

41 public final class TestSupport {
42     
43     public static FileObject createTestProject( FileObject workDir, String JavaDoc name ) throws IOException JavaDoc {
44         FileObject p = workDir.createFolder( name );
45         p.createFolder( "testproject" );
46         return p;
47     }
48             
49     public static void notifyDeleted(Project p) {
50         ((TestProject) p).state.notifyDeleted();
51     }
52         
53     /**
54      * A testing project factory which recognizes directories containing
55      * a subdirectory called "testproject".
56      * If that subdirectory contains a file named "broken" then loading the project
57      * will fail with an IOException.
58      */

59     public static final class TestProjectFactory implements ProjectFactory {
60         
61         public TestProjectFactory() {}
62         
63         public Project loadProject(FileObject projectDirectory, ProjectState state) throws IOException JavaDoc {
64             FileObject testproject = projectDirectory.getFileObject("testproject");
65             if (testproject != null && testproject.isFolder()) {
66                 return new TestProject(projectDirectory, state);
67             }
68             else {
69                 return null;
70             }
71         }
72         
73         public void saveProject(Project project) throws IOException JavaDoc, ClassCastException JavaDoc {
74             TestProject p = (TestProject)project;
75             Throwable JavaDoc t = p.error;
76             if (t != null) {
77                 p.error = null;
78                 if (t instanceof IOException JavaDoc) {
79                     throw (IOException JavaDoc)t;
80                 } else if (t instanceof Error JavaDoc) {
81                     throw (Error JavaDoc)t;
82                 } else {
83                     throw (RuntimeException JavaDoc)t;
84                 }
85             }
86         }
87         
88         public boolean isProject(FileObject dir) {
89             FileObject testproject = dir.getFileObject("testproject");
90             return testproject != null && testproject.isFolder();
91         }
92         
93     }
94     
95     public static final class TestProject implements Project {
96         
97         private Lookup lookup;
98         private final FileObject dir;
99         final ProjectState state;
100         Throwable JavaDoc error;
101         int saveCount = 0;
102         
103         public TestProject(FileObject dir, ProjectState state) {
104             this.dir = dir;
105             this.state = state;
106         }
107         
108         public void setLookup( Lookup lookup ) {
109             this.lookup = lookup;
110         }
111         
112         public Lookup getLookup() {
113             if ( lookup == null ) {
114                 return Lookup.EMPTY;
115             }
116             else {
117                 return lookup;
118             }
119         }
120         
121         public FileObject getProjectDirectory() {
122             return dir;
123         }
124         
125         public String JavaDoc toString() {
126             return "testproject:" + getProjectDirectory().getNameExt();
127         }
128         
129     }
130         
131     public static class ChangeableLookup extends ProxyLookup {
132         
133         public ChangeableLookup(Object JavaDoc... objects) {
134             super( new Lookup[] { Lookups.fixed( objects ) } );
135         }
136         
137         public void change(Object JavaDoc... objects) {
138             setLookups( new Lookup[] { Lookups.fixed( objects ) } );
139         }
140                 
141     }
142     
143     public static AuxiliaryConfiguration createAuxiliaryConfiguration () {
144         return new MemoryAuxiliaryConfiguration ();
145     }
146     
147     private static class MemoryAuxiliaryConfiguration implements AuxiliaryConfiguration {
148         private Document JavaDoc xml = XMLUtil.createDocument ("private", "http://www.netbeans.org/ns/test-support-project-private/1", null, null);
149
150         public Element JavaDoc getConfigurationFragment (String JavaDoc elementName, String JavaDoc namespace, boolean shared) {
151             if (shared) {
152                 assert false : "Shared not implemented";
153             }
154             Element JavaDoc root = xml.getDocumentElement ();
155             Element JavaDoc data = findElement (root, elementName, namespace);
156             if (data != null) {
157                 return (Element JavaDoc) data.cloneNode (true);
158             } else {
159                 //return xml.createElementNS (namespace, elementName);
160
return null;
161             }
162         }
163         
164         public void putConfigurationFragment(Element JavaDoc fragment, boolean shared) throws IllegalArgumentException JavaDoc {
165             if (shared) {
166                 assert false : "Shared not implemented";
167             }
168             
169             Element JavaDoc root = xml.getDocumentElement ();
170             Element JavaDoc existing = findElement (root, fragment.getLocalName (), fragment.getNamespaceURI ());
171             // XXX first compare to existing and return if the same
172
if (existing != null) {
173                 root.removeChild (existing);
174             }
175             // the children are alphabetize: find correct place to insert new node
176
Node JavaDoc ref = null;
177             NodeList JavaDoc list = root.getChildNodes ();
178             for (int i=0; i<list.getLength (); i++) {
179                 Node JavaDoc node = list.item (i);
180                 if (node.getNodeType () != Node.ELEMENT_NODE) {
181                     continue;
182                 }
183                 int comparison = node.getNodeName ().compareTo (fragment.getNodeName ());
184                 if (comparison == 0) {
185                     comparison = node.getNamespaceURI ().compareTo (fragment.getNamespaceURI ());
186                 }
187                 if (comparison > 0) {
188                     ref = node;
189                     break;
190                 }
191             }
192             root.insertBefore (root.getOwnerDocument ().importNode (fragment, true), ref);
193         }
194         
195         public boolean removeConfigurationFragment (String JavaDoc elementName, String JavaDoc namespace, boolean shared) throws IllegalArgumentException JavaDoc {
196             if (shared) {
197                 assert false : "Shared not implemented";
198             }
199
200             Element JavaDoc root = xml.getDocumentElement ();
201             Element JavaDoc data = findElement (root, elementName, namespace);
202             if (data != null) {
203                 root.removeChild (data);
204                 return true;
205             } else {
206                 return false;
207             }
208         }
209     }
210     
211     // copied from org.netbeans.modules.project.ant.Util
212
private static Element JavaDoc findElement(Element JavaDoc parent, String JavaDoc name, String JavaDoc namespace) {
213         Element JavaDoc result = null;
214         NodeList JavaDoc l = parent.getChildNodes();
215         int len = l.getLength();
216         for (int i = 0; i < len; i++) {
217             if (l.item(i).getNodeType() == Node.ELEMENT_NODE) {
218                 Element JavaDoc el = (Element JavaDoc)l.item(i);
219                 if (name.equals(el.getLocalName()) && namespace.equals(el.getNamespaceURI())) {
220                     if (result == null) {
221                         result = el;
222                     } else {
223                         return null;
224                     }
225                 }
226             }
227         }
228         return result;
229     }
230 }
231
Popular Tags