1 19 package org.netbeans.test.j2ee.libraries; 20 21 import java.io.BufferedOutputStream ; 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.jar.JarFile ; 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 59 public class LibraryTest extends JellyTestCase { 60 61 private static boolean CREATE_GOLDEN_FILES = Boolean.getBoolean("org.netbeans.test.j2ee.libraries.golden"); 62 64 protected String appName = "LibsInclusionTestApp"; 65 protected String libName = "My_Math_Library-1.0"; 66 protected String ejbName = "MultiSrcRootEjb"; 67 protected String webName = "MultiSrcRootWar"; 68 protected ProjectsTabOperator pto = new ProjectsTabOperator(); 69 70 71 public LibraryTest(String 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 83 public static void main(java.lang.String [] args) { 84 TestRunner.run(suite()); 86 } 87 88 private static final String EAR_BUNDLE 89 = "org.netbeans.modules.j2ee.earproject.ui.actions.Bundle"; 90 91 public void testDD() { 92 Utils.createLibrary(libName, 94 new String [] {getDataDir().getAbsolutePath() + File.separator + "libs" + File.separator + "MathLib.jar"}, 95 new String [] {getDataDir().getAbsolutePath() + File.separator + "libs" + File.separator + "math.zip"}, 96 null); 97 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 addJ2eeModule(pto, appName, ejbName); 109 addJ2eeModule(pto, appName, webName); 110 Utils.buildProject(appName); 112 List l = new ArrayList (); 114 File f = new File (getWorkDirPath(), appName); 115 f = new File (f, "src/conf"); 116 l.add(new File (f, "application.xml")); 117 l.add(new File (f, "sun-application.xml")); 118 checkFiles(l); 119 } 120 121 public void testDDandManifests() { 122 addLibrary(pto, ejbName, libName); 124 EditorOperator eo = new EditorWindowOperator().getEditor("DefaultWSinWebImpl.java"); 126 String 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 File ws = new File (NewFileWizardsTest.MULTI_WEB, 142 "src/webservices/multi/websvc/DefaultWSinWebImpl.java"); 143 checkFiles(Arrays.asList(new File [] {ws})); 144 editManifest(pto, ejbName); 146 editManifest(pto, webName); 147 editManifest(pto, appName); 148 Utils.cleanProject(appName); 150 Utils.buildProject(appName); 151 List l = new ArrayList (); 153 File f = new File (new File (getWorkDirPath()).getParentFile(), "testDD/" + appName); 154 f = new File (f, "src/conf"); 155 l.add(new File (f, "application.xml")); 156 l.add(new File (f, "sun-application.xml")); 157 JarFile ear = null; 158 try { 159 f = new File (f.getParentFile().getParentFile(), "build"); 160 l.add(getManifest(new JarFile (new File (f, "MultiSrcRootEjb.jar")), 161 new File (System.getProperty("xtest.tmpdir"), "libtest-ejb.mf"))); 162 l.add(getManifest(new JarFile (new File (f, "MultiSrcRootWar.war")), 163 new File (System.getProperty("xtest.tmpdir"), "libtest-web.mf"))); 164 ear = new JarFile (new File (f.getParentFile(), "dist/LibsInclusionTestApp.ear")); 165 l.add(getManifest(ear, 166 new File (System.getProperty("xtest.tmpdir"), "libtest-ear.mf"))); 167 } catch (IOException ioe) { 168 ioe.printStackTrace(System.err); 169 } 170 checkFiles(l); 171 assertNotNull("don't have ear", ear); 172 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 appName, 182 String 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 moduleName, 195 String 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 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 getManifest(JarFile jf, File f) throws IOException { 226 OutputStream os = new BufferedOutputStream (new FileOutputStream (f)); 227 jf.getManifest().write(os); 228 if (os != null) { 229 os.close(); 230 } 231 return f; 232 } 233 234 protected void checkFiles(List newFiles) { 235 try { 236 Thread.currentThread().sleep(10000); 238 } catch (InterruptedException ie) { 239 } 241 if (!CREATE_GOLDEN_FILES) { 242 List l = new ArrayList (newFiles.size()); 243 for (Iterator i = newFiles.iterator(); i.hasNext();) { 244 File newFile = (File ) 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 (getWorkDirPath(), newFile.getName() + ".diff"), new FilteringLineDiff()); 251 } else { 252 assertTrue(ContentComparator.equalsManifest( 253 newFile, 254 getGoldenFile(getName() + "/" + newFile.getName() + ".pass"), 255 new String [] {"Created-By"})); 256 } 257 } catch (Throwable t) { 258 Utils.copyFile(newFile, new File (getWorkDirPath(), newFile.getName() + ".bad")); 259 Utils.copyFile(getGoldenFile(getName() + "/" + newFile.getName() + ".pass"), 260 new File (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 from) { 271 File f = getDataDir();; 272 List names = new ArrayList (); 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 (f,(String )(names.get(i))); 282 } 283 f = new File (f, getClass().getName().replace('.', File.separatorChar)); 284 File destDir = new File (f, getName()); 285 destDir.mkdirs(); 286 for (Iterator i = from.iterator(); i.hasNext();) { 287 File src = (File ) i.next(); 288 Utils.copyFile(src, new File (destDir, src.getName() + ".pass")); 289 } 290 assertTrue("Golden files generated.", false); 291 } 292 293 } 294 | Popular Tags |