1 package org.apache.archiva.web.action;
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 net.sf.beanlib.provider.replicator.BeanReplicator;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
25 import org.apache.archiva.metadata.model.ArtifactMetadata;
26 import org.apache.archiva.metadata.repository.MetadataRepository;
27 import org.apache.archiva.metadata.repository.RepositorySession;
28 import org.apache.archiva.metadata.repository.memory.TestRepositorySessionFactory;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.archiva.configuration.ArchivaConfiguration;
31 import org.apache.archiva.configuration.Configuration;
32 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.archiva.repository.ManagedRepositoryContent;
34 import org.apache.archiva.repository.RepositoryContentFactory;
35 import org.apache.archiva.repository.content.ManagedDefaultRepositoryContent;
36 import org.apache.struts2.StrutsSpringTestCase;
37 import org.easymock.MockControl;
38 import org.easymock.classextension.MockClassControl;
41 import java.util.ArrayList;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
46 public class DeleteArtifactActionTest
47 extends StrutsSpringTestCase
49 private DeleteArtifactAction action;
51 private ArchivaConfiguration configuration;
53 private MockControl configurationControl;
55 private RepositoryContentFactory repositoryFactory;
57 private MockControl repositoryFactoryControl;
59 private MetadataRepository metadataRepository;
61 private MockControl metadataRepositoryControl;
63 private static final String REPOSITORY_ID = "test-repo";
65 private static final String GROUP_ID = "org.apache.archiva";
67 private static final String ARTIFACT_ID = "npe-metadata";
69 private static final String VERSION = "1.0";
71 private static final String REPO_LOCATION = "target/test-classes/test-repo";
74 protected String[] getContextLocations()
76 return new String[]{ "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" };
80 protected void setUp()
85 //action = (DeleteArtifactAction) lookup( Action.class.getName(), "deleteArtifactAction" );
86 action = (DeleteArtifactAction) getActionProxy( "/deleteArtifact.action" ).getAction();
87 assertNotNull( action );
89 configurationControl = MockControl.createControl( ArchivaConfiguration.class );
90 configuration = (ArchivaConfiguration) configurationControl.getMock();
92 repositoryFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
93 repositoryFactory = (RepositoryContentFactory) repositoryFactoryControl.getMock();
95 metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
96 metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
98 RepositorySession repositorySession = mock( RepositorySession.class );
99 when( repositorySession.getRepository() ).thenReturn( metadataRepository );
101 TestRepositorySessionFactory repositorySessionFactory =
102 applicationContext.getBean( "repositorySessionFactory#test", TestRepositorySessionFactory.class );
104 repositorySessionFactory.setRepositorySession( repositorySession );
106 (( DefaultManagedRepositoryAdmin)action.getManagedRepositoryAdmin()).setArchivaConfiguration( configuration );
107 action.setRepositoryFactory( repositoryFactory );
111 protected void tearDown()
119 public void testGetListeners()
122 assertNotNull( action.getListeners() );
123 assertFalse( action.getListeners().isEmpty() );
126 public void testNPEInDeleteArtifact()
129 action.setGroupId( GROUP_ID );
130 action.setArtifactId( ARTIFACT_ID );
131 action.setVersion( VERSION );
132 action.setRepositoryId( REPOSITORY_ID );
134 Configuration config = createConfiguration();
136 ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
137 repoContent.setRepository(
138 new BeanReplicator().replicateBean( config.findManagedRepositoryById( REPOSITORY_ID ),
139 ManagedRepository.class ) );
141 configurationControl.expectAndReturn( configuration.getConfiguration(), config );
142 repositoryFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( REPOSITORY_ID ),
144 metadataRepositoryControl.expectAndReturn(
145 metadataRepository.getArtifacts( REPOSITORY_ID, GROUP_ID, ARTIFACT_ID, VERSION ),
146 new ArrayList<ArtifactMetadata>() );
148 configurationControl.replay();
149 repositoryFactoryControl.replay();
150 metadataRepositoryControl.replay();
154 String artifactPath = REPO_LOCATION + "/" + StringUtils.replace( GROUP_ID, ".", "/" ) + "/"
155 + StringUtils.replace( ARTIFACT_ID, ".", "/" ) + "/" + VERSION + "/" + ARTIFACT_ID + "-" + VERSION;
157 assertFalse( new File( artifactPath + ".jar" ).exists() );
158 assertFalse( new File( artifactPath + ".jar.sha1" ).exists() );
159 assertFalse( new File( artifactPath + ".jar.md5" ).exists() );
161 assertFalse( new File( artifactPath + ".pom" ).exists() );
162 assertFalse( new File( artifactPath + ".pom.sha1" ).exists() );
163 assertFalse( new File( artifactPath + ".pom.md5" ).exists() );
166 private Configuration createConfiguration()
168 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
169 managedRepo.setId( REPOSITORY_ID );
170 managedRepo.setName( "Test Repository" );
172 managedRepo.setLocation( REPO_LOCATION );
173 managedRepo.setReleases( true );
175 Configuration config = new Configuration();
176 config.addManagedRepository( managedRepo );