1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.eclipse.writers;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Iterator;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.eclipse.writers.testutils.TestEclipseWriterConfig;
29  import org.apache.maven.plugin.eclipse.writers.wtp.EclipseWtpComponent15Writer;
30  import org.apache.maven.plugin.eclipse.writers.wtp.EclipseWtpComponentWriter;
31  import org.apache.maven.plugin.ide.IdeDependency;
32  import org.apache.maven.plugin.logging.Log;
33  import org.apache.maven.plugin.logging.SystemStreamLog;
34  import org.apache.maven.project.MavenProject;
35  import org.apache.maven.shared.tools.easymock.TestFileManager;
36  import org.jdom.Attribute;
37  import org.jdom.Document;
38  import org.jdom.JDOMException;
39  import org.jdom.input.SAXBuilder;
40  import org.jdom.xpath.XPath;
41  
42  /**
43   * Component writer test for WTP 1.5.
44   * 
45   * @author Steffen Grunwald
46   */
47  public class EclipseWtpComponent15WriterTest extends TestCase {
48  
49      private TestFileManager fileManager = new TestFileManager(
50              "EclipseWtpComponent15Writer.unitTest.", "");
51  
52      protected void tearDown() throws IOException {
53          fileManager.cleanUp();
54      }
55  
56      /**
57       * Tests the creation of the ejb module references in the org.eclipse.wst.common.component file for:
58       * <ul>
59       *  <li>component file of EAR
60       *  <li>WTP 1.5
61       *  <li>dep is referenced project
62       * </ul>
63       * 
64       * The archivename is expected to be jar - independent from the packaging (ejb).
65       * 
66       * @throws MojoExecutionException Exception
67       * @throws IOException Exception
68       * @throws JDOMException Exception 
69       */
70      public void testWriteEjbComponentMECLIPSE455()
71              throws MojoExecutionException, IOException, JDOMException {
72  
73          TestEclipseWriterConfig config = new TestEclipseWriterConfig();
74  
75          config.setWtpVersion(1.5f);
76          config.setEclipseProjectName("test-project");
77  
78          File basedir = fileManager.createTempDir();
79          File pom = new File(basedir, "pom.xml");
80          pom.createNewFile();
81          
82          MavenProject project = new MavenProject();
83          project.setFile(pom);
84          
85          config.setProject(project);
86          config.setProjectBaseDir(basedir);
87          
88          config.setEclipseProjectDirectory(basedir);
89          config.setPackaging("ear");
90          
91          // add an ejb3 and ejb packaged dependency
92          config.setDeps(new IdeDependency[]{createDep("ejb"), createDep("jar")});
93          
94          EclipseWtpComponentWriter lWriter = new EclipseWtpComponent15Writer();
95  
96          Log log = new TestLog();
97  
98          lWriter.init(log, config);
99  
100         lWriter.write();
101 
102         // now check extension of archivenames to be jar
103         SAXBuilder builder = new SAXBuilder( false );
104 
105         Document doc = builder.build( new File( basedir, ".settings/org.eclipse.wst.common.component" ) );
106 
107         XPath archiveNames = XPath.newInstance( "//dependent-module/@archiveName" );
108         
109         assertEquals("Must be 2 modules", 2, archiveNames.selectNodes( doc ).size());
110         for ( Iterator it = archiveNames.selectNodes( doc ).iterator(); it.hasNext(); )
111         {
112             Attribute attribute = (Attribute) it.next();
113             
114             String archiveName = attribute.getValue();
115             String extension = archiveName.substring(archiveName.lastIndexOf(".") + 1).toLowerCase();
116             
117             assertEquals("Must be of type jar", "jar", extension);
118         }
119         
120     }
121 
122     private IdeDependency createDep(String packagingType) {
123         IdeDependency dependency = new IdeDependency();
124         dependency.setGroupId("g");
125         dependency.setArtifactId(packagingType + "Artifact");
126         dependency.setVersion("v");
127         dependency.setReferencedProject(true);
128         dependency.setAddedToClasspath(true);
129         dependency.setEclipseProjectName(packagingType + "Project");
130         dependency.setType(packagingType);
131         return dependency;
132     }
133 
134     private static final class TestLog extends SystemStreamLog {
135         public boolean isDebugEnabled() {
136             return true;
137         }
138     }
139 
140 }