1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
35
36
37
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
65
66
67
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
84
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
96
97
98
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
117
118
119
120
121
122
123
124
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 }