Fix exception validation. Fix expectations syntax for latest JMockit

This commit is contained in:
Tim Donohue
2014-08-12 09:52:26 -05:00
parent 347a2dc6eb
commit 79d91e9439

View File

@@ -15,13 +15,13 @@ import org.dspace.core.Context;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.File; import java.io.File;
import java.sql.SQLException;
import org.dspace.AbstractUnitTest; import org.dspace.AbstractUnitTest;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.junit.*; import org.junit.*;
import static org.junit.Assert.* ; import static org.junit.Assert.* ;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import org.junit.rules.ExpectedException;
/** /**
@@ -34,33 +34,9 @@ public class InstallItemTest extends AbstractUnitTest
/** log4j category */ /** log4j category */
private static final Logger log = Logger.getLogger(InstallItemTest.class); private static final Logger log = Logger.getLogger(InstallItemTest.class);
/** /** Used to check/verify thrown exceptions in below tests **/
* This method will be run before every test as per @Before. It will @Rule
* initialize resources required for the tests. public ExpectedException thrown = ExpectedException.none();
*
* Other methods can be annotated with @Before here or in subclasses
* but no execution order is guaranteed
*/
@Before
@Override
public void init()
{
super.init();
}
/**
* This method will be run after every test as per @After. It will
* clean resources initialized by the @Before methods.
*
* Other methods can be annotated with @After here or in subclasses
* but no execution order is guaranteed
*/
@After
@Override
public void destroy()
{
super.destroy();
}
/** /**
* Test of installItem method, of class InstallItem. * Test of installItem method, of class InstallItem.
@@ -99,19 +75,18 @@ public class InstallItemTest extends AbstractUnitTest
/** /**
* Test of installItem method (with an invalid handle), of class InstallItem. * Test of installItem method (with an invalid handle), of class InstallItem.
*/ */
@Test(expected=SQLException.class) @Test
public void testInstallItem_invalidHandle() throws Exception public void testInstallItem_invalidHandle() throws Exception
{ {
//Default to Full-Admin rights //Default to Full-Admin rights
new NonStrictExpectations() new NonStrictExpectations(AuthorizeManager.class)
{ {{
AuthorizeManager authManager; // Deny Community ADD perms
{ AuthorizeManager.authorizeActionBoolean((Context) any, (Community) any,
AuthorizeManager.authorizeActionBoolean((Context) any, (Community) any, Constants.ADD); result = false;
Constants.ADD); result = false; // Allow full Admin perms
AuthorizeManager.isAdmin((Context) any); result = true; AuthorizeManager.isAdmin((Context) any); result = true;
} }};
};
String handle = "1345/567"; String handle = "1345/567";
Collection col = Collection.create(context); Collection col = Collection.create(context);
@@ -119,9 +94,12 @@ public class InstallItemTest extends AbstractUnitTest
WorkspaceItem is2 = WorkspaceItem.create(context, col, false); WorkspaceItem is2 = WorkspaceItem.create(context, col, false);
//Test assigning the same Handle to two different items //Test assigning the same Handle to two different items
// this should throw an exception InstallItem.installItem(context, is, handle);
Item result1 = InstallItem.installItem(context, is, handle);
Item result2 = InstallItem.installItem(context, is2, handle); // Assigning the same handle again should throw a RuntimeException
thrown.expect(RuntimeException.class);
thrown.expectMessage("Error while attempting to create identifier");
InstallItem.installItem(context, is2, handle);
fail("Exception expected"); fail("Exception expected");
} }