test we pass in audit event listener

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1163029 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2011-08-29 21:43:12 +00:00
parent 48ef3f20f2
commit 3573e5a36e
5 changed files with 100 additions and 3 deletions

View File

@ -55,4 +55,15 @@ public class AuditInformation
{
this.remoteAddr = remoteAddr;
}
@Override
public String toString()
{
final StringBuilder sb = new StringBuilder();
sb.append( "AuditInformation" );
sb.append( "{user=" ).append( user );
sb.append( ", remoteAddr='" ).append( remoteAddr ).append( '\'' );
sb.append( '}' );
return sb.toString();
}
}

View File

@ -0,0 +1,52 @@
package org.apache.archiva.admin.mock;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.archiva.audit.AuditEvent;
import org.apache.archiva.audit.AuditListener;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* @author Olivier Lamy
*/
@Service( "auditListener#mock" )
public class MockAuditListener
implements AuditListener
{
private List<AuditEvent> auditEvents = new ArrayList<AuditEvent>();
public void auditEvent( AuditEvent event )
{
auditEvents.add( event );
}
public List<AuditEvent> getAuditEvents()
{
return auditEvents;
}
public void clearEvents()
{
auditEvents.clear();
}
}

View File

@ -19,6 +19,7 @@ package org.apache.archiva.admin.repository.managed;
*/
import org.apache.archiva.admin.AuditInformation;
import org.apache.archiva.admin.mock.MockAuditListener;
import org.apache.commons.lang.StringUtils;
import org.codehaus.plexus.redback.users.User;
import org.codehaus.plexus.redback.users.memory.SimpleUser;
@ -38,10 +39,14 @@ public class ManagedRepositoryAdminTest
@Inject
private ManagedRepositoryAdmin managedRepositoryAdmin;
@Inject
private MockAuditListener mockAuditListener;
@Test
public void getAllManagedRepos()
throws Exception
{
mockAuditListener.clearEvents();
List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
assertNotNull( repos );
assertTrue( repos.size() > 0 );
@ -52,20 +57,24 @@ public class ManagedRepositoryAdminTest
assertNotNull( internal );
assertTrue( internal.isReleases() );
assertFalse( internal.isSnapshots() );
mockAuditListener.clearEvents();
}
@Test
public void getById()
throws Exception
{
mockAuditListener.clearEvents();
ManagedRepository repo = managedRepositoryAdmin.getManagedRepository( "internal" );
assertNotNull( repo );
mockAuditListener.clearEvents();
}
@Test
public void addDeleteManagedRepo()
throws Exception
{
mockAuditListener.clearEvents();
List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
assertNotNull( repos );
int initialSize = repos.size();
@ -87,12 +96,17 @@ public class ManagedRepositoryAdminTest
repos = managedRepositoryAdmin.getManagedRepositories();
assertNotNull( repos );
assertEquals( initialSize, repos.size() );
assertEquals( 0, mockAuditListener.getAuditEvents().size() );
mockAuditListener.clearEvents();
}
@Test
public void updateDeleteManagedRepo()
throws Exception
{
mockAuditListener.clearEvents();
List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
assertNotNull( repos );
int initialSize = repos.size();
@ -120,6 +134,14 @@ public class ManagedRepositoryAdminTest
assertEquals( newName, repo.getName() );
assertEquals( APPSERVER_BASE_PATH + "new-path", repo.getLocation() );
assertTrue( new File( APPSERVER_BASE_PATH + "new-path" ).exists() );
managedRepositoryAdmin.deleteManagedRepository( repo.getId(), getFakeAuditInformation() );
assertEquals( 1, mockAuditListener.getAuditEvents().size() );
log.info( "audit events {}", mockAuditListener.getAuditEvents() );
mockAuditListener.clearEvents();
}

View File

@ -26,6 +26,9 @@
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-lazy-init="true">
<context:annotation-config/>
<context:component-scan base-package="org.apache.archiva.admin.mock"/>
<bean name="scheduler" class="org.codehaus.redback.components.scheduler.DefaultScheduler">
<property name="properties">
<props>

View File

@ -327,8 +327,17 @@ public class AuditEvent
@Override
public String toString()
{
return "AuditEvent{" + "repositoryId='" + repositoryId + '\'' + ", userId='" + userId + '\'' + ", remoteIP='" +
remoteIP + '\'' + ", resource='" + resource + '\'' + ", action='" + action + '\'' + ", timestamp=" +
timestamp + '}';
final StringBuilder sb = new StringBuilder();
sb.append( "AuditEvent" );
sb.append( "{repositoryId='" ).append( repositoryId ).append( '\'' );
sb.append( ", userId='" ).append( userId ).append( '\'' );
sb.append( ", remoteIP='" ).append( remoteIP ).append( '\'' );
sb.append( ", resource='" ).append( resource ).append( '\'' );
sb.append( ", action='" ).append( action ).append( '\'' );
sb.append( ", timestamp=" ).append( timestamp );
sb.append( '}' );
return sb.toString();
}
}