simplify test case

git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@910453 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Porter 2010-02-16 10:19:58 +00:00
parent ec255eb8ee
commit 74d0a6376f
1 changed files with 100 additions and 96 deletions

View File

@ -100,21 +100,21 @@ public class AuditManagerTest
throws ParseException throws ParseException
{ {
int numEvents = 11; int numEvents = 11;
List<String> eventNames = new ArrayList<String>( numEvents ); List<AuditEvent> expectedEvents = new ArrayList<AuditEvent>( numEvents );
for ( int i = 0; i < numEvents; i++ ) for ( int i = 0; i < numEvents; i++ )
{ {
eventNames.add( createEventName( TIMESTAMP_FORMAT.parse( AUDIT_EVENT_BASE + MILLIS_FORMAT.format( i ) ) ) ); AuditEvent event = createEvent( AUDIT_EVENT_BASE + MILLIS_FORMAT.format( i ) );
expectedEvents.add( event );
} }
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ), eventNames ); metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ),
getEventNames( expectedEvents ) );
for ( String name : eventNames.subList( 1, eventNames.size() ) ) for ( AuditEvent event : expectedEvents.subList( 1, expectedEvents.size() ) )
{ {
AuditEvent event = createTestEvent( name );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name ), event ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, event.getName() ), event );
} }
metadataRepositoryControl.replay(); metadataRepositoryControl.replay();
@ -136,21 +136,20 @@ public class AuditManagerTest
throws ParseException throws ParseException
{ {
int numEvents = 5; int numEvents = 5;
List<String> eventNames = new ArrayList<String>( numEvents ); List<AuditEvent> expectedEvents = new ArrayList<AuditEvent>( numEvents );
for ( int i = 0; i < numEvents; i++ ) for ( int i = 0; i < numEvents; i++ )
{ {
eventNames.add( createEventName( TIMESTAMP_FORMAT.parse( AUDIT_EVENT_BASE + MILLIS_FORMAT.format( i ) ) ) ); expectedEvents.add( createEvent( AUDIT_EVENT_BASE + MILLIS_FORMAT.format( i ) ) );
} }
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ), eventNames ); metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ),
getEventNames( expectedEvents ) );
for ( String name : eventNames ) for ( AuditEvent event : expectedEvents )
{ {
AuditEvent event = createTestEvent( name );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name ), event ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, event.getName() ), event );
} }
metadataRepositoryControl.replay(); metadataRepositoryControl.replay();
@ -228,8 +227,7 @@ public class AuditManagerTest
public void testAddAuditEvent() public void testAddAuditEvent()
throws ParseException throws ParseException
{ {
String name = createEventName( new Date() ); AuditEvent event = createEvent( new Date() );
AuditEvent event = createTestEvent( name );
metadataRepository.addMetadataFacet( TEST_REPO_ID, event ); metadataRepository.addMetadataFacet( TEST_REPO_ID, event );
@ -243,8 +241,8 @@ public class AuditManagerTest
public void testAddAuditEventNoRepositoryId() public void testAddAuditEventNoRepositoryId()
throws ParseException throws ParseException
{ {
String name = createEventName( new Date() ); AuditEvent event = createEvent( new Date() );
AuditEvent event = createTestEvent( null, name ); event.setRepositoryId( null );
// should just be ignored // should just be ignored
@ -271,19 +269,19 @@ public class AuditManagerTest
{ {
Date current = new Date(); Date current = new Date();
String name1 = createEventName( new Date( current.getTime() - 12345 ) ); AuditEvent event1 = createEvent( new Date( current.getTime() - 12345 ) );
Date expectedTimestamp = new Date( current.getTime() - 3000 ); Date expectedTimestamp = new Date( current.getTime() - 3000 );
String name2 = createEventName( expectedTimestamp ); AuditEvent expectedEvent = createEvent( expectedTimestamp );
AuditEvent expectedEvent = createTestEvent( name2 ); AuditEvent event3 = createEvent( new Date( current.getTime() - 1000 ) );
String name3 = createEventName( new Date( current.getTime() - 1000 ) );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ), metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ),
Arrays.asList( name1, name2, name3 ) ); Arrays.asList( event1.getName(), expectedEvent.getName(), event3.getName() ) );
// only match the middle one // only match the middle one
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name2 ), expectedEvent ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent.getName() ),
expectedEvent );
metadataRepositoryControl.replay(); metadataRepositoryControl.replay();
@ -302,22 +300,22 @@ public class AuditManagerTest
{ {
Date current = new Date(); Date current = new Date();
String name1 = createEventName( new Date( current.getTime() - 12345 ) ); AuditEvent event1 = createEvent( new Date( current.getTime() - 12345 ) );
Date expectedTimestamp = new Date( current.getTime() - 3000 ); Date expectedTimestamp = new Date( current.getTime() - 3000 );
String name2 = createEventName( expectedTimestamp ); AuditEvent expectedEvent2 = createEvent( expectedTimestamp );
AuditEvent expectedEvent2 = createTestEvent( name2 );
Date ts3 = new Date( current.getTime() - 1000 ); Date ts3 = new Date( current.getTime() - 1000 );
String name3 = createEventName( ts3 ); AuditEvent expectedEvent3 = createEvent( ts3 );
AuditEvent expectedEvent3 = createTestEvent( name3 );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ), metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ),
Arrays.asList( name1, name2, name3 ) ); Arrays.asList( event1.getName(), expectedEvent2.getName(), expectedEvent3.getName() ) );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name2 ), expectedEvent2 ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent2.getName() ),
expectedEvent2 );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name3 ), expectedEvent3 ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent3.getName() ),
expectedEvent3 );
metadataRepositoryControl.replay(); metadataRepositoryControl.replay();
@ -337,21 +335,21 @@ public class AuditManagerTest
Date current = new Date(); Date current = new Date();
Date ts1 = new Date( current.getTime() - 12345 ); Date ts1 = new Date( current.getTime() - 12345 );
String name1 = createEventName( ts1 ); AuditEvent expectedEvent1 = createEvent( ts1 );
AuditEvent expectedEvent1 = createTestEvent( name1 );
Date expectedTimestamp = new Date( current.getTime() - 3000 ); Date expectedTimestamp = new Date( current.getTime() - 3000 );
String name2 = createEventName( expectedTimestamp ); AuditEvent expectedEvent2 = createEvent( expectedTimestamp );
AuditEvent expectedEvent2 = createTestEvent( name2 ); AuditEvent event3 = createEvent( new Date( current.getTime() - 1000 ) );
String name3 = createEventName( new Date( current.getTime() - 1000 ) );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ), metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ),
Arrays.asList( name1, name2, name3 ) ); Arrays.asList( expectedEvent1.getName(), expectedEvent2.getName(), event3.getName() ) );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name1 ), expectedEvent1 ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent1.getName() ),
expectedEvent1 );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name2 ), expectedEvent2 ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent2.getName() ),
expectedEvent2 );
metadataRepositoryControl.replay(); metadataRepositoryControl.replay();
@ -372,25 +370,25 @@ public class AuditManagerTest
Date current = new Date(); Date current = new Date();
Date ts1 = new Date( current.getTime() - 12345 ); Date ts1 = new Date( current.getTime() - 12345 );
String name1 = createEventName( ts1 ); AuditEvent expectedEvent1 = createEvent( ts1 );
AuditEvent expectedEvent1 = createTestEvent( name1 );
Date expectedTimestamp = new Date( current.getTime() - 3000 ); Date expectedTimestamp = new Date( current.getTime() - 3000 );
String name2 = createEventName( expectedTimestamp ); AuditEvent expectedEvent2 = createEvent( expectedTimestamp );
AuditEvent expectedEvent2 = createTestEvent( name2 );
Date ts3 = new Date( current.getTime() - 1000 ); Date ts3 = new Date( current.getTime() - 1000 );
String name3 = createEventName( ts3 ); AuditEvent expectedEvent3 = createEvent( ts3 );
AuditEvent expectedEvent3 = createTestEvent( name3 );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ), metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ),
Arrays.asList( name1, name2, name3 ) ); Arrays.asList( expectedEvent1.getName(), expectedEvent2.getName(), expectedEvent3.getName() ) );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name1 ), expectedEvent1 ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent1.getName() ),
expectedEvent1 );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name2 ), expectedEvent2 ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent2.getName() ),
expectedEvent2 );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name3 ), expectedEvent3 ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent3.getName() ),
expectedEvent3 );
metadataRepositoryControl.replay(); metadataRepositoryControl.replay();
@ -411,26 +409,26 @@ public class AuditManagerTest
Date current = new Date(); Date current = new Date();
Date ts1 = new Date( current.getTime() - 12345 ); Date ts1 = new Date( current.getTime() - 12345 );
String name1 = createEventName( ts1 ); AuditEvent expectedEvent1 = createEvent( ts1 );
AuditEvent expectedEvent1 = createTestEvent( name1 );
Date expectedTimestamp = new Date( current.getTime() - 3000 ); Date expectedTimestamp = new Date( current.getTime() - 3000 );
String name2 = createEventName( expectedTimestamp ); AuditEvent expectedEvent2 = createEvent( expectedTimestamp );
AuditEvent expectedEvent2 = createTestEvent( name2 );
expectedEvent2.setResource( "different-resource" ); expectedEvent2.setResource( "different-resource" );
Date ts3 = new Date( current.getTime() - 1000 ); Date ts3 = new Date( current.getTime() - 1000 );
String name3 = createEventName( ts3 ); AuditEvent expectedEvent3 = createEvent( ts3 );
AuditEvent expectedEvent3 = createTestEvent( name3 );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ), metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ),
Arrays.asList( name1, name2, name3 ) ); Arrays.asList( expectedEvent1.getName(), expectedEvent2.getName(), expectedEvent3.getName() ) );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name1 ), expectedEvent1 ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent1.getName() ),
expectedEvent1 );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name2 ), expectedEvent2 ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent2.getName() ),
expectedEvent2 );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name3 ), expectedEvent3 ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent3.getName() ),
expectedEvent3 );
metadataRepositoryControl.replay(); metadataRepositoryControl.replay();
@ -450,25 +448,25 @@ public class AuditManagerTest
{ {
Date current = new Date(); Date current = new Date();
String name1 = createEventName( new Date( current.getTime() - 12345 ) ); AuditEvent expectedEvent1 = createEvent( new Date( current.getTime() - 12345 ) );
AuditEvent expectedEvent1 = createTestEvent( name1 );
Date expectedTimestamp = new Date( current.getTime() - 3000 ); Date expectedTimestamp = new Date( current.getTime() - 3000 );
String name2 = createEventName( expectedTimestamp ); AuditEvent expectedEvent2 = createEvent( expectedTimestamp );
AuditEvent expectedEvent2 = createTestEvent( name2 );
expectedEvent2.setResource( "different-resource" ); expectedEvent2.setResource( "different-resource" );
String name3 = createEventName( new Date( current.getTime() - 1000 ) ); AuditEvent expectedEvent3 = createEvent( new Date( current.getTime() - 1000 ) );
AuditEvent expectedEvent3 = createTestEvent( name3 );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ), metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ),
Arrays.asList( name1, name2, name3 ) ); Arrays.asList( expectedEvent1.getName(), expectedEvent2.getName(), expectedEvent3.getName() ) );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name1 ), expectedEvent1 ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent1.getName() ),
expectedEvent1 );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name2 ), expectedEvent2 ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent2.getName() ),
expectedEvent2 );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name3 ), expectedEvent3 ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent3.getName() ),
expectedEvent3 );
metadataRepositoryControl.replay(); metadataRepositoryControl.replay();
@ -486,26 +484,29 @@ public class AuditManagerTest
Date current = new Date(); Date current = new Date();
Date ts1 = new Date( current.getTime() - 12345 ); Date ts1 = new Date( current.getTime() - 12345 );
String name1 = createEventName( ts1 ); AuditEvent expectedEvent1 = createEvent( ts1 );
AuditEvent expectedEvent1 = createTestEvent( TEST_REPO_ID, name1 );
Date expectedTimestamp = new Date( current.getTime() - 3000 ); Date expectedTimestamp = new Date( current.getTime() - 3000 );
String name2 = createEventName( expectedTimestamp ); AuditEvent expectedEvent2 = createEvent( expectedTimestamp );
AuditEvent expectedEvent2 = createTestEvent( TEST_REPO_ID_2, name2 ); expectedEvent2.setRepositoryId( TEST_REPO_ID_2 );
Date ts3 = new Date( current.getTime() - 1000 ); Date ts3 = new Date( current.getTime() - 1000 );
String name3 = createEventName( ts3 ); AuditEvent expectedEvent3 = createEvent( ts3 );
AuditEvent expectedEvent3 = createTestEvent( TEST_REPO_ID, name3 );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ), Arrays.asList( name1, name3 ) ); metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ),
Arrays.asList( expectedEvent1.getName(), expectedEvent3.getName() ) );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacets( TEST_REPO_ID_2, AuditEvent.FACET_ID ), Arrays.asList( name2 ) ); metadataRepository.getMetadataFacets( TEST_REPO_ID_2, AuditEvent.FACET_ID ),
Arrays.asList( expectedEvent2.getName() ) );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name1 ), expectedEvent1 ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent1.getName() ),
expectedEvent1 );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID_2, AuditEvent.FACET_ID, name2 ), expectedEvent2 ); metadataRepository.getMetadataFacet( TEST_REPO_ID_2, AuditEvent.FACET_ID, expectedEvent2.getName() ),
expectedEvent2 );
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name3 ), expectedEvent3 ); metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent3.getName() ),
expectedEvent3 );
metadataRepositoryControl.replay(); metadataRepositoryControl.replay();
@ -526,10 +527,10 @@ public class AuditManagerTest
{ {
Date current = new Date(); Date current = new Date();
String name1 = createEventName( new Date( current.getTime() - 12345 ) ); String name1 = createEvent( new Date( current.getTime() - 12345 ) ).getName();
Date expectedTimestamp = new Date( current.getTime() - 3000 ); Date expectedTimestamp = new Date( current.getTime() - 3000 );
String name2 = createEventName( expectedTimestamp ); String name2 = createEvent( expectedTimestamp ).getName();
String name3 = createEventName( new Date( current.getTime() - 1000 ) ); String name3 = createEvent( new Date( current.getTime() - 1000 ) ).getName();
metadataRepositoryControl.expectAndReturn( metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ), metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ),
@ -551,24 +552,17 @@ public class AuditManagerTest
return TEST_RESOURCE_BASE + "/" + num + ".xml"; return TEST_RESOURCE_BASE + "/" + num + ".xml";
} }
private static String createEventName( Date timestamp ) private static AuditEvent createEvent( Date timestamp )
throws ParseException throws ParseException
{ {
// TODO: I think we can reverse the calls. return createEvent( TIMESTAMP_FORMAT.format( timestamp ) );
return createEvent( TEST_REPO_ID, TIMESTAMP_FORMAT.format( timestamp ), null ).getName();
} }
private static AuditEvent createTestEvent( String name ) private static AuditEvent createEvent( String ts )
throws ParseException throws ParseException
{ {
return createTestEvent( TEST_REPO_ID, name ); return createEvent( TEST_REPO_ID, ts, getDefaultTestResourceName(
} ts.substring( AUDIT_EVENT_BASE.length(), AUDIT_EVENT_BASE.length() + 3 ) ) );
private static AuditEvent createTestEvent( String repoId, String name )
throws ParseException
{
return createEvent( repoId, name, getDefaultTestResourceName(
name.substring( AUDIT_EVENT_BASE.length(), AUDIT_EVENT_BASE.length() + 3 ) ) );
} }
private static AuditEvent createEvent( String repositoryId, String timestamp, String resource ) private static AuditEvent createEvent( String repositoryId, String timestamp, String resource )
@ -598,4 +592,14 @@ public class AuditManagerTest
assertEquals( TEST_USER, event.getUserId() ); assertEquals( TEST_USER, event.getUserId() );
assertEquals( resource, event.getResource() ); assertEquals( resource, event.getResource() );
} }
private List<String> getEventNames( List<AuditEvent> events )
{
List<String> names = new ArrayList<String>( events.size() );
for ( AuditEvent event : events )
{
names.add( event.getName() );
}
return names;
}
} }