View Javadoc
1   package org.apache.archiva.dependency.tree.maven2;
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.archiva.common.plexusbridge.PlexusSisuBridge;
24  import org.apache.archiva.configuration.ArchivaConfiguration;
25  import org.apache.archiva.configuration.Configuration;
26  import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
27  import org.apache.archiva.maven2.model.Artifact;
28  import org.apache.archiva.maven2.model.TreeEntry;
29  import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
30  import static org.assertj.core.api.Assertions.assertThat;
31  import org.junit.Before;
32  import org.junit.Test;
33  import org.junit.runner.RunWith;
34  import org.springframework.test.context.ContextConfiguration;
35  
36  import javax.inject.Inject;
37  import javax.inject.Named;
38  import java.io.File;
39  import java.util.Collections;
40  import java.util.List;
41  
42  @RunWith( ArchivaSpringJUnit4ClassRunner.class )
43  @ContextConfiguration( { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
44  public class DependencyTreeBuilderTestMaven3
45      extends TestCase
46  {
47      @Inject
48      @Named( "dependencyTreeBuilder#maven3" )
49      private Maven3DependencyTreeBuilder builder;
50  
51      @Inject
52      private PlexusSisuBridge plexusSisuBridge;
53  
54      private static final String TEST_REPO_ID = "test";
55  
56      private static final String TEST_VERSION = "1.2.1";
57  
58      private static final String TEST_ARTIFACT_ID = "archiva-common";
59  
60      private static final String TEST_GROUP_ID = "org.apache.archiva";
61  
62  
63      @Inject
64      @Named( "archivaConfiguration#test" )
65      ArchivaConfiguration config;
66  
67      @Before
68      @Override
69      public void setUp()
70          throws Exception
71      {
72          super.setUp();
73  
74          Configuration configuration = new Configuration();
75          ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
76          repoConfig.setId( TEST_REPO_ID );
77          repoConfig.setLocation( new File( "target/test-repository" ).getAbsolutePath() );
78          configuration.addManagedRepository( repoConfig );
79          config.save( configuration );
80  
81          //artifactFactory = ((DefaultDependencyTreeBuilder)this.builder).getFactory();
82      }
83  
84  
85      private Artifact createArtifact( String groupId, String artifactId, String version )
86      {
87          return new Artifact( groupId, artifactId, version );
88      }
89  
90      private String getId( Artifact artifact )
91      {
92          return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion();
93      }
94  
95      @Test
96      public void testBuilderDependencies()
97          throws Exception
98      {
99  
100         List<TreeEntry> treeEntries =
101             builder.buildDependencyTree( Collections.singletonList( TEST_REPO_ID ), TEST_GROUP_ID, TEST_ARTIFACT_ID,
102                                          TEST_VERSION );
103 
104         Artifact artifact = new Artifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION, "", "" );
105         artifact.setFileExtension("jar");
106         assertThat( treeEntries ).isNotNull().isNotEmpty().contains(new TreeEntry(artifact) );
107 
108         artifact = new Artifact( "commons-lang", "commons-lang", "2.2", "compile", "" );
109         artifact.setFileExtension("jar");
110         assertThat( treeEntries.get( 0 ).getChilds() ).isNotNull().isNotEmpty().contains(
111             new TreeEntry(artifact) );
112     }
113 
114 
115     public static class TestTreeEntry
116         extends TreeEntry
117     {
118         Artifact a;
119 
120         public TestTreeEntry( Artifact a )
121         {
122             this.a = a;
123         }
124 
125         @Override
126         public int hashCode()
127         {
128             return this.a.hashCode();
129         }
130 
131         @Override
132         public boolean equals( Object o )
133         {
134             Artifact artifact = ( (TreeEntry) o ).getArtifact();
135             return artifact.equals( this.a );
136         }
137     }
138 
139 }