1 package org.apache.maven.archiva.database.constraints;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.maven.archiva.database.AbstractArchivaDatabaseTestCase;
24 import org.apache.maven.archiva.database.ArchivaDAO;
25 import org.apache.maven.archiva.database.ArtifactDAO;
26 import org.apache.maven.archiva.database.SimpleConstraint;
27 import org.apache.maven.archiva.model.ArchivaArtifact;
28
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Date;
32 import java.util.Iterator;
33 import java.util.List;
34
35
36
37
38
39
40 public class UniqueGroupIdConstraintTest
41 extends AbstractArchivaDatabaseTestCase
42 {
43 private ArtifactDAO artifactDao;
44
45 public void testConstraintGroupIdParamCommonsLang()
46 throws Exception
47 {
48 setupArtifacts();
49
50 assertConstraint( new String[] { "commons-lang" }, new UniqueGroupIdConstraint( "commons-lang" ) );
51 }
52
53 public void testConstraintGroupIdParamNoRepos()
54 throws Exception
55 {
56 try
57 {
58 List<String> selectedRepos = new ArrayList<String>();
59 new UniqueGroupIdConstraint( selectedRepos, "org" );
60 fail( "Should have thrown an IllegalArgumentException due to lack of specified repos." );
61 }
62 catch ( IllegalArgumentException e )
63 {
64
65 }
66 }
67
68 public void testConstraintGroupIdParamNullRepos()
69 throws Exception
70 {
71 try
72 {
73 new UniqueGroupIdConstraint( (List<String>) null, "org" );
74 fail( "Should have thrown an NullPointerException due to lack of specified repos." );
75 }
76 catch ( NullPointerException e )
77 {
78
79 }
80 }
81
82 public void testConstraintGroupIdParamOrg()
83 throws Exception
84 {
85 setupArtifacts();
86
87 assertConstraint( new String[] {
88 "org.apache.maven.test",
89 "org.apache.maven.test.foo",
90 "org.apache.maven.shared",
91 "org.apache.archiva",
92 "org.codehaus.modello",
93 "org.codehaus.mojo" }, new UniqueGroupIdConstraint( "org" ) );
94 }
95
96 public void testConstraintGroupIdParamOrgApache()
97 throws Exception
98 {
99 setupArtifacts();
100
101 assertConstraint( new String[] {
102 "org.apache.maven.test",
103 "org.apache.maven.test.foo",
104 "org.apache.maven.shared",
105 "org.apache.archiva" }, new UniqueGroupIdConstraint( "org.apache" ) );
106 }
107
108 public void testConstraintGroupIdParamOrgApacheMaven()
109 throws Exception
110 {
111 setupArtifacts();
112
113 assertConstraint( new String[] {
114 "org.apache.maven.test",
115 "org.apache.maven.test.foo",
116 "org.apache.maven.shared" }, new UniqueGroupIdConstraint( "org.apache.maven" ) );
117 }
118
119 public void testConstraintGroupIdParamOrgApacheSnapshotsOnly()
120 throws Exception
121 {
122 setupArtifacts();
123
124 List<String> observableRepositories = new ArrayList<String>();
125 observableRepositories.add( "snapshots" );
126
127 assertConstraint( new String[] { "org.apache.archiva" }, new UniqueGroupIdConstraint( observableRepositories,
128 "org.apache" ) );
129 }
130
131 public void testConstraintGroupIdParamOrgSnapshotsOnly()
132 throws Exception
133 {
134 setupArtifacts();
135
136 List<String> observableRepositories = new ArrayList<String>();
137 observableRepositories.add( "snapshots" );
138
139 assertConstraint( new String[] { "org.apache.archiva", "org.codehaus.modello", "org.codehaus.mojo" },
140 new UniqueGroupIdConstraint( observableRepositories, "org" ) );
141 }
142
143 public void testConstraintNoGroupIdParam()
144 throws Exception
145 {
146 setupArtifacts();
147
148 assertConstraint( new String[] {
149 "commons-lang",
150 "org.apache.maven.test",
151 "org.apache.maven.test.foo",
152 "org.apache.maven.shared",
153 "org.codehaus.modello",
154 "org.codehaus.mojo",
155 "org.apache.archiva" }, new UniqueGroupIdConstraint() );
156 }
157
158 public void testConstraintNoGroupIdParamCentralAndSnapshots()
159 throws Exception
160 {
161 setupArtifacts();
162
163 List<String> observableRepositories = new ArrayList<String>();
164 observableRepositories.add( "central" );
165 observableRepositories.add( "snapshots" );
166
167 assertConstraint( new String[] {
168 "commons-lang",
169 "org.apache.maven.test",
170 "org.apache.maven.test.foo",
171 "org.apache.maven.shared",
172 "org.codehaus.modello",
173 "org.codehaus.mojo",
174 "org.apache.archiva" }, new UniqueGroupIdConstraint( observableRepositories ) );
175 }
176
177 public void testConstraintNoGroupIdParamCentralOnly()
178 throws Exception
179 {
180 setupArtifacts();
181
182 List<String> observableRepositories = new ArrayList<String>();
183 observableRepositories.add( "central" );
184
185 assertConstraint( new String[] {
186 "commons-lang",
187 "org.apache.maven.test",
188 "org.apache.maven.test.foo",
189 "org.apache.maven.shared",
190 "org.codehaus.modello" }, new UniqueGroupIdConstraint( observableRepositories ) );
191 }
192
193 public void testConstraintNoGroupIdParamNoRepos()
194 throws Exception
195 {
196 try
197 {
198 List<String> selectedRepos = new ArrayList<String>();
199 new UniqueGroupIdConstraint( selectedRepos );
200 fail( "Should have thrown an IllegalArgumentException due to lack of specified repos." );
201 }
202 catch ( IllegalArgumentException e )
203 {
204
205 }
206 }
207
208 public void testConstraintNoGroupIdParamNullRepos()
209 throws Exception
210 {
211 try
212 {
213 new UniqueGroupIdConstraint( (List<String>) null );
214 fail( "Should have thrown an NullPointerException due to lack of specified repos." );
215 }
216 catch ( NullPointerException e )
217 {
218
219 }
220 }
221
222 public void testConstraintNoGroupIdParamSnapshotsOnly()
223 throws Exception
224 {
225 setupArtifacts();
226
227 List<String> observableRepositories = new ArrayList<String>();
228 observableRepositories.add( "snapshots" );
229
230 assertConstraint( new String[] { "org.codehaus.modello", "org.codehaus.mojo", "org.apache.archiva" },
231 new UniqueGroupIdConstraint( observableRepositories ) );
232 }
233
234 @SuppressWarnings("unchecked")
235 private void assertConstraint( String[] expectedGroupIds, SimpleConstraint constraint )
236 throws Exception
237 {
238 String prefix = "Unique Group IDs: ";
239
240 List<String> results = (List<String>) dao.query( constraint );
241 assertNotNull( prefix + "Not Null", results );
242 assertEquals( prefix + "Results.size", expectedGroupIds.length, results.size() );
243
244 List<String> groupIdList = Arrays.asList( expectedGroupIds );
245
246 Iterator<String> it = results.iterator();
247 while ( it.hasNext() )
248 {
249 String actualGroupId = (String) it.next();
250 assertTrue( prefix + "groupId result should not be blank.", StringUtils.isNotBlank( actualGroupId ) );
251 assertTrue( prefix + " groupId result <" + actualGroupId + "> exists in expected GroupIds.", groupIdList
252 .contains( actualGroupId ) );
253 }
254 }
255
256 private ArchivaArtifact createArtifact( String repoId, String groupId, String artifactId, String version )
257 {
258 ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, "", "jar", "testrepo" );
259 artifact.getModel().setLastModified( new Date() );
260 artifact.getModel().setRepositoryId( repoId );
261 return artifact;
262 }
263
264 private void setupArtifacts()
265 throws Exception
266 {
267 ArchivaArtifact artifact;
268
269
270 artifact = createArtifact( "central", "commons-lang", "commons-lang", "2.0" );
271 artifactDao.saveArtifact( artifact );
272
273 artifact = createArtifact( "central", "commons-lang", "commons-lang", "2.1" );
274 artifactDao.saveArtifact( artifact );
275
276 artifact = createArtifact( "central", "org.apache.maven.test", "test-one", "1.2" );
277 artifactDao.saveArtifact( artifact );
278
279 artifact = createArtifact( "central", "org.apache.maven.test.foo", "test-two", "1.0" );
280 artifactDao.saveArtifact( artifact );
281
282 artifact = createArtifact( "central", "org.apache.maven.shared", "test-two", "2.0" );
283 artifactDao.saveArtifact( artifact );
284
285 artifact = createArtifact( "central", "org.apache.maven.shared", "test-two", "2.1" );
286 artifactDao.saveArtifact( artifact );
287
288 artifact = createArtifact( "central", "org.codehaus.modello", "test-two", "3.0" );
289 artifactDao.saveArtifact( artifact );
290
291
292 artifact = createArtifact( "snapshots", "org.codehaus.modello", "test-three", "1.0-SNAPSHOT" );
293 artifactDao.saveArtifact( artifact );
294
295 artifact = createArtifact( "snapshots", "org.codehaus.mojo", "testable-maven-plugin", "2.1-SNAPSHOT" );
296 artifactDao.saveArtifact( artifact );
297
298 artifact = createArtifact( "snapshots", "org.apache.archiva", "testable", "1.1-alpha-1-20070822.033400-43" );
299 artifactDao.saveArtifact( artifact );
300 }
301
302 protected void setUp()
303 throws Exception
304 {
305 super.setUp();
306
307 ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
308 artifactDao = dao.getArtifactDAO();
309 }
310 }