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 com.opensymphony.xwork2.Action;
23 import net.sf.beanlib.provider.replicator.BeanReplicator;
24 import org.apache.archiva.admin.model.beans.ManagedRepository;
25 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
26 import org.apache.archiva.configuration.ArchivaConfiguration;
27 import org.apache.archiva.configuration.Configuration;
28 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.archiva.metadata.model.ArtifactMetadata;
30 import org.apache.archiva.metadata.repository.MetadataRepository;
31 import org.apache.archiva.metadata.repository.RepositorySession;
32 import org.apache.archiva.repository.ManagedRepositoryContent;
33 import org.apache.archiva.repository.RepositoryContentFactory;
34 import org.apache.archiva.repository.content.ManagedDefaultRepositoryContent;
35 import org.apache.archiva.rest.services.DefaultRepositoriesService;
36 import org.apache.archiva.webtest.memory.TestRepositorySessionFactory;
37 import org.apache.commons.lang.StringUtils;
38 import org.apache.struts2.StrutsSpringTestCase;
39 import org.easymock.MockControl;
40 import org.easymock.classextension.MockClassControl;
43 import java.util.ArrayList;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.when;
47 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
48 import org.junit.After;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
53 public class DeleteArtifactActionTest
54 extends StrutsSpringTestCase
56 private DeleteArtifactAction action;
58 private ArchivaConfiguration configuration;
60 private MockControl configurationControl;
62 private RepositoryContentFactory repositoryFactory;
64 private MockControl repositoryFactoryControl;
66 private MetadataRepository metadataRepository;
68 private MockControl metadataRepositoryControl;
70 private static final String REPOSITORY_ID = "test-repo";
72 private static final String GROUP_ID = "org.apache.archiva";
74 private static final String ARTIFACT_ID = "npe-metadata";
76 private static final String VERSION = "1.0";
78 private static final String REPO_LOCATION = "target/test-classes/test-repo";
81 protected String[] getContextLocations()
83 return new String[]{ "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" };
93 action = (DeleteArtifactAction) getActionProxy( "/deleteArtifact.action" ).getAction();
94 action.setPrincipal( "admin" );
95 assertNotNull( action );
97 configurationControl = MockControl.createControl( ArchivaConfiguration.class );
98 configuration = (ArchivaConfiguration) configurationControl.getMock();
100 repositoryFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
101 repositoryFactory = (RepositoryContentFactory) repositoryFactoryControl.getMock();
103 metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
104 metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
106 RepositorySession repositorySession = mock( RepositorySession.class );
107 when( repositorySession.getRepository() ).thenReturn( metadataRepository );
109 TestRepositorySessionFactory repositorySessionFactory =
110 applicationContext.getBean( "repositorySessionFactory#test", TestRepositorySessionFactory.class );
112 repositorySessionFactory.setRepositorySession( repositorySession );
114 ( (DefaultManagedRepositoryAdmin) ( (DefaultRepositoriesService) action.getRepositoriesService() ).getManagedRepositoryAdmin() ).setArchivaConfiguration(
116 ( (DefaultRepositoriesService) action.getRepositoriesService() ).setRepositoryFactory( repositoryFactory );
121 public void tearDown()
131 public void testNPEInDeleteArtifact()
134 action.setGroupId( GROUP_ID );
135 action.setArtifactId( ARTIFACT_ID );
136 action.setVersion( VERSION );
137 action.setRepositoryId( REPOSITORY_ID );
139 Configuration config = createConfiguration();
141 ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
142 repoContent.setRepository(
143 new BeanReplicator().replicateBean( config.findManagedRepositoryById( REPOSITORY_ID ),
144 ManagedRepository.class ) );
146 configurationControl.expectAndReturn( configuration.getConfiguration(), config );
147 repositoryFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( REPOSITORY_ID ),
149 metadataRepositoryControl.expectAndReturn(
150 metadataRepository.getArtifacts( REPOSITORY_ID, GROUP_ID, ARTIFACT_ID, VERSION ),
151 new ArrayList<ArtifactMetadata>() );
153 configurationControl.replay();
154 repositoryFactoryControl.replay();
155 metadataRepositoryControl.replay();
157 assertEquals( Action.SUCCESS, action.doDelete() );
159 String artifactPath =
160 REPO_LOCATION + "/" + StringUtils.replace( GROUP_ID, ".", "/" ) + "/" + StringUtils.replace( ARTIFACT_ID,
162 + "/" + VERSION + "/" + ARTIFACT_ID + "-" + VERSION;
164 assertFalse( new File( artifactPath + ".jar" ).exists() );
165 assertFalse( new File( artifactPath + ".jar.sha1" ).exists() );
166 assertFalse( new File( artifactPath + ".jar.md5" ).exists() );
168 assertFalse( new File( artifactPath + ".pom" ).exists() );
169 assertFalse( new File( artifactPath + ".pom.sha1" ).exists() );
170 assertFalse( new File( artifactPath + ".pom.md5" ).exists() );
173 private Configuration createConfiguration()
175 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
176 managedRepo.setId( REPOSITORY_ID );
177 managedRepo.setName( "Test Repository" );
179 managedRepo.setLocation( REPO_LOCATION );
180 managedRepo.setReleases( true );
182 Configuration config = new Configuration();
183 config.addManagedRepository( managedRepo );