Browse Source

Converted to use jmock.

3.0.x
Luke Taylor 17 years ago
parent
commit
7bf47f2d97
  1. 201
      acl/src/test/java/org/springframework/security/acls/domain/AuditLoggerTests.java

201
acl/src/test/java/org/springframework/security/acls/domain/AuditLoggerTests.java

@ -1,137 +1,92 @@
package org.springframework.security.acls.domain; package org.springframework.security.acls.domain;
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.Serializable;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.security.acls.AccessControlEntry; import org.springframework.security.acls.AccessControlEntry;
import org.springframework.security.acls.Acl;
import org.springframework.security.acls.AuditableAccessControlEntry; import org.springframework.security.acls.AuditableAccessControlEntry;
import org.springframework.security.acls.Permission;
import org.springframework.security.acls.sid.Sid;
/** /**
* Test class for {@link ConsoleAuditLogger}. * Test class for {@link ConsoleAuditLogger}.
* *
* @author Andrei Stefan * @author Andrei Stefan
* @version $Id$
*/ */
public class AuditLoggerTests extends TestCase { public class AuditLoggerTests {
//~ Instance fields ================================================================================================ //~ Instance fields ================================================================================================
private Mockery jmock = new JUnit4Mockery();
private PrintStream console; private PrintStream console;
private ByteArrayOutputStream bytes = new ByteArrayOutputStream();
private ByteArrayOutputStream bytes = new ByteArrayOutputStream(); private ConsoleAuditLogger logger;
private AuditableAccessControlEntry ace;
//~ Methods ======================================================================================================== private Expectations aceRequiresAudit;
private Expectations aceDoesntRequireAudit;
public void setUp() throws Exception {
console = System.out; //~ Methods ========================================================================================================
System.setOut(new PrintStream(bytes));
} @Before
public void setUp() throws Exception {
public void tearDown() throws Exception { logger = new ConsoleAuditLogger();
System.setOut(console); ace = jmock.mock(AuditableAccessControlEntry.class);
} aceRequiresAudit = new Expectations() {{
allowing(ace).isAuditSuccess(); will(returnValue(true));
public void testLoggingTests() throws Exception { allowing(ace).isAuditFailure(); will(returnValue(true));
ConsoleAuditLogger logger = new ConsoleAuditLogger(); }};
MockAccessControlEntryImpl auditableAccessControlEntry = new MockAccessControlEntryImpl(); aceDoesntRequireAudit = new Expectations() {{
allowing(ace).isAuditSuccess(); will(returnValue(false));
logger.logIfNeeded(true, auditableAccessControlEntry); allowing(ace).isAuditFailure(); will(returnValue(false));
Assert.assertTrue(bytes.size() == 0); }};
bytes.reset(); console = System.out;
logger.logIfNeeded(false, auditableAccessControlEntry); System.setOut(new PrintStream(bytes));
Assert.assertTrue(bytes.size() == 0); }
auditableAccessControlEntry.setAuditSuccess(true); @After
bytes.reset(); public void tearDown() throws Exception {
System.setOut(console);
logger.logIfNeeded(true, auditableAccessControlEntry); bytes.reset();
Assert.assertTrue(bytes.toString().length() > 0); }
Assert.assertTrue(bytes.toString().startsWith("GRANTED due to ACE"));
@Test
auditableAccessControlEntry.setAuditFailure(true); public void nonAuditableAceIsIgnored() {
bytes.reset(); AccessControlEntry ace = jmock.mock(AccessControlEntry.class);
logger.logIfNeeded(true, ace);
logger.logIfNeeded(false, auditableAccessControlEntry); assertEquals(0, bytes.size());
Assert.assertTrue(bytes.toString().length() > 0); }
Assert.assertTrue(bytes.toString().startsWith("DENIED due to ACE"));
@Test
MockAccessControlEntry accessControlEntry = new MockAccessControlEntry(); public void successIsNotLoggedIfAceDoesntRequireSuccessAudit() throws Exception {
bytes.reset(); jmock.checking(aceDoesntRequireAudit);
logger.logIfNeeded(true, accessControlEntry); logger.logIfNeeded(true, ace);
Assert.assertTrue(bytes.size() == 0); assertEquals(0, bytes.size());
} }
//~ Inner Classes ================================================================================================== @Test
public void successIsLoggedIfAceRequiresSuccessAudit() throws Exception {
private class MockAccessControlEntryImpl implements AuditableAccessControlEntry { jmock.checking(aceRequiresAudit);
private boolean auditFailure = false; logger.logIfNeeded(true, ace);
assertTrue(bytes.toString().startsWith("GRANTED due to ACE"));
private boolean auditSuccess = false; }
public boolean isAuditFailure() { @Test
return auditFailure; public void failureIsntLoggedIfAceDoesntRequireFailureAudit() throws Exception {
} jmock.checking(aceDoesntRequireAudit);
logger.logIfNeeded(false, ace);
public boolean isAuditSuccess() { assertEquals(0, bytes.size());
return auditSuccess; }
}
@Test
public Acl getAcl() { public void failureIsLoggedIfAceRequiresFailureAudit() throws Exception {
return null; jmock.checking(aceRequiresAudit);
} logger.logIfNeeded(false, ace);
assertTrue(bytes.toString().startsWith("DENIED due to ACE"));
public Serializable getId() { }
return null;
}
public Permission getPermission() {
return null;
}
public Sid getSid() {
return null;
}
public boolean isGranting() {
return false;
}
public void setAuditFailure(boolean auditFailure) {
this.auditFailure = auditFailure;
}
public void setAuditSuccess(boolean auditSuccess) {
this.auditSuccess = auditSuccess;
}
}
private class MockAccessControlEntry implements AccessControlEntry {
public Acl getAcl() {
return null;
}
public Serializable getId() {
return null;
}
public Permission getPermission() {
return null;
}
public Sid getSid() {
return null;
}
public boolean isGranting() {
return false;
}
}
} }

Loading…
Cancel
Save