Fix dspace-api module per new code style

This commit is contained in:
Tim Donohue
2018-02-14 10:53:46 -06:00
parent 8ffc97f7f9
commit 8a48f782ea
1051 changed files with 53347 additions and 63373 deletions

View File

@@ -7,6 +7,15 @@
*/
package org.dspace.content;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@@ -14,28 +23,31 @@ import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.BitstreamFormatService;
import org.dspace.core.Context;
import org.junit.*;
import static org.junit.Assert.* ;
import static org.hamcrest.CoreMatchers.*;
import mockit.*;
import mockit.NonStrictExpectations;
import org.apache.log4j.Logger;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.BitstreamFormatService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* Unit Tests for class Bitstream
*
* @author pvillega
*/
public class BitstreamTest extends AbstractDSpaceObjectTest
{
/** log4j category */
public class BitstreamTest extends AbstractDSpaceObjectTest {
/**
* log4j category
*/
private static final Logger log = Logger.getLogger(BitstreamTest.class);
protected BitstreamFormatService bitstreamFormatService = ContentServiceFactory.getInstance().getBitstreamFormatService();
protected BitstreamFormatService bitstreamFormatService = ContentServiceFactory.getInstance()
.getBitstreamFormatService();
/**
* BitStream instance for the tests
@@ -51,23 +63,18 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
*/
@Before
@Override
public void init()
{
public void init() {
super.init();
try
{
try {
//we have to create a new bitstream in the database
File f = new File(testProps.get("test.bitstream").toString());
this.bs = bitstreamService.create(context, new FileInputStream(f));
this.dspaceObject = bs;
this.dspaceObject = bs;
//we need to commit the changes so we don't block the table for testing
}
catch (IOException ex) {
} catch (IOException ex) {
log.error("IO Error in init", ex);
fail("SQL Error in init: " + ex.getMessage());
}
catch (SQLException ex)
{
} catch (SQLException ex) {
log.error("SQL Error in init", ex);
fail("SQL Error in init: " + ex.getMessage());
}
@@ -82,8 +89,7 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
*/
@After
@Override
public void destroy()
{
public void destroy() {
bs = null;
super.destroy();
}
@@ -92,10 +98,9 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
* Test of find method, of class Bitstream.
*/
@Test
public void testBSFind() throws SQLException
{
public void testBSFind() throws SQLException {
UUID id = this.bs.getID();
Bitstream found = bitstreamService.find(context, id);
Bitstream found = bitstreamService.find(context, id);
assertThat("testBSFind 0", found, notNullValue());
//the item created by default has no name nor type set
assertThat("testBSFind 1", found.getFormat(context).getMIMEType(), equalTo("application/octet-stream"));
@@ -107,31 +112,27 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
* Test of findAll method, of class Bitstream.
*/
@Test
public void testFindAll() throws SQLException
{
List<Bitstream> found = bitstreamService.findAll(context);
public void testFindAll() throws SQLException {
List<Bitstream> found = bitstreamService.findAll(context);
assertThat("testFindAll 0", found, notNullValue());
//we have many bs, one created per test run, so at least we have 1 if
//this test is run first
assertTrue("testFindAll 1", found.size() >= 1);
boolean added = false;
for(Bitstream b: found)
{
if(b.equals(bs))
{
for (Bitstream b : found) {
if (b.equals(bs)) {
added = true;
}
}
assertTrue("testFindAll 2",added);
assertTrue("testFindAll 2", added);
}
/**
* Test of create method, of class Bitstream.
*/
@Test
public void testCreate() throws IOException, SQLException
{
public void testCreate() throws IOException, SQLException {
File f = new File(testProps.get("test.bitstream").toString());
Bitstream created = bitstreamService.create(context, new FileInputStream(f));
@@ -144,16 +145,15 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
* Test of register method, of class Bitstream.
*/
@Test
public void testRegister() throws IOException, SQLException, AuthorizeException
{
new NonStrictExpectations(authorizeService.getClass())
{{
public void testRegister() throws IOException, SQLException, AuthorizeException {
new NonStrictExpectations(authorizeService.getClass()) {{
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.WRITE); result = null;
Constants.WRITE);
result = null;
}};
int assetstore = 0;
File f = new File(testProps.get("test.bitstream").toString());
Bitstream registered = bitstreamService.register(context,assetstore, f.getName());
Bitstream registered = bitstreamService.register(context, assetstore, f.getName());
//the item created by default has no name nor type set
assertThat("testRegister 0", registered.getFormat(context).getMIMEType(), equalTo("application/octet-stream"));
assertThat("testRegister 1", registered.getName(), nullValue());
@@ -164,21 +164,21 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
*/
@Override
@Test
public void testGetID()
{
public void testGetID() {
assertTrue("testGetID 0", bs.getID() != null);
}
@Test
public void testLegacyID() { assertTrue("testGetLegacyID 0", bs.getLegacyId() == null);}
public void testLegacyID() {
assertTrue("testGetLegacyID 0", bs.getLegacyId() == null);
}
/**
* Test of getHandle method, of class Bitstream.
*/
@Override
@Test
public void testGetHandle()
{
public void testGetHandle() {
//no handle for bitstreams
assertThat("testGetHandle 0", bs.getHandle(), nullValue());
}
@@ -187,8 +187,7 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
* Test of getSequenceID method, of class Bitstream.
*/
@Test
public void testGetSequenceID()
{
public void testGetSequenceID() {
//sequence id is -1 when not set
assertThat("testGetSequenceID 0", bs.getSequenceID(), equalTo(-1));
}
@@ -197,8 +196,7 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
* Test of setSequenceID method, of class Bitstream.
*/
@Test
public void testSetSequenceID()
{
public void testSetSequenceID() {
int val = 2;
bs.setSequenceID(val);
assertThat("testSetSequenceID 0", bs.getSequenceID(), equalTo(val));
@@ -209,8 +207,7 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
*/
@Override
@Test
public void testGetName()
{
public void testGetName() {
//name is null when not set
assertThat("testGetName 0", bs.getName(), nullValue());
}
@@ -219,8 +216,7 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
* Test of setName method, of class Bitstream.
*/
@Test
public void testSetName() throws SQLException
{
public void testSetName() throws SQLException {
String name = "new name";
bs.setName(context, name);
assertThat("testGetName 0", bs.getName(), notNullValue());
@@ -232,8 +228,7 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
* Test of getSource method, of class Bitstream.
*/
@Test
public void testGetSource()
{
public void testGetSource() {
//source is null if not set
assertThat("testGetSource 0", bs.getSource(), nullValue());
}
@@ -242,8 +237,7 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
* Test of setSource method, of class Bitstream.
*/
@Test
public void testSetSource() throws SQLException
{
public void testSetSource() throws SQLException {
String source = "new source";
bs.setSource(context, source);
assertThat("testSetSource 0", bs.getSource(), notNullValue());
@@ -255,8 +249,7 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
* Test of getDescription method, of class Bitstream.
*/
@Test
public void testGetDescription()
{
public void testGetDescription() {
//default description is null if not set
assertThat("testGetDescription 0", bs.getDescription(), nullValue());
}
@@ -265,8 +258,7 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
* Test of setDescription method, of class Bitstream.
*/
@Test
public void testSetDescription() throws SQLException
{
public void testSetDescription() throws SQLException {
String description = "new description";
bs.setDescription(context, description);
assertThat("testSetDescription 0", bs.getDescription(), notNullValue());
@@ -278,8 +270,7 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
* Test of getChecksum method, of class Bitstream.
*/
@Test
public void testGetChecksum()
{
public void testGetChecksum() {
String checksum = "75a060bf6eb63fd0aad88b7d757728d3";
assertThat("testGetChecksum 0", bs.getChecksum(), notNullValue());
assertThat("testGetChecksum 1", bs.getChecksum(), not(equalTo("")));
@@ -290,23 +281,21 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
* Test of getChecksumAlgorithm method, of class Bitstream.
*/
@Test
public void testGetChecksumAlgorithm()
{
public void testGetChecksumAlgorithm() {
String alg = "MD5";
assertThat("testGetChecksumAlgorithm 0", bs.getChecksumAlgorithm(),
notNullValue());
notNullValue());
assertThat("testGetChecksumAlgorithm 1", bs.getChecksumAlgorithm(),
not(equalTo("")));
not(equalTo("")));
assertThat("testGetChecksumAlgorithm 2", bs.getChecksumAlgorithm(),
equalTo(alg));
equalTo(alg));
}
/**
* Test of getSize method, of class Bitstream.
*/
@Test
public void testGetSize()
{
public void testGetSize() {
long size = 238413; // yuck, hardcoded!
assertThat("testGetSize 0", bs.getSize(), equalTo(size));
}
@@ -315,27 +304,25 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
* Test of setUserFormatDescription method, of class Bitstream.
*/
@Test
public void testSetUserFormatDescription() throws SQLException
{
public void testSetUserFormatDescription() throws SQLException {
String userdescription = "user format description";
bs.setUserFormatDescription(context, userdescription);
assertThat("testSetUserFormatDescription 0", bs.getUserFormatDescription()
, notNullValue());
, notNullValue());
assertThat("testSetUserFormatDescription 1", bs.getUserFormatDescription()
, not(equalTo("")));
, not(equalTo("")));
assertThat("testSetUserFormatDescription 2", bs.getUserFormatDescription()
, equalTo(userdescription));
, equalTo(userdescription));
}
/**
* Test of getUserFormatDescription method, of class Bitstream.
*/
@Test
public void testGetUserFormatDescription()
{
public void testGetUserFormatDescription() {
//null by default if not set
assertThat("testGetUserFormatDescription 0", bs.getUserFormatDescription()
, nullValue());
, nullValue());
}
/**
@@ -346,19 +333,18 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
//format is unknown by default
String format = "Unknown";
assertThat("testGetFormatDescription 0", bs.getFormatDescription(context),
notNullValue());
notNullValue());
assertThat("testGetFormatDescription 1", bs.getFormatDescription(context),
not(equalTo("")));
not(equalTo("")));
assertThat("testGetFormatDescription 2", bs.getFormatDescription(context),
equalTo(format));
equalTo(format));
}
/**
* Test of getFormat method, of class Bitstream.
*/
@Test
public void testGetFormat() throws SQLException
{
public void testGetFormat() throws SQLException {
assertThat("testGetFormat 0", bs.getFormat(context), notNullValue());
assertThat("testGetFormat 1", bs.getFormat(context), equalTo(bitstreamFormatService.findUnknown(context)));
}
@@ -367,8 +353,7 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
* Test of setFormat method, of class Bitstream.
*/
@Test
public void testSetFormat() throws SQLException
{
public void testSetFormat() throws SQLException {
int id = 3;
BitstreamFormat format = bitstreamFormatService.find(context, id);
bs.setFormat(format);
@@ -379,14 +364,13 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
/**
* Test of update method, of class Bitstream.
*/
@Test(expected=AuthorizeException.class)
public void testUpdateNotAdmin() throws SQLException, AuthorizeException
{
new NonStrictExpectations(authorizeService.getClass())
{{
@Test(expected = AuthorizeException.class)
public void testUpdateNotAdmin() throws SQLException, AuthorizeException {
new NonStrictExpectations(authorizeService.getClass()) {{
// Disallow Bitstream WRITE perms
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.WRITE); result = new AuthorizeException();
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.WRITE);
result = new AuthorizeException();
}};
//TODO: we need to verify the update, how?
@@ -397,16 +381,15 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
* Test of update method, of class Bitstream.
*/
@Test
public void testUpdateAdmin() throws SQLException, AuthorizeException
{
new NonStrictExpectations(authorizeService.getClass())
{{
public void testUpdateAdmin() throws SQLException, AuthorizeException {
new NonStrictExpectations(authorizeService.getClass()) {{
// Allow Bitstream WRITE perms
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.WRITE); result = null;
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.WRITE);
result = null;
}};
//TODO: we need to verify the update, how?
bitstreamService.update(context, bs);
}
@@ -415,15 +398,15 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
* Test of delete method, of class Bitstream.
*/
@Test
public void testDeleteAndExpunge() throws IOException, SQLException, AuthorizeException
{
new NonStrictExpectations(authorizeService.getClass())
{{
public void testDeleteAndExpunge() throws IOException, SQLException, AuthorizeException {
new NonStrictExpectations(authorizeService.getClass()) {{
// Allow Bitstream WRITE perms
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.WRITE); result = null;
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.DELETE); result = null;
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.WRITE);
result = null;
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.DELETE);
result = null;
}};
// Create a new bitstream, which we can delete. As ordering of these
@@ -447,13 +430,12 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
*/
@Test
public void testRetrieveCanRead() throws IOException, SQLException,
AuthorizeException
{
new NonStrictExpectations(authorizeService.getClass())
{{
AuthorizeException {
new NonStrictExpectations(authorizeService.getClass()) {{
// Allow Bitstream READ perms
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.READ); result = null;
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.READ);
result = null;
}};
assertThat("testRetrieveCanRead 0", bitstreamService.retrieve(context, bs), notNullValue());
@@ -462,15 +444,14 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
/**
* Test of retrieve method, of class Bitstream.
*/
@Test(expected=AuthorizeException.class)
@Test(expected = AuthorizeException.class)
public void testRetrieveNoRead() throws IOException, SQLException,
AuthorizeException
{
new NonStrictExpectations(authorizeService.getClass())
{{
AuthorizeException {
new NonStrictExpectations(authorizeService.getClass()) {{
// Disallow Bitstream READ perms
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.READ); result = new AuthorizeException();
Constants.READ);
result = new AuthorizeException();
}};
assertThat("testRetrieveNoRead 0", bitstreamService.retrieve(context, bs), notNullValue());
@@ -480,8 +461,7 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
* Test of getBundles method, of class Bitstream.
*/
@Test
public void testGetBundles() throws SQLException
{
public void testGetBundles() throws SQLException {
assertThat("testGetBundles 0", bs.getBundles(), notNullValue());
//by default no bundles
assertTrue("testGetBundles 1", bs.getBundles().size() == 0);
@@ -492,20 +472,18 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
*/
@Override
@Test
public void testGetType()
{
assertThat("testGetType 0", bs.getType(), equalTo(Constants.BITSTREAM));
public void testGetType() {
assertThat("testGetType 0", bs.getType(), equalTo(Constants.BITSTREAM));
}
/**
* Test of isRegisteredBitstream method, of class Bitstream.
*/
@Test
public void testIsRegisteredBitstream()
{
public void testIsRegisteredBitstream() {
//false by default
assertThat("testIsRegisteredBitstream 0", bitstreamService.isRegisteredBitstream(bs),
equalTo(false));
equalTo(false));
}
/**
@@ -522,11 +500,10 @@ public class BitstreamTest extends AbstractDSpaceObjectTest
*/
@Test
@Override
public void testGetParentObject() throws SQLException
{
public void testGetParentObject() throws SQLException {
//by default this bitstream is not linked to any object
assertThat("testGetParentObject 0", bitstreamService.getParentObject(context, bs),
nullValue());
nullValue());
}
}