[128851] J2EE publish/export support for classpath containers (and other entries); product code and unit tests
diff --git a/tests/org.eclipse.jst.j2ee.tests/TestData/ClasspathDependencyTests/Test1.jar b/tests/org.eclipse.jst.j2ee.tests/TestData/ClasspathDependencyTests/Test1.jar new file mode 100644 index 0000000..80f089d --- /dev/null +++ b/tests/org.eclipse.jst.j2ee.tests/TestData/ClasspathDependencyTests/Test1.jar Binary files differ
diff --git a/tests/org.eclipse.jst.j2ee.tests/TestData/ClasspathDependencyTests/Test2.jar b/tests/org.eclipse.jst.j2ee.tests/TestData/ClasspathDependencyTests/Test2.jar new file mode 100644 index 0000000..66c077d --- /dev/null +++ b/tests/org.eclipse.jst.j2ee.tests/TestData/ClasspathDependencyTests/Test2.jar Binary files differ
diff --git a/tests/org.eclipse.jst.j2ee.tests/TestData/ClasspathDependencyTests/Test3.jar b/tests/org.eclipse.jst.j2ee.tests/TestData/ClasspathDependencyTests/Test3.jar new file mode 100644 index 0000000..7e59767 --- /dev/null +++ b/tests/org.eclipse.jst.j2ee.tests/TestData/ClasspathDependencyTests/Test3.jar Binary files differ
diff --git a/tests/org.eclipse.jst.j2ee.tests/TestData/ClasspathDependencyTests/Test3_bin/test/Test3.class b/tests/org.eclipse.jst.j2ee.tests/TestData/ClasspathDependencyTests/Test3_bin/test/Test3.class new file mode 100644 index 0000000..b0c3c88 --- /dev/null +++ b/tests/org.eclipse.jst.j2ee.tests/TestData/ClasspathDependencyTests/Test3_bin/test/Test3.class Binary files differ
diff --git a/tests/org.eclipse.jst.j2ee.tests/TestData/ClasspathDependencyTests/other/Test3.jar b/tests/org.eclipse.jst.j2ee.tests/TestData/ClasspathDependencyTests/other/Test3.jar new file mode 100644 index 0000000..7e59767 --- /dev/null +++ b/tests/org.eclipse.jst.j2ee.tests/TestData/ClasspathDependencyTests/other/Test3.jar Binary files differ
diff --git a/tests/org.eclipse.jst.j2ee.tests/defect-tests/org/eclipse/jst/j2ee/defect/tests/DefectVerificationTests.java b/tests/org.eclipse.jst.j2ee.tests/defect-tests/org/eclipse/jst/j2ee/defect/tests/DefectVerificationTests.java index 15b7def..4ead45b 100644 --- a/tests/org.eclipse.jst.j2ee.tests/defect-tests/org/eclipse/jst/j2ee/defect/tests/DefectVerificationTests.java +++ b/tests/org.eclipse.jst.j2ee.tests/defect-tests/org/eclipse/jst/j2ee/defect/tests/DefectVerificationTests.java
@@ -199,10 +199,24 @@ } runAndVerify(model); IVirtualComponent component = (IVirtualComponent) model.getProperty(IEARComponentImportDataModelProperties.COMPONENT); - EARArtifactEdit artifactEdit = EARArtifactEdit.getEARArtifactEditForRead(component); - EARFile earFile = (EARFile) artifactEdit.asArchive(true); - earFile.getEJBReferences(true, false); - artifactEdit.dispose(); + EARArtifactEdit artifactEdit = null; + try { + artifactEdit = EARArtifactEdit.getEARArtifactEditForRead(component); + EARFile earFile = null; + try { + earFile = (EARFile) artifactEdit.asArchive(true); + earFile.getEJBReferences(true, false); + } finally { + if (earFile != null) { + earFile.close(); + } + } + } finally { + if (artifactEdit != null) { + artifactEdit.dispose(); + } + + } } /**
diff --git a/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/AbstractTests.java b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/AbstractTests.java new file mode 100644 index 0000000..1b87800 --- /dev/null +++ b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/AbstractTests.java
@@ -0,0 +1,15 @@ +package org.eclipse.jst.j2ee.classpath.tests; + +import org.eclipse.wst.common.tests.OperationTestCase; + +/** + * Abstract base class for classpath dependency unit tests + * + * @author rfrost@bea.com + */ +public abstract class AbstractTests extends OperationTestCase { + + protected AbstractTests(final String name) { + super(name); + } +}
diff --git a/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/AllTests.java b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/AllTests.java new file mode 100644 index 0000000..0290997 --- /dev/null +++ b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/AllTests.java
@@ -0,0 +1,20 @@ +package org.eclipse.jst.j2ee.classpath.tests; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class AllTests extends TestCase { + public static Test suite() { + final TestSuite suite = new TestSuite(); + + suite.setName("All Classpath Dependency Tests"); + suite.addTest(ClasspathDependencyCreationTests.suite()); + suite.addTest(ClasspathDependencyValidationTests.suite()); + suite.addTest(ClasspathDependencyEARTests.suite()); + suite.addTest(ClasspathDependencyWebTests.suite()); + + return suite; + } + +}
diff --git a/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/ClasspathDependencyCreationTests.java b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/ClasspathDependencyCreationTests.java new file mode 100644 index 0000000..f1a886f --- /dev/null +++ b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/ClasspathDependencyCreationTests.java
@@ -0,0 +1,108 @@ +package org.eclipse.jst.j2ee.classpath.tests; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.eclipse.core.resources.IProject; +import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jst.j2ee.classpath.tests.util.ClasspathDependencyTestUtil; +import org.eclipse.jst.j2ee.classpathdep.UpdateClasspathAttributeUtil; +import org.eclipse.jst.j2ee.dependency.tests.util.ProjectUtil; +import org.eclipse.wst.common.componentcore.ComponentCore; +import org.eclipse.wst.common.componentcore.resources.IVirtualComponent; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * Tests creation of classpath component dependencies via addition of the + * classpath entry attribute. + */ +public class ClasspathDependencyCreationTests extends AbstractTests { + + private ClasspathDependencyCreationTests(final String name) { + super(name); + } + + public static Test suite(){ + final TestSuite suite = new TestSuite(); + suite.setName("Classpath Component Dependency Creation Tests" ); + suite.addTest(new ClasspathDependencyCreationTests("testNoClasspathDependencies")); + suite.addTest(new ClasspathDependencyCreationTests("testAddRemoveClasspathDependency")); + suite.addTest(new ClasspathDependencyCreationTests("testLibraryClasspathDependency")); + return suite; + } + + public void testNoClasspathDependencies() throws Exception { + final IProject project = ProjectUtil.createUtilityProject("TestUtil", "TestEAR"); + final IJavaProject javaProject = JavaCore.create(project); + final IVirtualComponent comp = ComponentCore.createComponent(project); + ClasspathDependencyTestUtil.verifyNoClasspathAttributes(javaProject); + ClasspathDependencyTestUtil.verifyNoPotentialClasspathEntries(javaProject); + ClasspathDependencyTestUtil.verifyNoClasspathDependencies(comp); + } + + public void testAddRemoveClasspathDependency() throws Exception { + final IProject project = ProjectUtil.createUtilityProject("TestUtil", "TestEAR"); + final IJavaProject javaProject = JavaCore.create(project); + final IVirtualComponent comp = ComponentCore.createComponent(project); + + ClasspathDependencyTestUtil.verifyNoClasspathAttributes(javaProject); + ClasspathDependencyTestUtil.verifyNoPotentialClasspathEntries(javaProject); + ClasspathDependencyTestUtil.verifyNoClasspathDependencies(comp); + + ClasspathDependencyTestUtil.addCustomClasspathContainer(javaProject); + + final Set entryPaths = new HashSet(); + entryPaths.add(ClasspathDependencyTestUtil.CUSTOM_CLASSPATH_CONTAINER); + List entries = ClasspathDependencyTestUtil.verifyPotentialClasspathEntries(javaProject, entryPaths); + ClasspathDependencyTestUtil.verifyNoClasspathAttributes(javaProject); + ClasspathDependencyTestUtil.verifyNoClasspathDependencies(comp); + + IClasspathEntry entry = (IClasspathEntry) entries.get(0); + UpdateClasspathAttributeUtil.addDependencyAttribute(null, project.getName(), entry); + + ClasspathDependencyTestUtil.verifyNoPotentialClasspathEntries(javaProject); + ClasspathDependencyTestUtil.verifyClasspathAttributes(javaProject, entryPaths); + final Set archiveNames = new HashSet(); + archiveNames.add(ClasspathDependencyTestUtil.TEST1_JAR); + archiveNames.add(ClasspathDependencyTestUtil.TEST2_JAR); + ClasspathDependencyTestUtil.verifyClasspathDependencies(comp, archiveNames); + + UpdateClasspathAttributeUtil.removeDependencyAttribute(null, project.getName(), entry); + + ClasspathDependencyTestUtil.verifyPotentialClasspathEntries(javaProject, entryPaths); + ClasspathDependencyTestUtil.verifyNoClasspathAttributes(javaProject); + ClasspathDependencyTestUtil.verifyNoClasspathDependencies(comp); + } + + public void testLibraryClasspathDependency() throws Exception { + final IProject project = ProjectUtil.createWebProject("TestWeb", "TestEAR"); + final IJavaProject javaProject = JavaCore.create(project); + final IVirtualComponent comp = ComponentCore.createComponent(project); + + ClasspathDependencyTestUtil.verifyNoClasspathAttributes(javaProject); + ClasspathDependencyTestUtil.verifyNoPotentialClasspathEntries(javaProject); + ClasspathDependencyTestUtil.verifyNoClasspathDependencies(comp); + + ClasspathDependencyTestUtil.addLibraryEntry(javaProject, ClasspathDependencyTestUtil.TEST3_JAR_PATH, true); + + final Set entryPaths = new HashSet(); + entryPaths.add(ClasspathDependencyTestUtil.TEST3_JAR_PATH); + List entries = ClasspathDependencyTestUtil.verifyPotentialClasspathEntries(javaProject, entryPaths); + ClasspathDependencyTestUtil.verifyNoClasspathAttributes(javaProject); + ClasspathDependencyTestUtil.verifyNoClasspathDependencies(comp); + + IClasspathEntry entry = (IClasspathEntry) entries.get(0); + UpdateClasspathAttributeUtil.addDependencyAttribute(null, project.getName(), entry); + + ClasspathDependencyTestUtil.verifyNoPotentialClasspathEntries(javaProject); + ClasspathDependencyTestUtil.verifyClasspathAttributes(javaProject, entryPaths); + final Set archiveNames = new HashSet(); + archiveNames.add(ClasspathDependencyTestUtil.TEST3_JAR); + ClasspathDependencyTestUtil.verifyClasspathDependencies(comp, archiveNames); + } +}
diff --git a/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/ClasspathDependencyEARTests.java b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/ClasspathDependencyEARTests.java new file mode 100644 index 0000000..b92b58e --- /dev/null +++ b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/ClasspathDependencyEARTests.java
@@ -0,0 +1,291 @@ +package org.eclipse.jst.j2ee.classpath.tests; + +import java.io.FileInputStream; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jst.j2ee.classpath.tests.util.ClasspathDependencyTestUtil; +import org.eclipse.jst.j2ee.classpathdep.UpdateClasspathAttributeUtil; +import org.eclipse.jst.j2ee.commonarchivecore.internal.EARFile; +import org.eclipse.jst.j2ee.commonarchivecore.internal.File; +import org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.ArchiveManifest; +import org.eclipse.jst.j2ee.commonarchivecore.internal.helpers.ArchiveManifestImpl; +import org.eclipse.jst.j2ee.commonarchivecore.internal.impl.ArchiveImpl; +import org.eclipse.jst.j2ee.componentcore.util.EARArtifactEdit; +import org.eclipse.jst.j2ee.dependency.tests.util.DependencyCreationUtil; +import org.eclipse.jst.j2ee.dependency.tests.util.ProjectUtil; +import org.eclipse.jst.j2ee.internal.deployables.J2EEFlexProjDeployable; +import org.eclipse.wst.common.componentcore.ComponentCore; +import org.eclipse.wst.common.componentcore.resources.IVirtualComponent; +import org.eclipse.wst.server.core.IModule; +import org.eclipse.wst.server.core.internal.ModuleFolder; +import org.eclipse.wst.server.core.model.IModuleResource; +import org.eclipse.wst.server.core.util.ProjectModule; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * Tests export and publish behavior for classpath component dependencies on the module projects associated with an EAR. + */ +public class ClasspathDependencyEARTests extends AbstractTests { + + private static final String UTIL_PROJECT = "TestUtil"; + private static final String WEB_PROJECT = "TestWeb"; + private static final String EAR_PROJECT = "TestEAR"; + + private ClasspathDependencyEARTests(final String name) { + super(name); + } + + public static Test suite(){ + final TestSuite suite = new TestSuite(); + suite.setName("Classpath Dependency EAR Tests" ); + suite.addTest(new ClasspathDependencyEARTests("testEARExport")); + suite.addTest(new ClasspathDependencyEARTests("testEARPublish")); + return suite; + } + + public void testEARExport() throws Exception { + + // create the EAR and module projects + createProjects(); + final IProject earProject = ProjectUtil.getProject(EAR_PROJECT); + final IVirtualComponent earComp = ComponentCore.createComponent(earProject); + final Set archiveNames = new HashSet(); + archiveNames.add(ClasspathDependencyTestUtil.TEST1_JAR); + archiveNames.add(ClasspathDependencyTestUtil.TEST2_JAR); + + // verify that the exported EAR does not contain the cp container Jars from the utility + // and that the utility MANIFEST classpath does not include entries for the cp container + // jars + verifyExportedEAR(earComp, archiveNames, false); + + // add the attribute to the cp container in the utility project + addDependencyAttribute(); + + // verify that the exported EAR does not contain the cp container Jars from the utility + // and that the utility MANIFEST classpath does include entries for the cp container + // jars + verifyExportedEAR(earComp, archiveNames, true); + } + + private void verifyExportedEAR(final IVirtualComponent comp, final Set archiveNames, final boolean shouldHaveArchives) throws Exception { + EARArtifactEdit earEdit = null; + EARFile earFile = null; + try { + earEdit = EARArtifactEdit.getEARArtifactEditForRead(comp); + if (earEdit != null) { + earFile = (EARFile) earEdit.asArchive(true); + List archives = earFile.getArchiveFiles(); + Iterator it = archiveNames.iterator(); + while (it.hasNext()) { + String name = (String) it.next(); + boolean hasArchive = false; + for (int i = 0; i < archives.size(); i++) { + File archive = (File) archives.get(i); + if (archive.isArchive()) { + if (archive.getName().equals(name)) { + hasArchive= true; + } + } + } + if (shouldHaveArchives) { + assertTrue("Exported EAR missing classpath dependency Jar " + name, hasArchive); + } else { + assertFalse("Exported EAR has unexpected classpath dependency Jar " + name, hasArchive); + } + } + for (int i = 0; i < archives.size(); i++) { + File archive = (File) archives.get(i); + if (archive.isArchive() && archive.getName().equals(UTIL_PROJECT + ".jar")) { + ArchiveImpl archiveImpl = (ArchiveImpl) archive; + String[] cp = archiveImpl.getManifest().getClassPathTokenized(); + while (it.hasNext()) { + String name = (String) it.next(); + boolean isOnCP = false; + for (int j = 0; j < cp.length; j++) { + if (cp[j].equals(name)) { + isOnCP = true; + } + } + if (shouldHaveArchives) { + assertTrue("Utility project MANIFEST.MF classpath in exported EAR is entry for dependency Jar " + name, isOnCP); + } else { + assertTrue("Utility project MANIFEST.MF classpath in exported EAR has unexpected entry for dependency Jar " + name, isOnCP); + } + } + } + } + } + } finally { + if (earEdit !=null) + earEdit.dispose(); + if(earFile != null){ + earFile.close(); + } + } + } + + public void testEARPublish() throws Exception { + + // create the EAR and module projects + createProjects(); + final IProject earProject = ProjectUtil.getProject(EAR_PROJECT); + final IVirtualComponent earComp = ComponentCore.createComponent(earProject); + final Set archiveNames = new HashSet(); + archiveNames.add(ClasspathDependencyTestUtil.TEST1_JAR); + archiveNames.add(ClasspathDependencyTestUtil.TEST2_JAR); + + // verify that the exported EAR does not contain the cp container Jars from the utility + // and that the utility MANIFEST classpath does not include entries for the cp container + // jars + verifyPublishedEAR(earComp, archiveNames, false); + + // add the attribute to the cp container in the utility project + addDependencyAttribute(); + + // verify that the exported EAR does not contain the cp container Jars from the utility + // and that the utility MANIFEST classpath does include entries for the cp container + // jars + verifyPublishedEAR(earComp, archiveNames, true); + } + + private void verifyPublishedEAR(final IVirtualComponent comp, final Set archiveNames, final boolean shouldHaveArchives) throws Exception { + // verify that the published EAR contains the cp container jars from the Utility + J2EEFlexProjDeployable deployable = new J2EEFlexProjDeployable(comp.getProject(), comp); + try { + IModuleResource[] members = deployable.members(); + Iterator it = archiveNames.iterator(); + while (it.hasNext()) { + String name = (String) it.next(); + boolean hasArchive = false; + for (int i=0; i<members.length; i++) { + if (members[i].getName().equals(name)) { + hasArchive = true; + } + } + if (shouldHaveArchives) { + assertTrue("Published EAR missing classpath dependency Jar " + name, hasArchive); + } else { + assertFalse("Published EAR has unexpected classpath dependency Jar " + name, hasArchive); + } + } + + IModule utilModule = null; + IModule[] childModules = deployable.getChildModules(); + for (int i=0; i < childModules.length; i++) { + if (childModules[i].getName().equals(UTIL_PROJECT)) { + utilModule = childModules[i]; + } + } + assertNotNull("Missing entry for utility project", utilModule); + J2EEFlexProjDeployable projectModule =(J2EEFlexProjDeployable) utilModule.loadAdapter(ProjectModule.class, null); + IModuleResource[] moduleMembers = projectModule.members(); + ArchiveManifest manifest = null; + boolean foundMetaInf = false; + for (int i=0; i< moduleMembers.length; i++) { + String name = moduleMembers[i].getName(); + if (name.equals("META-INF")) { + foundMetaInf = true; + IModuleResource manifestResource= ((ModuleFolder)moduleMembers[i]).members()[0]; + assertTrue(manifestResource.getModuleRelativePath().toString().equals("META-INF")); + assertTrue("Expected MANIFEST.MF, got " + manifestResource.getName(), manifestResource.getName().equals("MANIFEST.MF")); + java.io.File manifestFile = (java.io.File) manifestResource.getAdapter(java.io.File.class); + if (manifestFile == null) { + manifestFile = ((IFile) manifestResource.getAdapter(IFile.class)).getLocation().toFile(); + } + assertNotNull(manifestFile); + FileInputStream fis = null; + try { + fis = new FileInputStream(manifestFile); + manifest = new ArchiveManifestImpl(fis); + } finally { + if (fis != null) { + fis.close(); + } + } + } + } + if (!foundMetaInf) { + assertTrue("members() failed to return META-INF for utility project module in published EAR", foundMetaInf); + } + + assertNotNull("Failed to retrieve MANIFEST.MF from utility project module in published EAR", manifest); + + it = archiveNames.iterator(); + while (it.hasNext()) { + String name = (String) it.next(); + boolean isOnCP = false; + String[] cp = manifest.getClassPathTokenized(); + for (int j = 0; j < cp.length; j++) { + if (cp[j].equals(name)) { + isOnCP = true; + } + } + if (shouldHaveArchives) { + assertTrue("Utility project MANIFEST.MF classpath in published EAR missing entry for dependency Jar " + name, isOnCP); + } else { + assertFalse("Utility project MANIFEST.MF classpath in published EAR has unexpected entry for dependency Jar " + name, isOnCP); + } + } + + } catch (CoreException e) { + e.printStackTrace(); + fail(e.getMessage()); + } + } + + + private IVirtualComponent createProjects() throws Exception { + // create a Utility project + final IProject util = ProjectUtil.createUtilityProject(UTIL_PROJECT, EAR_PROJECT); + IFolder output = util.getFolder("build").getFolder("classes"); + final IJavaProject utilJava = JavaCore.create(util); + final IVirtualComponent utilComp = ComponentCore.createComponent(util); + + // create a Web project + final IProject webProject = ProjectUtil.createWebProject(WEB_PROJECT, EAR_PROJECT); + final IJavaProject webJavaProject = JavaCore.create(webProject); + final IVirtualComponent webComp = ComponentCore.createComponent(webProject); + + // add a dependency from the Web to the Utility + DependencyCreationUtil.createModuleDependency(webProject, util); + + // add a cp dependency to the Utility + ClasspathDependencyTestUtil.addCustomClasspathContainer(utilJava); + + return webComp; + } + + private void addDependencyAttribute() throws Exception { + final IProject util = ProjectUtil.getProject(UTIL_PROJECT); + final IJavaProject utilJava = JavaCore.create(util); + final IVirtualComponent utilComp = ComponentCore.createComponent(util); + + final Set entryPaths = new HashSet(); + entryPaths.add(ClasspathDependencyTestUtil.CUSTOM_CLASSPATH_CONTAINER); + List entries = ClasspathDependencyTestUtil.verifyPotentialClasspathEntries(utilJava, entryPaths); + ClasspathDependencyTestUtil.verifyNoClasspathAttributes(utilJava); + ClasspathDependencyTestUtil.verifyNoClasspathDependencies(utilComp); + + IClasspathEntry entry = (IClasspathEntry) entries.get(0); + UpdateClasspathAttributeUtil.addDependencyAttribute(null, util.getName(), entry); + + ClasspathDependencyTestUtil.verifyNoPotentialClasspathEntries(utilJava); + ClasspathDependencyTestUtil.verifyClasspathAttributes(utilJava, entryPaths); + final Set archiveNames = new HashSet(); + archiveNames.add(ClasspathDependencyTestUtil.TEST1_JAR); + archiveNames.add(ClasspathDependencyTestUtil.TEST2_JAR); + ClasspathDependencyTestUtil.verifyClasspathDependencies(utilComp, archiveNames); + } +}
diff --git a/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/ClasspathDependencyValidationTests.java b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/ClasspathDependencyValidationTests.java new file mode 100644 index 0000000..162e654 --- /dev/null +++ b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/ClasspathDependencyValidationTests.java
@@ -0,0 +1,346 @@ +package org.eclipse.jst.j2ee.classpath.tests; + +import java.util.Collections; +import java.util.List; +import java.util.Set; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.IPath; +import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.launching.JavaRuntime; +import org.eclipse.jst.j2ee.classpath.tests.util.ClasspathDependencyTestUtil; +import org.eclipse.jst.j2ee.classpathdep.UpdateClasspathAttributeUtil; +import org.eclipse.jst.j2ee.dependency.tests.util.DependencyCreationUtil; +import org.eclipse.jst.j2ee.dependency.tests.util.DependencyUtil; +import org.eclipse.jst.j2ee.dependency.tests.util.ProjectUtil; +import org.eclipse.jst.j2ee.internal.common.classpath.J2EEComponentClasspathContainer; +import org.eclipse.wst.common.componentcore.ComponentCore; +import org.eclipse.wst.common.componentcore.resources.IVirtualComponent; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * Tests validation rules for classpath component dependencies. + */ +public class ClasspathDependencyValidationTests extends AbstractTests { + + private ClasspathDependencyValidationTests(final String name) { + super(name); + } + + public static Test suite(){ + final TestSuite suite = new TestSuite(); + suite.setName("Classpath Component Dependency Validation Tests" ); + suite.addTest(new ClasspathDependencyValidationTests("testSourceEntryRule")); + suite.addTest(new ClasspathDependencyValidationTests("testProjectEntryRule")); + suite.addTest(new ClasspathDependencyValidationTests("testClassFolderRule")); + suite.addTest(new ClasspathDependencyValidationTests("testNonWebNonExportedRule")); + suite.addTest(new ClasspathDependencyValidationTests("testDuplicateArchiveNamesRule")); + suite.addTest(new ClasspathDependencyValidationTests("testRootMappingNonEARWARRefRule")); + suite.addTest(new ClasspathDependencyValidationTests("testInvalidContainerRules")); + suite.addTest(new ClasspathDependencyValidationTests("testNonTaggedExportedClassesRule")); + + return suite; + } + //SourceEntry=Invalid classpath component dependency {0}. Source entries not supported. + public void testSourceEntryRule() throws Exception { + final IProject project = ProjectUtil.createUtilityProject("TestUtil", "TestEAR"); + ClasspathDependencyTestUtil.verifyNoValidationError(project); + final IJavaProject javaProject = JavaCore.create(project); + final IVirtualComponent comp = ComponentCore.createComponent(project); + ClasspathDependencyTestUtil.verifyNoClasspathAttributes(javaProject); + ClasspathDependencyTestUtil.verifyNoPotentialClasspathEntries(javaProject); + ClasspathDependencyTestUtil.verifyNoClasspathDependencies(comp); + + IClasspathEntry[] entries = javaProject.getRawClasspath(); + IClasspathEntry src = null; + for (int i = 0; i < entries.length; i++) { + if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) { + src = entries[i]; + break; + } + } + UpdateClasspathAttributeUtil.addDependencyAttribute(null, project.getName(), src); + + DependencyUtil.waitForValidationJobs(project); + ClasspathDependencyTestUtil.verifyClasspathDependencyError(project); + + UpdateClasspathAttributeUtil.removeDependencyAttribute(null, project.getName(), src); + + DependencyUtil.waitForValidationJobs(project); + ClasspathDependencyTestUtil.verifyNoClasspathDependencyError(project); + } + + //ProjectClasspathEntry=Invalid classpath component dependency {0}. Project entries not supported. + public void testProjectEntryRule() throws Exception { + final IProject utilProject = ProjectUtil.createUtilityProject("TestUtil", "TestEAR"); + final IProject webProject = ProjectUtil.createWebProject("TestWeb", "TestEAR"); + final IJavaProject webJavaProject = JavaCore.create(webProject); + final IVirtualComponent webComp = ComponentCore.createComponent(webProject); + + final IClasspathEntry entry = JavaCore.newProjectEntry(utilProject.getFullPath()); + ClasspathDependencyTestUtil.addEntryToCP(webJavaProject, entry); + + DependencyUtil.waitForValidationJobs(webProject); + ClasspathDependencyTestUtil.verifyNoValidationError(webProject); + ClasspathDependencyTestUtil.verifyNoClasspathAttributes(webJavaProject); + ClasspathDependencyTestUtil.verifyNoPotentialClasspathEntries(webJavaProject); + ClasspathDependencyTestUtil.verifyNoClasspathDependencies(webComp); + + UpdateClasspathAttributeUtil.addDependencyAttribute(null, webProject.getName(), entry); + + DependencyUtil.waitForValidationJobs(webProject); + ClasspathDependencyTestUtil.verifyClasspathDependencyError(webProject); + + UpdateClasspathAttributeUtil.removeDependencyAttribute(null, webProject.getName(), entry); + + DependencyUtil.waitForValidationJobs(webProject); + ClasspathDependencyTestUtil.verifyNoClasspathDependencyError(webProject); + } + + //ClassFolderEntry=Invalid classpath component dependency {0}. Class folders not supported. + public void testClassFolderRule() throws Exception { + final IProject project = ProjectUtil.createUtilityProject("TestUtil", "TestEAR"); + final IJavaProject javaProject = JavaCore.create(project); + final IVirtualComponent comp = ComponentCore.createComponent(project); + + ClasspathDependencyTestUtil.addLibraryEntry(javaProject, ClasspathDependencyTestUtil.TEST3_BIN_PATH, true); + + DependencyUtil.waitForValidationJobs(project); + ClasspathDependencyTestUtil.verifyNoValidationError(project); + ClasspathDependencyTestUtil.verifyNoPotentialClasspathEntries(javaProject); + ClasspathDependencyTestUtil.verifyNoClasspathAttributes(javaProject); + ClasspathDependencyTestUtil.verifyNoClasspathDependencies(comp); + + IClasspathEntry[] entries = javaProject.getRawClasspath(); + IClasspathEntry lib = null; + for (int i = 0; i < entries.length; i++) { + if (entries[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY) { + lib = entries[i]; + break; + } + } + UpdateClasspathAttributeUtil.addDependencyAttribute(null, project.getName(), lib); + + final Set entryPaths = Collections.singleton(ClasspathDependencyTestUtil.TEST3_BIN_PATH); + ClasspathDependencyTestUtil.verifyNoPotentialClasspathEntries(javaProject); + ClasspathDependencyTestUtil.verifyClasspathAttributes(javaProject, entryPaths); + ClasspathDependencyTestUtil.verifyNoClasspathDependencies(comp); + + DependencyUtil.waitForValidationJobs(project); + ClasspathDependencyTestUtil.verifyClasspathDependencyError(project); + + UpdateClasspathAttributeUtil.removeDependencyAttribute(null, project.getName(), lib); + + DependencyUtil.waitForValidationJobs(project); + ClasspathDependencyTestUtil.verifyNoClasspathDependencyError(project); + + } + + //NonWebNonExported=Invalid classpath component dependency {0}. Classpath component dependencies for non-web projects must be exported. + public void testNonWebNonExportedRule() throws Exception { + final IProject project = ProjectUtil.createUtilityProject("TestUtil", "TestEAR"); + final IJavaProject javaProject = JavaCore.create(project); + final IVirtualComponent comp = ComponentCore.createComponent(project); + + ClasspathDependencyTestUtil.addLibraryEntry(javaProject, ClasspathDependencyTestUtil.TEST3_JAR_PATH, false); + + DependencyUtil.waitForValidationJobs(project); + ClasspathDependencyTestUtil.verifyNoValidationError(project); + final Set entryPaths = Collections.singleton(ClasspathDependencyTestUtil.TEST3_JAR_PATH); + List entries = ClasspathDependencyTestUtil.verifyPotentialClasspathEntries(javaProject, entryPaths); + ClasspathDependencyTestUtil.verifyNoClasspathAttributes(javaProject); + ClasspathDependencyTestUtil.verifyNoClasspathDependencies(comp); + + IClasspathEntry entry = (IClasspathEntry) entries.get(0); + UpdateClasspathAttributeUtil.addDependencyAttribute(null, project.getName(), entry); + + ClasspathDependencyTestUtil.verifyNoPotentialClasspathEntries(javaProject); + ClasspathDependencyTestUtil.verifyClasspathAttributes(javaProject, entryPaths); + ClasspathDependencyTestUtil.verifyNoClasspathDependencies(comp); + + DependencyUtil.waitForValidationJobs(project); + ClasspathDependencyTestUtil.verifyClasspathDependencyError(project); + + UpdateClasspathAttributeUtil.removeDependencyAttribute(null, project.getName(), entry); + + DependencyUtil.waitForValidationJobs(project); + ClasspathDependencyTestUtil.verifyNoClasspathDependencyError(project); + } + + //DuplicateArchiveName=Invalid classpath component dependency {0}. The project contains another classpath component dependency the same archive name. + public void testDuplicateArchiveNamesRule() throws Exception { + final IProject webProject = ProjectUtil.createWebProject("TestWeb", "TestEAR"); + final IJavaProject webJavaProject = JavaCore.create(webProject); + final IProject util1 = ProjectUtil.createUtilityProject("TestUtil1", "TestEAR"); + DependencyCreationUtil.createWebLibDependency(webProject, util1); + final IJavaProject utilJava1 = JavaCore.create(util1); + final IVirtualComponent utilComp1 = ComponentCore.createComponent(util1); + final IProject util2 = ProjectUtil.createUtilityProject("TestUtil2", "TestEAR"); + DependencyCreationUtil.createWebLibDependency(webProject, util2); + final IVirtualComponent utilComp2 = ComponentCore.createComponent(util2); + final IJavaProject utilJava2 = JavaCore.create(util2); + final IProject util3 = ProjectUtil.createUtilityProject("TestUtil3", "TestEAR"); + DependencyCreationUtil.createWebLibDependency(webProject, util3); + final IVirtualComponent utilComp3 = ComponentCore.createComponent(util3); + final IJavaProject utilJava3 = JavaCore.create(util3); + ClasspathDependencyTestUtil.addLibraryEntry(utilJava1, ClasspathDependencyTestUtil.TEST3_JAR_PATH, true); + ClasspathDependencyTestUtil.addLibraryEntry(utilJava2, ClasspathDependencyTestUtil.TEST3_JAR_PATH, true); + ClasspathDependencyTestUtil.addLibraryEntry(utilJava3, ClasspathDependencyTestUtil.TEST3_JAR_OTHER_LOCATION_PATH, true); + final Set entryPaths = Collections.singleton(ClasspathDependencyTestUtil.TEST3_JAR_PATH); + final Set otherEntryPaths = Collections.singleton(ClasspathDependencyTestUtil.TEST3_JAR_OTHER_LOCATION_PATH); + final Set archiveNames = Collections.singleton(ClasspathDependencyTestUtil.TEST3_JAR); + + + // add the dependency attribute to util1 and util2 (these point to the same jar so won't + // generate a validation error) + List entries = ClasspathDependencyTestUtil.verifyPotentialClasspathEntries(utilJava1, entryPaths); + IClasspathEntry entry = (IClasspathEntry) entries.get(0); + UpdateClasspathAttributeUtil.addDependencyAttribute(null, util1.getName(), entry); + ClasspathDependencyTestUtil.verifyClasspathDependencies(utilComp1, archiveNames); + entries = ClasspathDependencyTestUtil.verifyPotentialClasspathEntries(utilJava2, entryPaths); + entry = (IClasspathEntry) entries.get(0); + UpdateClasspathAttributeUtil.addDependencyAttribute(null, util2.getName(), entry); + ClasspathDependencyTestUtil.verifyClasspathDependencies(utilComp2, archiveNames); + + // no validation error on the EAR or Web + IProject earProject = ProjectUtil.getProject("TestEAR"); + ClasspathDependencyTestUtil.verifyNoValidationError(earProject); + ClasspathDependencyTestUtil.verifyNoValidationError(webProject); + + // add the dependency attribute to util3 + entries = ClasspathDependencyTestUtil.verifyPotentialClasspathEntries(utilJava3, otherEntryPaths); + entry = (IClasspathEntry) entries.get(0); + UpdateClasspathAttributeUtil.addDependencyAttribute(null, util3.getName(), entry); + ClasspathDependencyTestUtil.verifyClasspathDependencies(utilComp3, archiveNames); + + // will have a validation error on the EAR and on the Web + DependencyUtil.waitForValidationJobs(util3); + ClasspathDependencyTestUtil.verifyValidationError(earProject); + ClasspathDependencyTestUtil.verifyValidationError(webProject); + + // remove dependency attribute from util3 + UpdateClasspathAttributeUtil.removeDependencyAttribute(null, util3.getName(), entry); + + // will have no validation errors on the EAR and on the Web + DependencyUtil.waitForValidationJobs(util3); + ClasspathDependencyTestUtil.verifyNoValidationError(webProject); + ClasspathDependencyTestUtil.verifyNoValidationError(earProject); + } + + //RootMappingNonEARWARRef=Non-web projects must be referenced by an EAR or a WAR to use classpath component dependencies. + public void testRootMappingNonEARWARRefRule() throws Exception { + final IProject project = ProjectUtil.createUtilityProject("TestUtil", null); + final IJavaProject javaProject = JavaCore.create(project); + final IVirtualComponent comp = ComponentCore.createComponent(project); + + ClasspathDependencyTestUtil.addLibraryEntry(javaProject, ClasspathDependencyTestUtil.TEST3_JAR_PATH, true); + + DependencyUtil.waitForValidationJobs(project); + ClasspathDependencyTestUtil.verifyNoValidationError(project); + final Set entryPaths = Collections.singleton(ClasspathDependencyTestUtil.TEST3_JAR_PATH); + List entries = ClasspathDependencyTestUtil.verifyPotentialClasspathEntries(javaProject, entryPaths); + ClasspathDependencyTestUtil.verifyNoClasspathAttributes(javaProject); + ClasspathDependencyTestUtil.verifyNoClasspathDependencies(comp); + IClasspathEntry entry = (IClasspathEntry) entries.get(0); + UpdateClasspathAttributeUtil.addDependencyAttribute(null, project.getName(), entry); + + DependencyUtil.waitForValidationJobs(project); + ClasspathDependencyTestUtil.verifyClasspathDependencyError(project); + + ClasspathDependencyTestUtil.verifyNoPotentialClasspathEntries(javaProject); + ClasspathDependencyTestUtil.verifyClasspathAttributes(javaProject, entryPaths); + final Set archiveNames = Collections.singleton(ClasspathDependencyTestUtil.TEST3_JAR); + ClasspathDependencyTestUtil.verifyClasspathDependencies(comp, archiveNames); + + UpdateClasspathAttributeUtil.removeDependencyAttribute(null, project.getName(), entry); + + ClasspathDependencyTestUtil.verifyNoClasspathAttributes(javaProject); + DependencyUtil.waitForValidationJobs(project); + ClasspathDependencyTestUtil.verifyNoClasspathDependencyError(project); + } + + //EarLibrariesContainer=Invalid classpath component dependency {0}. The EAR Libraries classpath container cannot be exposed. + //WebAppLibrariesContainer=Invalid classpath component dependency {0}. The Web App Libraries classpath container cannot be exposed. + //RuntimeClasspathContainer=Invalid classpath component dependency {0}. Server Runtime classpath containers cannot be exposed. + //JREContainer=Invalid classpath component dependency {0}. JRE classpath containers cannot be exposed. + public void testInvalidContainerRules() throws Exception { + final IProject project = ProjectUtil.createWebProject("TestWeb", "TestEAR"); + final IJavaProject javaProject = JavaCore.create(project); + final IVirtualComponent comp = ComponentCore.createComponent(project); + + DependencyUtil.waitForValidationJobs(project); + ClasspathDependencyTestUtil.verifyNoValidationError(project); + ClasspathDependencyTestUtil.verifyNoPotentialClasspathEntries(javaProject); + ClasspathDependencyTestUtil.verifyNoClasspathAttributes(javaProject); + ClasspathDependencyTestUtil.verifyNoClasspathDependencies(comp); + + IClasspathEntry earLibContainer = null; + IClasspathEntry webAppLibContainer = null; + IClasspathEntry jreContainer = null; + IClasspathEntry[] rawCP = javaProject.getRawClasspath(); + for (int i = 0; i < rawCP.length; i++) { + if (rawCP[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER) { + final IPath path = rawCP[i].getPath(); + if (path.equals(J2EEComponentClasspathContainer.CONTAINER_PATH)) { + earLibContainer = rawCP[i]; + } else if (path.segment(0).equals("org.eclipse.jst.j2ee.internal.web.container")) { + webAppLibContainer = rawCP[i]; + } else if (path.segment(0).equals(JavaRuntime.JRE_CONTAINER)) { + jreContainer = rawCP[i]; + } + } + } + + // XXX Encountering sporadic JUnit problems trying to wait for and verify results of validator + // the ClasspathDependencyTestUtil.verifyNoPotentialClasspathEntries(javaProject) will need to be + // sufficient for now: the results of that call are computed by calling the validator + + // try adding the Ear Libraries container + //testInvalidContainer(javaProject, earLibContainer); + + // try adding the Web App Libraries container + //testInvalidContainer(javaProject, webAppLibContainer); + + // try adding the JRE container + //testInvalidContainer(javaProject, jreContainer); + } + + private void testInvalidContainer(final IJavaProject javaProject, final IClasspathEntry entry) throws Exception { + IProject project = javaProject.getProject(); + UpdateClasspathAttributeUtil.addDependencyAttribute(null, project.getName(), entry); + ClasspathDependencyTestUtil.verifyClasspathAttributes(javaProject, Collections.singleton(entry.getPath())); + DependencyUtil.waitForValidationJobs(project); + ClasspathDependencyTestUtil.verifyClasspathDependencyError(project); + UpdateClasspathAttributeUtil.removeDependencyAttribute(null, project.getName(), entry); + DependencyUtil.waitForValidationJobs(project); + ClasspathDependencyTestUtil.verifyNoClasspathDependencyError(project); + } + + //NonTaggedExportedClasses=Classpath entry {0} will not be exported or published. Runtime ClassNotFoundExceptions may result. + public void testNonTaggedExportedClassesRule() throws Exception { + final IProject project = ProjectUtil.createUtilityProject("TestUtil", "TestEAR"); + final IJavaProject javaProject = JavaCore.create(project); + final IVirtualComponent comp = ComponentCore.createComponent(project); + + ClasspathDependencyTestUtil.addLibraryEntry(javaProject, ClasspathDependencyTestUtil.TEST3_JAR_PATH, true); + + DependencyUtil.waitForValidationJobs(project); + ClasspathDependencyTestUtil.verifyClasspathDependencyMarker(project); + final Set entryPaths = Collections.singleton(ClasspathDependencyTestUtil.TEST3_JAR_PATH); + List entries = ClasspathDependencyTestUtil.verifyPotentialClasspathEntries(javaProject, entryPaths); + ClasspathDependencyTestUtil.verifyNoClasspathAttributes(javaProject); + ClasspathDependencyTestUtil.verifyNoClasspathDependencies(comp); + IClasspathEntry entry = (IClasspathEntry) entries.get(0); + UpdateClasspathAttributeUtil.addDependencyAttribute(null, project.getName(), entry); + + ClasspathDependencyTestUtil.verifyNoPotentialClasspathEntries(javaProject); + ClasspathDependencyTestUtil.verifyClasspathAttributes(javaProject, entryPaths); + DependencyUtil.waitForValidationJobs(project); + ClasspathDependencyTestUtil.verifyNoClasspathDependencyMarker(project); + } + +}
diff --git a/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/ClasspathDependencyWebTests.java b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/ClasspathDependencyWebTests.java new file mode 100644 index 0000000..dd35d0f --- /dev/null +++ b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/ClasspathDependencyWebTests.java
@@ -0,0 +1,204 @@ +package org.eclipse.jst.j2ee.classpath.tests; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jst.j2ee.classpath.tests.util.ClasspathDependencyTestUtil; +import org.eclipse.jst.j2ee.classpathdep.UpdateClasspathAttributeUtil; +import org.eclipse.jst.j2ee.commonarchivecore.internal.File; +import org.eclipse.jst.j2ee.commonarchivecore.internal.WARFile; +import org.eclipse.jst.j2ee.dependency.tests.util.DependencyCreationUtil; +import org.eclipse.jst.j2ee.dependency.tests.util.ProjectUtil; +import org.eclipse.jst.j2ee.internal.deployables.J2EEFlexProjDeployable; +import org.eclipse.jst.j2ee.web.componentcore.util.WebArtifactEdit; +import org.eclipse.wst.common.componentcore.ComponentCore; +import org.eclipse.wst.common.componentcore.resources.IVirtualComponent; +import org.eclipse.wst.server.core.internal.ModuleFolder; +import org.eclipse.wst.server.core.model.IModuleResource; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * Tests export and publish behavior for classpath component dependencies and web projects. + */ +public class ClasspathDependencyWebTests extends AbstractTests { + + private static final String UTIL_PROJECT = "TestUtil"; + private static final String WEB_PROJECT = "TestWeb"; + + private ClasspathDependencyWebTests(final String name) { + super(name); + } + + public static Test suite(){ + final TestSuite suite = new TestSuite(); + suite.setName("Classpath Dependency Web Tests" ); + suite.addTest(new ClasspathDependencyWebTests("testWebExport")); + suite.addTest(new ClasspathDependencyWebTests("testWebPublish")); + return suite; + } + + public void testWebExport() throws Exception { + + // create the Web and utility projects + IVirtualComponent webComp = createProjects(); + + // verify that the exported WAR WEB-INF/lib does not contain the cp container jars from the Utility + final Set archiveNames = new HashSet(); + archiveNames.add(ClasspathDependencyTestUtil.TEST1_JAR); + archiveNames.add(ClasspathDependencyTestUtil.TEST2_JAR); + verifyExportedWebInfLibs(webComp, archiveNames, false); + + // add the cp dependency attribute to the cp container in the util + addDependencyAttribute(); + + // verify that the exported WAR WEB-INF/lib does contain the cp container jars from the Utility + verifyExportedWebInfLibs(webComp, archiveNames, true); + } + + private void verifyExportedWebInfLibs(final IVirtualComponent comp, final Set archiveNames, final boolean shouldHaveArchives) throws Exception { + WebArtifactEdit webEdit = null; + WARFile warFile = null; + try { + webEdit = WebArtifactEdit.getWebArtifactEditForRead(comp); + if (webEdit != null) { + warFile = (WARFile) webEdit.asArchive(true); + List libArchives = warFile.getLibArchives(); + Iterator it = archiveNames.iterator(); + while (it.hasNext()) { + String name = (String) it.next(); + boolean hasArchive = false; + for (int i = 0; i < libArchives.size(); i++) { + File archive = (File) libArchives.get(i); + if (archive.isArchive()) { + if (archive.getName().equals(name)) { + hasArchive= true; + } + } + } + if (shouldHaveArchives) { + assertTrue("Exported WAR missing classpath dependency Jar " + name, hasArchive); + } else { + assertFalse("Exported WAR has unexpected classpath dependency Jar " + name, hasArchive); + } + } + } + } finally { + if (webEdit !=null) + webEdit.dispose(); + if(warFile != null){ + warFile.close(); + } + } + } + + public void testWebPublish() throws Exception { + + // create the web and utility projects + IVirtualComponent webComp = createProjects(); + + // verify that the exported WAR WEB-INF/lib does not contain the cp container jars from the Utility + final Set archiveNames = new HashSet(); + archiveNames.add(ClasspathDependencyTestUtil.TEST1_JAR); + archiveNames.add(ClasspathDependencyTestUtil.TEST2_JAR); + verifyPublishedWebInfLibs(webComp, archiveNames, false); + + // add the cp dependency attribute to the cp container in the util + addDependencyAttribute(); + + // verify that the exported WAR WEB-INF/lib does contain the cp container jars from the Utility + verifyPublishedWebInfLibs(webComp, archiveNames, true); + } + + private void verifyPublishedWebInfLibs(final IVirtualComponent comp, final Set archiveNames, final boolean shouldHaveArchives) throws Exception { + // verify that the published WAR's WEB-INF/lib contains the cp container jars from the Utility + J2EEFlexProjDeployable deployable = new J2EEFlexProjDeployable(comp.getProject(), comp); + try { + IModuleResource[] members = deployable.members(); + assertTrue(members.length==2); + + for (int i=0; i<members.length; i++) { + String name = members[i].getName(); + if (name.equals("WEB-INF")) { + IModuleResource[] webInf = ((ModuleFolder)members[i]).members(); + for (int j=0; j<webInf.length; j++) { + IModuleResource webResource = webInf[j]; + assertTrue(webResource.getModuleRelativePath().toString().equals("WEB-INF")); + if (webResource.getName().equals("lib")) { + IModuleResource[] webresMembers = ((ModuleFolder)webResource).members(); + Iterator it = archiveNames.iterator(); + while (it.hasNext()) { + String archiveName = (String) it.next(); + boolean hasArchive = false; + for (int k = 0; k < webresMembers.length; k++) { + String localName = webresMembers[k].getName(); + if (localName.equals(archiveName)) { + hasArchive= true; + } + } + if (shouldHaveArchives) { + assertTrue("Published WAR missing classpath dependency Jar " + archiveName, hasArchive); + } else { + assertFalse("Published WAR has unexpected classpath dependency Jar " + archiveName, hasArchive); + } + } + } + } + } + } + + } catch (CoreException e) { + e.printStackTrace(); + fail(e.getMessage()); + } + } + + private IVirtualComponent createProjects() throws Exception { + // create a Utility project + final IProject util = ProjectUtil.createUtilityProject(UTIL_PROJECT, null); + final IJavaProject utilJava = JavaCore.create(util); + final IVirtualComponent utilComp = ComponentCore.createComponent(util); + + // create a Web project + final IProject webProject = ProjectUtil.createWebProject(WEB_PROJECT, null); + final IJavaProject webJavaProject = JavaCore.create(webProject); + final IVirtualComponent webComp = ComponentCore.createComponent(webProject); + + // add a dependency from the Web to the Utility + DependencyCreationUtil.createWebLibDependency(webProject, util); + + // add a cp dependency to the Utility + ClasspathDependencyTestUtil.addCustomClasspathContainer(utilJava); + + return webComp; + } + + private void addDependencyAttribute() throws Exception { + final IProject util = ProjectUtil.getProject(UTIL_PROJECT); + final IJavaProject utilJava = JavaCore.create(util); + final IVirtualComponent utilComp = ComponentCore.createComponent(util); + + final Set entryPaths = new HashSet(); + entryPaths.add(ClasspathDependencyTestUtil.CUSTOM_CLASSPATH_CONTAINER); + List entries = ClasspathDependencyTestUtil.verifyPotentialClasspathEntries(utilJava, entryPaths); + ClasspathDependencyTestUtil.verifyNoClasspathAttributes(utilJava); + ClasspathDependencyTestUtil.verifyNoClasspathDependencies(utilComp); + IClasspathEntry entry = (IClasspathEntry) entries.get(0); + UpdateClasspathAttributeUtil.addDependencyAttribute(null, util.getName(), entry); + + ClasspathDependencyTestUtil.verifyNoPotentialClasspathEntries(utilJava); + ClasspathDependencyTestUtil.verifyClasspathAttributes(utilJava, entryPaths); + final Set archiveNames = new HashSet(); + archiveNames.add(ClasspathDependencyTestUtil.TEST1_JAR); + archiveNames.add(ClasspathDependencyTestUtil.TEST2_JAR); + ClasspathDependencyTestUtil.verifyClasspathDependencies(utilComp, archiveNames); + } +}
diff --git a/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/util/ClasspathDependencyTestUtil.java b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/util/ClasspathDependencyTestUtil.java new file mode 100644 index 0000000..98eb2a6 --- /dev/null +++ b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/classpath/tests/util/ClasspathDependencyTestUtil.java
@@ -0,0 +1,208 @@ +package org.eclipse.jst.j2ee.classpath.tests.util; + +import java.net.URL; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import junit.framework.Assert; + +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Platform; +import org.eclipse.jdt.core.IClasspathContainer; +import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jst.j2ee.classpathdep.ClasspathDependencyUtil; +import org.eclipse.jst.j2ee.componentcore.J2EEModuleVirtualComponent; +import org.eclipse.jst.j2ee.dependency.tests.util.DependencyUtil; +import org.eclipse.wst.common.componentcore.resources.IVirtualComponent; +import org.eclipse.wst.common.componentcore.resources.IVirtualReference; +import org.eclipse.wtp.j2ee.headless.tests.plugin.HeadlessTestsPlugin; + +/** + * Various utility methods. + */ +public class ClasspathDependencyTestUtil extends DependencyUtil { + + public static final IPath CUSTOM_CLASSPATH_CONTAINER = new Path("TEST_CP_CONTAINER"); + + public static final String TEST_DATA = "TestData" + IPath.SEPARATOR + "ClasspathDependencyTests"; + public static final String TEST1_JAR = "Test1.jar"; + public static final String TEST2_JAR = "Test2.jar"; + public static final String TEST3_JAR = "Test3.jar"; + public static final String TEST3_JAR_OTHER_LOCATION = "other/Test3.jar"; + public static final String TEST3_BIN= "Test3_bin"; + + public static final IPath TEST1_JAR_PATH = getFullTestDataPath(TEST1_JAR); + public static final IPath TEST2_JAR_PATH = getFullTestDataPath(TEST2_JAR); + public static final IPath TEST3_JAR_PATH = getFullTestDataPath(TEST3_JAR); + public static final IPath TEST3_JAR_OTHER_LOCATION_PATH = getFullTestDataPath(TEST3_JAR_OTHER_LOCATION); + public static final IPath TEST3_BIN_PATH = getFullTestDataPath(TEST3_BIN); + + public static final String CLASSPATH_DEPENDENCY_MARKER_TYPE = "org.eclipse.jst.j2ee.ClasspathDependencyValidatorMarker"; + public static final String VALIDATION_MARKER_TYPE = "org.eclipse.wst.validation.problemmarker"; + + public static IPath getFullTestDataPath(String dataPath) { + try { + final String testDataPath = TEST_DATA + IPath.SEPARATOR + dataPath; + HeadlessTestsPlugin plugin = HeadlessTestsPlugin.getDefault(); + if (plugin != null) { + URL url = plugin.getBundle().getEntry(testDataPath); + if (url != null) { + url = Platform.asLocalURL(url); + return new Path(url.getPath()); + } + } + return new Path(System.getProperty("user.dir") + IPath.SEPARATOR + testDataPath); + } catch (Exception e) { + e.printStackTrace(); + } + return new Path(""); + } + + + /** + * Adds a library entry that references a jar file and is optionally exported. + */ + public static IClasspathEntry addLibraryEntry(final IJavaProject javaProject, final IPath path, final boolean isExported) throws CoreException { + final IClasspathEntry entry = JavaCore.newLibraryEntry(path, null, null, isExported); + addEntryToCP(javaProject, entry); + return entry; + } + + public static void addEntryToCP(final IJavaProject javaProject, final IClasspathEntry entry) throws CoreException { + final IClasspathEntry[] rawCP = javaProject.getRawClasspath(); + final IClasspathEntry[] newCP = new IClasspathEntry[rawCP.length + 1]; + System.arraycopy(rawCP, 0, newCP, 0, rawCP.length); + newCP[rawCP.length] = entry; + javaProject.setRawClasspath(newCP, true, null); + } + /** + * Adds a custom classpath container with the specified name that references two jar files. + */ + public static IClasspathEntry addCustomClasspathContainer(final IJavaProject javaProject) throws CoreException { + final IClasspathEntry entry = JavaCore.newContainerEntry(CUSTOM_CLASSPATH_CONTAINER, true); + addEntryToCP(javaProject, entry); + JavaCore.setClasspathContainer( + CUSTOM_CLASSPATH_CONTAINER, + new IJavaProject[]{ javaProject }, + new IClasspathContainer[] { + new IClasspathContainer() { + public IClasspathEntry[] getClasspathEntries() { + return new IClasspathEntry[]{ + JavaCore.newLibraryEntry(TEST1_JAR_PATH, null, null, false), + JavaCore.newLibraryEntry(TEST2_JAR_PATH, null, null, false), + }; + } + public String getDescription() { return "Test classpath container"; } + public int getKind() { return IClasspathContainer.K_APPLICATION;} + public IPath getPath() { return CUSTOM_CLASSPATH_CONTAINER; } + } + }, + null); + return entry; + } + + public static void verifyNoClasspathAttributes(final IJavaProject javaProject) throws Exception { + verifyClasspathAttributes(javaProject, new HashSet()); + } + + public static Map verifyClasspathAttributes(final IJavaProject javaProject, final Set rawEntryPaths) throws Exception { + final Map entriesToAttrib = ClasspathDependencyUtil.getRawComponentClasspathDependencies(javaProject); + Assert.assertTrue("Project " + javaProject + " should have " + rawEntryPaths.size() + " raw classpath dependencies, only has: " + + entriesToAttrib.size(), entriesToAttrib.size()== rawEntryPaths.size()); + final Iterator i = entriesToAttrib.keySet().iterator(); + while (i.hasNext()) { + final IClasspathEntry entry = (IClasspathEntry) i.next(); + Assert.assertTrue("Project " + javaProject + " missing expected classpath dependency " + entry.getPath(), rawEntryPaths.contains(entry.getPath())); + } + return entriesToAttrib; + } + + public static void verifyNoClasspathDependencies(final IVirtualComponent component) throws Exception { + verifyClasspathDependencies(component, new HashSet()); + } + + public static IVirtualReference[] verifyClasspathDependencies(final IVirtualComponent component, final Set archiveNames) throws Exception { + Assert.assertTrue("Component " + component.getName() + " not a J2EEModuleVirtualComponent", component instanceof J2EEModuleVirtualComponent); + J2EEModuleVirtualComponent j2eeComp = (J2EEModuleVirtualComponent) component; + IVirtualReference[] refs = j2eeComp.getJavaClasspathReferences(); + Assert.assertTrue("Component " + component.getName()+ " should have " + archiveNames.size() + " component classpath dependencies, only has: " + + refs.length, refs.length == archiveNames.size()); + for (int i = 0; i < refs.length; i++) { + Assert.assertTrue("Component " + component.getName()+ " has unexpected component classpath dependency " + refs[i].getArchiveName(), archiveNames.contains(refs[i].getArchiveName())); + } + return refs; + } + + public static void verifyNoPotentialClasspathEntries(final IJavaProject javaProject) throws Exception { + verifyPotentialClasspathEntries(javaProject, new HashSet()); + } + + public static List verifyPotentialClasspathEntries(final IJavaProject javaProject, final Set potentialEntryPaths) throws Exception { + List entries = ClasspathDependencyUtil.getPotentialComponentClasspathDependencies(javaProject); + Assert.assertTrue("Project " + javaProject + " should have " + potentialEntryPaths.size() + " potential classpath dependencies, only has: " + + entries.size(), entries.size() == potentialEntryPaths.size()); + for (int i = 0; i < entries.size(); i++) { + IClasspathEntry entry = (IClasspathEntry) entries.get(i); + Assert.assertTrue("Project " + javaProject + " missing expected potential classpath entry " + entry.getPath(), potentialEntryPaths.contains(entry.getPath())); + } + return entries; + } + + public static void verifyValidationError(final IProject project) throws CoreException { + verifyError(project, VALIDATION_MARKER_TYPE, true); + } + + public static void verifyNoValidationError(final IProject project) throws CoreException { + verifyError(project, VALIDATION_MARKER_TYPE, false); + } + + public static void verifyClasspathDependencyError(final IProject project) throws CoreException { + verifyError(project, CLASSPATH_DEPENDENCY_MARKER_TYPE, true); + } + + public static void verifyNoClasspathDependencyError(final IProject project) throws CoreException { + verifyError(project, CLASSPATH_DEPENDENCY_MARKER_TYPE, false); + } + + private static void verifyError(final IProject project, final String markerType, final boolean shouldHaveErrorMarker) throws CoreException { + final IMarker[] markers = project.findMarkers(markerType, true, IProject.DEPTH_ZERO); + boolean hasError = false; + for (int i = 0; i < markers.length; i++) { + if (markers[i].getAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO) == IMarker.SEVERITY_ERROR) { + hasError = true; + break; + } + } + if (shouldHaveErrorMarker) { + Assert.assertTrue("Project " + project + " missing expected validation error problem marker", hasError); + } else { + Assert.assertFalse("Project " + project + " has unexpected validation error problem marker", hasError); + } + } + + public static void verifyClasspathDependencyMarker(final IProject project) throws CoreException { + verifyMarker(project, CLASSPATH_DEPENDENCY_MARKER_TYPE, true); + } + + public static void verifyNoClasspathDependencyMarker(final IProject project) throws CoreException { + verifyMarker(project, CLASSPATH_DEPENDENCY_MARKER_TYPE, false); + } + + private static void verifyMarker(final IProject project, final String markerType, final boolean shouldHaveMarker) throws CoreException { + final IMarker[] markers = project.findMarkers(markerType, true, IProject.DEPTH_ZERO); + if (shouldHaveMarker) { + Assert.assertTrue("Project " + project + " missing expected " + markerType + " problem marker", markers.length > 0); + } else { + Assert.assertFalse("Project " + project + " has unexpected " + markerType + " problem marker", markers.length > 0); + } + } +}
diff --git a/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/dependency/tests/util/DependencyUtil.java b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/dependency/tests/util/DependencyUtil.java index 14830b4..e6646bf 100644 --- a/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/dependency/tests/util/DependencyUtil.java +++ b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/jst/j2ee/dependency/tests/util/DependencyUtil.java
@@ -57,17 +57,17 @@ IProject[] projects = ProjectUtility.getAllProjects(); for (int i = 0; i < projects.length; i++) { final IProject p = projects[i]; - final String family = p.getName() + OperationTestCase.VALIDATOR_JOB_FAMILY; - final Job[] jobs = jobMgr.find(family); - if (jobs.length > 0) { - try { - Platform.getJobManager().join(family, null); - } catch (InterruptedException ie) { - } - } + waitForValidationJobs(p); } } + public static void waitForValidationJobs(final IProject project) { + // Wait for all validation jobs to end + final IJobManager jobMgr = Platform.getJobManager(); + final String family = project.getName() + OperationTestCase.VALIDATOR_JOB_FAMILY; + waitForJobs(family); + } + public static void waitForProjectRefactoringJobs() { waitForJobs(ProjectRefactoringListener.PROJECT_REFACTORING_JOB_FAMILY); } @@ -88,6 +88,9 @@ try { jobMgr.join(family, null); } catch (InterruptedException ie) { + // make one last check for jobs before exiting + i = 999; + continue; } break; }
diff --git a/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/wtp/j2ee/headless/tests/plugin/AllPluginTests.java b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/wtp/j2ee/headless/tests/plugin/AllPluginTests.java index fd0e718..5cf8ffe 100644 --- a/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/wtp/j2ee/headless/tests/plugin/AllPluginTests.java +++ b/tests/org.eclipse.jst.j2ee.tests/j2ee-tests/org/eclipse/wtp/j2ee/headless/tests/plugin/AllPluginTests.java
@@ -45,6 +45,7 @@ addTest(ProjectMigrationTest.suite()); addTest(AnnotationProviderTest.suite()); addTest(org.eclipse.jst.j2ee.dependency.tests.AllTests.suite()); + addTest(org.eclipse.jst.j2ee.classpath.tests.AllTests.suite()); addTest(WebAppLibrariesContainerTests.suite()); addTest(DefectVerificationTestsSuite.suite()); }
diff --git a/tests/org.eclipse.jst.j2ee.tests/plugin.xml b/tests/org.eclipse.jst.j2ee.tests/plugin.xml index a0bdde3..1eddb72 100644 --- a/tests/org.eclipse.jst.j2ee.tests/plugin.xml +++ b/tests/org.eclipse.jst.j2ee.tests/plugin.xml
@@ -35,6 +35,7 @@ <import plugin="org.eclipse.jst.j2ee.webservice"/> <import plugin="org.eclipse.jem.util"/> <import plugin="org.eclipse.jdt.core"/> + <import plugin="org.eclipse.jdt.launching"/> <import plugin="org.eclipse.wst.web"/> <import plugin="org.eclipse.wst.common.project.facet.core"/> <import plugin="org.eclipse.jst.server.core"/>