added a test case that asserts that a lambda's enclosing object is serialized if and only if the lambda accesses it

This commit is contained in:
Axel Uhl committed 2014-11-21 19:47:51 +01:00
1 parent fcfde37fad
commit 2248de0f72
2 files changed
+47

No files matched your search

@@ -5,14 +5,25 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Logger;
import org.junit.Test;
import com.sap.sse.security.User;
import com.sap.sse.security.operations.CreateUser;
import com.sap.sse.security.operations.SecurityOperation;
import com.sap.sse.security.shared.UserManagementException;
public class SimpleSecurityReplicationTest extends AbstractSecurityReplicationTest {
private static final Logger logger = Logger.getLogger(SimpleSecurityReplicationTest.class.getName());
@Test
public void testSimpleReplication() throws InterruptedException, UserManagementException {
final String username = "Ernie";
@@ -59,4 +70,27 @@ public class SimpleSecurityReplicationTest extends AbstractSecurityReplicationTe
assertEquals(email, replicatedErnie.getEmail());
assertTrue(replica.checkPassword(username, password));
}
private void readObject(ObjectInputStream ois) {
fail("This test case is not serializable; how did we get here in the first place...?");
}
@Test
public void testLambdaDoesNotSerializeEnclosingInstance() throws InterruptedException, UserManagementException, IOException {
final String username = "Ernie";
final String email = "ernie@sesame-street.com";
final String password = "BertMyFriend";
final String validationBaseURL = "http://me.to.back.com";
assertNull(master.getUserByName(username));
SecurityOperation<User> op = (SecurityOperation<User>) (master -> master.createSimpleUser(username, email, password, validationBaseURL));
new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(op); // the test case is not serializable; if it were, its writeObject() would thrown an exception
SecurityOperation<String> opWithRefToEnclosingInstance = (SecurityOperation<String>) (master -> this.toString());
try {
new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(opWithRefToEnclosingInstance);
fail("Expected the lambda not to be serializable because it references the non-serializable enclosing instance");
} catch (NotSerializableException nse) {
// this is expected
logger.info("Caught expected exception "+nse);
}
}
}
@@ -275,6 +275,19 @@ public class SecurityServiceImpl extends RemoteServiceServlet implements Securit
@Override
public User createSimpleUser(final String username, final String email, String password,
final String validationBaseURL) throws UserManagementException, MailException {
return apply(thiz -> internalCreateSimpleUser(username, email, password, validationBaseURL));
}
/**
* @param username
* @param email
* @param password
* @param validationBaseURL
* @return
* @throws UserManagementException
*/
private User internalCreateSimpleUser(final String username, final String email, String password,
final String validationBaseURL) throws UserManagementException {
if (store.getUserByName(username) != null) {
throw new UserManagementException(UserManagementException.USER_ALREADY_EXISTS);
}