KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > jnlp > GenerateJNLPApplicationTest


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.apisupport.project.jnlp;
21
22 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.jar.JarEntry JavaDoc;
32 import java.util.jar.JarFile JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import org.netbeans.api.project.ProjectManager;
35 import org.netbeans.modules.apisupport.project.DialogDisplayerImpl;
36 import org.netbeans.modules.apisupport.project.InstalledFileLocatorImpl;
37 import org.netbeans.modules.apisupport.project.NbModuleProject;
38 import org.netbeans.modules.apisupport.project.TestBase;
39 import org.netbeans.modules.apisupport.project.layers.LayerTestBase;
40 import org.netbeans.modules.apisupport.project.suite.SuiteProject;
41 import org.netbeans.modules.apisupport.project.suite.SuiteProjectTest;
42 import org.netbeans.modules.apisupport.project.ui.SuiteActions;
43 import org.netbeans.spi.project.ActionProvider;
44 import org.netbeans.spi.project.support.ant.AntProjectHelper;
45 import org.netbeans.spi.project.support.ant.EditableProperties;
46 import org.openide.DialogDescriptor;
47 import org.openide.ErrorManager;
48 import org.openide.execution.ExecutorTask;
49 import org.openide.filesystems.FileLock;
50 import org.openide.filesystems.FileObject;
51 import org.openide.filesystems.FileUtil;
52
53 /**
54  * Checks JNLP support behaviour.
55  * @author Jaroslav Tulach
56  */

57 public class GenerateJNLPApplicationTest extends TestBase {
58     
59     static {
60         // #65461: do not try to load ModuleInfo instances from ant module
61
System.setProperty("org.netbeans.core.startup.ModuleSystem.CULPRIT", "true");
62         LayerTestBase.Lkp.setLookup(new Object JavaDoc[0]);
63     }
64     
65     private ErrorManager err;
66     private SuiteProject suite;
67     
68     public GenerateJNLPApplicationTest(String JavaDoc name) {
69         super(name);
70     }
71     
72     @Override JavaDoc
73     protected Level JavaDoc logLevel() {
74         return Level.CONFIG;
75     }
76
77
78     protected void setUp() throws Exception JavaDoc {
79         super.setUp();
80
81         InstalledFileLocatorImpl.registerDestDir(destDirF);
82         
83         suite = TestBase.generateSuite(new File JavaDoc(getWorkDir(), "projects"), "suite");
84         NbModuleProject proj = TestBase.generateSuiteComponent(suite, "mod1");
85         
86         SuiteProjectTest.openSuite(suite);
87         proj.open();
88         
89         err = ErrorManager.getDefault().getInstance("TEST-" + getName());
90     }
91     
92     public void testBuildTheJNLPAppWhenAppNamePropIsNotSet() throws Exception JavaDoc {
93         SuiteActions p = (SuiteActions) suite.getLookup().lookup(ActionProvider.class);
94         assertNotNull("Provider is here");
95         
96         List JavaDoc l = Arrays.asList(p.getSupportedActions());
97         assertTrue("We support build-jnlp: " + l, l.contains("build-jnlp"));
98         
99         DialogDisplayerImpl.returnFromNotify(DialogDescriptor.NO_OPTION);
100         ExecutorTask task = p.invokeActionImpl("build-jnlp", suite.getLookup());
101         assertNull("did not even run task", task);
102     }
103     
104     public void testBuildTheJNLPAppWhenAppNamePropIsSet() throws Exception JavaDoc {
105         EditableProperties ep = suite.getHelper().getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
106         ep.setProperty("app.name", "fakeapp");
107         
108         File JavaDoc someJar = createNewJarFile("fake-jnlp-servlet");
109         
110         ep.setProperty("enabled.clusters", TestBase.CLUSTER_PLATFORM);
111         ep.setProperty("disabled.modules", "org.netbeans.modules.autoupdate," +
112             "org.openide.compat," +
113             "org.netbeans.api.progress," +
114             "org.netbeans.core.multiview," +
115             "org.openide.filesystems," +
116             "org.openide.modules," +
117             "org.openide.util," +
118             "org.netbeans.core.execution," +
119             "org.netbeans.core.output2," +
120             "org.netbeans.core.ui," +
121             "org.netbeans.core.windows," +
122             "org.netbeans.core," +
123             "org.netbeans.modules.favorites," +
124             "org.netbeans.modules.javahelp," +
125             "org.netbeans.modules.masterfs," +
126             "org.netbeans.modules.queries," +
127             "org.netbeans.modules.settings," +
128             "org.netbeans.swing.plaf," +
129             "org.netbeans.swing.tabcontrol," +
130             "org.openide.actions," +
131             "org.openide.awt," +
132             "org.openide.dialogs," +
133             "org.openide.execution," +
134             "org.openide.explorer," +
135             "org.openide.io," +
136             "org.openide.loaders," +
137             "org.openide.nodes," +
138             "org.openide.options," +
139             "org.openide.text," +
140             "org.openide.windows," +
141             "org.openide.util.enumerations" +
142             "");
143         ep.setProperty("jnlp.servlet.jar", someJar.toString());
144         suite.getHelper().putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
145         ProjectManager.getDefault().saveProject(suite);
146         err.log("Properties stored");
147         
148         SuiteActions p = (SuiteActions)suite.getLookup().lookup(ActionProvider.class);
149         assertNotNull("Provider is here");
150         
151         List JavaDoc l = Arrays.asList(p.getSupportedActions());
152         assertTrue("We support build-jnlp: " + l, l.contains("build-jnlp"));
153         
154         DialogDisplayerImpl.returnFromNotify(null);
155         err.log("invoking build-jnlp");
156         ExecutorTask task = p.invokeActionImpl("build-jnlp", suite.getLookup());
157         err.log("Invocation started");
158         
159         assertNotNull("Task was started", task);
160         err.log("Waiting for task to finish");
161         task.waitFinished();
162         err.log("Checking the result");
163         assertEquals("Finished ok", 0, task.result());
164         err.log("Testing the content of the directory");
165         
166         FileObject[] arr = suite.getProjectDirectory().getChildren();
167         List JavaDoc subobj = new ArrayList JavaDoc (Arrays.asList(arr));
168         subobj.remove(suite.getProjectDirectory().getFileObject("mod1"));
169         subobj.remove(suite.getProjectDirectory().getFileObject("nbproject"));
170         subobj.remove(suite.getProjectDirectory().getFileObject("build.xml"));
171         FileObject master = suite.getProjectDirectory().getFileObject("master.jnlp");
172         assertNotNull("Master must be created", master);
173         subobj.remove(master);
174         subobj.remove(suite.getProjectDirectory().getFileObject("build"));
175         FileObject dist = suite.getProjectDirectory().getFileObject("dist");
176         assertNotNull("dist created", dist);
177         subobj.remove(dist);
178         
179         if (!subobj.isEmpty()) {
180             fail("There should be no created directories in the suite dir: " + subobj);
181         }
182         
183         FileObject war = dist.getFileObject("fakeapp.war");
184         assertNotNull("War file created: " + war, war);
185         
186         File JavaDoc warF = FileUtil.toFile(war);
187         JarFile JavaDoc warJ = new JarFile JavaDoc(warF);
188         Enumeration JavaDoc en = warJ.entries();
189         int cntJnlp = 0;
190         while (en.hasMoreElements()) {
191             JarEntry JavaDoc entry = (JarEntry JavaDoc)en.nextElement();
192             if (!entry.getName().endsWith(".jnlp")) {
193                 continue;
194             }
195             cntJnlp++;
196
197             byte[] data = new byte[(int)entry.getSize()];
198             int len = 0;
199             InputStream JavaDoc is = warJ.getInputStream(entry);
200             for(int pos = 0; pos < data.length; ) {
201                 int r = is.read(data, pos, data.length - pos);
202                 pos += r;
203                 len += r;
204             }
205             is.close();
206             assertEquals("Correct data read: " + entry, data.length, len);
207             
208             String JavaDoc s = new String JavaDoc(data);
209             if (s.indexOf(getWorkDir().getName()) >= 0) {
210                 fail("Name of work dir in a file, means that there is very likely local reference to a file: " + entry + "\n" + s);
211             }
212         }
213         
214         if (cntJnlp == 0) {
215             fail("There should be at least one jnlp entry");
216         }
217     }
218     
219     public void testItIsPossibleToGenerateStaticRepository() throws Exception JavaDoc {
220         EditableProperties ep = suite.getHelper().getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
221         ep.setProperty("app.name", "fakeapp");
222         
223         ep.setProperty("enabled.clusters", TestBase.CLUSTER_PLATFORM);
224         ep.setProperty("disabled.modules", "org.netbeans.modules.autoupdate," +
225             "org.openide.compat," +
226             "org.netbeans.api.progress," +
227             "org.netbeans.core.multiview," +
228             "org.openide.filesystems," +
229             "org.openide.modules," +
230             "org.openide.util," +
231             "org.netbeans.core.execution," +
232             "org.netbeans.core.output2," +
233             "org.netbeans.core.ui," +
234             "org.netbeans.core.windows," +
235             "org.netbeans.core," +
236             "org.netbeans.modules.favorites," +
237             "org.netbeans.modules.javahelp," +
238             "org.netbeans.modules.masterfs," +
239             "org.netbeans.modules.queries," +
240             "org.netbeans.modules.settings," +
241             "org.netbeans.swing.plaf," +
242             "org.netbeans.swing.tabcontrol," +
243             "org.openide.actions," +
244             "org.openide.awt," +
245             "org.openide.dialogs," +
246             "org.openide.execution," +
247             "org.openide.explorer," +
248             "org.openide.io," +
249             "org.openide.loaders," +
250             "org.openide.nodes," +
251             "org.openide.options," +
252             "org.openide.text," +
253             "org.openide.windows," +
254             "org.openide.util.enumerations" +
255             "");
256         suite.getHelper().putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
257         ProjectManager.getDefault().saveProject(suite);
258         err.log("Properties stored");
259         
260         FileObject fo = suite.getProjectDirectory().getFileObject("build.xml");
261         assertNotNull("There should be a build script", fo);
262         
263         {
264             String JavaDoc s = readFile(fo);
265             int insert = s.indexOf("</project>");
266             if (insert == -1) {
267                 fail("there should be insert place: " + s);
268             }
269
270             s = s.substring(0, insert) +
271                 " <target name='build-jnlp' depends='build,-jdk-init' description='Builds a static JNLP version of the app.' >" +
272                 " <ant antfile='${harness.dir}/jnlp.xml' target='build-jnlp-nowar'>" +
273                 " <property name='jnlp.codebase' value='http://www.netbeans.org/download/samples/jnlp/htmleditor/' />" +
274                 " </ant>" +
275                 "</target>"
276                 + s.substring(insert);
277             FileLock lock = fo.lock();
278             OutputStream JavaDoc os = fo.getOutputStream(lock);
279             try {
280                 os.write(s.getBytes());
281             } finally {
282                 os.close();
283                 lock.releaseLock();
284             }
285         }
286         
287         SuiteActions p = (SuiteActions)suite.getLookup().lookup(ActionProvider.class);
288         assertNotNull("Provider is here");
289         
290         List JavaDoc l = Arrays.asList(p.getSupportedActions());
291         assertTrue("We support build-jnlp: " + l, l.contains("build-jnlp"));
292         
293         DialogDisplayerImpl.returnFromNotify(null);
294         err.log("invoking build-jnlp");
295         ExecutorTask task = p.invokeActionImpl("build-jnlp", suite.getLookup());
296         err.log("Invocation started");
297         
298         assertNotNull("Task was started", task);
299         err.log("Waiting for task to finish");
300         task.waitFinished();
301         err.log("Checking the result");
302         assertEquals("Finished ok", 0, task.result());
303         err.log("Testing the content of the directory");
304         
305         FileObject[] arr = suite.getProjectDirectory().getChildren();
306         List JavaDoc subobj = new ArrayList JavaDoc (Arrays.asList(arr));
307         subobj.remove(suite.getProjectDirectory().getFileObject("mod1"));
308         subobj.remove(suite.getProjectDirectory().getFileObject("nbproject"));
309         FileObject buildXML = suite.getProjectDirectory().getFileObject("build.xml");
310         subobj.remove(buildXML);
311         FileObject master = suite.getProjectDirectory().getFileObject("master.jnlp");
312         assertNotNull("Master must be created", master);
313         subobj.remove(master);
314         FileObject build = suite.getProjectDirectory().getFileObject("build");
315         subobj.remove(build);
316         
317         {
318             // check content of build
319
FileObject jnlpDir = build.getFileObject("jnlp/app/");
320             assertNotNull("app dir exists", jnlpDir);
321             FileObject[] arrX = jnlpDir.getChildren();
322             int cnt = 0;
323             for (int i = 0; i < arrX.length; i++) {
324                 if (arrX[i].hasExt("jnlp")) {
325                     cnt++;
326                     String JavaDoc jnlpContent = readFile(arrX[i]);
327                     if (jnlpContent.indexOf("http://www.netbeans.org/download/samples/jnlp/htmleditor/app/") == -1) {
328                         fail(" for " + arrX[i] + " URL with /app/ must be present: " + jnlpContent);
329                     }
330                 }
331             }
332
333             if (cnt == 0) fail("At least one jnlp file in app dir");
334         }
335         
336         {
337             // check content of netbeans default dir
338
FileObject jnlpDir = build.getFileObject("jnlp/netbeans/");
339             assertNotNull("netbeans dir exists", jnlpDir);
340             FileObject[] arrX = jnlpDir.getChildren();
341             int cnt = 0;
342             for (int i = 0; i < arrX.length; i++) {
343                 if (arrX[i].hasExt("jnlp")) {
344                     cnt++;
345                     String JavaDoc jnlpContent = readFile(arrX[i]);
346                     if (jnlpContent.indexOf("http://www.netbeans.org/download/samples/jnlp/htmleditor/netbeans/") == -1) {
347                         fail(" for " + arrX[i] + " URL with /netbeans/ must be present: " + jnlpContent);
348                     }
349                 }
350             }
351
352             if (cnt == 0) fail("At least one jnlp file in app dir");
353         }
354         
355         // check master file has it
356
String JavaDoc masterContent = readFile(master);
357         if (masterContent.indexOf("http://www.netbeans.org/download/samples/jnlp/htmleditor/") == -1) {
358             fail("URL must be present in master: " + masterContent);
359         }
360         
361         
362         FileObject dist = suite.getProjectDirectory().getFileObject("dist");
363         assertNull("no dist created", dist);
364
365         
366         if (!subobj.isEmpty()) {
367             fail("There should be no created directories in the suite dir: " + subobj);
368         }
369         
370     }
371
372     private static String JavaDoc readFile(final FileObject fo) throws IOException JavaDoc, FileNotFoundException JavaDoc {
373         // write user modified version of the file
374
byte[] arr = new byte[(int)fo.getSize()];
375         InputStream JavaDoc is = fo.getInputStream();
376         int len = is.read(arr);
377         assertEquals("Read all", arr.length, len);
378         String JavaDoc s = new String JavaDoc(arr);
379         is.close();
380         return s;
381     }
382     
383     private File JavaDoc createNewJarFile (String JavaDoc prefix) throws IOException JavaDoc {
384         if (prefix == null) {
385             prefix = "modules";
386         }
387         
388         File JavaDoc dir = new File JavaDoc(this.getWorkDir(), prefix);
389         dir.mkdirs();
390         
391         int i = 0;
392         for (;;) {
393             File JavaDoc f = new File JavaDoc (dir, i++ + ".jar");
394             if (!f.exists ()) {
395                 f.createNewFile();
396                 return f;
397             }
398         }
399     }
400 }
401
Popular Tags