1 package org.apache.maven.archiva.database.browsing;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
31 import java.util.ArrayList;
32 import java.util.Date;
33 import java.util.List;
36 * RepositoryBrowsingTest
40 public class RepositoryBrowsingTest
41 extends AbstractArchivaDatabaseTestCase
43 private static final List<String> GUEST_REPO_IDS;
45 private static final String USER_GUEST = "guest";
49 GUEST_REPO_IDS = new ArrayList<String>();
50 GUEST_REPO_IDS.add( "snapshots" );
51 GUEST_REPO_IDS.add( "central" );
54 private ArtifactDAO artifactDao;
56 private ArchivaArtifact createArtifact( String groupId, String artifactId, String version )
58 ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, "", "jar", "central" );
59 artifact.getModel().setLastModified( new Date() ); // mandatory field.
60 artifact.getModel().setRepositoryId( "central" );
64 private RepositoryBrowsing lookupBrowser()
67 RepositoryBrowsing browser = (RepositoryBrowsing) lookup( RepositoryBrowsing.class );
68 assertNotNull( "RepositoryBrowsing should not be null.", browser );
72 private void saveTestData()
75 ArchivaArtifact artifact;
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" );
82 artifact = createArtifact( "commons-lang", "commons-lang", "2.1" );
83 artifactDao.saveArtifact( artifact );
84 assertArtifactWasSaved( "commons-lang", "commons-lang", "2.1" );
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" );
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" );
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" );
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" );
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" );
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" );
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" );
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" );
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" );
122 artifact = createArtifact( "org.codehaus.modello", "modellong", "3.0" );
123 artifactDao.saveArtifact( artifact );
124 assertArtifactWasSaved( "org.codehaus.modello", "modellong", "3.0" );
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" );
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" );
135 private void assertArtifactWasSaved(String groupId, String artifactId, String version)
136 throws ObjectNotFoundException, ArchivaDatabaseException
138 Constraint constraint = new ArtifactsRelatedConstraint( groupId, artifactId, version );
139 List<ArchivaArtifact> artifacts = artifactDao.queryArtifacts( constraint );
141 assertFalse( "Artifact '" + groupId + ":" + artifactId + ":" + version + "' should have been found.",
142 artifacts.isEmpty() );
145 public void testBrowseIntoGroupWithSubgroups()
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 );
152 String expectedSubGroupIds[] = new String[] { "org.apache.maven.test.foo" };
153 assertGroupIds( "Browsing Results (subgroup org.apache.maven.test)", results.getGroupIds(), expectedSubGroupIds );
156 public void testSimpleBrowse()
159 RepositoryBrowsing browser = lookupBrowser();
160 BrowsingResults results = browser.getRoot( USER_GUEST, GUEST_REPO_IDS );
161 assertNotNull( "Browsing Results should not be null.", results );
163 String expectedRootGroupIds[] = new String[] { "commons-lang", "org" };
165 assertGroupIds( "Browsing Results (root)", results.getGroupIds(), expectedRootGroupIds );
168 public void testViewArtifact()
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() );
180 String repoId = browser.getRepositoryId( USER_GUEST, GUEST_REPO_IDS, "commons-lang", "commons-lang", "2.0" );
181 assertEquals( "central", repoId );
184 public void testViewArtifactWithMultipleTimestampedVersions()
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.143249-1", artifact.getVersion() );
193 assertEquals( "jar", artifact.getPackaging() );
195 String repoId = browser.getRepositoryId( USER_GUEST, GUEST_REPO_IDS, "org.apache.archiva", "archiva-indexer", "1.0-SNAPSHOT" );
196 assertEquals( "central", repoId );
199 public void testSelectArtifactId()
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" ) );
213 public void testGetOtherSnapshotVersionsRequestedVersionIsGeneric()
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" ) );
226 public void testGetOtherSnapshotVersionsRequestedVersionIsUnique()
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" ) );
239 private void assertGroupIds( String msg, List<String> actualGroupIds, String[] expectedGroupIds )
241 assertEquals( msg + ": groupIds.length", expectedGroupIds.length, actualGroupIds.size() );
243 for ( int i = 0; i < expectedGroupIds.length; i++ )
245 String expectedGroupId = expectedGroupIds[i];
246 assertTrue( msg + ": actual groupIds.contains(" + expectedGroupId + ")", actualGroupIds
247 .contains( expectedGroupId ) );
252 protected void setUp()
257 artifactDao = dao.getArtifactDAO();