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.maven.archiva.database.AbstractArchivaDatabaseTestCase;
23 import org.apache.maven.archiva.database.DeclarativeConstraint;
24 import org.apache.maven.archiva.model.ArchivaArtifact;
25 import org.apache.maven.archiva.model.ArchivaProjectModel;
26 import org.apache.maven.archiva.model.ArtifactReference;
27 import org.apache.maven.archiva.model.Dependency;
28 import org.apache.maven.archiva.model.VersionedReference;
29
30 import java.util.Date;
31 import java.util.List;
32
33
34
35
36
37
38 public class ProjectsByArtifactUsageConstraintTest
39 extends AbstractArchivaDatabaseTestCase
40 {
41 @Override
42 protected void setUp()
43 throws Exception
44 {
45 super.setUp();
46 }
47
48 private void saveModel( String modelId, String deps[] )
49 throws Exception
50 {
51 ArchivaProjectModel model = new ArchivaProjectModel();
52
53 VersionedReference ref = toVersionedReference( modelId );
54 model.setGroupId( ref.getGroupId() );
55 model.setArtifactId( ref.getArtifactId() );
56 model.setVersion( ref.getVersion() );
57 model.setPackaging( "jar" );
58 model.setOrigin( "testcase" );
59
60 if ( deps != null )
61 {
62 for ( int i = 0; i < deps.length; i++ )
63 {
64 ArtifactReference artiref = toArtifactReference( deps[i] );
65 Dependency dep = new Dependency();
66 dep.setGroupId( artiref.getGroupId() );
67 dep.setArtifactId( artiref.getArtifactId() );
68 dep.setVersion( artiref.getVersion() );
69 dep.setClassifier( artiref.getClassifier() );
70 dep.setClassifier( artiref.getType() );
71
72 model.addDependency( dep );
73 }
74 }
75
76 dao.getProjectModelDAO().saveProjectModel( model );
77 }
78
79 public ArchivaArtifact toArtifact( String id )
80 {
81 ArtifactReference ref = toArtifactReference( id );
82
83 ArchivaArtifact artifact = new ArchivaArtifact( ref.getGroupId(), ref.getArtifactId(), ref.getVersion(), ref
84 .getClassifier(), ref.getType(), "testable_repo" );
85 artifact.getModel().setLastModified( new Date() );
86 artifact.getModel().setRepositoryId( "testable_repo" );
87 return artifact;
88 }
89
90 public void testContraint()
91 throws Exception
92 {
93 saveModel( "org.apache.maven.archiva:archiva-configuration:1.0",
94 new String[] { "org.codehaus.plexus:plexus-digest:1.0::jar:" } );
95
96 saveModel( "org.apache.maven.archiva:archiva-common:1.0", new String[] {
97 "org.codehaus.plexus:plexus-digest:1.0::jar:",
98 "junit:junit:3.8.1::jar:" } );
99
100 ArchivaArtifact artifact;
101
102 artifact = toArtifact( "org.foo:bar:4.0::jar:" );
103 assertConstraint( 0, new ProjectsByArtifactUsageConstraint( artifact ) );
104 artifact = toArtifact( "org.codehaus.plexus:plexus-digest:1.0::jar:testable_repo" );
105 assertConstraint( 2, new ProjectsByArtifactUsageConstraint( artifact ) );
106 }
107
108 private void assertConstraint( int expectedHits, DeclarativeConstraint constraint )
109 throws Exception
110 {
111 List<ArchivaProjectModel> results = dao.getProjectModelDAO().queryProjectModels( constraint );
112 assertNotNull( "Projects By Artifact Usage: Not Null", results );
113 assertEquals( "Projects By Artifact Usage: Results.size", expectedHits, results.size() );
114 }
115 }