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.plexusbridge.PlexusSisuBridge;
30 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
31 import org.apache.archiva.metadata.repository.RepositorySession;
32 import org.apache.archiva.metadata.repository.memory.TestMetadataResolver;
33 import org.apache.archiva.metadata.repository.memory.TestRepositorySessionFactory;
34 import org.apache.archiva.common.ArchivaException;
35 import org.apache.archiva.configuration.ArchivaConfiguration;
36 import org.apache.archiva.configuration.Configuration;
37 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
38 import org.apache.maven.artifact.Artifact;
39 import org.apache.maven.artifact.factory.ArtifactFactory;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.springframework.context.ApplicationContext;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
47 import java.util.List;
48 import javax.inject.Inject;
50 import static org.mockito.Mockito.mock;
51 import static org.mockito.Mockito.when;
53 @RunWith( SpringJUnit4ClassRunner.class )
54 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml",
55 "classpath:/spring-context-DependencyTreeTest.xml"} )
56 public class DependencyTreeTest
60 private DependencyTree tree;
63 private PlexusSisuBridge plexusSisuBridge;
66 private ApplicationContext applicationContext;
68 private ArtifactFactory artifactFactory;
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.save( configuration );
106 artifactFactory = plexusSisuBridge.lookup( ArtifactFactory.class );
108 TestMetadataResolver metadataResolver = applicationContext.getBean( TestMetadataResolver.class );
109 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
110 metadata.setId( TEST_VERSION );
111 metadataResolver.setProjectVersion( TEST_REPO_ID, TEST_GROUP_ID, TEST_ARTIFACT_ID, metadata );
113 RepositorySession repositorySession = mock( RepositorySession.class );
114 when( repositorySession.getResolver() ).thenReturn( metadataResolver );
115 TestRepositorySessionFactory repositorySessionFactory =
116 applicationContext.getBean( TestRepositorySessionFactory.class );
117 repositorySessionFactory.setRepositorySession( repositorySession );
121 public void testTree()
122 throws ArchivaException
124 List<DependencyTree.TreeEntry> entries = tree.gatherTreeList( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION );
125 assertEquals( 8, entries.size() );
127 DependencyTree.TreeEntry artifactId = entries.get( 0 );
128 assertEquals( "<ul><li>", artifactId.getPre() );
129 assertEquals( createPomArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ), artifactId.getArtifact() );
130 assertEquals( "</li>", artifactId.getPost() );
132 DependencyTree.TreeEntry child1 = entries.get( 1 );
133 assertEquals( "<ul><li>", child1.getPre() );
134 assertEquals( createArtifact( TEST_GROUP_ID, "child1", "1.0" ), child1.getArtifact() );
135 assertEquals( "</li>", child1.getPost() );
137 DependencyTree.TreeEntry grandchild = entries.get( 2 );
138 assertEquals( "<ul><li>", grandchild.getPre() );
139 assertEquals( createArtifact( TEST_GROUP_ID, "grandchild1", "2.0" ), grandchild.getArtifact() );
140 assertEquals( "</li>", grandchild.getPost() );
142 DependencyTree.TreeEntry greatGrandchild = entries.get( 3 );
143 assertEquals( "<ul><li>", greatGrandchild.getPre() );
144 assertEquals( createArtifact( TEST_GROUP_ID, "great-grandchild", "3.0" ), greatGrandchild.getArtifact() );
145 assertEquals( "</li></ul></ul>", greatGrandchild.getPost() );
147 DependencyTree.TreeEntry child2 = entries.get( 4 );
148 assertEquals( "<li>", child2.getPre() );
149 assertEquals( createArtifact( TEST_GROUP_ID, "child2", "1.0" ), child2.getArtifact() );
150 assertEquals( "</li>", child2.getPost() );
152 DependencyTree.TreeEntry grandchild2 = entries.get( 5 );
153 assertEquals( "<ul><li>", grandchild2.getPre() );
154 assertEquals( createArtifact( TEST_GROUP_ID, "grandchild2", "2.0" ), grandchild2.getArtifact() );
155 assertEquals( "</li>", grandchild2.getPost() );
157 DependencyTree.TreeEntry grandchild3 = entries.get( 6 );
158 assertEquals( "<li>", grandchild3.getPre() );
159 assertEquals( createArtifact( TEST_GROUP_ID, "grandchild3", "2.0" ), grandchild3.getArtifact() );
160 assertEquals( "</li></ul>", grandchild3.getPost() );
162 DependencyTree.TreeEntry child3 = entries.get( 7 );
163 assertEquals( "<li>", child3.getPre() );
164 assertEquals( createArtifact( TEST_GROUP_ID, "child3", "1.0" ), child3.getArtifact() );
165 assertEquals( "</li></ul></ul>", child3.getPost() );
168 private Artifact createPomArtifact( String groupId, String artifactId, String version )
170 return artifactFactory.createProjectArtifact( groupId, artifactId, version );
173 private Artifact createArtifact( String groupId, String artifactId, String version )
175 return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );