KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > TestBase


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.ant.freeform;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34 import javax.swing.event.ChangeEvent JavaDoc;
35 import javax.swing.event.ChangeListener JavaDoc;
36 import org.netbeans.api.project.Project;
37 import org.netbeans.api.project.ProjectManager;
38 import org.netbeans.junit.NbTestCase;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileUtil;
41 import org.openide.modules.ModuleInfo;
42 import org.openide.util.Lookup;
43
44 /**
45  * Base class for tests.
46  * @author Jesse Glick
47  */

48 public abstract class TestBase extends NbTestCase {
49     
50     static {
51         TestBase.class.getClassLoader().setDefaultAssertionStatus(true);
52     }
53     
54     protected TestBase(String JavaDoc name) {
55         super(name);
56     }
57     
58     protected File JavaDoc egdir;
59     protected File JavaDoc datadir;
60     protected FileObject egdirFO;
61     protected FreeformProject simple;
62     protected FreeformProject simple2;
63     protected FreeformProject extsrcroot;
64     protected FreeformProject extbuildroot;
65     protected FreeformProject extbuildscript;
66     protected FileObject myAppJava;
67     protected FileObject specialTaskJava;
68     protected FileObject buildProperties;
69     
70     protected void setUp() throws Exception JavaDoc {
71         super.setUp();
72         Lookup.getDefault().lookup(ModuleInfo.class);
73
74         egdir = FileUtil.normalizeFile(new File JavaDoc(getDataDir(), "example-projects"));
75         assertTrue("example dir " + egdir + " exists", egdir.exists());
76         egdirFO = FileUtil.toFileObject(egdir);
77         assertNotNull("have FileObject for " + egdir, egdirFO);
78         FileObject projdir = egdirFO.getFileObject("simple");
79         assertNotNull("found projdir", projdir);
80         Project _simple = ProjectManager.getDefault().findProject(projdir);
81         assertNotNull("have a project", _simple);
82         simple = (FreeformProject) _simple;
83         myAppJava = projdir.getFileObject("src/org/foo/myapp/MyApp.java");
84         assertNotNull("found MyApp.java", myAppJava);
85         specialTaskJava = projdir.getFileObject("antsrc/org/foo/ant/SpecialTask.java");
86         assertNotNull("found SpecialTask.java", specialTaskJava);
87         buildProperties = projdir.getFileObject("build.properties");
88         assertNotNull("found build.properties", buildProperties);
89         projdir = egdirFO.getFileObject("simple2");
90         Project _simple2 = ProjectManager.getDefault().findProject(projdir);
91         assertNotNull("have a project", _simple2);
92         simple2 = (FreeformProject) _simple2;
93         projdir = egdirFO.getFileObject("extsrcroot/proj");
94         assertNotNull("found projdir", projdir);
95         Project _extsrcroot = ProjectManager.getDefault().findProject(projdir);
96         assertNotNull("have a project", _extsrcroot);
97         extsrcroot = (FreeformProject) _extsrcroot;
98         projdir = egdirFO.getFileObject("extbuildroot/proj");
99         assertNotNull("found projdir", projdir);
100         Project _extbuildroot = ProjectManager.getDefault().findProject(projdir);
101         assertNotNull("have a project", _extbuildroot);
102         extbuildroot = (FreeformProject) _extbuildroot;
103         projdir = egdirFO.getFileObject("extbuildscript");
104         assertNotNull("found projdir", projdir);
105         Project _extbuildscript = ProjectManager.getDefault().findProject(projdir);
106         assertNotNull("have a project", _extbuildscript);
107         extbuildscript = (FreeformProject) _extbuildscript;
108         datadir = FileUtil.normalizeFile(getDataDir());
109         assertTrue("data dir exists", datadir.exists());
110     }
111     
112     /** ChangeListener for tests. */
113     protected static final class TestCL implements ChangeListener JavaDoc {
114         private int changed = 0;
115         public TestCL() {}
116         public synchronized void stateChanged(ChangeEvent JavaDoc changeEvent) {
117             changed++;
118         }
119         /** Return count of change events since last call. Resets count. */
120         public synchronized int changeCount() {
121             int x = changed;
122             changed = 0;
123             return x;
124         }
125     }
126     
127     /**
128      * Make a temporary copy of a whole folder into some new dir in the scratch area.
129      */

130     protected File JavaDoc copyFolder(File JavaDoc d) throws IOException JavaDoc {
131         assert d.isDirectory();
132         File JavaDoc workdir = getWorkDir();
133         String JavaDoc name = d.getName();
134         while (name.length() < 3) {
135             name = name + "x";
136         }
137         File JavaDoc todir = workdir.createTempFile(name, null, workdir);
138         todir.delete();
139         doCopy(d, todir);
140         return todir;
141     }
142     
143     private static void doCopy(File JavaDoc from, File JavaDoc to) throws IOException JavaDoc {
144         if (from.isDirectory()) {
145             to.mkdir();
146             String JavaDoc[] kids = from.list();
147             for (int i = 0; i < kids.length; i++) {
148                 doCopy(new File JavaDoc(from, kids[i]), new File JavaDoc(to, kids[i]));
149             }
150         } else {
151             assert from.isFile();
152             InputStream JavaDoc is = new FileInputStream JavaDoc(from);
153             try {
154                 OutputStream JavaDoc os = new FileOutputStream JavaDoc(to);
155                 try {
156                     FileUtil.copy(is, os);
157                 } finally {
158                     os.close();
159                 }
160             } finally {
161                 is.close();
162             }
163         }
164     }
165     
166     /**
167      * Make a temporary copy of a project to test dynamic changes.
168      * Note: only copies the main project directory, not any external source roots.
169      * (So don't use it on extsrcroot.)
170      */

171     protected FreeformProject copyProject(FreeformProject p) throws IOException JavaDoc {
172         FileObject dir = p.getProjectDirectory();
173         File JavaDoc newdir = copyFolder(FileUtil.toFile(dir));
174         FileObject newdirFO = FileUtil.toFileObject(newdir);
175         return (FreeformProject) ProjectManager.getDefault().findProject(newdirFO);
176     }
177
178     // XXX copied from AntBasedTestUtil in ant/project
179
protected static final class TestPCL implements PropertyChangeListener JavaDoc {
180         
181         public final Set JavaDoc<String JavaDoc> changed = new HashSet JavaDoc<String JavaDoc>();
182         public final Map JavaDoc<String JavaDoc,String JavaDoc> newvals = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
183         public final Map JavaDoc<String JavaDoc,String JavaDoc> oldvals = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
184         
185         public TestPCL() {}
186         
187         public void reset() {
188             changed.clear();
189             newvals.clear();
190             oldvals.clear();
191         }
192         
193         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
194             String JavaDoc prop = evt.getPropertyName();
195             String JavaDoc nue = (String JavaDoc)evt.getNewValue();
196             String JavaDoc old = (String JavaDoc)evt.getOldValue();
197             changed.add(prop);
198             if (prop != null) {
199                 newvals.put(prop, nue);
200                 oldvals.put(prop, old);
201             } else {
202                 assert nue == null : "null prop name -> null new value";
203                 assert old == null : "null prop name -> null old value";
204             }
205         }
206         
207     }
208     
209 }
210
Popular Tags