1 19 20 package org.netbeans.modules.web.freeform; 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.List ; 27 import java.util.jar.JarEntry ; 28 import java.util.jar.JarOutputStream ; 29 import org.netbeans.api.project.Project; 30 import org.netbeans.api.project.ProjectManager; 31 import org.netbeans.junit.NbTestCase; 32 import org.netbeans.modules.ant.freeform.FreeformProjectGenerator; 33 import org.netbeans.modules.ant.freeform.spi.support.Util; 34 import org.netbeans.modules.web.api.webmodule.WebModule; 35 import org.netbeans.spi.project.AuxiliaryConfiguration; 36 import org.netbeans.spi.project.support.ant.AntProjectHelper; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.modules.ModuleInfo; 40 import org.openide.util.Lookup; 41 42 47 public class WebProjectGeneratorTest extends NbTestCase { 48 49 private File lib1; 50 private File lib2; 51 private File src; 52 private File web; 53 private File doc; 54 private File buildClasses; 55 56 public WebProjectGeneratorTest(String testName) { 57 super(testName); 58 } 59 60 protected void setUp() throws Exception { 61 super.setUp(); 62 Lookup.getDefault().lookup(ModuleInfo.class); 63 } 64 65 private AntProjectHelper createEmptyProject(String projectFolder, String projectName) throws Exception { 66 File base = new File (getWorkDir(), projectFolder); 67 base.mkdir(); 68 File antScript = new File (base, "build.xml"); 69 antScript.createNewFile(); 70 src = new File (base, "src"); 71 src.mkdir(); 72 web = new File (base, "web"); 73 web.mkdir(); 74 doc = new File (base, "doc"); 75 doc.mkdir(); 76 buildClasses = new File (base, "buildClasses"); 77 buildClasses.mkdir(); 78 File libs = new File (base, "libs"); 79 libs.mkdir(); 80 lib1 = new File (libs, "some.jar"); 81 createRealJarFile(lib1); 82 lib2 = new File (libs, "some2.jar"); 83 createRealJarFile(lib2); 84 85 ArrayList webModules = new ArrayList (); 86 WebProjectGenerator.WebModule wm = new WebProjectGenerator.WebModule (); 87 wm.docRoot = web.getAbsolutePath(); 88 wm.contextPath = "/context"; 89 wm.j2eeSpecLevel = WebModule.J2EE_14_LEVEL; 90 wm.classpath = base.getAbsolutePath() + "/buildClasses:" + lib1.getAbsolutePath(); 91 webModules.add (wm); 92 93 AntProjectHelper helper = FreeformProjectGenerator.createProject(base, base, projectName, null); 94 WebProjectGenerator.putWebModules(helper, Util.getAuxiliaryConfiguration(helper), webModules); 95 return helper; 96 } 97 98 public void testWebModules () throws Exception { 99 clearWorkDir(); 100 AntProjectHelper helper = createEmptyProject("proj2", "proj-2"); 101 FileObject base = helper.getProjectDirectory(); 102 Project p = ProjectManager.getDefault().findProject(base); 103 assertNotNull("Project was not created", p); 104 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 105 106 WebModule wm = WebModule.getWebModule(FileUtil.toFileObject(web)); 107 assertNotNull("WebModule not found", wm); 108 assertEquals("correct document base", FileUtil.toFileObject(web), wm.getDocumentBase()); 109 assertEquals("correct j2ee version", WebModule.J2EE_14_LEVEL, wm.getJ2eePlatformVersion()); 110 assertEquals("correct context path", "/context", wm.getContextPath()); 111 WebModule wm2 = WebModule.getWebModule(FileUtil.toFileObject (src)); 112 } 115 116 public void test2WebModules () throws Exception { 117 clearWorkDir(); 118 AntProjectHelper helper = createEmptyProject("proj6", "proj-6"); 119 FileObject base = helper.getProjectDirectory(); 120 Project p = ProjectManager.getDefault().findProject(base); 121 assertNotNull("Project was not created", p); 122 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 123 124 File src2 = FileUtil.toFile (base.createFolder("src2")); 125 File web2 = FileUtil.toFile (base.createFolder("web2")); 126 127 AuxiliaryConfiguration aux = Util.getAuxiliaryConfiguration(helper); 128 List webModules = WebProjectGenerator.getWebmodules(helper, aux); 129 WebProjectGenerator.WebModule wm = new WebProjectGenerator.WebModule (); 130 wm.docRoot = web2.getAbsolutePath(); 131 wm.contextPath = "/context2"; 132 wm.j2eeSpecLevel = WebModule.J2EE_13_LEVEL; 133 wm.classpath = FileUtil.toFile (base).getAbsolutePath() + "/buildClasses2:" + lib2.getAbsolutePath(); 134 webModules.add (wm); 135 WebProjectGenerator.putWebModules(helper, aux, webModules); 136 ProjectManager.getDefault().saveProject(p); 137 138 WebModule webModule = WebModule.getWebModule(base.getFileObject("web2")); 139 assertNotNull("WebModule not found", webModule); 140 assertEquals("correct document base", base.getFileObject("web2"), webModule.getDocumentBase()); 141 WebModule webModule2 = WebModule.getWebModule(base.getFileObject("src2")); 142 } 145 146 public void createRealJarFile(File f) throws Exception { 148 OutputStream os = new FileOutputStream (f); 149 try { 150 JarOutputStream jos = new JarOutputStream (os); 151 JarEntry entry = new JarEntry ("foo.txt"); 152 jos.putNextEntry(entry); 153 jos.flush(); 154 jos.close(); 155 } finally { 156 os.close(); 157 } 158 } 159 160 } 161 | Popular Tags |