1 package org.apache.maven.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 org.apache.archiva.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.repository.MetadataRepository;
24 import org.apache.archiva.metadata.repository.RepositorySession;
25 import org.apache.archiva.metadata.repository.memory.TestRepositorySessionFactory;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28 import org.apache.maven.archiva.configuration.Configuration;
29 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
31 import org.apache.maven.archiva.repository.RepositoryContentFactory;
32 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
33 import org.apache.struts2.StrutsSpringTestCase;
34 import org.easymock.MockControl;
35 import org.easymock.classextension.MockClassControl;
38 import java.util.ArrayList;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
43 public class DeleteArtifactActionTest
44 extends StrutsSpringTestCase
46 private DeleteArtifactAction action;
48 private ArchivaConfiguration configuration;
50 private MockControl configurationControl;
52 private RepositoryContentFactory repositoryFactory;
54 private MockControl repositoryFactoryControl;
56 private MetadataRepository metadataRepository;
58 private MockControl metadataRepositoryControl;
60 private static final String REPOSITORY_ID = "test-repo";
62 private static final String GROUP_ID = "org.apache.archiva";
64 private static final String ARTIFACT_ID = "npe-metadata";
66 private static final String VERSION = "1.0";
68 private static final String REPO_LOCATION = "target/test-classes/test-repo";
71 protected String[] getContextLocations()
73 return new String[]{ "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" };
77 protected void setUp()
82 //action = (DeleteArtifactAction) lookup( Action.class.getName(), "deleteArtifactAction" );
83 action = (DeleteArtifactAction) getActionProxy( "/deleteArtifact.action" ).getAction();
84 assertNotNull( action );
86 configurationControl = MockControl.createControl( ArchivaConfiguration.class );
87 configuration = (ArchivaConfiguration) configurationControl.getMock();
89 repositoryFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
90 repositoryFactory = (RepositoryContentFactory) repositoryFactoryControl.getMock();
92 metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
93 metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
95 RepositorySession repositorySession = mock( RepositorySession.class );
96 when( repositorySession.getRepository() ).thenReturn( metadataRepository );
98 TestRepositorySessionFactory repositorySessionFactory =
99 applicationContext.getBean( "repositorySessionFactory#test", TestRepositorySessionFactory.class );
101 repositorySessionFactory.setRepositorySession( repositorySession );
103 action.setConfiguration( configuration );
104 action.setRepositoryFactory( repositoryFactory );
108 protected void tearDown()
116 public void testGetListeners()
119 assertNotNull( action.getListeners() );
120 assertFalse( action.getListeners().isEmpty() );
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( config.findManagedRepositoryById( REPOSITORY_ID ) );
136 configurationControl.expectAndReturn( configuration.getConfiguration(), config );
137 repositoryFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( REPOSITORY_ID ),
139 metadataRepositoryControl.expectAndReturn(
140 metadataRepository.getArtifacts( REPOSITORY_ID, GROUP_ID, ARTIFACT_ID, VERSION ),
141 new ArrayList<ArtifactMetadata>() );
143 configurationControl.replay();
144 repositoryFactoryControl.replay();
145 metadataRepositoryControl.replay();
149 String artifactPath = REPO_LOCATION + "/" + StringUtils.replace( GROUP_ID, ".", "/" ) + "/"
150 + StringUtils.replace( ARTIFACT_ID, ".", "/" ) + "/" + VERSION + "/" + ARTIFACT_ID + "-" + VERSION;
152 assertFalse( new File( artifactPath + ".jar" ).exists() );
153 assertFalse( new File( artifactPath + ".jar.sha1" ).exists() );
154 assertFalse( new File( artifactPath + ".jar.md5" ).exists() );
156 assertFalse( new File( artifactPath + ".pom" ).exists() );
157 assertFalse( new File( artifactPath + ".pom.sha1" ).exists() );
158 assertFalse( new File( artifactPath + ".pom.md5" ).exists() );
161 private Configuration createConfiguration()
163 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
164 managedRepo.setId( REPOSITORY_ID );
165 managedRepo.setName( "Test Repository" );
167 managedRepo.setLocation( REPO_LOCATION );
168 managedRepo.setReleases( true );
170 Configuration config = new Configuration();
171 config.addManagedRepository( managedRepo );