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;
48 public class DeleteArtifactActionTest
49 extends StrutsSpringTestCase
51 private DeleteArtifactAction action;
53 private ArchivaConfiguration configuration;
55 private MockControl configurationControl;
57 private RepositoryContentFactory repositoryFactory;
59 private MockControl repositoryFactoryControl;
61 private MetadataRepository metadataRepository;
63 private MockControl metadataRepositoryControl;
65 private static final String REPOSITORY_ID = "test-repo";
67 private static final String GROUP_ID = "org.apache.archiva";
69 private static final String ARTIFACT_ID = "npe-metadata";
71 private static final String VERSION = "1.0";
73 private static final String REPO_LOCATION = "target/test-classes/test-repo";
76 protected String[] getContextLocations()
78 return new String[]{ "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" };
82 protected void setUp()
87 action = (DeleteArtifactAction) getActionProxy( "/deleteArtifact.action" ).getAction();
88 action.setPrincipal( "admin" );
89 assertNotNull( action );
91 configurationControl = MockControl.createControl( ArchivaConfiguration.class );
92 configuration = (ArchivaConfiguration) configurationControl.getMock();
94 repositoryFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
95 repositoryFactory = (RepositoryContentFactory) repositoryFactoryControl.getMock();
97 metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
98 metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
100 RepositorySession repositorySession = mock( RepositorySession.class );
101 when( repositorySession.getRepository() ).thenReturn( metadataRepository );
103 TestRepositorySessionFactory repositorySessionFactory =
104 applicationContext.getBean( "repositorySessionFactory#test", TestRepositorySessionFactory.class );
106 repositorySessionFactory.setRepositorySession( repositorySession );
108 ( (DefaultManagedRepositoryAdmin) ( (DefaultRepositoriesService) action.getRepositoriesService() ).getManagedRepositoryAdmin() ).setArchivaConfiguration(
110 ( (DefaultRepositoriesService) action.getRepositoriesService() ).setRepositoryFactory( repositoryFactory );
114 protected void tearDown()
123 public void testNPEInDeleteArtifact()
126 action.setGroupId( GROUP_ID );
127 action.setArtifactId( ARTIFACT_ID );
128 action.setVersion( VERSION );
129 action.setRepositoryId( REPOSITORY_ID );
131 Configuration config = createConfiguration();
133 ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
134 repoContent.setRepository(
135 new BeanReplicator().replicateBean( config.findManagedRepositoryById( REPOSITORY_ID ),
136 ManagedRepository.class ) );
138 configurationControl.expectAndReturn( configuration.getConfiguration(), config );
139 repositoryFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( REPOSITORY_ID ),
141 metadataRepositoryControl.expectAndReturn(
142 metadataRepository.getArtifacts( REPOSITORY_ID, GROUP_ID, ARTIFACT_ID, VERSION ),
143 new ArrayList<ArtifactMetadata>() );
145 configurationControl.replay();
146 repositoryFactoryControl.replay();
147 metadataRepositoryControl.replay();
149 assertEquals( Action.SUCCESS, action.doDelete() );
151 String artifactPath =
152 REPO_LOCATION + "/" + StringUtils.replace( GROUP_ID, ".", "/" ) + "/" + StringUtils.replace( ARTIFACT_ID,
154 + "/" + VERSION + "/" + ARTIFACT_ID + "-" + VERSION;
156 assertFalse( new File( artifactPath + ".jar" ).exists() );
157 assertFalse( new File( artifactPath + ".jar.sha1" ).exists() );
158 assertFalse( new File( artifactPath + ".jar.md5" ).exists() );
160 assertFalse( new File( artifactPath + ".pom" ).exists() );
161 assertFalse( new File( artifactPath + ".pom.sha1" ).exists() );
162 assertFalse( new File( artifactPath + ".pom.md5" ).exists() );
165 private Configuration createConfiguration()
167 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
168 managedRepo.setId( REPOSITORY_ID );
169 managedRepo.setName( "Test Repository" );
171 managedRepo.setLocation( REPO_LOCATION );
172 managedRepo.setReleases( true );
174 Configuration config = new Configuration();
175 config.addManagedRepository( managedRepo );