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.shared.release.util;
20  
21  import java.nio.file.Path;
22  import java.nio.file.Paths;
23  
24  import org.codehaus.plexus.util.Os;
25  import org.junit.Test;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertNull;
29  import static org.junit.Assert.assertTrue;
30  import static org.junit.Assume.assumeTrue;
31  
32  /**
33   * Tests for ReleaseUtil methods
34   */
35  public class ReleaseUtilTest {
36      /**
37       * MRELEASE-273 : Tests if there no pom passed as parameter
38       */
39      @Test
40      public void testProjectIsNull() {
41          assertNull(ReleaseUtil.getReleasePom(null));
42          assertNull(ReleaseUtil.getStandardPom(null));
43      }
44  
45      @Test
46      public void testGetBaseScmUrlSingleLevel() throws Exception {
47          assertEquals(
48                  "scm:svn:http://svn.repo.com/flat-multi-module/trunk",
49                  ReleaseUtil.realignScmUrl(0, "scm:svn:http://svn.repo.com/flat-multi-module/trunk"));
50          assertEquals(
51                  "scm:svn:http://svn.repo.com/flat-multi-module/trunk/",
52                  ReleaseUtil.realignScmUrl(0, "scm:svn:http://svn.repo.com/flat-multi-module/trunk/"));
53      }
54  
55      @Test
56      public void testGetBaseScmUrlSingleLevelDotCharacter() throws Exception {
57          assertEquals(
58                  "scm:svn:http://svn.repo.com/flat-multi-module/trunk",
59                  ReleaseUtil.realignScmUrl(0, "scm:svn:http://svn.repo.com/flat-multi-module/trunk/."));
60          assertEquals(
61                  "scm:svn:http://svn.repo.com/flat-multi-module/trunk/",
62                  ReleaseUtil.realignScmUrl(0, "scm:svn:http://svn.repo.com/flat-multi-module/trunk/./"));
63          assertEquals(
64                  "scm:svn:http://svn.repo.com/flat-multi-module/trunk/project",
65                  ReleaseUtil.realignScmUrl(0, "scm:svn:http://svn.repo.com/flat-multi-module/trunk/./project"));
66          assertEquals(
67                  "scm:svn:http://svn.repo.com/flat-multi-module",
68                  ReleaseUtil.realignScmUrl(0, "scm:svn:http://svn.repo.com/flat-multi-module/trunk/.."));
69          assertEquals(
70                  "scm:svn:http://svn.repo.com/flat-multi-module/",
71                  ReleaseUtil.realignScmUrl(0, "scm:svn:http://svn.repo.com/flat-multi-module/trunk/../"));
72          assertEquals(
73                  "scm:svn:http://svn.repo.com/flat-multi-module/branches",
74                  ReleaseUtil.realignScmUrl(0, "scm:svn:http://svn.repo.com/flat-multi-module/trunk/../branches"));
75      }
76  
77      @Test
78      public void testGetBaseScmUrlReturnOriginal() throws Exception {
79          assertEquals("no-path-elements", ReleaseUtil.realignScmUrl(1, "no-path-elements"));
80          assertEquals("no-path-elements", ReleaseUtil.realignScmUrl(15, "no-path-elements"));
81      }
82  
83      @Test
84      public void testGetBaseScmUrlOfFlatMultiModule() throws Exception {
85          String actual =
86                  ReleaseUtil.realignScmUrl(1, "scm:svn:http://svn.repo.com/flat-multi-module/trunk/root-project");
87          assertEquals("scm:svn:http://svn.repo.com/flat-multi-module/trunk", actual);
88  
89          actual = ReleaseUtil.realignScmUrl(1, "scm:svn:http://svn.repo.com/flat-multi-module/trunk/root-project/");
90          assertEquals("scm:svn:http://svn.repo.com/flat-multi-module/trunk/", actual);
91      }
92  
93      @Test
94      public void testGetBaseScmUrlOfFlatMultiModuleMultipleLevels() throws Exception {
95          String actual =
96                  ReleaseUtil.realignScmUrl(3, "scm:svn:http://svn.repo.com/flat-multi-module/trunk/root-project/1/2");
97          assertEquals("scm:svn:http://svn.repo.com/flat-multi-module/trunk", actual);
98  
99          actual = ReleaseUtil.realignScmUrl(3, "scm:svn:http://svn.repo.com/flat-multi-module/trunk/root-project/1/2/");
100         assertEquals("scm:svn:http://svn.repo.com/flat-multi-module/trunk/", actual);
101     }
102 
103     @Test
104     public void testGetBaseWorkingDirectoryParentCountSameDirectory() {
105         Path workingDirectory = Paths.get("/working/directory/maven/release");
106         Path basedir = Paths.get("/working/directory/maven/release");
107         assertEquals(0, ReleaseUtil.getBaseWorkingDirectoryParentCount(basedir, workingDirectory));
108     }
109 
110     @Test
111     public void testGetBaseWorkingDirectoryParentCountSameDirectoryDotCharacter() {
112         Path workingDirectory = Paths.get("/working/directory/maven/release/.").toAbsolutePath();
113         assertTrue(workingDirectory.toString().contains("."));
114         Path basedir = Paths.get("/working/directory/maven/release").toAbsolutePath();
115         assertEquals(0, ReleaseUtil.getBaseWorkingDirectoryParentCount(basedir, workingDirectory));
116 
117         // finish with slash
118         workingDirectory = Paths.get("/working/directory/maven/release/./").toAbsolutePath();
119         assertTrue(workingDirectory.toString().contains("."));
120         basedir = Paths.get("/working/directory/maven/release").toAbsolutePath();
121         assertEquals(0, ReleaseUtil.getBaseWorkingDirectoryParentCount(basedir, workingDirectory));
122     }
123 
124     @Test
125     public void testGetBaseWorkingDirectoryParentCountSubdirectory() {
126         Path workingDirectory = Paths.get("/working/directory/maven/release").toAbsolutePath();
127         Path basedir = Paths.get("/working/directory/maven/release/maven-release-manager")
128                 .toAbsolutePath();
129         assertEquals(0, ReleaseUtil.getBaseWorkingDirectoryParentCount(basedir, workingDirectory));
130     }
131 
132     @Test
133     public void testGetBaseWorkingDirectoryParentCountParentDirectory() {
134         Path workingDirectory = Paths.get("/working/directory/maven/release/maven-release-manager")
135                 .toAbsolutePath();
136         Path basedir = Paths.get("/working/directory/maven/release").toAbsolutePath();
137         assertEquals(1, ReleaseUtil.getBaseWorkingDirectoryParentCount(basedir, workingDirectory));
138     }
139 
140     @Test
141     public void testGetBaseWorkingDirectoryParentCountParentDirectoryDotCharacter() {
142         Path workingDirectory = Paths.get("/working/directory/maven/release/maven-release-manager/.")
143                 .toAbsolutePath();
144         assertTrue(workingDirectory.toString().contains("."));
145         Path basedir = Paths.get("/working/directory/maven/release").toAbsolutePath();
146         assertEquals(1, ReleaseUtil.getBaseWorkingDirectoryParentCount(basedir, workingDirectory));
147 
148         // finish with slash
149         workingDirectory = Paths.get("/working/directory/maven/release/maven-release-manager/./")
150                 .toAbsolutePath();
151         assertTrue(workingDirectory.toString().contains("."));
152         basedir = Paths.get("/working/directory/maven/release").toAbsolutePath();
153         assertEquals(1, ReleaseUtil.getBaseWorkingDirectoryParentCount(basedir, workingDirectory));
154     }
155 
156     @Test
157     public void testGetBaseWorkingDirectoryParentCountParentDirectoryMultiple() {
158         Path workingDirectory = Paths.get("/working/directory/maven/release/maven-release-manager")
159                 .toAbsolutePath();
160         Path basedir = Paths.get("/working/directory").toAbsolutePath();
161         assertEquals(3, ReleaseUtil.getBaseWorkingDirectoryParentCount(basedir, workingDirectory));
162     }
163 
164     @Test
165     public void testGetBaseWorkingDirectoryParentCountParentDirectoryMultipleDotCharacter() {
166         Path workingDirectory = Paths.get("/working/directory/maven/release/maven-release-manager/./.")
167                 .toAbsolutePath();
168         assertTrue(workingDirectory.toString().contains("."));
169         Path basedir = Paths.get("/working/directory").toAbsolutePath();
170         assertEquals(3, ReleaseUtil.getBaseWorkingDirectoryParentCount(basedir, workingDirectory));
171 
172         // finish with slash
173         workingDirectory = Paths.get("/working/directory/maven/release/maven-release-manager/././")
174                 .toAbsolutePath();
175         assertTrue(workingDirectory.toString().contains("."));
176         basedir = Paths.get("/working/directory").toAbsolutePath();
177         assertEquals(3, ReleaseUtil.getBaseWorkingDirectoryParentCount(basedir, workingDirectory));
178     }
179 
180     @Test
181     public void testGetBaseWorkingDirectoryParentCountDifferentCase() {
182         Path workingDirectory = Paths.get("/Working/Directory/maven/release/maven-release-manager")
183                 .toAbsolutePath();
184         Path basedir = Paths.get("/working/directory").toAbsolutePath();
185         assertEquals(3, ReleaseUtil.getBaseWorkingDirectoryParentCount(basedir, workingDirectory));
186     }
187 
188     /**
189      * MRELEASE-663
190      */
191     @Test
192     public void testGetWindowsRootBaseWorkingDirectoryParentCountDifferentCase() {
193         assumeTrue(Os.isFamily(Os.FAMILY_WINDOWS));
194 
195         assertEquals(
196                 2,
197                 ReleaseUtil.getBaseWorkingDirectoryParentCount(Paths.get("C:"), Paths.get("C:\\working\\directory")));
198         assertEquals(
199                 2,
200                 ReleaseUtil.getBaseWorkingDirectoryParentCount(Paths.get("C:"), Paths.get("C:\\working\\directory\\")));
201         assertEquals(
202                 2,
203                 ReleaseUtil.getBaseWorkingDirectoryParentCount(Paths.get("C:\\"), Paths.get("C:\\working\\directory")));
204         assertEquals(
205                 2,
206                 ReleaseUtil.getBaseWorkingDirectoryParentCount(
207                         Paths.get("C:\\"), Paths.get("C:\\working\\directory\\")));
208 
209         assertEquals(
210                 2,
211                 ReleaseUtil.getBaseWorkingDirectoryParentCount(Paths.get("c:"), Paths.get("C:\\working\\directory")));
212         assertEquals(
213                 2,
214                 ReleaseUtil.getBaseWorkingDirectoryParentCount(Paths.get("C:"), Paths.get("c:\\working\\directory")));
215         assertEquals(
216                 2,
217                 ReleaseUtil.getBaseWorkingDirectoryParentCount(Paths.get("c:"), Paths.get("c:\\working\\directory")));
218     }
219 }