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