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.ArchivaDAO;
24  import org.apache.maven.archiva.database.ArtifactDAO;
25  import org.apache.maven.archiva.database.RepositoryProblemDAO;
26  import org.apache.maven.archiva.database.SimpleConstraint;
27  import org.apache.maven.archiva.model.ArchivaArtifact;
28  import org.apache.maven.archiva.model.ArchivaArtifactModel;
29  import org.apache.maven.archiva.model.RepositoryProblem;
30  
31  import java.util.Date;
32  import java.util.List;
33  
34  /**
35   * UniqueFieldConstraintTest
36   */
37  public class UniqueFieldConstraintTest
38      extends AbstractArchivaDatabaseTestCase
39  {
40      private static final String GROUP_ID_1 = "org.apache.maven.archiva.test.1";
41  
42      private static final String GROUP_ID_2 = "org.apache.maven.archiva.test.2";
43  
44      private static final String GROUP_ID_3 = "org.apache.maven.archiva.test.3";
45  
46      private ArchivaDAO archivaDao;
47  
48      private ArtifactDAO artifactDao;
49  
50      private RepositoryProblemDAO repoProblemDao;
51  
52      protected void setUp()
53          throws Exception
54      {
55          super.setUp();
56  
57          archivaDao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
58          artifactDao = archivaDao.getArtifactDAO();
59          repoProblemDao = archivaDao.getRepositoryProblemDAO();
60      }
61  
62      public ArchivaArtifact createArtifact( String groupId )
63      {
64          ArchivaArtifact artifact = artifactDao.createArtifact( groupId, "artifactId", "version", "classifier", "jar", "testrepo" );
65  
66          artifact.getModel().setLastModified( new Date() );
67          artifact.getModel().setRepositoryId( "repoId" );
68  
69          return artifact;
70      }
71  
72      public RepositoryProblem createRepoProblem( String groupId )
73      {
74          RepositoryProblem repoProblem = new RepositoryProblem();
75  
76          repoProblem.setGroupId( groupId );
77          repoProblem.setArtifactId( "artifactId" );
78          repoProblem.setMessage( "message" );
79          repoProblem.setOrigin( "origin" );
80          repoProblem.setPath( "path" );
81          repoProblem.setRepositoryId( "repoId" );
82          repoProblem.setType( "type" );
83          repoProblem.setVersion( "version" );
84  
85          return repoProblem;
86      }
87  
88      public void testArtifact()
89          throws Exception
90      {
91          artifactDao.saveArtifact( createArtifact( GROUP_ID_1 ) );
92          artifactDao.saveArtifact( createArtifact( GROUP_ID_2 ) );
93          artifactDao.saveArtifact( createArtifact( GROUP_ID_3 ) );
94  
95          assertConstraint( 1, new UniqueFieldConstraint( ArchivaArtifactModel.class.getName(), "artifactId" ) );
96          assertConstraint( 3, new UniqueFieldConstraint( ArchivaArtifactModel.class.getName(), "groupId" ) );
97      }
98  
99      public void testRepoProblem()
100         throws Exception
101     {
102         repoProblemDao.saveRepositoryProblem( createRepoProblem( GROUP_ID_1 ) );
103         repoProblemDao.saveRepositoryProblem( createRepoProblem( GROUP_ID_2 ) );
104         repoProblemDao.saveRepositoryProblem( createRepoProblem( GROUP_ID_3 ) );
105 
106         assertConstraint( 1, new UniqueFieldConstraint( RepositoryProblem.class.getName(), "artifactId" ) );
107         assertConstraint( 3, new UniqueFieldConstraint( RepositoryProblem.class.getName(), "groupId" ) );
108     }
109 
110     private void assertConstraint( int expectedHits, SimpleConstraint constraint )
111         throws Exception
112     {
113         List<?> results = archivaDao.query( constraint );
114         assertNotNull( "Repository Problems: Not Null", results );
115         assertEquals( "Repository Problems: Results.size", expectedHits, results.size() );
116     }
117 }