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 com.opensymphony.xwork2.Action;
23 import org.apache.archiva.metadata.model.ArtifactMetadata;
24 import org.apache.archiva.metadata.repository.MetadataRepository;
25 import org.apache.archiva.metadata.repository.RepositorySession;
26 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
27 import org.apache.archiva.metadata.repository.memory.TestRepositorySessionFactory;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
30 import org.apache.maven.archiva.configuration.Configuration;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
33 import org.apache.maven.archiva.repository.RepositoryContentFactory;
34 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
35 import org.apache.struts2.StrutsSpringTestCase;
36 import org.easymock.MockControl;
37 import org.easymock.classextension.MockClassControl;
40 import java.util.ArrayList;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
45 public class DeleteArtifactActionTest
46 extends StrutsSpringTestCase
48 private DeleteArtifactAction action;
50 private ArchivaConfiguration configuration;
52 private MockControl configurationControl;
54 private RepositoryContentFactory repositoryFactory;
56 private MockControl repositoryFactoryControl;
58 private MetadataRepository metadataRepository;
60 private MockControl metadataRepositoryControl;
62 private static final String REPOSITORY_ID = "test-repo";
64 private static final String GROUP_ID = "org.apache.archiva";
66 private static final String ARTIFACT_ID = "npe-metadata";
68 private static final String VERSION = "1.0";
70 private static final String REPO_LOCATION = "target/test-classes/test-repo";
73 protected String[] getContextLocations()
75 return new String[]{ "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" };
79 protected void setUp()
84 //action = (DeleteArtifactAction) lookup( Action.class.getName(), "deleteArtifactAction" );
85 action = (DeleteArtifactAction) getActionProxy( "/deleteArtifact.action" ).getAction();
86 assertNotNull( action );
88 configurationControl = MockControl.createControl( ArchivaConfiguration.class );
89 configuration = (ArchivaConfiguration) configurationControl.getMock();
91 repositoryFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
92 repositoryFactory = (RepositoryContentFactory) repositoryFactoryControl.getMock();
94 metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
95 metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
97 RepositorySession repositorySession = mock( RepositorySession.class );
98 when( repositorySession.getRepository() ).thenReturn( metadataRepository );
100 //TestRepositorySessionFactory repositorySessionFactory = (TestRepositorySessionFactory) lookup(
101 // RepositorySessionFactory.class );
103 TestRepositorySessionFactory repositorySessionFactory = new TestRepositorySessionFactory();
105 repositorySessionFactory.setRepositorySession( repositorySession );
107 action.setConfiguration( configuration );
108 action.setRepositoryFactory( repositoryFactory );
112 protected void tearDown()
120 public void testGetListeners()
123 assertNotNull( action.getListeners() );
124 assertFalse( action.getListeners().isEmpty() );
127 public void testNPEInDeleteArtifact()
130 action.setGroupId( GROUP_ID );
131 action.setArtifactId( ARTIFACT_ID );
132 action.setVersion( VERSION );
133 action.setRepositoryId( REPOSITORY_ID );
135 Configuration config = createConfiguration();
137 ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
138 repoContent.setRepository( config.findManagedRepositoryById( REPOSITORY_ID ) );
140 configurationControl.expectAndReturn( configuration.getConfiguration(), config );
141 repositoryFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( REPOSITORY_ID ),
143 metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifacts( REPOSITORY_ID, GROUP_ID,
144 ARTIFACT_ID, VERSION ),
145 new ArrayList<ArtifactMetadata>() );
147 configurationControl.replay();
148 repositoryFactoryControl.replay();
149 metadataRepositoryControl.replay();
153 String artifactPath = REPO_LOCATION + "/" + StringUtils.replace( GROUP_ID, ".", "/" ) + "/" +
154 StringUtils.replace( ARTIFACT_ID, ".", "/" ) + "/" + 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 );