1 package org.apache.maven.archiva.database.constraints;
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.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;
31 import java.util.Date;
32 import java.util.List;
35 * UniqueFieldConstraintTest
37 public class UniqueFieldConstraintTest
38 extends AbstractArchivaDatabaseTestCase
40 private static final String GROUP_ID_1 = "org.apache.maven.archiva.test.1";
42 private static final String GROUP_ID_2 = "org.apache.maven.archiva.test.2";
44 private static final String GROUP_ID_3 = "org.apache.maven.archiva.test.3";
46 private ArchivaDAO archivaDao;
48 private ArtifactDAO artifactDao;
50 private RepositoryProblemDAO repoProblemDao;
52 protected void setUp()
57 archivaDao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
58 artifactDao = archivaDao.getArtifactDAO();
59 repoProblemDao = archivaDao.getRepositoryProblemDAO();
62 public ArchivaArtifact createArtifact( String groupId )
64 ArchivaArtifact artifact = artifactDao.createArtifact( groupId, "artifactId", "version", "classifier", "jar", "testrepo" );
66 artifact.getModel().setLastModified( new Date() );
67 artifact.getModel().setRepositoryId( "repoId" );
72 public RepositoryProblem createRepoProblem( String groupId )
74 RepositoryProblem repoProblem = new RepositoryProblem();
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" );
88 public void testArtifact()
91 artifactDao.saveArtifact( createArtifact( GROUP_ID_1 ) );
92 artifactDao.saveArtifact( createArtifact( GROUP_ID_2 ) );
93 artifactDao.saveArtifact( createArtifact( GROUP_ID_3 ) );
95 assertConstraint( 1, new UniqueFieldConstraint( ArchivaArtifactModel.class.getName(), "artifactId" ) );
96 assertConstraint( 3, new UniqueFieldConstraint( ArchivaArtifactModel.class.getName(), "groupId" ) );
99 public void testRepoProblem()
102 repoProblemDao.saveRepositoryProblem( createRepoProblem( GROUP_ID_1 ) );
103 repoProblemDao.saveRepositoryProblem( createRepoProblem( GROUP_ID_2 ) );
104 repoProblemDao.saveRepositoryProblem( createRepoProblem( GROUP_ID_3 ) );
106 assertConstraint( 1, new UniqueFieldConstraint( RepositoryProblem.class.getName(), "artifactId" ) );
107 assertConstraint( 3, new UniqueFieldConstraint( RepositoryProblem.class.getName(), "groupId" ) );
110 private void assertConstraint( int expectedHits, SimpleConstraint constraint )
113 List results = archivaDao.query( constraint );
114 assertNotNull( "Repository Problems: Not Null", results );
115 assertEquals( "Repository Problems: Results.size", expectedHits, results.size() );