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.ide;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Collections;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.maven.model.Dependency;
28  import org.codehaus.plexus.util.Os;
29  
30  /**
31   * Test for {@link IdeUtils}
32   * 
33   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
34   * @version $Id: IdeUtilsTest.java 1154560 2011-08-06 17:46:34Z rfscholte $
35   */
36  public class IdeUtilsTest
37      extends TestCase
38  {
39  
40      public void testGetProjectNameStringIdeDependency()
41      {
42          IdeDependency dependency = new IdeDependency();
43          dependency.setGroupId( "g" );
44          dependency.setArtifactId( "a" );
45          dependency.setVersion( "v" );
46  
47          String name = IdeUtils.getProjectName( IdeUtils.PROJECT_NAME_DEFAULT_TEMPLATE, dependency );
48          assertEquals( dependency.getArtifactId(), name );
49  
50          name = IdeUtils.getProjectName( IdeUtils.PROJECT_NAME_WITH_GROUP_AND_VERSION_TEMPLATE, dependency );
51          assertEquals( dependency.getGroupId() + "." + dependency.getArtifactId() + "-" + dependency.getVersion(), name );
52  
53          name = IdeUtils.getProjectName( IdeUtils.PROJECT_NAME_WITH_GROUP_TEMPLATE, dependency );
54          assertEquals( dependency.getGroupId() + "." + dependency.getArtifactId(), name );
55  
56          name = IdeUtils.getProjectName( IdeUtils.PROJECT_NAME_WITH_VERSION_TEMPLATE, dependency );
57          assertEquals( dependency.getArtifactId() + "-" + dependency.getVersion(), name );
58      }
59  
60      /**
61       * When the file to add is on a different drive and an absolute path expect that the returned value is the same as
62       * the file to add (but with /s)
63       * 
64       * @throws Exception
65       */
66      public void testToRelativeAndFixSeparator_WhereOnDifferentDrivesAndAbsolutePaths()
67          throws Exception
68      {
69          if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
70          {
71              return;
72          }
73          File basedir = new File( "C:\\TEMP\\EclipsePlugin.unitTest.1165557188766\\" );
74          File fileToAdd = new File( "D:\\ide\\workspace\\maven\\maven-eclipse-plugin\\target\\main-output" );
75          try
76          {
77              fileToAdd.getCanonicalPath();
78          }
79          catch ( IOException e )
80          {
81              // skip the test if the fileToAdd can't be canonicalized.
82              // Likely it is because D refers to a CD drive that is not ready.
83              return;
84          }
85  
86          String actual = IdeUtils.toRelativeAndFixSeparator( basedir, fileToAdd, false );
87          String expected = "D:/ide/workspace/maven/maven-eclipse-plugin/target/main-output";
88  
89          assertEquals( expected, actual );
90      }
91  
92      /**
93       * When the file to add is a relative file then expect the result to be relative to the basedir (not whatever the
94       * current processes basedir is set to)
95       * 
96       * @throws Exception
97       */
98      public void testToRelativeAndFixSeparator_WhereOnDifferentDrivesAndFileToAddRelative()
99          throws Exception
100     {
101         if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
102         {
103             return;
104         }
105 
106         File basedir = new File( "C:\\TEMP\\EclipsePlugin.unitTest.1165557188766\\" );
107         File fileToAdd = new File( "target/main-output" );
108 
109         String actual = IdeUtils.toRelativeAndFixSeparator( basedir, fileToAdd, false );
110         String expected = "target/main-output";
111 
112         assertEquals( expected, actual );
113     }
114 
115     /**
116      * See MECLIPSE-261.
117      * <p>
118      * When the base dir is a windows root directory the assumption that the full path to fileToAdd is basedir + "/" +
119      * fileToAdd is incorrect.
120      * <p>
121      * As the canonical form of a windows root dir ends in a slash, whereas the canonical form of any other file does
122      * not.
123      * 
124      * @throws Exception
125      */
126     public void testToRelativeAndFixSeparator_MECLIPSE_261()
127         throws Exception
128     {
129         if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
130         {
131             return;
132         }
133 
134         File basedir = new File( new File( "" ).getAbsolutePath().substring( 0, 3 ) );
135         File fileToAdd = new File( "target/main-output" );
136 
137         String actual = IdeUtils.toRelativeAndFixSeparator( basedir, fileToAdd, false );
138         String expected = "target/main-output";
139 
140         assertEquals( expected, actual );
141     }
142     
143     public void testGetArtifactVersion()
144     {
145         Dependency dep = new Dependency();
146         dep.setArtifactId( "artifactId" );
147 
148         dep.setVersion( "5" );
149         assertEquals( "5",
150                       IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 1 ) );
151         assertEquals( "5.0",
152                       IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 3 ) );
153         assertEquals( "5.0.0",
154                       IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 5 ) );
155 
156         dep.setVersion( "5.3" );
157         assertEquals( "5",
158                       IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 1 ) );
159         assertEquals( "5.3",
160                       IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 3 ) );
161         assertEquals( "5.3.0",
162                       IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 5 ) );
163 
164         dep.setVersion( "5.3.1" );
165         assertEquals( "5",
166                       IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 1 ) );
167         assertEquals( "5.3",
168                       IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 3 ) );
169         assertEquals( "5.3.1",
170                       IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.singletonList( dep ), 5 ) );
171         
172         assertNull( IdeUtils.getArtifactVersion( new String[] { "artifactId" }, Collections.EMPTY_LIST, 5 ) );
173         assertNull( IdeUtils.getArtifactVersion( new String[0], Collections.singletonList( dep ), 5 ) );
174     }
175 
176 }