1   package org.apache.maven.scm.provider.svn;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ScmBranch;
23  import org.apache.maven.scm.ScmRevision;
24  import org.apache.maven.scm.ScmTag;
25  import org.apache.maven.scm.ScmTestCase;
26  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
27  import org.apache.maven.scm.repository.ScmRepository;
28  
29  /**
30   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
31   * @version $Id: SvnTagBranchUtilsTest.java 687028 2008-08-19 12:02:05Z vsiveton $
32   */
33  public class SvnTagBranchUtilsTest
34      extends ScmTestCase
35  {
36      // ----------------------------------------------------------------------
37      // appendPath
38      // ----------------------------------------------------------------------
39  
40      public void testAppendPath()
41          throws Exception
42      {
43          assertEquals( "http://foo.com/svn/myproject/tags/foo",
44                        SvnTagBranchUtils.appendPath( "http://foo.com/svn", "myproject/tags/foo" ) );
45      }
46  
47      public void testAppendPathNullAddlPath()
48          throws Exception
49      {
50          assertEquals( "http://foo.com/svn", SvnTagBranchUtils.appendPath( "http://foo.com/svn", null ) );
51      }
52  
53      public void testAppendPathNullAddlTrailingSlash()
54          throws Exception
55      {
56          assertEquals( "http://foo.com/svn", SvnTagBranchUtils.appendPath( "http://foo.com/svn/", null ) );
57      }
58  
59      public void testAppendPathTrailingSlash()
60          throws Exception
61      {
62          assertEquals( "http://foo.com/svn/myproject/tags/foo",
63                        SvnTagBranchUtils.appendPath( "http://foo.com/svn/", "myproject/tags/foo" ) );
64      }
65  
66      public void testAppendPathLeadingAndTrailingSlash()
67          throws Exception
68      {
69          assertEquals( "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      public void testResolveTagBase()
78      {
79          assertEquals( "http://foo.com/svn/myproject/tags",
80                        SvnTagBranchUtils.resolveTagBase( "http://foo.com/svn/myproject/trunk" ) );
81          assertEquals( "http://foo.com/svn/myproject/tags",
82                        SvnTagBranchUtils.resolveTagBase( "http://foo.com/svn/myproject/trunk/" ) );
83      }
84  
85      // ----------------------------------------------------------------------
86      // getProjectRoot
87      // ----------------------------------------------------------------------
88  
89      public void testGetProjectRootTagBranchTrunk()
90          throws Exception
91      {
92          // All of these should equate to the same project root
93          String[] paths = new String[]{"scm:svn:http://foo.com/svn/tags/my-tag", "scm:svn:http://foo.com/svn/tags",
94              "scm:svn:http://foo.com/svn/branches/my-branch", "scm:svn:http://foo.com/svn/branches",
95              "scm:svn:http://foo.com/svn/trunk", "scm:svn:http://foo.com/svn/trunk/some/path/to/some/file"};
96  
97          for ( int i = 0; i < paths.length; i++ )
98          {
99              testGetProjectRoot( paths[i], "http://foo.com/svn" );
100         }
101     }
102 
103     public void testGetProjectRootNoRootSpecifier()
104         throws Exception
105     {
106         testGetProjectRoot( "scm:svn:http://foo.com/svn/", "http://foo.com/svn" );
107 
108         testGetProjectRoot( "scm:svn:http://foo.com/svn", "http://foo.com/svn" );
109 
110         testGetProjectRoot( "scm:svn:http://foo.com/svn/ntags", "http://foo.com/svn/ntags" );
111 
112         testGetProjectRoot( "scm:svn:http://foo.com/svn/nbranches", "http://foo.com/svn/nbranches" );
113     }
114 
115     public void testGetProjectRootLooksLikeRootSpecifier()
116         throws Exception
117     {
118         testGetProjectRoot( "scm:svn:http://foo.com/svn/tagst", "http://foo.com/svn/tagst" );
119 
120         testGetProjectRoot( "scm:svn:http://foo.com/svn/tagst/tags", "http://foo.com/svn/tagst" );
121 
122         testGetProjectRoot( "scm:svn:http://foo.com/svn/branchess", "http://foo.com/svn/branchess" );
123 
124     }
125 
126     public void testGetProjectRootDoubleProjectRoots()
127         throws Exception
128     {
129         testGetProjectRoot( "scm:svn:http://foo.com/svn/tags/my-tag/tags/another-tag/",
130                             "http://foo.com/svn/tags/my-tag" );
131         testGetProjectRoot( "scm:svn:http://foo.com/svn/trunk/a_directory/trunk/",
132                             "http://foo.com/svn/trunk/a_directory" );
133     }
134 
135     // ----------------------------------------------------------------------
136     // resolveTagUrl
137     // ----------------------------------------------------------------------
138 
139     public void testResolveTagRelative()
140         throws Exception
141     {
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     public void testResolveTagAbsolute()
154         throws Exception
155     {
156         testResolveTagUrl( "scm:svn:http://foo.com/svn/", "http://foo.com/svn/branches/my-tag",
157                            "http://foo.com/svn/branches/my-tag" );
158 
159         testResolveTagUrl( "scm:svn:http://foo.com/svn/", "file://C://svn/some/crazy/path/my-tag",
160                            "file://C://svn/some/crazy/path/my-tag" );
161 
162     }
163 
164     public void testResolveTagViewCVS()
165         throws Exception
166     {
167         assertEquals( "http://foo.com/cgi-bin/viewcvs.cgi/svn/tags/my-tag?root=test", SvnTagBranchUtils.resolveTagUrl(
168             "http://foo.com/cgi-bin/viewcvs.cgi/svn/trunk/?root=test", new ScmTag( "/my-tag/" ) ) );
169     }
170 
171     public void testResolveTagWithSlashes()
172         throws Exception
173     {
174         testResolveTagUrl( "scm:svn:http://foo.com/svn/", "/my-tag/", "http://foo.com/svn/tags/my-tag" );
175 
176         testResolveBranchUrl( "scm:svn:http://foo.com/svn/", "/my-branch/", "http://foo.com/svn/branches/my-branch" );
177 
178         testResolveBranchUrl( "scm:svn:http://foo.com/svn/", "http://foo.com/svn/myproject/branches/", "/my-branch/",
179                               "http://foo.com/svn/myproject/branches/my-branch" );
180     }
181 
182     public void testResolveTagWithTagOverwritingBase()
183         throws Exception
184     {
185         testResolveTagUrl( "scm:svn:http://foo.com/svn/", "branches/my-tag", "http://foo.com/svn/branches/my-tag" );
186 
187         testResolveTagUrl( "scm:svn:http://foo.com/svn/", "tags/my-tag", "http://foo.com/svn/tags/my-tag" );
188 
189         // Not sure why you would ever specify a tag of /trunk/foo,
190         // but create the test case to assure consistent behavior in the future
191         testResolveTagUrl( "scm:svn:http://foo.com/svn/", "trunk/my-tag", "http://foo.com/svn/trunk/my-tag" );
192 
193         testResolveTagUrl( "scm:svn:svn+ssh://foo.com/svn/trunk/my_path/to/my_dir", "my-tag",
194                            "svn+ssh://foo.com/svn/tags/my-tag" );
195 
196         testResolveTagUrl( "scm:svn:svn+ssh://foo.com/svn/trunk/my_path/to/my_dir/trunk/mydir", "my-tag",
197                            "svn+ssh://foo.com/svn/trunk/my_path/to/my_dir/tags/my-tag" );
198         testResolveTagUrl( "scm:svn:file://localhost/C:/mydir/myproject/trunk/my-module/target/scm-src/trunk", "my-tag",
199                            "file://localhost/C:/mydir/myproject/trunk/my-module/target/scm-src/tags/my-tag" );
200     }
201 
202     public void testResolveTagWithTagBaseSpecified()
203         throws Exception
204     {
205         testResolveTagUrl( "scm:svn:http://foo.com/svn/", "../tags", "my-tag", "../tags/my-tag" );
206 
207         testResolveTagUrl( "scm:svn:http://foo.com/svn/", "http://foo.com/svn/non-standard/tag/dir/", "my-tag",
208                            "http://foo.com/svn/non-standard/tag/dir/my-tag" );
209     }
210 
211     public void testResolveTagLooksLikeOverwriteTagBase()
212         throws Exception
213     {
214         testResolveTagUrl( "scm:svn:http://foo.com/svn/", "tagst/my-tag", "http://foo.com/svn/tags/tagst/my-tag" );
215 
216         testResolveTagUrl( "scm:svn:http://foo.com/svn/", "metatags/my-tag",
217                            "http://foo.com/svn/tags/metatags/my-tag" );
218     }
219 
220     public void testResolveBranchSimple()
221         throws Exception
222     {
223         testResolveBranchUrl( "scm:svn:http://foo.com/svn/", "my-branch", "http://foo.com/svn/branches/my-branch" );
224 
225         testResolveBranchUrl( "scm:svn:svn+ssh://foo.com/svn/trunk", "my-branch",
226                               "svn+ssh://foo.com/svn/branches/my-branch" );
227 
228         testResolveBranchUrl( "scm:svn:svn+ssh://foo.com/svn/trunk/my_path/to/my_dir", "my-branch",
229                               "svn+ssh://foo.com/svn/branches/my-branch" );
230 
231         testResolveBranchUrl( "scm:svn:http://foo.com/svn/trunk", "branches/my-branch",
232                               "http://foo.com/svn/branches/my-branch" );
233 
234         testResolveBranchUrl( "scm:svn:http://foo.com/svn/", "subbranches/my-branch",
235                               "http://foo.com/svn/branches/subbranches/my-branch" );
236 
237     }
238 
239     public void testResolveBranchTagBase()
240         throws Exception
241     {
242         testResolveBranchUrl( "scm:svn:http://foo.com/svn/", "../branches", "my-branch", "../branches/my-branch" );
243 
244         testResolveBranchUrl( "scm:svn:http://foo.com/svn/", "http://foo.com/svn/non-standard/branch/dir", "my-branch",
245                               "http://foo.com/svn/non-standard/branch/dir/my-branch" );
246     }
247 
248     // ----------------------------------------------------------------------
249     // revisionArgument
250     // ----------------------------------------------------------------------
251 
252     public void testIsRevisionArgumentSimple()
253     {
254         assertTrue( SvnTagBranchUtils.isRevisionSpecifier( new ScmRevision( "12345" ) ) );
255 
256         assertTrue( SvnTagBranchUtils.isRevisionSpecifier( new ScmRevision( "hEaD" ) ) );
257 
258         assertTrue( SvnTagBranchUtils.isRevisionSpecifier( new ScmRevision( "bAsE" ) ) );
259 
260         assertTrue( SvnTagBranchUtils.isRevisionSpecifier( new ScmRevision( "cOmMiTtEd" ) ) );
261 
262         assertTrue( SvnTagBranchUtils.isRevisionSpecifier( new ScmRevision( "pReV" ) ) );
263 
264         assertTrue( SvnTagBranchUtils.isRevisionSpecifier( new ScmRevision( "{2005-1-1}" ) ) );
265 
266     }
267 
268     public void testIsRevisionArgumentRange()
269     {
270         assertTrue( SvnTagBranchUtils.isRevisionSpecifier( new ScmRevision( "12345:12345" ) ) );
271 
272         assertTrue( SvnTagBranchUtils.isRevisionSpecifier( new ScmRevision( "1:2" ) ) );
273 
274         assertTrue( SvnTagBranchUtils.isRevisionSpecifier( new ScmRevision( "hEaD:bAsE" ) ) );
275 
276         assertTrue( SvnTagBranchUtils.isRevisionSpecifier( new ScmRevision( "BaSe:CoMmItTeD" ) ) );
277 
278         assertTrue( SvnTagBranchUtils.isRevisionSpecifier( new ScmRevision( "{2004-1-1}:{2005-1-1}" ) ) );
279 
280         assertFalse( SvnTagBranchUtils.isRevisionSpecifier( new ScmRevision( "BASE:" ) ) );
281         assertFalse( SvnTagBranchUtils.isRevisionSpecifier( new ScmRevision( ":BASE" ) ) );
282 
283     }
284 
285     /**
286      * Test SCM-379
287      *
288      * @throws Exception if any
289      */
290     public void testResolveUrlWithQuery()
291         throws Exception
292     {
293         String url = "https://myserver/plugins/scmsvn/viewcvs.php/pom/trunk?root=myproj";
294 
295         SvnScmProviderRepository repo = new SvnScmProviderRepository( url );
296 
297         assertEquals( "https://myserver/plugins/scmsvn/viewcvs.php/pom/trunk/tags/mytag-1?root=myproj",
298                       SvnTagBranchUtils.resolveTagUrl( repo, new ScmTag( "mytag-1" ) ) );
299     }
300 
301     // ----------------------------------------------------------------------
302     //
303     // ----------------------------------------------------------------------
304 
305     private SvnScmProviderRepository getSvnRepository( String scmUrl )
306         throws Exception
307     {
308         ScmRepository repository = getScmManager().makeScmRepository( scmUrl );
309 
310         return (SvnScmProviderRepository) repository.getProviderRepository();
311     }
312 
313     private void testGetProjectRoot( String scmUrl, String expected )
314         throws Exception
315     {
316         assertEquals( expected, SvnTagBranchUtils.getProjectRoot( getSvnRepository( scmUrl ).getUrl() ) );
317     }
318 
319     private void testResolveTagUrl( String scmUrl, String tag, String expected )
320         throws Exception
321     {
322         testResolveTagUrl( scmUrl, null, tag, expected );
323     }
324 
325     private void testResolveTagUrl( String scmUrl, String tagBase, String tag, String expected )
326         throws Exception
327     {
328         SvnScmProviderRepository repository = getSvnRepository( scmUrl );
329 
330         if ( tagBase != null )
331         {
332             repository.setTagBase( tagBase );
333         }
334 
335         if ( tagBase != null )
336         {
337             assertEquals( repository.getTagBase(), tagBase );
338         }
339         else
340         {
341             assertEquals( repository.getTagBase(), SvnTagBranchUtils.resolveTagBase( repository.getUrl() ) );
342         }
343 
344         assertEquals( expected, SvnTagBranchUtils.resolveTagUrl( repository, new ScmTag( tag ) ) );
345     }
346 
347     private void testResolveBranchUrl( String scmUrl, String branch, String expected )
348         throws Exception
349     {
350         testResolveBranchUrl( scmUrl, null, branch, expected );
351     }
352 
353     private void testResolveBranchUrl( String scmUrl, String branchBase, String branch, String expected )
354         throws Exception
355     {
356         SvnScmProviderRepository repository = getSvnRepository( scmUrl );
357         if ( branchBase != null )
358         {
359             repository.setBranchBase( branchBase );
360         }
361 
362         if ( branchBase != null )
363         {
364             assertEquals( repository.getBranchBase(), branchBase );
365         }
366         else
367         {
368             assertEquals( repository.getBranchBase(), SvnTagBranchUtils.resolveBranchBase( repository.getUrl() ) );
369         }
370 
371         assertEquals( expected, SvnTagBranchUtils.resolveBranchUrl( repository, new ScmBranch( branch ) ) );
372     }
373 
374 }