KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > freeform > WebProjectGeneratorTest


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.web.freeform;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.jar.JarEntry JavaDoc;
28 import java.util.jar.JarOutputStream JavaDoc;
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 /**
43  * Tests for FreeformProjectGenerator.
44  *
45  * @author Pavel Buzek
46  */

47 public class WebProjectGeneratorTest extends NbTestCase {
48
49     private File JavaDoc lib1;
50     private File JavaDoc lib2;
51     private File JavaDoc src;
52     private File JavaDoc web;
53     private File JavaDoc doc;
54     private File JavaDoc buildClasses;
55     
56     public WebProjectGeneratorTest(String JavaDoc testName) {
57         super(testName);
58     }
59     
60         protected void setUp() throws Exception JavaDoc {
61             super.setUp();
62             Lookup.getDefault().lookup(ModuleInfo.class);
63         }
64     
65     private AntProjectHelper createEmptyProject(String JavaDoc projectFolder, String JavaDoc projectName) throws Exception JavaDoc {
66         File JavaDoc base = new File JavaDoc(getWorkDir(), projectFolder);
67         base.mkdir();
68         File JavaDoc antScript = new File JavaDoc(base, "build.xml");
69         antScript.createNewFile();
70         src = new File JavaDoc(base, "src");
71         src.mkdir();
72         web = new File JavaDoc(base, "web");
73         web.mkdir();
74         doc = new File JavaDoc(base, "doc");
75         doc.mkdir();
76         buildClasses = new File JavaDoc(base, "buildClasses");
77         buildClasses.mkdir();
78         File JavaDoc libs = new File JavaDoc(base, "libs");
79         libs.mkdir();
80         lib1 = new File JavaDoc(libs, "some.jar");
81         createRealJarFile(lib1);
82         lib2 = new File JavaDoc(libs, "some2.jar");
83         createRealJarFile(lib2);
84         
85         ArrayList JavaDoc webModules = new ArrayList JavaDoc ();
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 JavaDoc {
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 // assertNotNull("WebModule not found", wm2);
113
// assertEquals("the same wm for web and src folder", wm, wm2);
114
}
115     
116     public void test2WebModules () throws Exception JavaDoc {
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 JavaDoc src2 = FileUtil.toFile (base.createFolder("src2"));
125         File JavaDoc web2 = FileUtil.toFile (base.createFolder("web2"));
126
127         AuxiliaryConfiguration aux = Util.getAuxiliaryConfiguration(helper);
128         List JavaDoc 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 // assertNotNull("WebModule not found", webModule2);
143
// assertEquals("correct document base", webModule, webModule2);
144
}
145     
146     // create real Jar otherwise FileUtil.isArchiveFile returns false for it
147
public void createRealJarFile(File JavaDoc f) throws Exception JavaDoc {
148         OutputStream JavaDoc os = new FileOutputStream JavaDoc(f);
149         try {
150             JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(os);
151             JarEntry JavaDoc entry = new JarEntry JavaDoc("foo.txt");
152             jos.putNextEntry(entry);
153             jos.flush();
154             jos.close();
155         } finally {
156             os.close();
157         }
158     }
159     
160 }
161
Popular Tags