diff --git a/dspace-api/src/main/java/org/dspace/app/bulkedit/DSpaceCSV.java b/dspace-api/src/main/java/org/dspace/app/bulkedit/DSpaceCSV.java index 3533a2397b3..1533ea9b121 100644 --- a/dspace-api/src/main/java/org/dspace/app/bulkedit/DSpaceCSV.java +++ b/dspace-api/src/main/java/org/dspace/app/bulkedit/DSpaceCSV.java @@ -19,6 +19,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -457,7 +458,7 @@ public final void addItem(Item i) throws Exception { List collections = i.getCollections(); for (Collection c : collections) { // Only add if it is not the owning collection - if (!c.getHandle().equals(owningCollectionHandle)) { + if (!Objects.equals(c.getHandle(), owningCollectionHandle)) { line.add("collection", c.getHandle()); } } diff --git a/dspace-api/src/test/java/org/dspace/identifier/VersionedHandleIdentifierProviderIT.java b/dspace-api/src/test/java/org/dspace/identifier/VersionedHandleIdentifierProviderIT.java index 66096b4ea03..54d3f703511 100644 --- a/dspace-api/src/test/java/org/dspace/identifier/VersionedHandleIdentifierProviderIT.java +++ b/dspace-api/src/test/java/org/dspace/identifier/VersionedHandleIdentifierProviderIT.java @@ -10,9 +10,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; -import java.text.MessageFormat; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import org.dspace.AbstractIntegrationTestWithDatabase; @@ -28,15 +29,16 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.springframework.beans.factory.config.AutowireCapableBeanFactory; +import org.springframework.test.util.ReflectionTestUtils; public class VersionedHandleIdentifierProviderIT extends AbstractIntegrationTestWithDatabase { - private static final String REGISTERED_PROVIDERS_PREFIX = "VersionedHandleIdentifierProvider{0}"; private List originalProviders; - private List registeredProviders = new ArrayList<>(1); private ServiceManager serviceManager; private IdentifierServiceImpl identifierService; + private List registeredBeans = new ArrayList<>(); private String firstHandle; @@ -50,10 +52,11 @@ public class VersionedHandleIdentifierProviderIT extends AbstractIntegrationTest public void setUp() throws Exception { super.setUp(); serviceManager = DSpaceServicesFactory.getInstance().getServiceManager(); - originalProviders = serviceManager.getServicesByType(IdentifierProvider.class); identifierService = serviceManager.getServicesByType(IdentifierServiceImpl.class).get(0); + originalProviders = (List) ReflectionTestUtils.getField(identifierService, "providers"); context.turnOffAuthorisationSystem(); + identifierService.setProviders(List.of()); parentCommunity = CommunityBuilder.createCommunity(context) .withName("Parent Community") .build(); @@ -65,42 +68,59 @@ public void setUp() throws Exception { } @After - public void destroy() throws Exception { - // set all providers to cleanup the context. - identifierService.setProviders(serviceManager.getServicesByType(IdentifierProvider.class)); - super.destroy(); - // unregister the created services - unregisterServices(); + public void tearDown() throws Exception { + // restore providers + identifierService.setProviders(originalProviders); + // clean beans + unregisterBeans(registeredBeans); } - private void unregisterServices() { - // restore the default providers - identifierService.setProviders(originalProviders); - if (registeredProviders.isEmpty()) { - return; - } - // clear the registered additional providers - for (String serviceName : registeredProviders) { - serviceManager.unregisterService(serviceName); + private void unregisterBeans(List registeredBeans) { + AutowireCapableBeanFactory factory = + DSpaceServicesFactory.getInstance() + .getServiceManager() + .getApplicationContext() + .getAutowireCapableBeanFactory(); + Iterator iterator = registeredBeans.iterator(); + while (iterator.hasNext()) { + Object registeredBean = iterator.next(); + factory.destroyBean(registeredBean); + iterator.remove(); + registeredBeans.remove(registeredBean); } - registeredProviders.clear(); - originalProviders.clear(); + } + + private T registerBean(Class type) + throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + AutowireCapableBeanFactory factory = + DSpaceServicesFactory.getInstance() + .getServiceManager() + .getApplicationContext() + .getAutowireCapableBeanFactory(); + // Define our special bean for testing the target class. + T bean = type.getDeclaredConstructor() + .newInstance(); + + registeredBeans.add(bean); + + factory.autowireBean(bean); + return bean; } private void registerSingleProvider(IdentifierProvider provider) { identifierService.setProviders(List.of(provider)); } - private T getOrProvide(Class type) { + private T getOrProvide(Class type) + throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { List servicesByType = serviceManager.getServicesByType(type); if (servicesByType == null || servicesByType.isEmpty()) { - String serviceBeanName = MessageFormat.format(REGISTERED_PROVIDERS_PREFIX, type.getName()); - servicesByType = List.of(serviceManager.registerServiceClass(serviceBeanName, type)); - registeredProviders.add(serviceBeanName); + servicesByType = List.of(registerBean(type)); } return servicesByType.get(0); } + private void createVersions() throws SQLException, AuthorizeException { context.turnOffAuthorisationSystem();