1   package org.apache.maven.plugin.ear.util;
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 junit.framework.TestCase;
23  import org.apache.maven.plugin.ear.ArtifactTestStub;
24  
25  import java.util.Set;
26  import java.util.TreeSet;
27  
28  /**
29   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
30   * @version $Id: ArtifactRepositoryTest.java 522047 2007-03-24 16:09:22Z snicoll $
31   */
32  public class ArtifactRepositoryTest
33      extends TestCase
34  {
35  
36  
37      protected void setUp()
38          throws Exception
39      {
40          super.setUp();
41          ArtifactTypeMappingService.getInstance().configure( null );
42      }
43  
44      public static final String DEFAULT_GROUPID = "eartest";
45  
46      public static final String DEFAULT_TYPE = "jar";
47  
48      public static final String MAIN_ARTIFACT_ID = "none";
49  
50      public void testEmptyRepository()
51      {
52          ArtifactRepository repo = new ArtifactRepository( createArtifacts( null ), MAIN_ARTIFACT_ID );
53          assertNull( repo.getUniqueArtifact( "ear", "ar", "jar" ) );
54          assertNull( repo.getUniqueArtifact( "ear", "ar", "jar", null ) );
55          assertNull( repo.getUniqueArtifact( "ear", "ar", "jar", "class" ) );
56      }
57  
58      public void testRepositoryWithOneUnclassifiedArtifact()
59      {
60          ArtifactRepository repo =
61              new ArtifactRepository( createArtifacts( new String[]{"myartifact"} ), MAIN_ARTIFACT_ID );
62          assertNotNull( repo.getUniqueArtifact( DEFAULT_GROUPID, "myartifact", "jar" ) );
63          assertNotNull( repo.getUniqueArtifact( DEFAULT_GROUPID, "myartifact", "jar", null ) );
64      }
65  
66      public void testRepositoryWithOneClassifiedArtifact()
67      {
68          ArtifactRepository repo = new ArtifactRepository(
69              createArtifacts( new String[]{"myartifact"}, null, null, new String[]{"classified"} ), MAIN_ARTIFACT_ID );
70          assertNotNull( repo.getUniqueArtifact( DEFAULT_GROUPID, "myartifact", "jar" ) );
71          assertNotNull( repo.getUniqueArtifact( DEFAULT_GROUPID, "myartifact", "jar", "classified" ) );
72          assertNull( repo.getUniqueArtifact( DEFAULT_GROUPID, "myartifact", "jar", "wrong" ) );
73      }
74  
75      public void testRepositoryWithMultipleClassifiedArtifacts()
76      {
77          ArtifactRepository repo = new ArtifactRepository( createArtifacts(
78              new String[]{"myartifact", "myartifact", "myartifact"}, null, null,
79              new String[]{"class1", "class2", "class3"} ), MAIN_ARTIFACT_ID );
80  
81          assertNull( repo.getUniqueArtifact( DEFAULT_GROUPID, "myartifact", "jar" ) );
82          assertNotNull( repo.getUniqueArtifact( DEFAULT_GROUPID, "myartifact", "jar", "class1" ) );
83          assertNotNull( repo.getUniqueArtifact( DEFAULT_GROUPID, "myartifact", "jar", "class2" ) );
84          assertNotNull( repo.getUniqueArtifact( DEFAULT_GROUPID, "myartifact", "jar", "class3" ) );
85          assertNull( repo.getUniqueArtifact( DEFAULT_GROUPID, "myartifact", "jar", "wrong" ) );
86      }
87  
88      public void testRepositoryWithMultipleClassifiedArtifactsAndMainArtifact()
89      {
90          ArtifactRepository repo = new ArtifactRepository( createArtifacts(
91              new String[]{"myartifact", "myartifact", "myartifact"}, null, null,
92              new String[]{"class1", "class2", null} ), MAIN_ARTIFACT_ID );
93  
94          assertNull( repo.getUniqueArtifact( DEFAULT_GROUPID, "myartifact", "jar" ) );
95          assertNotNull( repo.getUniqueArtifact( DEFAULT_GROUPID, "myartifact", "jar", "class1" ) );
96          assertNotNull( repo.getUniqueArtifact( DEFAULT_GROUPID, "myartifact", "jar", "class2" ) );
97          assertNotNull( repo.getUniqueArtifact( DEFAULT_GROUPID, "myartifact", "jar", MAIN_ARTIFACT_ID ) );
98          assertNull( repo.getUniqueArtifact( DEFAULT_GROUPID, "myartifact", "jar", "wrong" ) );
99      }
100 
101 
102     private Set createArtifacts( String[] artifactsId )
103     {
104         return createArtifacts( artifactsId, null );
105     }
106 
107     private Set createArtifacts( String[] artifactsId, String[] types )
108     {
109         return createArtifacts( artifactsId, types, null );
110     }
111 
112     private Set createArtifacts( String[] artifactsId, String[] types, String[] groupsId )
113     {
114         return createArtifacts( artifactsId, types, groupsId, null );
115     }
116 
117     private Set createArtifacts( String[] artifactsId, String[] types, String[] groupsId, String[] classifiers )
118     {
119         Set result = new TreeSet();
120         if ( artifactsId == null || artifactsId.length == 0 )
121         {
122             return result;
123         }
124         for ( int i = 0; i < artifactsId.length; i++ )
125         {
126             String artifactId = artifactsId[i];
127             String type = getData( types, i, DEFAULT_TYPE );
128             String groupId = getData( groupsId, i, DEFAULT_GROUPID );
129             String classifier = getData( classifiers, i, null );
130             result.add( new ArtifactTestStub( groupId, artifactId, type, classifier ) );
131 
132         }
133         return result;
134     }
135 
136     private String getData( String[] data, int i, String defaultValue )
137     {
138         if ( data == null || data[i] == null )
139         {
140             return defaultValue;
141         }
142         else
143         {
144             return data[i];
145 
146         }
147     }
148 
149 
150 }