View Javadoc

1   package org.apache.maven.archiva.database.constraints;
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.ArtifactDAO;
24  import org.apache.maven.archiva.database.ArchivaDAO;
25  import org.apache.maven.archiva.model.ArchivaArtifact;
26  
27  import java.util.Date;
28  import java.util.List;
29  
30  /**
31   * ArtifactsByChecksumConstraintTest
32   *
33   * @version
34   */
35  public class ArtifactsByChecksumConstraintTest
36      extends AbstractArchivaDatabaseTestCase
37  {
38      private static final String SHA1_HASH3 = "f3f653289f3217c65324830ab3415bc92feddefa";
39  
40      private static final String SHA1_HASH2 = "a49810ad3eba8651677ab57cd40a0f76fdef9538";
41  
42      private static final String SHA1_HASH1 = "232f01b24b1617c46a3d4b0ab3415bc9237dcdec";
43  
44      private static final String MD5_HASH3 = "5440efd724c9a5246ddc148662a4f20a";
45  
46      private static final String MD5_HASH2 = "4685525525d82dea68c6a6cd5a08f726";
47  
48      private static final String MD5_HASH1 = "53e3b856aa1a3f3cb7fe0f7ac6163aaf";
49  
50      private ArtifactDAO artifactDao;
51  
52      @Override
53      protected void setUp()
54          throws Exception
55      {
56          super.setUp();
57  
58          ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
59          artifactDao = dao.getArtifactDAO();
60      }
61  
62      public ArchivaArtifact createArtifact( String artifactId, String version )
63      {
64          ArchivaArtifact artifact =
65              artifactDao.createArtifact( "org.apache.maven.archiva.test", artifactId, version, "", "jar", "testable_repo" );
66          artifact.getModel().setLastModified( new Date() );
67          artifact.getModel().setRepositoryId( "testable_repo" );
68          return artifact;
69      }
70  
71      public void testConstraintSHA1()
72          throws Exception
73      {
74          ArchivaArtifact artifact;
75  
76          // Setup artifacts in fresh DB.
77          artifact = createArtifact( "test-sha1-one", "1.0" );
78          artifact.getModel().setChecksumSHA1( SHA1_HASH1 );
79          artifactDao.saveArtifact( artifact );
80  
81          artifact = createArtifact( "test-sha1-one", "1.1" );
82          artifact.getModel().setChecksumSHA1( SHA1_HASH1 );
83          artifactDao.saveArtifact( artifact );
84  
85          artifact = createArtifact( "test-sha1-one", "1.2" );
86          artifact.getModel().setChecksumSHA1( SHA1_HASH1 );
87          artifactDao.saveArtifact( artifact );
88  
89          artifact = createArtifact( "test-sha1-two", "1.0" );
90          artifact.getModel().setChecksumSHA1( SHA1_HASH1 );
91          artifactDao.saveArtifact( artifact );
92  
93          artifact = createArtifact( "test-sha1-two", "2.0" );
94          artifact.getModel().setChecksumSHA1( SHA1_HASH3 );
95          artifactDao.saveArtifact( artifact );
96  
97          artifact = createArtifact( "test-sha1-two", "2.1" );
98          artifact.getModel().setChecksumSHA1( SHA1_HASH2 );
99          artifactDao.saveArtifact( artifact );
100 
101         artifact = createArtifact( "test-sha1-two", "3.0" );
102         artifact.getModel().setChecksumSHA1( SHA1_HASH2 );
103         artifactDao.saveArtifact( artifact );
104 
105         assertConstraint( "Artifacts by SHA1 Checksum", 4,
106                           new ArtifactsByChecksumConstraint( SHA1_HASH1, ArtifactsByChecksumConstraint.SHA1 ) );
107         assertConstraint( "Artifacts by SHA1 Checksum", 2,
108                           new ArtifactsByChecksumConstraint( SHA1_HASH2, ArtifactsByChecksumConstraint.SHA1 ) );
109         assertConstraint( "Artifacts by SHA1 Checksum", 1,
110                           new ArtifactsByChecksumConstraint( SHA1_HASH3, ArtifactsByChecksumConstraint.SHA1 ) );
111     }
112 
113     public void testConstraintMD5()
114         throws Exception
115     {
116         ArchivaArtifact artifact;
117 
118         artifact = createArtifact( "test-md5-one", "1.0" );
119         artifact.getModel().setChecksumMD5( MD5_HASH1 );
120         artifactDao.saveArtifact( artifact );
121 
122         artifact = createArtifact( "test-md5-one", "1.1" );
123         artifact.getModel().setChecksumMD5( MD5_HASH1 );
124         artifactDao.saveArtifact( artifact );
125 
126         artifact = createArtifact( "test-md5-one", "1.2" );
127         artifact.getModel().setChecksumMD5( MD5_HASH1 );
128         artifactDao.saveArtifact( artifact );
129 
130         artifact = createArtifact( "test-md5-two", "1.0" );
131         artifact.getModel().setChecksumMD5( MD5_HASH1 );
132         artifactDao.saveArtifact( artifact );
133 
134         artifact = createArtifact( "test-md5-two", "2.0" );
135         artifact.getModel().setChecksumMD5( MD5_HASH3 );
136         artifactDao.saveArtifact( artifact );
137 
138         artifact = createArtifact( "test-md5-two", "2.1" );
139         artifact.getModel().setChecksumMD5( MD5_HASH2 );
140         artifactDao.saveArtifact( artifact );
141 
142         artifact = createArtifact( "test-md5-two", "3.0" );
143         artifact.getModel().setChecksumMD5( MD5_HASH2 );
144         artifactDao.saveArtifact( artifact );
145 
146         assertConstraint( "Artifacts by MD5 Checksum", 4,
147                           new ArtifactsByChecksumConstraint( MD5_HASH1, ArtifactsByChecksumConstraint.MD5 ) );
148         assertConstraint( "Artifacts by MD5 Checksum", 2,
149                           new ArtifactsByChecksumConstraint( MD5_HASH2, ArtifactsByChecksumConstraint.MD5 ) );
150         assertConstraint( "Artifacts by MD5 Checksum", 1,
151                           new ArtifactsByChecksumConstraint( MD5_HASH3, ArtifactsByChecksumConstraint.MD5 ) );
152     }
153 
154     public void testConstraintOR()
155         throws Exception
156     {
157         ArchivaArtifact artifact;
158 
159         artifact = createArtifact( "test-one", "1.0" );
160         artifact.getModel().setChecksumMD5( MD5_HASH1 );
161         artifactDao.saveArtifact( artifact );
162 
163         artifact = createArtifact( "test-one", "1.1" );
164         artifact.getModel().setChecksumMD5( MD5_HASH1 );
165         artifactDao.saveArtifact( artifact );
166 
167         artifact = createArtifact( "test-one", "1.2" );
168         artifact.getModel().setChecksumMD5( MD5_HASH1 );
169         artifactDao.saveArtifact( artifact );
170 
171         artifact = createArtifact( "test-two", "1.0" );
172         artifact.getModel().setChecksumMD5( MD5_HASH1 );
173         artifactDao.saveArtifact( artifact );
174 
175         artifact = createArtifact( "test-two", "2.0" );
176         artifact.getModel().setChecksumMD5( MD5_HASH3 );
177         artifactDao.saveArtifact( artifact );
178 
179         artifact = createArtifact( "test-two", "2.1" );
180         artifact.getModel().setChecksumMD5( MD5_HASH2 );
181         artifactDao.saveArtifact( artifact );
182 
183         artifact = createArtifact( "test-two", "3.0" );
184         artifact.getModel().setChecksumMD5( MD5_HASH2 );
185         artifactDao.saveArtifact( artifact );
186 
187         assertConstraint( "Artifacts by MD5 Checksum", 4, new ArtifactsByChecksumConstraint( MD5_HASH1 ) );
188         assertConstraint( "Artifacts by MD5 Checksum", 2, new ArtifactsByChecksumConstraint( MD5_HASH2 ) );
189         assertConstraint( "Artifacts by MD5 Checksum", 1, new ArtifactsByChecksumConstraint( MD5_HASH3 ) );
190     }
191 
192 
193     private void assertConstraint( String msg, int count, ArtifactsByChecksumConstraint constraint )
194         throws Exception
195     {
196         List<ArchivaArtifact> results = artifactDao.queryArtifacts( constraint );
197         assertNotNull( msg + ": Not Null", results );
198         assertEquals( msg + ": Results.size", count, results.size() );
199     }
200 }