View Javadoc

1   package org.apache.maven.archiva.database.browsing;
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.archiva.database.AbstractArchivaDatabaseTestCase;
23  import org.apache.maven.archiva.database.ArchivaDatabaseException;
24  import org.apache.maven.archiva.database.ArtifactDAO;
25  import org.apache.maven.archiva.database.Constraint;
26  import org.apache.maven.archiva.database.ObjectNotFoundException;
27  import org.apache.maven.archiva.database.constraints.ArtifactsRelatedConstraint;
28  import org.apache.maven.archiva.model.ArchivaArtifact;
29  import org.apache.maven.archiva.model.ArchivaProjectModel;
30  
31  import java.util.ArrayList;
32  import java.util.Date;
33  import java.util.List;
34  
35  /**
36   * RepositoryBrowsingTest 
37   *
38   * @version $Id: RepositoryBrowsingTest.java 930242 2010-04-02 11:18:43Z oching $
39   */
40  public class RepositoryBrowsingTest
41      extends AbstractArchivaDatabaseTestCase
42  {
43      private static final List<String> GUEST_REPO_IDS;
44  
45      private static final String USER_GUEST = "guest";
46      
47      static
48      {
49          GUEST_REPO_IDS = new ArrayList<String>();        
50          GUEST_REPO_IDS.add( "snapshots" );
51          GUEST_REPO_IDS.add( "central" );        
52      }
53      
54      private ArtifactDAO artifactDao;
55  
56      private ArchivaArtifact createArtifact( String groupId, String artifactId, String version )
57      {
58          ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, "", "jar", "central" );
59          artifact.getModel().setLastModified( new Date() ); // mandatory field.
60          artifact.getModel().setRepositoryId( "central" );
61          return artifact;
62      }
63  
64      private RepositoryBrowsing lookupBrowser()
65          throws Exception
66      {
67          RepositoryBrowsing browser = (RepositoryBrowsing) lookup( RepositoryBrowsing.class );
68          assertNotNull( "RepositoryBrowsing should not be null.", browser );
69          return browser;
70      }
71  
72      private void saveTestData()
73          throws Exception
74      {
75          ArchivaArtifact artifact;
76  
77          // Setup artifacts in fresh DB.
78          artifact = createArtifact( "commons-lang", "commons-lang", "2.0" );
79          artifactDao.saveArtifact( artifact );
80          assertArtifactWasSaved( "commons-lang", "commons-lang", "2.0" );
81          
82          artifact = createArtifact( "commons-lang", "commons-lang", "2.1" );
83          artifactDao.saveArtifact( artifact );
84          assertArtifactWasSaved( "commons-lang", "commons-lang", "2.1" );
85  
86          artifact = createArtifact( "org.apache.maven.test", "test-one", "1.2" );
87          artifactDao.saveArtifact( artifact );
88          assertArtifactWasSaved( "org.apache.maven.test", "test-one", "1.2" );
89  
90          artifact = createArtifact( "org.apache.maven.test.foo", "test-two", "1.0" );
91          artifactDao.saveArtifact( artifact );
92          assertArtifactWasSaved( "org.apache.maven.test.foo", "test-two", "1.0" );
93  
94          artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.0" );
95          artifactDao.saveArtifact( artifact );
96          assertArtifactWasSaved( "org.apache.maven.shared", "test-two", "2.0" );
97  
98          artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1-SNAPSHOT" );
99          artifactDao.saveArtifact( artifact );
100         assertArtifactWasSaved( "org.apache.maven.shared", "test-two", "2.1-SNAPSHOT" );
101         
102         artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1-20070522.143249-1" );
103         artifactDao.saveArtifact( artifact );
104         assertArtifactWasSaved( "org.apache.maven.shared", "test-two", "2.1-20070522.143249-1" );
105         
106         artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1-20070522.153141-2" );
107         artifactDao.saveArtifact( artifact );
108         assertArtifactWasSaved( "org.apache.maven.shared", "test-two", "2.1-20070522.153141-2" );
109 
110         artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1.1" );
111         artifactDao.saveArtifact( artifact );
112         assertArtifactWasSaved( "org.apache.maven.shared", "test-two", "2.1.1" );
113 
114         artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1-alpha-1" );
115         artifactDao.saveArtifact( artifact );
116         assertArtifactWasSaved( "org.apache.maven.shared", "test-two", "2.1-alpha-1" );
117 
118         artifact = createArtifact( "org.apache.maven.shared", "test-bar", "2.1" );
119         artifactDao.saveArtifact( artifact );
120         assertArtifactWasSaved( "org.apache.maven.shared", "test-bar", "2.1" );
121 
122         artifact = createArtifact( "org.codehaus.modello", "modellong", "3.0" );
123         artifactDao.saveArtifact( artifact );
124         assertArtifactWasSaved( "org.codehaus.modello", "modellong", "3.0" );
125         
126         artifact = createArtifact( "org.apache.archiva", "archiva-indexer", "1.0-20070522.143249-1" );
127         artifactDao.saveArtifact( artifact );
128         assertArtifactWasSaved( "org.apache.archiva", "archiva-indexer", "1.0-20070522.143249-1" );
129         
130         artifact = createArtifact( "org.apache.archiva", "archiva-indexer", "1.0-20070522.153141-2" );
131         artifactDao.saveArtifact( artifact );
132         assertArtifactWasSaved( "org.apache.archiva", "archiva-indexer", "1.0-20070522.153141-2" );
133     }
134 
135     private void assertArtifactWasSaved(String groupId, String artifactId, String version)
136         throws ObjectNotFoundException, ArchivaDatabaseException
137     {
138         Constraint constraint = new ArtifactsRelatedConstraint( groupId, artifactId, version );
139         List<ArchivaArtifact> artifacts = artifactDao.queryArtifacts( constraint );
140         
141         assertFalse( "Artifact '" + groupId + ":" + artifactId + ":" + version + "' should have been found.",
142                      artifacts.isEmpty() );
143     }
144 
145     public void testBrowseIntoGroupWithSubgroups()
146         throws Exception
147     {
148         RepositoryBrowsing browser = lookupBrowser();
149         BrowsingResults results = browser.selectGroupId( USER_GUEST, GUEST_REPO_IDS, "org.apache.maven.test" );
150         assertNotNull( "Browsing Results should not be null.", results );
151 
152         String expectedSubGroupIds[] = new String[] { "org.apache.maven.test.foo" };
153         assertGroupIds( "Browsing Results (subgroup org.apache.maven.test)", results.getGroupIds(), expectedSubGroupIds );
154     }
155 
156     public void testSimpleBrowse()
157         throws Exception
158     {
159         RepositoryBrowsing browser = lookupBrowser();
160         BrowsingResults results = browser.getRoot( USER_GUEST, GUEST_REPO_IDS );
161         assertNotNull( "Browsing Results should not be null.", results );
162 
163         String expectedRootGroupIds[] = new String[] { "commons-lang", "org" };
164 
165         assertGroupIds( "Browsing Results (root)", results.getGroupIds(), expectedRootGroupIds );
166     }
167 
168     public void testViewArtifact()
169         throws Exception
170     {
171         RepositoryBrowsing browser = lookupBrowser();
172         ArchivaProjectModel artifact = browser.selectVersion( USER_GUEST, GUEST_REPO_IDS, "commons-lang", "commons-lang", "2.0" );
173         assertNotNull( "Artifact should not be null.", artifact );
174 		assertEquals( "commons-lang", artifact.getGroupId() );
175 		assertEquals( "commons-lang", artifact.getArtifactId() );
176 		assertEquals( "2.0", artifact.getVersion() );		
177 		assertEquals( "jar", artifact.getPackaging() );
178 	
179 		// MRM-1278
180 		String repoId = browser.getRepositoryId( USER_GUEST, GUEST_REPO_IDS, "commons-lang", "commons-lang", "2.0" );
181 		assertEquals( "central", repoId );
182     }    
183     
184     public void testViewArtifactWithMultipleTimestampedVersions()
185         throws Exception
186     {   
187         RepositoryBrowsing browser = lookupBrowser();
188         ArchivaProjectModel artifact = browser.selectVersion( USER_GUEST, GUEST_REPO_IDS, "org.apache.archiva", "archiva-indexer", "1.0-SNAPSHOT" );
189         assertNotNull( "Artifact should not be null.", artifact );
190         assertEquals( "org.apache.archiva", artifact.getGroupId() );
191         assertEquals( "archiva-indexer", artifact.getArtifactId() );
192         assertEquals( "1.0-20070522.153141-2", artifact.getVersion() );       
193         assertEquals( "jar", artifact.getPackaging() );
194         
195         String repoId = browser.getRepositoryId( USER_GUEST, GUEST_REPO_IDS, "org.apache.archiva", "archiva-indexer", "1.0-SNAPSHOT" );
196         assertEquals( "central", repoId );
197     }
198     
199     public void testSelectArtifactId()
200         throws Exception
201     {   
202         RepositoryBrowsing browser = lookupBrowser();
203         BrowsingResults results =
204             browser.selectArtifactId( USER_GUEST, GUEST_REPO_IDS, "org.apache.maven.shared", "test-two" );
205         assertNotNull( "Browsing results should not be null.", results );
206         assertEquals( 4, results.getVersions().size() );
207         assertTrue( results.getVersions().contains( "2.0" ) );
208         assertTrue( results.getVersions().contains( "2.1-SNAPSHOT" ) );
209         assertTrue( results.getVersions().contains( "2.1.1" ) );
210         assertTrue( results.getVersions().contains( "2.1-alpha-1" ) ); 
211     }
212     
213     public void testGetOtherSnapshotVersionsRequestedVersionIsGeneric()
214         throws Exception
215     {
216         RepositoryBrowsing browser = lookupBrowser();
217         List<String> results =
218             browser.getOtherSnapshotVersions( GUEST_REPO_IDS, "org.apache.maven.shared", "test-two", "2.1-SNAPSHOT" );
219         assertNotNull( "Returned list of versions should not be null.", results );
220         assertEquals( 3, results.size() );
221         assertTrue( results.contains( "2.1-SNAPSHOT" ) );
222         assertTrue( results.contains( "2.1-20070522.143249-1" ) );
223         assertTrue( results.contains( "2.1-20070522.153141-2" ) ); 
224     }
225     
226     public void testGetOtherSnapshotVersionsRequestedVersionIsUnique()
227         throws Exception
228     {
229         RepositoryBrowsing browser = lookupBrowser();
230         List<String> results =
231             browser.getOtherSnapshotVersions( GUEST_REPO_IDS, "org.apache.maven.shared", "test-two", "2.1-20070522.143249-1" );
232         assertNotNull( "Returned list of versions should not be null.", results );
233         assertEquals( 3, results.size() );
234         assertTrue( results.contains( "2.1-SNAPSHOT" ) );
235         assertTrue( results.contains( "2.1-20070522.143249-1" ) );
236         assertTrue( results.contains( "2.1-20070522.153141-2" ) ); 
237     }
238     
239     private void assertGroupIds( String msg, List<String> actualGroupIds, String[] expectedGroupIds )
240     {
241         assertEquals( msg + ": groupIds.length", expectedGroupIds.length, actualGroupIds.size() );
242 
243         for ( int i = 0; i < expectedGroupIds.length; i++ )
244         {
245             String expectedGroupId = expectedGroupIds[i];
246             assertTrue( msg + ": actual groupIds.contains(" + expectedGroupId + ")", actualGroupIds
247                 .contains( expectedGroupId ) );
248         }
249     }
250 
251     @Override
252     protected void setUp()
253         throws Exception
254     {
255         super.setUp();
256         
257         artifactDao = dao.getArtifactDAO();        
258         saveTestData();
259     }
260 }