KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > freeform > JavaFreeformFileBuiltQueryTest


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.java.freeform;
21
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileUtil;
29 import org.netbeans.api.project.Project;
30 import org.netbeans.api.project.ProjectManager;
31 import org.netbeans.api.queries.FileBuiltQuery;
32 import org.netbeans.modules.ant.freeform.TestBase;
33 import org.netbeans.spi.queries.FileBuiltQueryImplementation;
34
35 /**
36  *
37  * @author Jan Lahoda
38  */

39 public class JavaFreeformFileBuiltQueryTest extends TestBase {
40     
41     public JavaFreeformFileBuiltQueryTest(String JavaDoc testName) {
42         super(testName);
43     }
44     
45     private Project simpleProject;
46
47     private void deepCopy(FileObject source, FileObject target) throws IOException JavaDoc {
48         List JavaDoc toCopy = new LinkedList JavaDoc();
49         
50         FileObject[] children = source.getChildren();
51         
52         for (int cntr = 0; cntr < children.length; cntr++) {
53             toCopy.add(children[cntr].getNameExt());
54         }
55         
56         while (toCopy.size() > 0) {
57             String JavaDoc copying = (String JavaDoc) toCopy.remove(0);
58             FileObject toCopyFO = source.getFileObject(copying);
59             FileObject targetParent = target.getFileObject(FileUtil.getRelativePath(source, toCopyFO.getParent()));
60             
61             if (toCopyFO.isFolder()) {
62                 targetParent.createFolder(toCopyFO.getNameExt());
63                 
64                 children = toCopyFO.getChildren();
65                 
66                 for (int cntr = 0; cntr < children.length; cntr++) {
67                     toCopy.add(FileUtil.getRelativePath(source, children[cntr]));
68                 }
69             } else {
70                 FileUtil.copyFile(toCopyFO, targetParent, toCopyFO.getName(), toCopyFO.getExt());
71             }
72         }
73     }
74     
75     protected void setUp() throws Exception JavaDoc {
76         super.setUp();
77         assertNotNull("Must have built ant/freeform unit tests first, INCLUDING copying non-*.java resources to the classes build directory",
78             JavaFreeformFileBuiltQueryTest.class.getResource("/META-INF/services/org.openide.modules.InstalledFileLocator"));
79         
80         FileObject root = simple.getProjectDirectory();
81         clearWorkDir();
82         File JavaDoc workdir = getWorkDir();
83         FileObject workdirFO = FileUtil.toFileObject(workdir);
84         FileObject simpleFO = workdirFO.createFolder("simple");
85         
86         deepCopy(root, simpleFO);
87         
88         simpleProject = ProjectManager.getDefault().findProject(simpleFO);
89     }
90     
91     private void createEmptyFile(File JavaDoc f) throws IOException JavaDoc {
92         f.getParentFile().mkdirs();
93         new FileOutputStream JavaDoc(f).close();
94     }
95     
96     private void refreshRecursively(FileObject of) {
97         List JavaDoc toRefresh = new LinkedList JavaDoc();
98         
99         toRefresh.add(of);
100         
101         while (toRefresh.size() > 0) {
102             FileObject f = (FileObject) toRefresh.remove(0);
103             
104             f.refresh();
105             
106             FileObject[] children = f.getChildren();
107             
108             for (int cntr = 0; cntr < children.length; cntr++) {
109                 toRefresh.add(children[cntr]);
110             }
111         }
112     }
113     
114     public void testStatusUpdated() throws Exception JavaDoc {
115         //src->build/classes
116
//antsrc->build/antclasses
117
//src: org/foo/myapp/MyApp.java
118
// /SomeFile.java
119
//antsrc: org/foo/ant/SpecialTask.java
120
FileObject src = simpleProject.getProjectDirectory().getFileObject("src");
121         FileObject antsrc = simpleProject.getProjectDirectory().getFileObject("antsrc");
122         
123         FileObject MyAppJava = src.getFileObject("org/foo/myapp/MyApp.java");
124         FileObject SomeFileJava = src.getFileObject("org/foo/myapp/SomeFile.java");
125         FileObject SpecialTaskJava = antsrc.getFileObject("org/foo/ant/SpecialTask.java");
126         
127         File JavaDoc MyAppClass = new File JavaDoc(FileUtil.toFile(simpleProject.getProjectDirectory()), "build/classes/org/foo/myapp/MyApp.class".replace('/', File.separatorChar));
128         File JavaDoc SomeFileClass = new File JavaDoc(FileUtil.toFile(simpleProject.getProjectDirectory()), "build/classes/org/foo/myapp/SomeFile.class".replace('/', File.separatorChar));
129         File JavaDoc SpecialTaskClass = new File JavaDoc(FileUtil.toFile(simpleProject.getProjectDirectory()), "build/antclasses/org/foo/ant/SpecialTask.class".replace('/', File.separatorChar));
130         
131         FileBuiltQueryImplementation fbqi = simpleProject.getLookup().lookup(FileBuiltQueryImplementation.class);
132         
133         assertNotNull("have FileBuiltQueryImplementation in lookup", fbqi);
134         
135         FileBuiltQuery.Status MyAppStatus = fbqi.getStatus(MyAppJava);
136         FileBuiltQuery.Status SomeFileStatus = fbqi.getStatus(SomeFileJava);
137         FileBuiltQuery.Status SpecialTaskStatus = fbqi.getStatus(SpecialTaskJava);
138         
139         assertFalse("MyApp.java is not built", MyAppStatus.isBuilt());
140         assertFalse("SomeFile.java is not built", SomeFileStatus.isBuilt());
141         assertFalse("SpecialTask.java is not built", SpecialTaskStatus.isBuilt());
142         
143         createEmptyFile(MyAppClass);
144         createEmptyFile(SpecialTaskClass);
145         
146         refreshRecursively(simpleProject.getProjectDirectory());
147
148         assertTrue("MyApp.java is built", MyAppStatus.isBuilt());
149         assertFalse("SomeFile.java is not built", SomeFileStatus.isBuilt());
150         assertTrue("SpecialTask.java is built", SpecialTaskStatus.isBuilt());
151         
152         MyAppClass.delete();
153         
154         refreshRecursively(simpleProject.getProjectDirectory());
155         
156         assertFalse("MyApp.java is built", MyAppStatus.isBuilt());
157         assertFalse("SomeFile.java is not built", SomeFileStatus.isBuilt());
158         assertTrue("SpecialTask.java is built", SpecialTaskStatus.isBuilt());
159         
160         SpecialTaskClass.delete();
161         createEmptyFile(MyAppClass);
162         createEmptyFile(SomeFileClass);
163         
164         refreshRecursively(simpleProject.getProjectDirectory());
165         
166         assertTrue("MyApp.java is built", MyAppStatus.isBuilt());
167         assertTrue("SomeFile.java is not built", SomeFileStatus.isBuilt());
168         assertFalse("SpecialTask.java is built", SpecialTaskStatus.isBuilt());
169     }
170 }
171
Popular Tags