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.repository;
20  
21  import org.apache.maven.scm.ScmTestCase;
22  import org.apache.maven.scm.manager.ScmManager;
23  import org.apache.maven.scm.repository.ScmRepository;
24  import org.apache.maven.scm.repository.ScmRepositoryException;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  import static org.junit.Assert.assertEquals;
29  import static org.junit.Assert.assertNotNull;
30  import static org.junit.Assert.assertTrue;
31  import static org.junit.Assert.fail;
32  
33  /**
34   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
35   *
36   */
37  public class SvnScmProviderRepositoryTest extends ScmTestCase {
38      private ScmManager scmManager;
39  
40      @Before
41      @Override
42      public void setUp() throws Exception {
43          super.setUp();
44  
45          scmManager = getScmManager();
46      }
47  
48      // ----------------------------------------------------------------------
49      // Testing legal URLs
50      // ----------------------------------------------------------------------
51  
52      @Test
53      public void testLegalFileURL() throws Exception {
54          testUrl("scm:svn:file:///tmp/repo", "file:///tmp/repo", null, null, null);
55      }
56  
57      @Test
58      public void testLegalLocalhostFileURL() throws Exception {
59          testUrl("scm:svn:file://localhost/tmp/repo", "file://localhost/tmp/repo", null, null, null);
60      }
61  
62      @Test
63      public void testLegalHistnameFileURL() throws Exception {
64          testUrl("scm:svn:file://my_server/tmp/repo", "file://my_server/tmp/repo", null, null, null);
65      }
66  
67      @Test
68      public void testLegalHttpURL() throws Exception {
69          testUrl(
70                  "scm:svn:http://subversion.tigris.org",
71                  "http://subversion.tigris.org",
72                  null,
73                  null,
74                  "subversion.tigris.org");
75      }
76  
77      @Test
78      public void testLegalHttpURLWithUser() throws Exception {
79          testUrl(
80                  "scm:svn:http://user@subversion.tigris.org",
81                  "http://subversion.tigris.org",
82                  "user",
83                  null,
84                  "subversion.tigris.org");
85      }
86  
87      @Test
88      public void testLegalHttpURLWithUserPassword() throws Exception {
89          testUrl(
90                  "scm:svn:http://user:password@subversion.tigris.org",
91                  "http://subversion.tigris.org",
92                  "user",
93                  "password",
94                  "subversion.tigris.org");
95      }
96  
97      @Test
98      public void testLegalHttpsURL() throws Exception {
99          testUrl(
100                 "scm:svn:https://subversion.tigris.org",
101                 "https://subversion.tigris.org",
102                 null,
103                 null,
104                 "subversion.tigris.org");
105     }
106 
107     @Test
108     public void testLegalHttpsURLWithUser() throws Exception {
109         testUrl(
110                 "scm:svn:https://user@subversion.tigris.org",
111                 "https://subversion.tigris.org",
112                 "user",
113                 null,
114                 "subversion.tigris.org");
115     }
116 
117     @Test
118     public void testLegalHttpsURLWithUserPassword() throws Exception {
119         testUrl(
120                 "scm:svn:https://user:password@subversion.tigris.org",
121                 "https://subversion.tigris.org",
122                 "user",
123                 "password",
124                 "subversion.tigris.org");
125     }
126 
127     @Test
128     public void testLegalSvnURL() throws Exception {
129         testUrl(
130                 "scm:svn:svn://subversion.tigris.org",
131                 "svn://subversion.tigris.org",
132                 null,
133                 null,
134                 "subversion.tigris.org");
135     }
136 
137     @Test
138     public void testLegalSvnPlusUsernameURL() throws Exception {
139         testUrl(
140                 "scm:svn:svn://username@subversion.tigris.org",
141                 "svn://subversion.tigris.org",
142                 "username",
143                 null,
144                 "subversion.tigris.org");
145     }
146 
147     @Test
148     public void testLegalSvnPlusUsernamePasswordURL() throws Exception {
149         testUrl(
150                 "scm:svn:svn://username:password@subversion.tigris.org",
151                 "svn://subversion.tigris.org",
152                 "username",
153                 "password",
154                 "subversion.tigris.org");
155     }
156 
157     @Test
158     public void testLegalSvnPlusSshURL() throws Exception {
159         testUrl(
160                 "scm:svn:svn+ssh://subversion.tigris.org",
161                 "svn+ssh://subversion.tigris.org",
162                 null,
163                 null,
164                 "subversion.tigris.org");
165     }
166 
167     /* This test require a specific subversion config file
168     public void testLegalSvnPlusXxxURL()
169         throws Exception
170     {
171         testUrl( "scm:svn:svn+something://subversion.tigris.org", "svn+something://subversion.tigris.org", null,
172                  "subversion.tigris.org" );
173     }*/
174 
175     @Test
176     public void testLegalSvnPlusSshPlusUsernameURL() throws Exception {
177         testUrl(
178                 "scm:svn:svn+ssh://username@subversion.tigris.org",
179                 "svn+ssh://username@subversion.tigris.org",
180                 null,
181                 null,
182                 "username@subversion.tigris.org");
183     }
184 
185     /* This test require a specific subversion config file
186     public void testLegalSvnPlusXxxPlusUsernameURL()
187         throws Exception
188     {
189         testUrl( "scm:svn:svn+something://username@subversion.tigris.org", "svn+something://username@subversion.tigris.org", null,
190                  "username@subversion.tigris.org" );
191     }*/
192 
193     @Test
194     public void testLegalSvnPortUrl() throws Exception {
195         testUrl(
196                 "scm:svn:http://username@subversion.tigris.org:8800/pmgt/trunk",
197                 "http://subversion.tigris.org:8800/pmgt/trunk",
198                 "username",
199                 "subversion.tigris.org",
200                 8800);
201         testUrl(
202                 "scm:svn:https://username@subversion.tigris.org:8080/pmgt/trunk",
203                 "https://subversion.tigris.org:8080/pmgt/trunk",
204                 "username",
205                 "subversion.tigris.org",
206                 8080);
207         testUrl(
208                 "scm:svn:svn://username@subversion.tigris.org:8800/pmgt/trunk",
209                 "svn://subversion.tigris.org:8800/pmgt/trunk",
210                 "username",
211                 "subversion.tigris.org",
212                 8800);
213         testUrl(
214                 "scm:svn:svn+ssh://username@subversion.tigris.org:8080/pmgt/trunk",
215                 "svn+ssh://username@subversion.tigris.org:8080/pmgt/trunk",
216                 null,
217                 "username@subversion.tigris.org",
218                 8080);
219     }
220 
221     // ----------------------------------------------------------------------
222     // Testing illegal URLs
223     // ----------------------------------------------------------------------
224 
225     @Test
226     public void testIllegalFileUrl() throws Exception {
227         testIllegalUrl("file:/tmp/svn");
228     }
229 
230     // ----------------------------------------------------------------------
231     //
232     // ----------------------------------------------------------------------
233 
234     private void testUrl(
235             String scmUrl, String expectedUrl, String expectedUser, String expectedPassword, String expectedHost)
236             throws Exception {
237         ScmRepository repository = scmManager.makeScmRepository(scmUrl);
238 
239         assertNotNull("ScmManager.makeScmRepository() returned null", repository);
240 
241         assertNotNull("The provider repository was null.", repository.getProviderRepository());
242 
243         assertTrue(
244                 "The SCM Repository isn't a " + SvnScmProviderRepository.class.getName() + ".",
245                 repository.getProviderRepository() instanceof SvnScmProviderRepository);
246 
247         SvnScmProviderRepository providerRepository = (SvnScmProviderRepository) repository.getProviderRepository();
248 
249         assertEquals("url is incorrect", expectedUrl, providerRepository.getUrl());
250 
251         assertEquals("url string is incorrect", "svn:" + expectedUrl, repository.toString());
252 
253         assertEquals("User is incorrect", expectedUser, providerRepository.getUser());
254 
255         assertEquals("Password is incorrect", expectedPassword, providerRepository.getPassword());
256 
257         assertEquals(
258                 "Host is incorrect",
259                 expectedHost,
260                 ((SvnScmProviderRepository) repository.getProviderRepository()).getHost());
261     }
262 
263     private void testUrl(String scmUrl, String expectedUrl, String expectedUser, String expectedHost, int expectedPort)
264             throws Exception {
265         testUrl(scmUrl, expectedUrl, expectedUser, null, expectedHost);
266     }
267 
268     @SuppressWarnings("unused")
269     private void testUrl(
270             String scmUrl,
271             String expectedUrl,
272             String expectedUser,
273             String expectedPassword,
274             String expectedHost,
275             int expectedPort)
276             throws Exception {
277         testUrl(scmUrl, expectedUrl, expectedUser, expectedPassword, expectedHost);
278 
279         ScmRepository repository = scmManager.makeScmRepository(scmUrl);
280 
281         assertEquals(
282                 "Port is incorrect",
283                 expectedPort,
284                 ((SvnScmProviderRepository) repository.getProviderRepository()).getPort());
285     }
286 
287     private void testIllegalUrl(String url) throws Exception {
288         try {
289             scmManager.makeScmRepository("scm:svn:" + url);
290 
291             fail("Expected a ScmRepositoryException while testing the url '" + url + "'.");
292         } catch (ScmRepositoryException e) {
293             // expected
294         }
295     }
296 
297     @Test
298     public void testGetParent() {
299         new SvnScmProviderRepository("http://subversion.tigris.org");
300     }
301 
302     @Test
303     public void testGetParentDotSlashEndingURL() {
304         SvnScmProviderRepository slashDotRepo = new SvnScmProviderRepository("file://a/b/c/././.");
305         assertTrue(slashDotRepo.getParent() instanceof SvnScmProviderRepository);
306         assertEquals("file://a/b", ((SvnScmProviderRepository) slashDotRepo.getParent()).getUrl());
307     }
308 
309     @Test
310     public void testGetParentSlashEndingURL() {
311         SvnScmProviderRepository slashRepo = new SvnScmProviderRepository("file://a/b/c///");
312         assertTrue(slashRepo.getParent() instanceof SvnScmProviderRepository);
313         assertEquals("file://a/b", ((SvnScmProviderRepository) slashRepo.getParent()).getUrl());
314     }
315 }