View Javadoc
1   package org.apache.maven.scm.provider.jazz.repository;
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.ScmTestCase;
23  import org.apache.maven.scm.manager.NoSuchScmProviderException;
24  import org.apache.maven.scm.manager.ScmManager;
25  import org.apache.maven.scm.repository.ScmRepository;
26  import org.apache.maven.scm.repository.ScmRepositoryException;
27  
28  import java.util.List;
29  
30  /**
31   * @author <a href="mailto:ChrisGWarp@gmail.com">Chris Graham</a>
32   */
33  public class JazzScmProviderRepositoryTest
34      extends ScmTestCase
35  {
36      private ScmManager scmManager;
37  
38      public void setUp()
39          throws Exception
40      {
41          super.setUp();
42  
43          scmManager = getScmManager();
44      }
45  
46      // ----------------------------------------------------------------------
47      // Testing legal URLs
48      // "scm:jazz:[username[;password]@]http[s]://server_name[:port]/contextRoot:repositoryWorkspace";
49      // ----------------------------------------------------------------------
50  
51      public void testLegalFullHttpURI()
52          throws Exception
53      {
54          testUrl( "scm:jazz:username;password@http://server_name:9443/contextRoot:repositoryWorkspace",
55                   "http://server_name:9443/contextRoot", "username", "password", "server_name", 9443,
56                   "repositoryWorkspace" );
57      }
58  
59      public void testLegalHttpURI()
60          throws Exception
61      {
62          testUrl( "scm:jazz:http://server_name:9443/contextRoot:repositoryWorkspace",
63                   "http://server_name:9443/contextRoot", null, null, "server_name", 9443, "repositoryWorkspace" );
64      }
65  
66      public void testLegalFullHttpURIWithLongPath()
67          throws Exception
68      {
69          testUrl( "scm:jazz:username;password@http://server_name:9443/some/long/contextRoot:repositoryWorkspace",
70                   "http://server_name:9443/some/long/contextRoot", "username", "password", "server_name", 9443,
71                   "repositoryWorkspace" );
72      }
73  
74      public void testLegalHttpURIWithLongPath()
75          throws Exception
76      {
77          testUrl( "scm:jazz:http://server_name:9443/some/long/contextRoot:repositoryWorkspace",
78                   "http://server_name:9443/some/long/contextRoot", null, null, "server_name", 9443,
79                   "repositoryWorkspace" );
80      }
81  
82      public void testLegalFullHttpURIWithShortPath()
83          throws Exception
84      {
85          testUrl( "scm:jazz:username;password@http://server_name:9443/:repositoryWorkspace", "http://server_name:9443/",
86                   "username", "password", "server_name", 9443, "repositoryWorkspace" );
87      }
88  
89      public void testLegalHttpURIWithShortPathPath()
90          throws Exception
91      {
92          testUrl( "scm:jazz:http://server_name:9443/:repositoryWorkspace", "http://server_name:9443/", null, null,
93                   "server_name", 9443, "repositoryWorkspace" );
94      }
95  
96  
97      public void testLegalFullHttpURIWithShortPathAndNoPort()
98          throws Exception
99      {
100         testUrl( "scm:jazz:username;password@http://server_name/:repositoryWorkspace", "http://server_name/",
101                  "username", "password", "server_name", 0, "repositoryWorkspace" );
102     }
103 
104     public void testLegalHttpURIWithShortPathPathAndNoPort()
105         throws Exception
106     {
107         testUrl( "scm:jazz:http://server_name/:repositoryWorkspace", "http://server_name/", null, null, "server_name",
108                  0, "repositoryWorkspace" );
109     }
110 
111     public void testLegalHttpURIWithUser()
112         throws Exception
113     {
114         testUrl( "scm:jazz:username@http://server_name:9443/contextRoot:repositoryWorkspace",
115                  "http://server_name:9443/contextRoot", "username", null, "server_name", 9443, "repositoryWorkspace" );
116     }
117 
118     public void testLegalHttpURIWithUserAndPassword()
119         throws Exception
120     {
121         testUrl( "scm:jazz:username;password@http://server_name:9443/contextRoot:repositoryWorkspace",
122                  "http://server_name:9443/contextRoot", "username", "password", "server_name", 9443,
123                  "repositoryWorkspace" );
124     }
125 
126     public void testLegalFullHttpsURI()
127         throws Exception
128     {
129         testUrl( "scm:jazz:username;password@https://server_name:9443/contextRoot:repositoryWorkspace",
130                  "https://server_name:9443/contextRoot", "username", "password", "server_name", 9443,
131                  "repositoryWorkspace" );
132     }
133 
134     public void testLegalHttpsURI()
135         throws Exception
136     {
137         testUrl( "scm:jazz:https://server_name:9443/contextRoot:repositoryWorkspace",
138                  "https://server_name:9443/contextRoot", null, null, "server_name", 9443, "repositoryWorkspace" );
139     }
140 
141     public void testLegalHttpsURINoPort()
142         throws Exception
143     {
144         testUrl( "scm:jazz:https://server_name/contextRoot:repositoryWorkspace", "https://server_name/contextRoot",
145                  null, null, "server_name", 0, "repositoryWorkspace" );
146     }
147 
148     public void testLegalHttpsURIWithUser()
149         throws Exception
150     {
151         testUrl( "scm:jazz:username@https://server_name:9443/contextRoot:repositoryWorkspace",
152                  "https://server_name:9443/contextRoot", "username", null, "server_name", 9443, "repositoryWorkspace" );
153     }
154 
155     public void testLegalHttpsURIWithUserAndPassword()
156         throws Exception
157     {
158         testUrl( "scm:jazz:username;password@https://server_name:9443/contextRoot:repositoryWorkspace",
159                  "https://server_name:9443/contextRoot", "username", "password", "server_name", 9443,
160                  "repositoryWorkspace" );
161     }
162 
163     public void testLegalFullHttpURIWithSpaces()
164         throws Exception
165     {
166         testUrl( "scm:jazz:username;password@http://server_name:9443/contextRoot:repository Workspace",
167                  "http://server_name:9443/contextRoot", "username", "password", "server_name", 9443,
168                  "repository Workspace" );
169     }
170 
171     public void testLegalFullHttpsURIWithSpaces()
172         throws Exception
173     {
174         testUrl( "scm:jazz:username;password@https://server_name:9443/contextRoot:repository Workspace",
175                  "https://server_name:9443/contextRoot", "username", "password", "server_name", 9443,
176                  "repository Workspace" );
177     }
178 
179     public void testLegalFullHttpsURIWithSpacesAndQuote()
180         throws Exception
181     {
182         testUrl( "scm:jazz:username;password@https://server_name:9443/contextRoot:Dave's Repository Workspace",
183                  "https://server_name:9443/contextRoot", "username", "password", "server_name", 9443,
184                  "Dave's Repository Workspace" );
185     }
186 
187     // ----------------------------------------------------------------------
188     // Testing illegal URLs
189     // "scm:jazz:[username[;password]@]http[s]://server_name[:port]/contextRoot:repositoryWorkspace";
190     // Something missing or broken in the above.
191     // ----------------------------------------------------------------------
192 
193     public void testIllegalFullHttpsURIWithMissingPathOrWorkspace()
194         throws Exception
195     {
196         testBrokenUrl( "scm:jazz:username;password@https://server_name:9443/contextRootOrWorkspaceIsMissing" );
197     }
198 
199     public void testIllegalFullHttpsURIWithMissingPathOrWorkspaceAndPort()
200         throws Exception
201     {
202         testBrokenUrl( "scm:jazz:username;password@https://server_name/contextRootOrWorkspaceIsMissing" );
203     }
204 
205     public void testIllegalFullHttpsURIWithMissingPathOrWorkspaceAndUsernameAndPasswordAndPort()
206         throws Exception
207     {
208         testBrokenUrl( "scm:jazz:https://server_name/contextRootOrWorkspaceIsMissing" );
209     }
210 
211     public void testIllegalFullHttpsURIWithMissingPortButWithColon()
212         throws Exception
213     {
214         testBrokenUrl( "scm:jazz:username;password@https://server_name:/contextRoot:repositoryWorkspace" );
215     }
216 
217     public void testIllegalFullHttpsURIWithMissingPortAndPathOrWorkspace()
218         throws Exception
219     {
220         testBrokenUrl( "scm:jazz:username;password@https://server_name/contextRootOrWorkspaceIsMissing" );
221     }
222 
223     public void testIllegalWrongProtocolURI()
224         throws Exception
225     {
226         testBrokenUrl( "scm:jazz:username;password@ssh://server_name/contextRoot/repositoryWorkspace" );
227     }
228 
229     public void testIllegalFullHttpsURIWithBadPort()
230         throws Exception
231     {
232         testBrokenUrl( "scm:jazz:username;password@https://server_name:xxxx/contextRoot:repositoryWorkspace" );
233     }
234 
235     // ----------------------------------------------------------------------
236     //
237     // ----------------------------------------------------------------------
238 
239     private void testUrl( String scmUrl, String expectedrepositoryURI, String expectedUser, String expectedPassword,
240                           String expectedHost, int expectedPort, String expectedRepositoryWorkspace )
241         throws Exception
242     {
243         // The messages are the lines returned from the ScmRepositoryException when thrown on a failure.
244         List<String> messages = scmManager.validateScmRepository( scmUrl );
245         assertEquals(
246             "Excepected zero messages back from URL Validation, but got: " + messages.size() + " messages. Contents = "
247                 + messages, 0, messages.size() );
248 
249         // Get an instance of the JazzScmProviderRepository, parsing the URL as we go.
250         ScmRepository repository = scmManager.makeScmRepository( scmUrl );
251 
252         assertNotNull( "ScmManager.makeScmRepository() returned null!", repository );
253 
254         assertNotNull( "The provider repository was null!", repository.getProviderRepository() );
255 
256         assertTrue( "The SCM Repository isn't a " + JazzScmProviderRepository.class.getName() + "!",
257                     repository.getProviderRepository() instanceof JazzScmProviderRepository );
258 
259         // Now that we have it, query the rest of the jazz specific values. 
260         JazzScmProviderRepository providerRepository = (JazzScmProviderRepository) repository.getProviderRepository();
261 
262         assertEquals( "The URI is incorrect!", expectedrepositoryURI, providerRepository.getRepositoryURI() );
263 
264         assertEquals( "The URI string is incorrect!",
265                       "jazz:" + expectedrepositoryURI + ":" + expectedRepositoryWorkspace, repository.toString() );
266 
267         assertEquals( "The user is incorrect!", expectedUser, providerRepository.getUser() );
268 
269         assertEquals( "The password is incorrect!", expectedPassword, providerRepository.getPassword() );
270 
271         assertEquals( "The host is incorrect!", expectedHost,
272                       ( (JazzScmProviderRepository) repository.getProviderRepository() ).getHost() );
273 
274         if ( expectedPort > 0 )
275         {
276             assertEquals( "The port is incorrect!", expectedPort, providerRepository.getPort() );
277         }
278 
279         assertEquals( "The RepositoryWorkspace is incorrect!", expectedRepositoryWorkspace,
280                       providerRepository.getRepositoryWorkspace() );
281     }
282 
283     private void testBrokenUrl( String scmUrl )
284     {
285         try
286         {
287             ScmRepository repository = scmManager.makeScmRepository( scmUrl );
288             fail( "The expected ScmRepositoryException did not occur! " + repository );
289         }
290         catch ( ScmRepositoryException expected )
291         {
292             // This is the expected behaviour, so we do nothing.
293         }
294         catch ( NoSuchScmProviderException unexpected )
295         {
296             fail( "Unexpected failure! " + unexpected.getMessage() );
297         }
298     }
299 }