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  
24  import junit.framework.TestCase;
25  
26  import org.codehaus.plexus.util.Os;
27  
28  /**
29   * Test for {@link IdeUtils}
30   * 
31   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
32   * @version $Id: IdeUtilsTest.java 728549 2008-12-21 23:11:22Z bentmann $
33   */
34  public class IdeUtilsTest
35      extends TestCase
36  {
37  
38      public void testGetProjectNameStringIdeDependency()
39      {
40          IdeDependency dependency = new IdeDependency();
41          dependency.setGroupId( "g" );
42          dependency.setArtifactId( "a" );
43          dependency.setVersion( "v" );
44  
45          String name = IdeUtils.getProjectName( IdeUtils.PROJECT_NAME_DEFAULT_TEMPLATE, dependency );
46          assertEquals( dependency.getArtifactId(), name );
47  
48          name = IdeUtils.getProjectName( IdeUtils.PROJECT_NAME_WITH_GROUP_AND_VERSION_TEMPLATE, dependency );
49          assertEquals( dependency.getGroupId() + "." + dependency.getArtifactId() + "-" + dependency.getVersion(), name );
50  
51          name = IdeUtils.getProjectName( IdeUtils.PROJECT_NAME_WITH_GROUP_TEMPLATE, dependency );
52          assertEquals( dependency.getGroupId() + "." + dependency.getArtifactId(), name );
53  
54          name = IdeUtils.getProjectName( IdeUtils.PROJECT_NAME_WITH_VERSION_TEMPLATE, dependency );
55          assertEquals( dependency.getArtifactId() + "-" + dependency.getVersion(), name );
56      }
57  
58      /**
59       * When the file to add is on a different drive and an absolute path expect that the returned value is the same as
60       * the file to add (but with /s)
61       * 
62       * @throws Exception
63       */
64      public void testToRelativeAndFixSeparator_WhereOnDifferentDrivesAndAbsolutePaths()
65          throws Exception
66      {
67          if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
68          {
69              return;
70          }
71          File basedir = new File( "C:\\TEMP\\EclipsePlugin.unitTest.1165557188766\\" );
72          File fileToAdd = new File( "D:\\ide\\workspace\\maven\\maven-eclipse-plugin\\target\\main-output" );
73          try
74          {
75              fileToAdd.getCanonicalPath();
76          }
77          catch ( IOException e )
78          {
79              // skip the test if the fileToAdd can't be canonicalized.
80              // Likely it is because D refers to a CD drive that is not ready.
81              return;
82          }
83          
84          String actual = IdeUtils.toRelativeAndFixSeparator( basedir, fileToAdd, false );
85          String expected = "D:/ide/workspace/maven/maven-eclipse-plugin/target/main-output";
86  
87          assertEquals( expected, actual );
88      }
89  
90      /**
91       * When the file to add is a relative file then expect the result to be relative to the basedir (not whatever the
92       * current processes basedir is set to)
93       * 
94       * @throws Exception
95       */
96      public void testToRelativeAndFixSeparator_WhereOnDifferentDrivesAndFileToAddRelative()
97          throws Exception
98      {
99          if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
100         {
101             return;
102         }
103 
104         File basedir = new File( "C:\\TEMP\\EclipsePlugin.unitTest.1165557188766\\" );
105         File fileToAdd = new File( "target/main-output" );
106 
107         String actual = IdeUtils.toRelativeAndFixSeparator( basedir, fileToAdd, false );
108         String expected = "target/main-output";
109 
110         assertEquals( expected, actual );
111     }
112 
113     /**
114      * See MECLIPSE-261.
115      * <p>
116      * When the base dir is a windows root directory the assumption that the full path to fileToAdd is basedir + "/" +
117      * fileToAdd is incorrect.
118      * <p>
119      * As the canonical form of a windows root dir ends in a slash, whereas the canonical form of any other file does
120      * not.
121      * 
122      * @throws Exception
123      */
124     public void testToRelativeAndFixSeparator_MECLIPSE_261()
125         throws Exception
126     {
127         if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
128         {
129             return;
130         }
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 }