1 package org.apache.archiva.web.tags;
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 com.opensymphony.xwork2.ActionContext;
23 import com.opensymphony.xwork2.config.ConfigurationManager;
24 import com.opensymphony.xwork2.config.providers.XWorkConfigurationProvider;
25 import com.opensymphony.xwork2.inject.Container;
26 import com.opensymphony.xwork2.util.ValueStack;
27 import com.opensymphony.xwork2.util.ValueStackFactory;
28 import junit.framework.TestCase;
29 import org.apache.archiva.common.ArchivaException;
30 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
31 import org.apache.archiva.configuration.ArchivaConfiguration;
32 import org.apache.archiva.configuration.Configuration;
33 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
34 import org.apache.archiva.configuration.RepositoryScanningConfiguration;
35 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
36 import org.apache.archiva.metadata.repository.RepositorySession;
37 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
38 import org.apache.archiva.webtest.memory.TestMetadataResolver;
39 import org.apache.archiva.webtest.memory.TestRepositorySessionFactory;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.sonatype.aether.artifact.Artifact;
44 import org.sonatype.aether.util.artifact.DefaultArtifact;
45 import org.springframework.context.ApplicationContext;
46 import org.springframework.test.context.ContextConfiguration;
48 import javax.inject.Inject;
49 import java.util.List;
51 import static org.mockito.Mockito.mock;
52 import static org.mockito.Mockito.when;
54 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
55 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml",
56 "classpath:/spring-context-DependencyTreeTest.xml" } )
57 public class DependencyTreeTest
61 private DependencyTree tree;
64 private PlexusSisuBridge plexusSisuBridge;
67 private ApplicationContext applicationContext;
70 private static final String TEST_VERSION = "version";
72 private static final String TEST_REPO_ID = "test-repo";
74 private static final String TEST_GROUP_ID = "groupId";
76 private static final String TEST_ARTIFACT_ID = "artifactId";
86 ConfigurationManager configurationManager = new ConfigurationManager();
87 configurationManager.addContainerProvider( new XWorkConfigurationProvider() );
88 com.opensymphony.xwork2.config.Configuration config = configurationManager.getConfiguration();
89 Container container = config.getContainer();
91 ValueStack stack = container.getInstance( ValueStackFactory.class ).createValueStack();
92 stack.getContext().put( ActionContext.CONTAINER, container );
93 ActionContext.setContext( new ActionContext( stack.getContext() ) );
95 assertNotNull( ActionContext.getContext() );
97 Configuration configuration = new Configuration();
98 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
99 repoConfig.setId( TEST_REPO_ID );
100 repoConfig.setLocation( "src/test/repositories/test" );
101 configuration.addManagedRepository( repoConfig );
103 ArchivaConfiguration archivaConfiguration = applicationContext.getBean( ArchivaConfiguration.class );
104 archivaConfiguration.getConfiguration().setRepositoryScanning( new RepositoryScanningConfiguration() );
105 archivaConfiguration.save( configuration );
107 TestMetadataResolver metadataResolver = applicationContext.getBean( TestMetadataResolver.class );
108 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
109 metadata.setId( TEST_VERSION );
110 metadataResolver.setProjectVersion( TEST_REPO_ID, TEST_GROUP_ID, TEST_ARTIFACT_ID, metadata );
112 RepositorySession repositorySession = mock( RepositorySession.class );
113 when( repositorySession.getResolver() ).thenReturn( metadataResolver );
114 TestRepositorySessionFactory repositorySessionFactory =
115 applicationContext.getBean( TestRepositorySessionFactory.class );
116 repositorySessionFactory.setRepositorySession( repositorySession );
120 public void testTree()
121 throws ArchivaException
123 List<DependencyTree.TreeEntry> entries = tree.gatherTreeList( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION );
124 assertEquals( 8, entries.size() );
126 DependencyTree.TreeEntry artifactId = entries.get( 0 );
127 assertEquals( "<ul><li>", artifactId.getPre() );
128 // olamy tree with aether always create jar so createPomArtifact failed but it's not used
129 assertTrue( assertArtifactsEquals( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ),
130 artifactId.getArtifact() ) );
131 assertEquals( "</li>", artifactId.getPost() );
133 DependencyTree.TreeEntry child1 = entries.get( 1 );
134 assertEquals( "<ul><li>", child1.getPre() );
135 assertTrue( assertArtifactsEquals( createArtifact( TEST_GROUP_ID, "child1", "1.0" ), child1.getArtifact() ) );
136 assertEquals( "</li>", child1.getPost() );
138 DependencyTree.TreeEntry grandchild = entries.get( 2 );
139 assertEquals( "<ul><li>", grandchild.getPre() );
141 assertArtifactsEquals( createArtifact( TEST_GROUP_ID, "grandchild1", "2.0" ), grandchild.getArtifact() ) );
142 assertEquals( "</li>", grandchild.getPost() );
144 DependencyTree.TreeEntry greatGrandchild = entries.get( 3 );
145 assertEquals( "<ul><li>", greatGrandchild.getPre() );
146 assertTrue( assertArtifactsEquals( createArtifact( TEST_GROUP_ID, "great-grandchild", "3.0" ),
147 greatGrandchild.getArtifact() ) );
148 assertEquals( "</li></ul></ul>", greatGrandchild.getPost() );
150 DependencyTree.TreeEntry child2 = entries.get( 4 );
151 assertEquals( "<li>", child2.getPre() );
152 assertTrue( assertArtifactsEquals( createArtifact( TEST_GROUP_ID, "child2", "1.0" ), child2.getArtifact() ) );
153 assertEquals( "</li>", child2.getPost() );
155 DependencyTree.TreeEntry grandchild2 = entries.get( 5 );
156 assertEquals( "<ul><li>", grandchild2.getPre() );
158 assertArtifactsEquals( createArtifact( TEST_GROUP_ID, "grandchild2", "2.0" ), grandchild2.getArtifact() ) );
159 assertEquals( "</li>", grandchild2.getPost() );
161 DependencyTree.TreeEntry grandchild3 = entries.get( 6 );
162 assertEquals( "<li>", grandchild3.getPre() );
164 assertArtifactsEquals( createArtifact( TEST_GROUP_ID, "grandchild3", "2.0" ), grandchild3.getArtifact() ) );
165 assertEquals( "</li></ul>", grandchild3.getPost() );
167 DependencyTree.TreeEntry child3 = entries.get( 7 );
168 assertEquals( "<li>", child3.getPre() );
169 assertTrue( assertArtifactsEquals( createArtifact( TEST_GROUP_ID, "child3", "1.0" ), child3.getArtifact() ) );
170 assertEquals( "</li></ul></ul>", child3.getPost() );
173 private Artifact createPomArtifact( String groupId, String artifactId, String version )
175 return new DefaultArtifact( groupId + ":" + artifactId + ":" + version );
178 private Artifact createArtifact( String groupId, String artifactId, String version )
180 return new DefaultArtifact( groupId, artifactId, "jar", version );
184 public boolean assertArtifactsEquals( Artifact a, Artifact b )
186 return a.getArtifactId().equals( b.getArtifactId() ) && a.getGroupId().equals( b.getGroupId() )
187 && a.getVersion().equals( b.getVersion() ) && a.getExtension().equals( b.getExtension() )
188 && a.getClassifier().equals( b.getClassifier() );