View Javadoc
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  
24  import junit.framework.TestCase;
25  
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.eclipse.writers.testutils.TestEclipseWriterConfig;
28  import org.apache.maven.plugin.eclipse.writers.wtp.EclipseWtpComponent15Writer;
29  import org.apache.maven.plugin.eclipse.writers.wtp.EclipseWtpComponentWriter;
30  import org.apache.maven.plugin.ide.IdeDependency;
31  import org.apache.maven.plugin.logging.Log;
32  import org.apache.maven.plugin.logging.SystemStreamLog;
33  import org.apache.maven.project.MavenProject;
34  import org.apache.maven.shared.tools.easymock.TestFileManager;
35  import org.jdom.Attribute;
36  import org.jdom.Document;
37  import org.jdom.JDOMException;
38  import org.jdom.input.SAXBuilder;
39  import org.jdom.xpath.XPath;
40  
41  /**
42   * Component writer test for WTP 1.5.
43   * 
44   * @author Steffen Grunwald
45   */
46  public class EclipseWtpComponent15WriterTest
47      extends TestCase
48  {
49  
50      private TestFileManager fileManager = new TestFileManager( "EclipseWtpComponent15Writer.unitTest.", "" );
51  
52      protected void tearDown()
53          throws IOException
54      {
55          fileManager.cleanUp();
56      }
57  
58      /**
59       * Tests the creation of the ejb module references in the org.eclipse.wst.common.component file for:
60       * <ul>
61       * <li>component file of EAR
62       * <li>WTP 1.5
63       * <li>dep is referenced project
64       * </ul>
65       * The archivename is expected to be jar - independent from the packaging (ejb).
66       * 
67       * @throws MojoExecutionException Exception
68       * @throws IOException Exception
69       * @throws JDOMException Exception
70       */
71      public void testWriteEjbComponentMECLIPSE455()
72          throws MojoExecutionException, IOException, JDOMException
73      {
74  
75          TestEclipseWriterConfig config = new TestEclipseWriterConfig();
76  
77          config.setWtpVersion( 1.5f );
78          config.setEclipseProjectName( "test-project" );
79  
80          File basedir = fileManager.createTempDir();
81          File pom = new File( basedir, "pom.xml" );
82          pom.createNewFile();
83  
84          MavenProject project = new MavenProject();
85          project.setFile( pom );
86  
87          config.setProject( project );
88          config.setProjectBaseDir( basedir );
89  
90          config.setEclipseProjectDirectory( basedir );
91          config.setPackaging( "ear" );
92  
93          // add an ejb3 and ejb packaged dependency
94          config.setDeps( new IdeDependency[] { createDep( "ejb" ), createDep( "jar" ) } );
95  
96          EclipseWtpComponentWriter lWriter = new EclipseWtpComponent15Writer();
97  
98          Log log = new TestLog();
99  
100         lWriter.init( log, config );
101 
102         lWriter.write();
103 
104         // now check extension of archivenames to be jar
105         SAXBuilder builder = new SAXBuilder( false );
106 
107         Document doc = builder.build( new File( basedir, ".settings/org.eclipse.wst.common.component" ) );
108 
109         XPath archiveNames = XPath.newInstance( "//dependent-module/@archiveName" );
110 
111         assertEquals( "Must be 2 modules", 2, archiveNames.selectNodes( doc ).size() );
112         for (Object o : archiveNames.selectNodes(doc)) {
113             Attribute attribute = (Attribute) o;
114 
115             String archiveName = attribute.getValue();
116             String extension = archiveName.substring(archiveName.lastIndexOf(".") + 1).toLowerCase();
117 
118             assertEquals("Must be of type jar", "jar", extension);
119         }
120 
121     }
122 
123     private IdeDependency createDep( String packagingType )
124     {
125         IdeDependency dependency = new IdeDependency();
126         dependency.setGroupId( "g" );
127         dependency.setArtifactId( packagingType + "Artifact" );
128         dependency.setVersion( "v" );
129         dependency.setReferencedProject( true );
130         dependency.setAddedToClasspath( true );
131         dependency.setEclipseProjectName( packagingType + "Project" );
132         dependency.setType( packagingType );
133         return dependency;
134     }
135 
136     private static final class TestLog
137         extends SystemStreamLog
138     {
139         public boolean isDebugEnabled()
140         {
141             return true;
142         }
143     }
144 
145 }