diff --git a/archiva-modules/archiva-base/archiva-repository-admin/src/main/java/org/apache/archiva/admin/AuditInformation.java b/archiva-modules/archiva-base/archiva-repository-admin/src/main/java/org/apache/archiva/admin/AuditInformation.java index d01ec4bda..098e09351 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/src/main/java/org/apache/archiva/admin/AuditInformation.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/src/main/java/org/apache/archiva/admin/AuditInformation.java @@ -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(); + } } diff --git a/archiva-modules/archiva-base/archiva-repository-admin/src/test/java/org/apache/archiva/admin/mock/MockAuditListener.java b/archiva-modules/archiva-base/archiva-repository-admin/src/test/java/org/apache/archiva/admin/mock/MockAuditListener.java new file mode 100644 index 000000000..42387a64d --- /dev/null +++ b/archiva-modules/archiva-base/archiva-repository-admin/src/test/java/org/apache/archiva/admin/mock/MockAuditListener.java @@ -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 auditEvents = new ArrayList(); + + public void auditEvent( AuditEvent event ) + { + auditEvents.add( event ); + } + + public List getAuditEvents() + { + return auditEvents; + } + + public void clearEvents() + { + auditEvents.clear(); + } +} diff --git a/archiva-modules/archiva-base/archiva-repository-admin/src/test/java/org/apache/archiva/admin/repository/managed/ManagedRepositoryAdminTest.java b/archiva-modules/archiva-base/archiva-repository-admin/src/test/java/org/apache/archiva/admin/repository/managed/ManagedRepositoryAdminTest.java index dd932ce6a..0d2923721 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/src/test/java/org/apache/archiva/admin/repository/managed/ManagedRepositoryAdminTest.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/src/test/java/org/apache/archiva/admin/repository/managed/ManagedRepositoryAdminTest.java @@ -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 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 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 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(); } diff --git a/archiva-modules/archiva-base/archiva-repository-admin/src/test/resources/spring-context.xml b/archiva-modules/archiva-base/archiva-repository-admin/src/test/resources/spring-context.xml index 176462a2f..9f5efb007 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/src/test/resources/spring-context.xml +++ b/archiva-modules/archiva-base/archiva-repository-admin/src/test/resources/spring-context.xml @@ -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"> + + + diff --git a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditEvent.java b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditEvent.java index 77d7ad8de..6e6d3cf6c 100644 --- a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditEvent.java +++ b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditEvent.java @@ -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(); } + + }