KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > j2ee > libraries > LibraryTest


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 package org.netbeans.test.j2ee.libraries;
20
21 import java.io.BufferedOutputStream JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.jar.JarFile JavaDoc;
31 import junit.framework.Test;
32 import junit.textui.TestRunner;
33 import org.netbeans.api.project.libraries.Library;
34 import org.netbeans.jellytools.EditorOperator;
35 import org.netbeans.jellytools.EditorWindowOperator;
36 import org.netbeans.jellytools.JellyTestCase;
37 import org.netbeans.jellytools.Bundle;
38 import org.netbeans.jellytools.NbDialogOperator;
39 import org.netbeans.jellytools.NewProjectNameLocationStepOperator;
40 import org.netbeans.jellytools.ProjectsTabOperator;
41 import org.netbeans.jellytools.actions.ActionNoBlock;
42 import org.netbeans.jellytools.nodes.Node;
43 import org.netbeans.jemmy.EventTool;
44 import org.netbeans.jemmy.operators.JButtonOperator;
45 import org.netbeans.jemmy.operators.JCheckBoxOperator;
46 import org.netbeans.jemmy.operators.JListOperator;
47 import org.netbeans.jemmy.operators.JTreeOperator;
48 import org.netbeans.junit.NbTestSuite;
49 import org.netbeans.test.j2ee.lib.ContentComparator;
50 import org.netbeans.test.j2ee.lib.FilteringLineDiff;
51 import org.netbeans.test.j2ee.lib.Utils;
52 import org.netbeans.test.j2ee.wizard.NewFileWizardsTest;
53 import org.netbeans.test.j2ee.wizard.WizardUtils;
54
55 /**
56  *
57  * @author jungi
58  */

59 public class LibraryTest extends JellyTestCase {
60     
61     private static boolean CREATE_GOLDEN_FILES = Boolean.getBoolean("org.netbeans.test.j2ee.libraries.golden");
62     //private static boolean CREATE_GOLDEN_FILES = true;
63

64     protected String JavaDoc appName = "LibsInclusionTestApp";
65     protected String JavaDoc libName = "My_Math_Library-1.0";
66     protected String JavaDoc ejbName = "MultiSrcRootEjb";
67     protected String JavaDoc webName = "MultiSrcRootWar";
68     protected ProjectsTabOperator pto = new ProjectsTabOperator();
69     
70     /** Creates a new instance of LibraryTest */
71     public LibraryTest(String JavaDoc s) {
72         super(s);
73     }
74     
75     public static Test suite() {
76         NbTestSuite suite = new NbTestSuite();
77         suite.addTest(new LibraryTest("testDD"));
78         suite.addTest(new LibraryTest("testDDandManifests"));
79         return suite;
80     }
81     
82     /** Use for execution inside IDE */
83     public static void main(java.lang.String JavaDoc[] args) {
84         // run only selected test case
85
TestRunner.run(suite());
86     }
87     
88     private static final String JavaDoc EAR_BUNDLE
89             = "org.netbeans.modules.j2ee.earproject.ui.actions.Bundle";
90     
91     public void testDD() {
92         //create library
93
Utils.createLibrary(libName,
94                 new String JavaDoc[] {getDataDir().getAbsolutePath() + File.separator + "libs" + File.separator + "MathLib.jar"},
95                 new String JavaDoc[] {getDataDir().getAbsolutePath() + File.separator + "libs" + File.separator + "math.zip"},
96                 null);
97         //create empty j2ee project
98
WizardUtils.createNewProject("Enterprise", "Enterprise Application");
99         NewProjectNameLocationStepOperator npnlso =
100                 WizardUtils.setProjectNameLocation(appName, getWorkDirPath());
101         JCheckBoxOperator jcbo = new JCheckBoxOperator(npnlso, 1);
102         jcbo.setSelected(false);
103         jcbo = new JCheckBoxOperator(npnlso, 2);
104         jcbo.setSelected(false);
105         WizardUtils.setJ2eeSpecVersion(npnlso, WizardUtils.MODULE_EAR, "1.4");
106         npnlso.finish();
107         //add modules to j2ee app
108
addJ2eeModule(pto, appName, ejbName);
109         addJ2eeModule(pto, appName, webName);
110         //build ear
111
Utils.buildProject(appName);
112         //check ear's DDs
113
List JavaDoc l = new ArrayList JavaDoc();
114         File JavaDoc f = new File JavaDoc(getWorkDirPath(), appName);
115         f = new File JavaDoc(f, "src/conf");
116         l.add(new File JavaDoc(f, "application.xml"));
117         l.add(new File JavaDoc(f, "sun-application.xml"));
118         checkFiles(l);
119     }
120     
121     public void testDDandManifests() {
122         //add library to EJB module
123
addLibrary(pto, ejbName, libName);
124         //call EJB from websvc in web => should add ejbs on web's classpath
125
EditorOperator eo = new EditorWindowOperator().getEditor("DefaultWSinWebImpl.java");
126         String JavaDoc ejbjar_bundle
127                 = "org.netbeans.modules.j2ee.ejbcore.ui.logicalview.entres.Bundle";
128         ActionNoBlock anb = new ActionNoBlock(null, Bundle.getStringTrimmed(
129                 ejbjar_bundle, "LBL_EnterpriseActionGroup")
130                 + "|" + Bundle.getStringTrimmed(
131                 ejbjar_bundle, "LBL_CallEjbAction"));
132         eo.select(11);
133         new EventTool().waitNoEvent(2000);
134         anb.performPopup(eo);
135         NbDialogOperator ndo = new NbDialogOperator("Call Enterprise Bean");
136         Node n = new Node(new JTreeOperator(ndo), "MultiSrcRootEjb|LocalSessionSB");
137         n.select();
138         ndo.ok();
139         eo.save();
140         //check ws impl class if code is added correctly
141
File JavaDoc ws = new File JavaDoc(NewFileWizardsTest.MULTI_WEB,
142                 "src/webservices/multi/websvc/DefaultWSinWebImpl.java");
143         checkFiles(Arrays.asList(new File JavaDoc[] {ws}));
144         //edit manifests
145
editManifest(pto, ejbName);
146         editManifest(pto, webName);
147         editManifest(pto, appName);
148         //build ear
149
Utils.cleanProject(appName);
150         Utils.buildProject(appName);
151         //check ear's DDs & MFs in all components
152
List JavaDoc l = new ArrayList JavaDoc();
153         File JavaDoc f = new File JavaDoc(new File JavaDoc(getWorkDirPath()).getParentFile(), "testDD/" + appName);
154         f = new File JavaDoc(f, "src/conf");
155         l.add(new File JavaDoc(f, "application.xml"));
156         l.add(new File JavaDoc(f, "sun-application.xml"));
157         JarFile JavaDoc ear = null;
158         try {
159             f = new File JavaDoc(f.getParentFile().getParentFile(), "build");
160             l.add(getManifest(new JarFile JavaDoc(new File JavaDoc(f, "MultiSrcRootEjb.jar")),
161                     new File JavaDoc(System.getProperty("xtest.tmpdir"), "libtest-ejb.mf")));
162             l.add(getManifest(new JarFile JavaDoc(new File JavaDoc(f, "MultiSrcRootWar.war")),
163                     new File JavaDoc(System.getProperty("xtest.tmpdir"), "libtest-web.mf")));
164             ear = new JarFile JavaDoc(new File JavaDoc(f.getParentFile(), "dist/LibsInclusionTestApp.ear"));
165             l.add(getManifest(ear,
166                     new File JavaDoc(System.getProperty("xtest.tmpdir"), "libtest-ear.mf")));
167         } catch (IOException JavaDoc ioe) {
168             ioe.printStackTrace(System.err);
169         }
170         checkFiles(l);
171         assertNotNull("don't have ear", ear);
172         //check if all components are in ear
173
assertNotNull("MultiSrcRootWar.war is not in created ear",
174                 ear.getEntry("MultiSrcRootWar.war"));
175         assertNotNull("MultiSrcRootEjb.jar is not in created ear",
176                 ear.getEntry("MultiSrcRootEjb.jar"));
177         assertNotNull("MathLib.jar is not in created ear",
178                 ear.getEntry("MathLib.jar"));
179     }
180     
181     protected void addJ2eeModule(ProjectsTabOperator pto, String JavaDoc appName,
182             String JavaDoc moduleName) {
183         Node node = pto.getProjectRootNode(appName);
184         node.performPopupActionNoBlock(Bundle.getStringTrimmed(
185                 EAR_BUNDLE, "LBL_AddModuleAction"));
186         NbDialogOperator ndo = new NbDialogOperator(Bundle.getStringTrimmed(
187                 EAR_BUNDLE, "LBL_ModuleSelectorTitle"));
188         JTreeOperator jto = new JTreeOperator(ndo);
189         jto.selectPath(jto.findPath(moduleName));
190         ndo.ok();
191         new EventTool().waitNoEvent(2500);
192     }
193     
194     protected void addLibrary(ProjectsTabOperator pto, String JavaDoc moduleName,
195             String JavaDoc libName) {
196         Node root = pto.getProjectRootNode(moduleName);
197         root.expand();
198         Node node = new Node(root, "Libraries");
199         node.performPopupActionNoBlock("Add Library...");
200         NbDialogOperator ndo = new NbDialogOperator("Add Library");
201         JListOperator jlo = new JListOperator(ndo);
202         int i = 0;
203         for ( ; i <= jlo.getModel().getSize(); i++) {
204             jlo.selectItem(i);
205             Library lib = (Library) jlo.getSelectedValue();
206             if (lib.getDisplayName().indexOf(libName) > -1) {
207                 break;
208             }
209             jlo.clearSelection();
210         }
211         new JButtonOperator(ndo, "Add Library").push();
212         new EventTool().waitNoEvent(2500);
213     }
214     
215     protected void editManifest(ProjectsTabOperator pto, String JavaDoc moduleName) {
216         Node n = new Node(pto.getProjectRootNode(moduleName),
217                 "Configuration Files|MANIFEST.MF");
218         n.performPopupAction("Open");
219         EditorOperator eo = new EditorWindowOperator().getEditor("MANIFEST.MF");
220         eo.insert("Implementation-Vendor: example.com", 2, 1);
221         eo.close(true);
222         new EventTool().waitNoEvent(1000);
223     }
224     
225     private File JavaDoc getManifest(JarFile JavaDoc jf, File JavaDoc f) throws IOException JavaDoc {
226         OutputStream JavaDoc os = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(f));
227         jf.getManifest().write(os);
228         if (os != null) {
229             os.close();
230         }
231         return f;
232     }
233     
234     protected void checkFiles(List JavaDoc newFiles) {
235         try {
236             //have to wait till server specific DD is saved
237
Thread.currentThread().sleep(10000);
238         } catch (InterruptedException JavaDoc ie) {
239             //ignore
240
}
241         if (!CREATE_GOLDEN_FILES) {
242             List JavaDoc l = new ArrayList JavaDoc(newFiles.size());
243             for (Iterator JavaDoc i = newFiles.iterator(); i.hasNext();) {
244                 File JavaDoc newFile = (File JavaDoc) i.next();
245                 try {
246                     if (newFile.getName().endsWith(".xml") && !newFile.getName().startsWith("sun-")) {
247                         assertTrue(ContentComparator.equalsXML(getGoldenFile(getName() + "/" + newFile.getName() + ".pass"), newFile));
248                     } else if (!newFile.getName().endsWith(".mf")) {
249                         assertFile(newFile, getGoldenFile(getName() + "/" + newFile.getName() + ".pass"),
250                                 new File JavaDoc(getWorkDirPath(), newFile.getName() + ".diff"), new FilteringLineDiff());
251                     } else {
252                         assertTrue(ContentComparator.equalsManifest(
253                                 newFile,
254                                 getGoldenFile(getName() + "/" + newFile.getName() + ".pass"),
255                                 new String JavaDoc[] {"Created-By"}));
256                     }
257                 } catch (Throwable JavaDoc t) {
258                     Utils.copyFile(newFile, new File JavaDoc(getWorkDirPath(), newFile.getName() + ".bad"));
259                     Utils.copyFile(getGoldenFile(getName() + "/" + newFile.getName() + ".pass"),
260                             new File JavaDoc(getWorkDirPath(), newFile.getName() + ".gf"));
261                     l.add(newFile.getName());
262                 }
263             }
264             assertTrue("File(s) " + l.toString() + " differ(s) from golden files.", l.isEmpty());
265         } else {
266             createGoldenFiles(newFiles);
267         }
268     }
269     
270     private void createGoldenFiles(List JavaDoc/*File*/ from) {
271         File JavaDoc f = getDataDir();;
272         List JavaDoc names = new ArrayList JavaDoc();
273         names.add("goldenfiles");
274         while (!f.getName().equals("test")) {
275             if (!f.getName().equals("sys") && !f.getName().equals("work") &&!f.getName().equals("tests")) {
276                 names.add(f.getName());
277             }
278             f=f.getParentFile();
279         }
280         for (int i=names.size()-1;i > -1;i--) {
281             f=new File JavaDoc(f,(String JavaDoc)(names.get(i)));
282         }
283         f = new File JavaDoc(f, getClass().getName().replace('.', File.separatorChar));
284         File JavaDoc destDir = new File JavaDoc(f, getName());
285         destDir.mkdirs();
286         for (Iterator JavaDoc i = from.iterator(); i.hasNext();) {
287             File JavaDoc src = (File JavaDoc) i.next();
288             Utils.copyFile(src, new File JavaDoc(destDir, src.getName() + ".pass"));
289         }
290         assertTrue("Golden files generated.", false);
291     }
292     
293 }
294
Popular Tags