1 19 20 package org.netbeans.modules.ant.freeform; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.OutputStream ; 30 import java.util.HashMap ; 31 import java.util.HashSet ; 32 import java.util.Map ; 33 import java.util.Set ; 34 import javax.swing.event.ChangeEvent ; 35 import javax.swing.event.ChangeListener ; 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 48 public abstract class TestBase extends NbTestCase { 49 50 static { 51 TestBase.class.getClassLoader().setDefaultAssertionStatus(true); 52 } 53 54 protected TestBase(String name) { 55 super(name); 56 } 57 58 protected File egdir; 59 protected File 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 { 71 super.setUp(); 72 Lookup.getDefault().lookup(ModuleInfo.class); 73 74 egdir = FileUtil.normalizeFile(new File (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 113 protected static final class TestCL implements ChangeListener { 114 private int changed = 0; 115 public TestCL() {} 116 public synchronized void stateChanged(ChangeEvent changeEvent) { 117 changed++; 118 } 119 120 public synchronized int changeCount() { 121 int x = changed; 122 changed = 0; 123 return x; 124 } 125 } 126 127 130 protected File copyFolder(File d) throws IOException { 131 assert d.isDirectory(); 132 File workdir = getWorkDir(); 133 String name = d.getName(); 134 while (name.length() < 3) { 135 name = name + "x"; 136 } 137 File todir = workdir.createTempFile(name, null, workdir); 138 todir.delete(); 139 doCopy(d, todir); 140 return todir; 141 } 142 143 private static void doCopy(File from, File to) throws IOException { 144 if (from.isDirectory()) { 145 to.mkdir(); 146 String [] kids = from.list(); 147 for (int i = 0; i < kids.length; i++) { 148 doCopy(new File (from, kids[i]), new File (to, kids[i])); 149 } 150 } else { 151 assert from.isFile(); 152 InputStream is = new FileInputStream (from); 153 try { 154 OutputStream os = new FileOutputStream (to); 155 try { 156 FileUtil.copy(is, os); 157 } finally { 158 os.close(); 159 } 160 } finally { 161 is.close(); 162 } 163 } 164 } 165 166 171 protected FreeformProject copyProject(FreeformProject p) throws IOException { 172 FileObject dir = p.getProjectDirectory(); 173 File newdir = copyFolder(FileUtil.toFile(dir)); 174 FileObject newdirFO = FileUtil.toFileObject(newdir); 175 return (FreeformProject) ProjectManager.getDefault().findProject(newdirFO); 176 } 177 178 protected static final class TestPCL implements PropertyChangeListener { 180 181 public final Set <String > changed = new HashSet <String >(); 182 public final Map <String ,String > newvals = new HashMap <String ,String >(); 183 public final Map <String ,String > oldvals = new HashMap <String ,String >(); 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 evt) { 194 String prop = evt.getPropertyName(); 195 String nue = (String )evt.getNewValue(); 196 String old = (String )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 |