001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.svn;
020
021import org.apache.maven.scm.ScmBranch;
022import org.apache.maven.scm.ScmRevision;
023import org.apache.maven.scm.ScmTag;
024import org.apache.maven.scm.ScmTestCase;
025import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
026import org.apache.maven.scm.repository.ScmRepository;
027import org.junit.Test;
028
029import static org.junit.Assert.assertEquals;
030import static org.junit.Assert.assertFalse;
031import static org.junit.Assert.assertTrue;
032
033/**
034 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
035 *
036 */
037public class SvnTagBranchUtilsTest extends ScmTestCase {
038    // ----------------------------------------------------------------------
039    // appendPath
040    // ----------------------------------------------------------------------
041
042    @Test
043    public void testAppendPath() throws Exception {
044        assertEquals(
045                "http://foo.com/svn/myproject/tags/foo",
046                SvnTagBranchUtils.appendPath("http://foo.com/svn", "myproject/tags/foo"));
047    }
048
049    @Test
050    public void testAppendPathNullAddlPath() throws Exception {
051        assertEquals("http://foo.com/svn", SvnTagBranchUtils.appendPath("http://foo.com/svn", null));
052    }
053
054    @Test
055    public void testAppendPathNullAddlTrailingSlash() throws Exception {
056        assertEquals("http://foo.com/svn", SvnTagBranchUtils.appendPath("http://foo.com/svn/", null));
057    }
058
059    @Test
060    public void testAppendPathTrailingSlash() throws Exception {
061        assertEquals(
062                "http://foo.com/svn/myproject/tags/foo",
063                SvnTagBranchUtils.appendPath("http://foo.com/svn/", "myproject/tags/foo"));
064    }
065
066    @Test
067    public void testAppendPathLeadingAndTrailingSlash() throws Exception {
068        assertEquals(
069                "http://foo.com/svn/myproject/tags/foo",
070                SvnTagBranchUtils.appendPath("http://foo.com/svn/", "/myproject/tags/foo"));
071    }
072
073    // ----------------------------------------------------------------------
074    // resolveTagBase
075    // ----------------------------------------------------------------------
076
077    @Test
078    public void testResolveTagBase() {
079        assertEquals(
080                "http://foo.com/svn/myproject/tags",
081                SvnTagBranchUtils.resolveTagBase("http://foo.com/svn/myproject/trunk"));
082        assertEquals(
083                "http://foo.com/svn/myproject/tags",
084                SvnTagBranchUtils.resolveTagBase("http://foo.com/svn/myproject/trunk/"));
085    }
086
087    // ----------------------------------------------------------------------
088    // getProjectRoot
089    // ----------------------------------------------------------------------
090
091    @Test
092    public void testGetProjectRootTagBranchTrunk() throws Exception {
093        // All of these should equate to the same project root
094        String[] paths = new String[] {
095            "scm:svn:http://foo.com/svn/tags/my-tag",
096            "scm:svn:http://foo.com/svn/tags",
097            "scm:svn:http://foo.com/svn/branches/my-branch",
098            "scm:svn:http://foo.com/svn/branches",
099            "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}