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.scm.provider.svn;
20  
21  import org.apache.maven.scm.ScmBranch;
22  import org.apache.maven.scm.ScmRevision;
23  import org.apache.maven.scm.ScmTag;
24  import org.apache.maven.scm.ScmTestCase;
25  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
26  import org.apache.maven.scm.repository.ScmRepository;
27  import org.junit.Test;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertFalse;
31  import static org.junit.Assert.assertTrue;
32  
33  /**
34   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
35   *
36   */
37  public class SvnTagBranchUtilsTest extends ScmTestCase {
38      // ----------------------------------------------------------------------
39      // appendPath
40      // ----------------------------------------------------------------------
41  
42      @Test
43      public void testAppendPath() throws Exception {
44          assertEquals(
45                  "http://foo.com/svn/myproject/tags/foo",
46                  SvnTagBranchUtils.appendPath("http://foo.com/svn", "myproject/tags/foo"));
47      }
48  
49      @Test
50      public void testAppendPathNullAddlPath() throws Exception {
51          assertEquals("http://foo.com/svn", SvnTagBranchUtils.appendPath("http://foo.com/svn", null));
52      }
53  
54      @Test
55      public void testAppendPathNullAddlTrailingSlash() throws Exception {
56          assertEquals("http://foo.com/svn", SvnTagBranchUtils.appendPath("http://foo.com/svn/", null));
57      }
58  
59      @Test
60      public void testAppendPathTrailingSlash() throws Exception {
61          assertEquals(
62                  "http://foo.com/svn/myproject/tags/foo",
63                  SvnTagBranchUtils.appendPath("http://foo.com/svn/", "myproject/tags/foo"));
64      }
65  
66      @Test
67      public void testAppendPathLeadingAndTrailingSlash() throws Exception {
68          assertEquals(
69                  "http://foo.com/svn/myproject/tags/foo",
70                  SvnTagBranchUtils.appendPath("http://foo.com/svn/", "/myproject/tags/foo"));
71      }
72  
73      // ----------------------------------------------------------------------
74      // resolveTagBase
75      // ----------------------------------------------------------------------
76  
77      @Test
78      public void testResolveTagBase() {
79          assertEquals(
80                  "http://foo.com/svn/myproject/tags",
81                  SvnTagBranchUtils.resolveTagBase("http://foo.com/svn/myproject/trunk"));
82          assertEquals(
83                  "http://foo.com/svn/myproject/tags",
84                  SvnTagBranchUtils.resolveTagBase("http://foo.com/svn/myproject/trunk/"));
85      }
86  
87      // ----------------------------------------------------------------------
88      // getProjectRoot
89      // ----------------------------------------------------------------------
90  
91      @Test
92      public void testGetProjectRootTagBranchTrunk() throws Exception {
93          // All of these should equate to the same project root
94          String[] paths = new String[] {
95              "scm:svn:http://foo.com/svn/tags/my-tag",
96              "scm:svn:http://foo.com/svn/tags",
97              "scm:svn:http://foo.com/svn/branches/my-branch",
98              "scm:svn:http://foo.com/svn/branches",
99              "scm:svn:http://foo.com/svn/trunk",
100             "scm:svn:http://foo.com/svn/trunk/some/path/to/some/file"
101         };
102 
103         for (int i = 0; i < paths.length; i++) {
104             testGetProjectRoot(paths[i], "http://foo.com/svn");
105         }
106     }
107 
108     @Test
109     public void testGetProjectRootNoRootSpecifier() throws Exception {
110         testGetProjectRoot("scm:svn:http://foo.com/svn/", "http://foo.com/svn");
111 
112         testGetProjectRoot("scm:svn:http://foo.com/svn", "http://foo.com/svn");
113 
114         testGetProjectRoot("scm:svn:http://foo.com/svn/ntags", "http://foo.com/svn/ntags");
115 
116         testGetProjectRoot("scm:svn:http://foo.com/svn/nbranches", "http://foo.com/svn/nbranches");
117     }
118 
119     @Test
120     public void testGetProjectRootLooksLikeRootSpecifier() throws Exception {
121         testGetProjectRoot("scm:svn:http://foo.com/svn/tagst", "http://foo.com/svn/tagst");
122 
123         testGetProjectRoot("scm:svn:http://foo.com/svn/tagst/tags", "http://foo.com/svn/tagst");
124 
125         testGetProjectRoot("scm:svn:http://foo.com/svn/branchess", "http://foo.com/svn/branchess");
126     }
127 
128     @Test
129     public void testGetProjectRootDoubleProjectRoots() throws Exception {
130         testGetProjectRoot(
131                 "scm:svn:http://foo.com/svn/tags/my-tag/tags/another-tag/", "http://foo.com/svn/tags/my-tag");
132         testGetProjectRoot(
133                 "scm:svn:http://foo.com/svn/trunk/a_directory/trunk/", "http://foo.com/svn/trunk/a_directory");
134     }
135 
136     // ----------------------------------------------------------------------
137     // resolveTagUrl
138     // ----------------------------------------------------------------------
139 
140     @Test
141     public void testResolveTagRelative() throws Exception {
142         testResolveTagUrl("scm:svn:http://foo.com/svn/", "my-tag", "http://foo.com/svn/tags/my-tag");
143 
144         testResolveTagUrl("scm:svn:http://foo.com/svn/trunk", "my-tag", "http://foo.com/svn/tags/my-tag");
145 
146         testResolveTagUrl("scm:svn:http://foo.com/svn/trunk/", "my-tag", "http://foo.com/svn/tags/my-tag");
147 
148         testResolveTagUrl("scm:svn:http://foo.com/svn/branches", "my-tag", "http://foo.com/svn/tags/my-tag");
149 
150         testResolveTagUrl("scm:svn:http://foo.com/svn/tags", "my-tag", "http://foo.com/svn/tags/my-tag");
151     }
152 
153     @Test
154     public void testResolveTagAbsolute() throws Exception {
155         testResolveTagUrl(
156                 "scm:svn:http://foo.com/svn/",
157                 "http://foo.com/svn/branches/my-tag",
158                 "http://foo.com/svn/branches/my-tag");
159 
160         testResolveTagUrl(
161                 "scm:svn:http://foo.com/svn/",
162                 "file://C://svn/some/crazy/path/my-tag",
163                 "file://C://svn/some/crazy/path/my-tag");
164     }
165 
166     @Test
167     public void testResolveTagWithSlashes() throws Exception {
168         testResolveTagUrl("scm:svn:http://foo.com/svn/", "/my-tag/", "http://foo.com/svn/tags/my-tag");
169 
170         testResolveBranchUrl("scm:svn:http://foo.com/svn/", "/my-branch/", "http://foo.com/svn/branches/my-branch");
171 
172         testResolveBranchUrl(
173                 "scm:svn:http://foo.com/svn/",
174                 "http://foo.com/svn/myproject/branches/",
175                 "/my-branch/",
176                 "http://foo.com/svn/myproject/branches/my-branch");
177     }
178 
179     @Test
180     public void testResolveTagWithTagOverwritingBase() throws Exception {
181         testResolveTagUrl("scm:svn:http://foo.com/svn/", "branches/my-tag", "http://foo.com/svn/branches/my-tag");
182 
183         testResolveTagUrl("scm:svn:http://foo.com/svn/", "tags/my-tag", "http://foo.com/svn/tags/my-tag");
184 
185         // Not sure why you would ever specify a tag of /trunk/foo,
186         // but create the test case to assure consistent behavior in the future
187         testResolveTagUrl("scm:svn:http://foo.com/svn/", "trunk/my-tag", "http://foo.com/svn/trunk/my-tag");
188 
189         testResolveTagUrl(
190                 "scm:svn:svn+ssh://foo.com/svn/trunk/my_path/to/my_dir", "my-tag", "svn+ssh://foo.com/svn/tags/my-tag");
191 
192         testResolveTagUrl(
193                 "scm:svn:svn+ssh://foo.com/svn/trunk/my_path/to/my_dir/trunk/mydir",
194                 "my-tag",
195                 "svn+ssh://foo.com/svn/trunk/my_path/to/my_dir/tags/my-tag");
196         testResolveTagUrl(
197                 "scm:svn:file://localhost/C:/mydir/myproject/trunk/my-module/target/scm-src/trunk",
198                 "my-tag",
199                 "file://localhost/C:/mydir/myproject/trunk/my-module/target/scm-src/tags/my-tag");
200     }
201 
202     @Test
203     public void testResolveTagWithTagBaseSpecified() throws Exception {
204         testResolveTagUrl("scm:svn:http://foo.com/svn/", "../tags", "my-tag", "../tags/my-tag");
205 
206         testResolveTagUrl(
207                 "scm:svn:http://foo.com/svn/",
208                 "http://foo.com/svn/non-standard/tag/dir/",
209                 "my-tag",
210                 "http://foo.com/svn/non-standard/tag/dir/my-tag");
211     }
212 
213     @Test
214     public void testResolveTagLooksLikeOverwriteTagBase() throws Exception {
215         testResolveTagUrl("scm:svn:http://foo.com/svn/", "tagst/my-tag", "http://foo.com/svn/tags/tagst/my-tag");
216 
217         testResolveTagUrl("scm:svn:http://foo.com/svn/", "metatags/my-tag", "http://foo.com/svn/tags/metatags/my-tag");
218     }
219 
220     @Test
221     public void testResolveBranchSimple() throws Exception {
222         testResolveBranchUrl("scm:svn:http://foo.com/svn/", "my-branch", "http://foo.com/svn/branches/my-branch");
223 
224         testResolveBranchUrl(
225                 "scm:svn:svn+ssh://foo.com/svn/trunk", "my-branch", "svn+ssh://foo.com/svn/branches/my-branch");
226 
227         testResolveBranchUrl(
228                 "scm:svn:svn+ssh://foo.com/svn/trunk/my_path/to/my_dir",
229                 "my-branch",
230                 "svn+ssh://foo.com/svn/branches/my-branch");
231 
232         testResolveBranchUrl(
233                 "scm:svn:http://foo.com/svn/trunk", "branches/my-branch", "http://foo.com/svn/branches/my-branch");
234 
235         testResolveBranchUrl(
236                 "scm:svn:http://foo.com/svn/",
237                 "subbranches/my-branch",
238                 "http://foo.com/svn/branches/subbranches/my-branch");
239     }
240 
241     @Test
242     public void testResolveBranchTagBase() throws Exception {
243         testResolveBranchUrl("scm:svn:http://foo.com/svn/", "../branches", "my-branch", "../branches/my-branch");
244 
245         testResolveBranchUrl(
246                 "scm:svn:http://foo.com/svn/",
247                 "http://foo.com/svn/non-standard/branch/dir",
248                 "my-branch",
249                 "http://foo.com/svn/non-standard/branch/dir/my-branch");
250     }
251 
252     // ----------------------------------------------------------------------
253     // revisionArgument
254     // ----------------------------------------------------------------------
255 
256     @Test
257     public void testIsRevisionArgumentSimple() {
258         assertTrue(SvnTagBranchUtils.isRevisionSpecifier(new ScmRevision("12345")));
259 
260         assertTrue(SvnTagBranchUtils.isRevisionSpecifier(new ScmRevision("hEaD")));
261 
262         assertTrue(SvnTagBranchUtils.isRevisionSpecifier(new ScmRevision("bAsE")));
263 
264         assertTrue(SvnTagBranchUtils.isRevisionSpecifier(new ScmRevision("cOmMiTtEd")));
265 
266         assertTrue(SvnTagBranchUtils.isRevisionSpecifier(new ScmRevision("pReV")));
267 
268         assertTrue(SvnTagBranchUtils.isRevisionSpecifier(new ScmRevision("{2005-1-1}")));
269     }
270 
271     @Test
272     public void testIsRevisionArgumentRange() {
273         assertTrue(SvnTagBranchUtils.isRevisionSpecifier(new ScmRevision("12345:12345")));
274 
275         assertTrue(SvnTagBranchUtils.isRevisionSpecifier(new ScmRevision("1:2")));
276 
277         assertTrue(SvnTagBranchUtils.isRevisionSpecifier(new ScmRevision("hEaD:bAsE")));
278 
279         assertTrue(SvnTagBranchUtils.isRevisionSpecifier(new ScmRevision("BaSe:CoMmItTeD")));
280 
281         assertTrue(SvnTagBranchUtils.isRevisionSpecifier(new ScmRevision("{2004-1-1}:{2005-1-1}")));
282 
283         assertFalse(SvnTagBranchUtils.isRevisionSpecifier(new ScmRevision("BASE:")));
284         assertFalse(SvnTagBranchUtils.isRevisionSpecifier(new ScmRevision(":BASE")));
285     }
286 
287     // ----------------------------------------------------------------------
288     //
289     // ----------------------------------------------------------------------
290 
291     private SvnScmProviderRepository getSvnRepository(String scmUrl) throws Exception {
292         ScmRepository repository = getScmManager().makeScmRepository(scmUrl);
293 
294         return (SvnScmProviderRepository) repository.getProviderRepository();
295     }
296 
297     private void testGetProjectRoot(String scmUrl, String expected) throws Exception {
298         assertEquals(
299                 expected,
300                 SvnTagBranchUtils.getProjectRoot(getSvnRepository(scmUrl).getUrl()));
301     }
302 
303     private void testResolveTagUrl(String scmUrl, String tag, String expected) throws Exception {
304         testResolveTagUrl(scmUrl, null, tag, expected);
305     }
306 
307     private void testResolveTagUrl(String scmUrl, String tagBase, String tag, String expected) throws Exception {
308         SvnScmProviderRepository repository = getSvnRepository(scmUrl);
309 
310         if (tagBase != null) {
311             repository.setTagBase(tagBase);
312         }
313 
314         if (tagBase != null) {
315             assertEquals(repository.getTagBase(), tagBase);
316         } else {
317             assertEquals(repository.getTagBase(), SvnTagBranchUtils.resolveTagBase(repository.getUrl()));
318         }
319 
320         assertEquals(expected, SvnTagBranchUtils.resolveTagUrl(repository, new ScmTag(tag)));
321     }
322 
323     private void testResolveBranchUrl(String scmUrl, String branch, String expected) throws Exception {
324         testResolveBranchUrl(scmUrl, null, branch, expected);
325     }
326 
327     private void testResolveBranchUrl(String scmUrl, String branchBase, String branch, String expected)
328             throws Exception {
329         SvnScmProviderRepository repository = getSvnRepository(scmUrl);
330         if (branchBase != null) {
331             repository.setBranchBase(branchBase);
332         }
333 
334         if (branchBase != null) {
335             assertEquals(repository.getBranchBase(), branchBase);
336         } else {
337             assertEquals(repository.getBranchBase(), SvnTagBranchUtils.resolveBranchBase(repository.getUrl()));
338         }
339 
340         assertEquals(expected, SvnTagBranchUtils.resolveBranchUrl(repository, new ScmBranch(branch)));
341     }
342 }