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