1 19 20 package org.netbeans.modules.j2ee.ejbfreeform; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.OutputStream ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Collections ; 28 import java.util.HashSet ; 29 import java.util.List ; 30 import java.util.Set ; 31 import java.util.jar.JarEntry ; 32 import java.util.jar.JarOutputStream ; 33 import javax.xml.parsers.SAXParser ; 34 import javax.xml.parsers.SAXParserFactory ; 35 import org.netbeans.api.project.Project; 36 import org.netbeans.api.project.ProjectInformation; 37 import org.netbeans.api.project.ProjectManager; 38 import org.netbeans.api.project.ProjectUtils; 39 import org.netbeans.modules.ant.freeform.spi.support.Util; 40 import org.netbeans.modules.j2ee.api.ejbjar.EjbJar; 41 import org.netbeans.modules.java.freeform.JavaProjectGenerator; 42 import org.netbeans.spi.project.support.ant.AntProjectHelper; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.FileUtil; 45 import org.netbeans.modules.ant.freeform.FreeformProjectGenerator; 46 import org.netbeans.modules.project.ant.AntBasedProjectFactorySingleton; 47 import org.netbeans.spi.project.AuxiliaryConfiguration; 48 import org.w3c.dom.Element ; 49 import org.xml.sax.SAXException ; 50 import org.xml.sax.SAXParseException ; 51 import org.xml.sax.helpers.DefaultHandler ; 52 53 58 public class EJBProjectGeneratorTest extends TestBase { 59 60 private File lib1; 61 private File lib2; 62 private File src; 63 private File test; 64 private File conf; 65 66 protected void setUp() throws Exception { 67 super.setUp(); 68 } 69 70 public EJBProjectGeneratorTest(java.lang.String testName) { 71 super(testName); 72 } 73 74 private AntProjectHelper createEmptyProject(String projectFolder, String projectName, boolean notSoEmpty) throws Exception { 75 File base = new File (getWorkDir(), projectFolder); 76 base.mkdir(); 77 File antScript = new File (base, "build.xml"); 78 antScript.createNewFile(); 79 src = new File (base, "src"); 80 src.mkdir(); 81 conf = new File (base, "conf"); 82 conf.mkdir(); 83 new File (conf, "ejb-jar.xml").createNewFile(); 84 test = new File (base, "test"); 85 test.mkdir(); 86 File libs = new File (base, "libs"); 87 libs.mkdir(); 88 lib1 = new File (libs, "some.jar"); 89 createRealJarFile(lib1); 90 lib2 = new File (libs, "some2.jar"); 91 createRealJarFile(lib2); 92 EJBProjectGenerator.EJBModule ejbMod = new EJBProjectGenerator.EJBModule(); 93 ejbMod.j2eeSpecLevel = "1.4"; 94 ejbMod.classpath = ""; 95 ejbMod.configFiles = conf.getAbsolutePath(); 96 List mods = new ArrayList (); 97 mods.add(ejbMod); 98 AntProjectHelper helper = FreeformProjectGenerator.createProject(base, base, projectName, null); 99 EJBProjectGenerator.putEJBModules(helper, Util.getAuxiliaryConfiguration(helper), mods); 100 EJBProjectGenerator.putServerID(helper, "GENERIC"); 101 102 putSrcRoot(helper); 104 List l = new ArrayList (); 105 String s = conf.getAbsolutePath(); 106 l.add(s); 107 l.add(conf.getName()); 108 EJBProjectGenerator.putEJBSourceFolder(helper, l); 109 EJBProjectGenerator.putEJBNodeView(helper, l); 110 111 if (notSoEmpty) { 112 ArrayList sources = new ArrayList (); 113 JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder(); 114 sf.label = "src"; 115 sf.type = "java"; 116 sf.style = "packages"; 117 sf.location = src.getAbsolutePath(); 118 sources.add(sf); 119 sf = new JavaProjectGenerator.SourceFolder(); 120 sf.label = "test"; 121 sf.type = "java"; 122 sf.style = "packages"; 123 sf.location = test.getAbsolutePath(); 124 sources.add(sf); 125 JavaProjectGenerator.putSourceFolders(helper, sources, "java"); 126 JavaProjectGenerator.putSourceViews(helper, sources, "packages"); 127 128 ArrayList compUnits = new ArrayList (); 129 JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit(); 130 JavaProjectGenerator.JavaCompilationUnit.CP cp = new JavaProjectGenerator.JavaCompilationUnit.CP(); 131 cp.classpath = lib1.getAbsolutePath(); 132 cp.mode = "compile"; 133 cu.classpath = Collections.singletonList(cp); 134 cu.sourceLevel = "1.4"; 135 cu.packageRoots = Collections.singletonList(src.getAbsolutePath()); 136 compUnits.add(cu); 137 cu = new JavaProjectGenerator.JavaCompilationUnit(); 138 cp = new JavaProjectGenerator.JavaCompilationUnit.CP(); 139 cp.classpath = lib2.getAbsolutePath(); 140 cp.mode = "compile"; 141 cu.classpath = Collections.singletonList(cp); 142 cu.sourceLevel = "1.4"; 143 cu.packageRoots = Collections.singletonList(test.getAbsolutePath()); 144 cu.isTests = true; 145 compUnits.add(cu); 146 JavaProjectGenerator.putJavaCompilationUnits(helper, Util.getAuxiliaryConfiguration(helper), compUnits); 147 } 148 149 return helper; 150 } 151 152 public void testEJBModules() throws Exception { 153 AntProjectHelper helper = createEmptyProject("proj1", "proj-1", false); 154 FileObject base = helper.getProjectDirectory(); 155 Project p = ProjectManager.getDefault().findProject(base); 156 assertNotNull("Project was not created", p); 157 validate(p); 159 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 160 ProjectInformation pi = ProjectUtils.getInformation(p); 161 assertEquals("Project name was not set", "proj-1", pi.getName()); 162 EjbJar e = EjbJar.getEjbJar(FileUtil.toFileObject(conf)); 163 assertNotNull("EjbJar not found", e); 164 assertEquals("Incorrect ejb-jar.xml.", FileUtil.toFileObject(new File (conf, "ejb-jar.xml")), 165 e.getDeploymentDescriptor()); 166 assertEquals("Incorrect J2EE spec. version.", "1.4", e.getJ2eePlatformVersion()); 167 Element ejb = ((AuxiliaryConfiguration) p.getLookup().lookup(AuxiliaryConfiguration.class)) 168 .getConfigurationFragment("ejb-data", EJBProjectNature.NS_EJB, true); 169 assertNotNull(ejb); 170 List ejbModules = Util.findSubElements(ejb); 171 assertEquals("One ejb-module element should be found.", 1, ejbModules.size()); 172 Element module = (Element ) ejbModules.get(0); 173 assertEquals("Classpath element is not empty.", "" , Util.findText(Util.findElement(module, "classpath", EJBProjectNature.NS_EJB))); 174 } 175 176 public void testSourceFolders() throws Exception { 177 AntProjectHelper helper = createEmptyProject("proj-2", "proj-2", true); 178 FileObject base = helper.getProjectDirectory(); 179 Project p = ProjectManager.getDefault().findProject(base); 180 assertNotNull("Project was not created", p); 182 validate(p); 183 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 184 ProjectInformation pi = ProjectUtils.getInformation(p); 185 assertEquals("Project name was not set", "proj-2", pi.getName()); 186 EjbJar e = EjbJar.getEjbJar(FileUtil.toFileObject(conf)); 187 assertNotNull("EjbJar not found", e); 188 Set srcRoots = new HashSet (); 189 srcRoots.add(FileUtil.toFileObject(src)); 190 Set testRoots = new HashSet (); 191 testRoots.add(FileUtil.toFileObject(test)); 192 FileObject[] fos = e.getJavaSources(); 195 Set s = new HashSet (); 196 for (int i = 0; i < fos.length; i++) { 197 s.add(fos[i]); 198 } 199 assertTrue("There are missing java src roots", s.containsAll(srcRoots)); 200 } 201 202 public void createRealJarFile(File f) throws Exception { 204 OutputStream os = new FileOutputStream (f); 205 try { 206 JarOutputStream jos = new JarOutputStream (os); 207 JarEntry entry = new JarEntry ("foo.txt"); 208 jos.putNextEntry(entry); 209 jos.flush(); 210 jos.close(); 211 } finally { 212 os.close(); 213 } 214 } 215 216 public static void validate(Project proj) throws Exception { 217 File projF = FileUtil.toFile(proj.getProjectDirectory()); 218 File xml = new File (new File (projF, "nbproject"), "project.xml"); 219 SAXParserFactory f = (SAXParserFactory )Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance(); 220 if (f == null) { 221 System.err.println("Validation skipped because org.apache.xerces.jaxp.SAXParserFactoryImpl was not found on classpath"); 222 return; 223 } 224 f.setNamespaceAware(true); 225 f.setValidating(true); 226 SAXParser p = f.newSAXParser(); 227 p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 228 "http://www.w3.org/2001/XMLSchema"); 229 p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", getSchemas()); 230 try { 231 p.parse(xml.toURI().toString(), new Handler ()); 232 } catch (SAXParseException e) { 233 assertTrue("Validation of XML document "+xml+" against schema failed. Details: "+ 234 e.getSystemId() + ":" + e.getLineNumber() + ": " + e.getLocalizedMessage(), false); 235 } 236 } 237 238 private static String [] getSchemas() throws Exception { 239 return new String [] { 240 FreeformProjectGenerator.class.getResource("resources/freeform-project-general.xsd").toExternalForm(), 241 JavaProjectGenerator.class.getResource("resources/freeform-project-java.xsd").toExternalForm(), 242 JavaProjectGenerator.class.getResource("resources/freeform-project-java-2.xsd").toExternalForm(), 243 EJBProjectGenerator.class.getResource("resources/freeform-project-ejb.xsd").toExternalForm(), 244 AntBasedProjectFactorySingleton.class.getResource("project.xsd").toExternalForm(), 245 }; 246 } 247 248 private static final class Handler extends DefaultHandler { 249 public void warning(SAXParseException e) throws SAXException { 250 throw e; 251 } 252 public void error(SAXParseException e) throws SAXException { 253 throw e; 254 } 255 public void fatalError(SAXParseException e) throws SAXException { 256 throw e; 257 } 258 } 259 260 private void putSrcRoot(AntProjectHelper helper) { 262 JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder(); 263 sf.label = "src"; 264 sf.type = "java"; 265 sf.style = "packages"; 266 sf.location = src.getAbsolutePath(); 267 ArrayList sources = new ArrayList (); 268 sources.add(sf); 269 JavaProjectGenerator.putSourceFolders(helper, sources, null); 270 JavaProjectGenerator.putSourceViews(helper, sources, null); 271 ArrayList compUnits = new ArrayList (); 272 JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit(); 273 JavaProjectGenerator.JavaCompilationUnit.CP cp = new JavaProjectGenerator.JavaCompilationUnit.CP(); 274 cp.classpath = lib1.getAbsolutePath(); 275 cp.mode = "compile"; 276 cu.classpath = Collections.singletonList(cp); 277 cu.sourceLevel = "1.4"; 278 cu.packageRoots = Collections.singletonList(src.getAbsolutePath()); 279 compUnits.add(cu); 280 JavaProjectGenerator.putJavaCompilationUnits(helper, Util.getAuxiliaryConfiguration(helper), compUnits); 281 } 282 283 } 284 | Popular Tags |