KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > suite > BuildZipDistributionTest


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

50 public class BuildZipDistributionTest extends TestBase {
51     
52     static {
53         // #65461: do not try to load ModuleInfo instances from ant module
54
System.setProperty("org.netbeans.core.startup.ModuleSystem.CULPRIT", "true");
55         LayerTestBase.Lkp.setLookup(new Object JavaDoc[0]);
56     }
57     
58     private SuiteProject suite;
59     
60     public BuildZipDistributionTest(String JavaDoc name) {
61         super(name);
62     }
63
64     @Override JavaDoc
65     protected Level JavaDoc logLevel() {
66         return Level.FINE;
67     }
68
69     
70     protected void setUp() throws Exception JavaDoc {
71         clearWorkDir();
72         
73         super.setUp();
74
75         InstalledFileLocatorImpl.registerDestDir(destDirF);
76         
77         suite = TestBase.generateSuite(new File JavaDoc(getWorkDir(), "projects"), "suite");
78         NbModuleProject proj = TestBase.generateSuiteComponent(suite, "mod1");
79         
80         SuiteProjectTest.openSuite(suite);
81         proj.open();
82     }
83     
84     public void testBuildTheZipAppWhenAppNamePropIsNotSet() throws Exception JavaDoc {
85         SuiteActions p = (SuiteActions) suite.getLookup().lookup(ActionProvider.class);
86         assertNotNull("Provider is here", p);
87         
88         List JavaDoc l = Arrays.asList(p.getSupportedActions());
89         assertTrue("We support build-zip: " + l, l.contains("build-zip"));
90         
91         DialogDisplayerImpl.returnFromNotify(DialogDescriptor.NO_OPTION);
92         ExecutorTask task = p.invokeActionImpl("build-zip", suite.getLookup());
93         assertNull("did not even run task", task);
94     }
95     
96     public void testBuildTheZipAppWhenAppNamePropIsSet() throws Exception JavaDoc {
97         EditableProperties ep = suite.getHelper().getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
98         ep.setProperty("app.name", "fakeapp");
99         
100         ep.setProperty("enabled.clusters", TestBase.CLUSTER_PLATFORM);
101         ep.setProperty("disabled.modules", "org.netbeans.modules.autoupdate," +
102             "org.openide.compat," +
103             "org.netbeans.api.progress," +
104             "org.netbeans.core.multiview," +
105             "org.openide.util.enumerations" +
106             "");
107         suite.getHelper().putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
108         ProjectManager.getDefault().saveProject(suite);
109         
110         SuiteActions p = (SuiteActions)suite.getLookup().lookup(ActionProvider.class);
111         assertNotNull("Provider is here", p);
112         
113         List JavaDoc l = Arrays.asList(p.getSupportedActions());
114         assertTrue("We support build-zip: " + l, l.contains("build-zip"));
115         
116         DialogDisplayerImpl.returnFromNotify(DialogDescriptor.NO_OPTION);
117         ExecutorTask task = p.invokeActionImpl("build-zip", suite.getLookup());
118         
119         assertNotNull("Task was started", task);
120         assertEquals("Finished ok", 0, task.result());
121         
122         FileObject[] arr = suite.getProjectDirectory().getChildren();
123         List JavaDoc subobj = new ArrayList JavaDoc (Arrays.asList(arr));
124         subobj.remove(suite.getProjectDirectory().getFileObject("mod1"));
125         subobj.remove(suite.getProjectDirectory().getFileObject("nbproject"));
126         subobj.remove(suite.getProjectDirectory().getFileObject("build.xml"));
127         subobj.remove(suite.getProjectDirectory().getFileObject("build"));
128         FileObject dist = suite.getProjectDirectory().getFileObject("dist");
129         assertNotNull("dist created", dist);
130         subobj.remove(dist);
131         
132         if (!subobj.isEmpty()) {
133             fail("There should be no created directories in the suite dir: " + subobj);
134         }
135         
136         FileObject zip = dist.getFileObject("fakeapp.zip");
137         assertNotNull("ZIP file created: " + zip, zip);
138         
139         File JavaDoc zipF = FileUtil.toFile(zip);
140         JarFile JavaDoc zipJ = new JarFile JavaDoc(zipF);
141         Enumeration JavaDoc en = zipJ.entries();
142         int cntzip = 0;
143         
144         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
145         StringBuffer JavaDoc hidden = new StringBuffer JavaDoc();
146         while (en.hasMoreElements()) {
147             JarEntry JavaDoc entry = (JarEntry JavaDoc)en.nextElement();
148             sb.append("\n");
149             sb.append(entry.getName());
150             cntzip++;
151             
152             if (entry.getName().endsWith("_hidden")) {
153                 hidden.append("\n");
154                 hidden.append(entry.getName());
155             }
156         }
157         
158         if (cntzip == 0) {
159             fail("There should be at least one zip entry: " + sb);
160         }
161         
162         if (hidden.length() != 0) {
163             fail("There should be no hidden files in the zip file: " + hidden);
164         }
165     }
166     
167     private File JavaDoc createNewJarFile (String JavaDoc prefix) throws IOException JavaDoc {
168         if (prefix == null) {
169             prefix = "modules";
170         }
171         
172         File JavaDoc dir = new File JavaDoc(this.getWorkDir(), prefix);
173         dir.mkdirs();
174         
175         int i = 0;
176         for (;;) {
177             File JavaDoc f = new File JavaDoc (dir, i++ + ".jar");
178             if (!f.exists ()) {
179                 f.createNewFile();
180                 return f;
181             }
182         }
183     }
184 }
185
Popular Tags