From ac0c84d8387ea32fec0d83ae9e573a6d46fcb9d4 Mon Sep 17 00:00:00 2001 From: Tom Page Date: Tue, 13 Oct 2015 13:43:38 +0000 Subject: [PATCH] RM-2604 Fun with generics and add missing @Test annotation. (Thanks Neil). git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/DEV/caveatmarkdatatype@114266 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../caveat/dao/CaveatDAOCache.java | 11 ++++--- .../caveat/dao/CaveatDAOFromJSON.java | 8 +++-- .../dao/CaveatDAOFromJSONBootstrap.java | 33 +++++++++---------- .../caveat/dao/CaveatDAOInterface.java | 5 +-- .../caveat/dao/CaveatDAOFromJSONUnitTest.java | 4 ++- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOCache.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOCache.java index 70f03fe56e..1c3135e5ac 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOCache.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOCache.java @@ -18,6 +18,7 @@ */ package org.alfresco.module.org_alfresco_module_rm.caveat.dao; +import java.io.Serializable; import java.util.Map; import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup; @@ -28,18 +29,18 @@ import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup; * @author Tom Page * @since 2.4.a */ -public class CaveatDAOCache implements CaveatDAOInterface +public class CaveatDAOCache & Serializable> implements CaveatDAOInterface { /** The wrapped caveat DAO. */ - private CaveatDAOInterface caveatDAO; + private CaveatDAOInterface caveatDAO; /** A cache of the system caveat groups. */ - private Map caveatGroups; + private SMAP caveatGroups; /** * {@inheritDoc} The first call to this method will be cached and returned for every successive call. */ @Override - public Map getCaveatGroups() + public SMAP getCaveatGroups() { if (caveatGroups == null) { @@ -53,7 +54,7 @@ public class CaveatDAOCache implements CaveatDAOInterface * * @param caveatDAO The caveat DAO to be wrapped. */ - public void setCaveatDAO(CaveatDAOInterface caveatDAO) + public void setCaveatDAO(CaveatDAOInterface caveatDAO) { this.caveatDAO = caveatDAO; } diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSON.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSON.java index 7b9fee0d4d..8d6fa67634 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSON.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSON.java @@ -21,6 +21,7 @@ package org.alfresco.module.org_alfresco_module_rm.caveat.dao; import java.io.IOException; import java.io.InputStream; +import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -46,7 +47,7 @@ import org.slf4j.LoggerFactory; * @author Tom Page * @since 2.4.a */ -public class CaveatDAOFromJSON implements CaveatDAOInterface +public class CaveatDAOFromJSON & Serializable> implements CaveatDAOInterface { /** JSON key for the group id. */ private static final String GROUP_ID_JSON_KEY = "id"; @@ -80,9 +81,10 @@ public class CaveatDAOFromJSON implements CaveatDAOInterface * @throws MalformedConfiguration If the configuration file cannot be interpreted. */ @Override - public Map getCaveatGroups() + public SMAP getCaveatGroups() { - Map result = new HashMap<>(); + @SuppressWarnings("unchecked") + SMAP result = (SMAP) new HashMap(); try (final InputStream in = this.getClass().getResourceAsStream(configLocation)) { if (in != null) diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONBootstrap.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONBootstrap.java index a8b2132dfb..3d98844a5a 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONBootstrap.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONBootstrap.java @@ -25,7 +25,6 @@ import java.util.Map; import org.alfresco.module.org_alfresco_module_rm.caveat.CaveatException.MalformedConfiguration; import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup; -import org.alfresco.module.org_alfresco_module_rm.classification.model.ClassifiedContentModel; import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork; import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; @@ -42,7 +41,7 @@ import org.springframework.extensions.surf.util.AbstractLifecycleBean; * @author Tom Page * @since 2.4.a */ -public class CaveatDAOFromJSONBootstrap extends AbstractLifecycleBean implements ClassifiedContentModel +public class CaveatDAOFromJSONBootstrap & Serializable> extends AbstractLifecycleBean { /** Logging utility for the class. */ private static final Logger LOGGER = LoggerFactory.getLogger(CaveatDAOFromJSONBootstrap.class); @@ -50,14 +49,14 @@ public class CaveatDAOFromJSONBootstrap extends AbstractLifecycleBean implements private final AuthenticationUtil authenticationUtil; private final TransactionService transactionService; private AttributeService attributeService; - private CaveatDAOInterface caveatDAO; + private CaveatDAOInterface caveatDAO; private boolean isInitialised = false; public CaveatDAOFromJSONBootstrap(AuthenticationUtil authUtil, TransactionService txService, AttributeService attributeService, - CaveatDAOInterface caveatDAO) + CaveatDAOInterface caveatDAO) { this.authenticationUtil = authUtil; this.transactionService = txService; @@ -66,7 +65,7 @@ public class CaveatDAOFromJSONBootstrap extends AbstractLifecycleBean implements } /** Set the object from which configuration options will be read. */ - public void setClassificationServiceDAO(CaveatDAOInterface caveatDAO) { this.caveatDAO = caveatDAO; } + public void setClassificationServiceDAO(CaveatDAOInterface caveatDAO) { this.caveatDAO = caveatDAO; } public void setAttributeService(AttributeService attributeService) { this.attributeService = attributeService; } public boolean isInitialised() @@ -100,15 +99,15 @@ public class CaveatDAOFromJSONBootstrap extends AbstractLifecycleBean implements * * @return the persisted caveat groups if they have been persisted, else {@code null}. */ - private Map getPersistedValues(final Serializable[] key) + private SMAP getPersistedCaveatGroups(final Serializable[] key) { - return authenticationUtil.runAsSystem(new RunAsWork>() + return authenticationUtil.runAsSystem(new RunAsWork() { @Override @SuppressWarnings("unchecked") - public Map doWork() throws Exception + public SMAP doWork() throws Exception { - return (Map) attributeService.getAttribute(key); + return (SMAP) attributeService.getAttribute(key); } }); } @@ -134,24 +133,24 @@ public class CaveatDAOFromJSONBootstrap extends AbstractLifecycleBean implements */ protected void initialiseConfiguredCaveatGroups(Serializable[] key) { - final Map persistedValues = getPersistedValues(key); - final Map classpathValues = caveatDAO.getCaveatGroups(); + final SMAP persistedGroups = getPersistedCaveatGroups(key); + final SMAP classpathGroups = caveatDAO.getCaveatGroups(); // Note! We cannot log the entities or even the size of these lists for security reasons. - LOGGER.debug("Persisted CaveatGroup: {}", loggableStatusOf(persistedValues)); - LOGGER.debug("Classpath CaveatGroup: {}", loggableStatusOf(classpathValues)); + LOGGER.debug("Persisted CaveatGroup: {}", loggableStatusOf(persistedGroups)); + LOGGER.debug("Classpath CaveatGroup: {}", loggableStatusOf(classpathGroups)); - if (isEmpty(classpathValues)) + if (isEmpty(classpathGroups)) { throw new MalformedConfiguration("CaveatGroup configuration is missing."); } - if (!classpathValues.equals(persistedValues)) + if (!classpathGroups.equals(persistedGroups)) { - if (!isEmpty(persistedValues)) + if (!isEmpty(persistedGroups)) { LOGGER.warn("CaveatGroup configuration changed. This may result in unpredictable results if the caveat scheme is already in use."); } - attributeService.setAttribute((Serializable) classpathValues, key); + attributeService.setAttribute(classpathGroups, key); } } diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOInterface.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOInterface.java index d25ed58faf..5001f3b05b 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOInterface.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOInterface.java @@ -19,6 +19,7 @@ package org.alfresco.module.org_alfresco_module_rm.caveat.dao; +import java.io.Serializable; import java.util.Map; import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup; @@ -29,10 +30,10 @@ import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup; * @author Tom Page * @since 2.4.a */ -public interface CaveatDAOInterface +public interface CaveatDAOInterface & Serializable> { /** * Gets a map of all the available caveat groups keyed by id. */ - Map getCaveatGroups(); + SMAP getCaveatGroups(); } diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONUnitTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONUnitTest.java index 470781fc01..c6d4139b74 100644 --- a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONUnitTest.java +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONUnitTest.java @@ -37,7 +37,8 @@ import org.junit.Test; public class CaveatDAOFromJSONUnitTest { /** The class under test. */ - CaveatDAOFromJSON caveatDAOFromJSON = new CaveatDAOFromJSON(); + @SuppressWarnings("rawtypes") + CaveatDAOFromJSON caveatDAOFromJSON = new CaveatDAOFromJSON(); /** Test that loading the default caveat configuration file doesn't throw any exceptions. */ @Test @@ -90,6 +91,7 @@ public class CaveatDAOFromJSONUnitTest } /** Test that a duplicate mark id (in different groups) doesn't cause an exception. */ + @Test public void testGetCaveatGroups_duplicateMarkIdInDifferentGroups() { caveatDAOFromJSON.setConfigLocation("/alfresco/caveat/rm-caveats-duplicateMarkIdInDifferentGroups.json");