From 36f21d221ba906842ca7caa6e0f69a082a7d23b7 Mon Sep 17 00:00:00 2001 From: Cleverson Sacramento Date: Fri, 17 Aug 2012 07:33:57 -0300 Subject: [PATCH] Organização do código e ajustes relacionados à serialização dos objetos internos do framework --- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/PaginationConfig.java | 1 - impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ContextStore.java | 1 - impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/MessageContextImpl.java | 20 ++++++++++++++------ impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/PaginationContextImpl.java | 12 +++++++++--- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/SecurityContextImpl.java | 20 ++++++++++++++++---- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/StrategySelector.java | 8 +++++++- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/ExceptionHandlerInterceptor.java | 43 ++++++++++++++++++++++++++++--------------- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/RequiredPermissionInterceptor.java | 57 +++++++++++++++++++++++++++++++++++++-------------------- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/RequiredRoleInterceptor.java | 59 ++++++++++++++++++++++++++++++++++++++--------------------- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/TransactionalInterceptor.java | 31 ++++++++++++++++++++----------- impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/producer/AbstractStrategyProducer.java | 153 --------------------------------------------------------------------------------------------------------------------------------------------------------- impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Beans.java | 5 ++++- impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Exceptions.java | 5 ++++- impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java | 9 ++++++--- impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Strings.java | 5 ++++- impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/interceptor/ExceptionHandlerInterceptorTest.java | 672 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ impl/core/src/test/java/br/gov/frameworkdemoiselle/util/BeansTest.java | 21 ++++++++------------- impl/core/src/test/java/br/gov/frameworkdemoiselle/util/ExceptionsTest.java | 19 +++++++++---------- impl/core/src/test/java/br/gov/frameworkdemoiselle/util/StringsTest.java | 15 ++++++--------- 20 files changed, 610 insertions(+), 659 deletions(-) delete mode 100644 impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/producer/AbstractStrategyProducer.java diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java index 82dccc2..a2897ec 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java @@ -48,7 +48,6 @@ import java.util.Iterator; import java.util.Properties; import java.util.Set; -import javax.inject.Inject; import javax.validation.constraints.NotNull; import org.apache.commons.configuration.DataConfiguration; @@ -63,6 +62,8 @@ import br.gov.frameworkdemoiselle.configuration.ConfigType; import br.gov.frameworkdemoiselle.configuration.Configuration; import br.gov.frameworkdemoiselle.configuration.ConfigurationException; import br.gov.frameworkdemoiselle.internal.bootstrap.CoreBootstrap; +import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; +import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; import br.gov.frameworkdemoiselle.util.Reflections; import br.gov.frameworkdemoiselle.util.ResourceBundle; import br.gov.frameworkdemoiselle.util.Strings; @@ -77,12 +78,9 @@ public class ConfigurationLoader implements Serializable { private static final long serialVersionUID = 1L; - @Inject - @Name("demoiselle-core-bundle") - private ResourceBundle bundle; + private static ResourceBundle bundle; - @Inject - private Logger logger; + private static Logger logger; /** * Loads a config class filling it with the corresponding values. @@ -96,10 +94,10 @@ public class ConfigurationLoader implements Serializable { if (!CoreBootstrap.isAnnotatedType(config)) { config = config.getSuperclass(); - logger.debug(bundle.getString("proxy-detected", config, config.getClass().getSuperclass())); + getLogger().debug(getBundle().getString("proxy-detected", config, config.getClass().getSuperclass())); } - logger.debug(bundle.getString("loading-configuration-class", config.getName())); + getLogger().debug(getBundle().getString("loading-configuration-class", config.getName())); for (Field field : Reflections.getNonStaticDeclaredFields(config)) { loadField(field, object, config); @@ -123,13 +121,14 @@ public class ConfigurationLoader implements Serializable { private void setValue(Field field, String key, Object object, Object value) { if (value != null) { Reflections.setFieldValue(field, object, value); - logger.debug(bundle.getString("configuration-field-loaded", key, field.getName(), value)); + getLogger().debug(getBundle().getString("configuration-field-loaded", key, field.getName(), value)); } } private void validate(Field field, String key, Object value, String resource) { if (field.isAnnotationPresent(NotNull.class) && value == null) { - throw new ConfigurationException(bundle.getString("configuration-attribute-is-mandatory", key, resource)); + throw new ConfigurationException(getBundle().getString("configuration-attribute-is-mandatory", key, + resource)); } } @@ -166,7 +165,7 @@ public class ConfigurationLoader implements Serializable { Name nameAnnotation = field.getAnnotation(Name.class); if (Strings.isEmpty(nameAnnotation.value())) { - throw new ConfigurationException(bundle.getString("configuration-name-attribute-cant-be-empty")); + throw new ConfigurationException(getBundle().getString("configuration-name-attribute-cant-be-empty")); } else { key = nameAnnotation.value(); } @@ -193,9 +192,9 @@ public class ConfigurationLoader implements Serializable { } if (matches == 0) { - logger.debug(bundle.getString("configuration-key-not-found", key, conventions)); + getLogger().debug(getBundle().getString("configuration-key-not-found", key, conventions)); } else if (matches > 1) { - throw new ConfigurationException(bundle.getString("ambiguous-key", field.getName(), + throw new ConfigurationException(getBundle().getString("ambiguous-key", field.getName(), field.getDeclaringClass())); } @@ -222,7 +221,7 @@ public class ConfigurationLoader implements Serializable { case PROPERTIES: url = getResourceAsURL(resource + ".properties"); - config = new DataConfiguration(new PropertiesConfiguration(url)); + config = new DataConfiguration(new PropertiesConfiguration(url)); break; case XML: @@ -231,13 +230,13 @@ public class ConfigurationLoader implements Serializable { break; default: - throw new ConfigurationException(bundle.getString("configuration-type-not-implemented-yet", + throw new ConfigurationException(getBundle().getString("configuration-type-not-implemented-yet", type.name())); } } catch (Exception cause) { - throw new ConfigurationException(bundle.getString("error-creating-configuration-from-resource", resource), - cause); + throw new ConfigurationException(getBundle().getString("error-creating-configuration-from-resource", + resource), cause); } return config; @@ -248,7 +247,7 @@ public class ConfigurationLoader implements Serializable { Object value; Class fieldClass = (Class) field.getType(); - + if (fieldClass.isArray()) { value = getArray(key, field, config); } else if (fieldClass.equals(Properties.class)) { @@ -260,27 +259,28 @@ public class ConfigurationLoader implements Serializable { return (T) value; } - + private Object getArray(String key, Field field, org.apache.commons.configuration.Configuration config) { Object value = null; Class fieldClass = (Class) field.getType(); - + try { Method method; - + String methodName = "get"; - + methodName += Strings.firstToUpper(fieldClass.getSimpleName()); methodName = Strings.removeChars(methodName, '[', ']'); - + methodName += "Array"; method = config.getClass().getMethod(methodName, String.class); value = method.invoke(config, key); } catch (Throwable cause) { - throw new ConfigurationException(bundle.getString("error-converting-to-type", fieldClass.getName()), cause); + throw new ConfigurationException(getBundle().getString("error-converting-to-type", fieldClass.getName()), + cause); } return value; @@ -290,62 +290,61 @@ public class ConfigurationLoader implements Serializable { Object value = null; Class fieldClass = (Class) field.getType(); - + try { Method method; - + String methodName = "get"; - + methodName += discoveryGenericType(field); - + methodName += Strings.firstToUpper(fieldClass.getSimpleName()); if (!fieldClass.isPrimitive()) { method = config.getClass().getMethod(methodName, String.class, fieldClass); value = method.invoke(config, key, null); - + } else if (config.containsKey(key)) { method = config.getClass().getMethod(methodName, String.class); value = method.invoke(config, key); } } catch (Throwable cause) { - throw new ConfigurationException(bundle.getString("error-converting-to-type", fieldClass.getName()), cause); + throw new ConfigurationException(getBundle().getString("error-converting-to-type", fieldClass.getName()), + cause); } return value; } /** - * Discovery the Generic's type. - * - * for example: the generic's type of List list is an Integer type + * Discovery the Generic's type. for example: the generic's type of List list is an Integer type * * @param field * @return */ private String discoveryGenericType(Field field) { - + Type genericFieldType = field.getGenericType(); - - if(genericFieldType instanceof ParameterizedType){ - ParameterizedType type = (ParameterizedType) genericFieldType; - Type[] fieldArgumentTypes = type.getActualTypeArguments(); - for(Type fieldArgumentType : fieldArgumentTypes){ - @SuppressWarnings("rawtypes") + + if (genericFieldType instanceof ParameterizedType) { + ParameterizedType type = (ParameterizedType) genericFieldType; + Type[] fieldArgumentTypes = type.getActualTypeArguments(); + for (Type fieldArgumentType : fieldArgumentTypes) { + @SuppressWarnings("rawtypes") Class fieldArgumentClass = (Class) fieldArgumentType; - - if("String".equals(fieldArgumentClass.getSimpleName())) { - return ""; - } - - return fieldArgumentClass.getSimpleName(); - } + + if ("String".equals(fieldArgumentClass.getSimpleName())) { + return ""; + } + + return fieldArgumentClass.getSimpleName(); + } } - + return ""; } - + private Object getProperty(String key, org.apache.commons.configuration.Configuration config) { Object value = null; @@ -397,4 +396,20 @@ public class ConfigurationLoader implements Serializable { ClassLoader classLoader = getClassLoaderForResource(resource); return classLoader.getResource(resource); } + + private static ResourceBundle getBundle() { + if (bundle == null) { + bundle = ResourceBundleProducer.create("demoiselle-core-bundle"); + } + + return bundle; + } + + private static Logger getLogger() { + if (logger == null) { + logger = LoggerProducer.create(ConfigurationLoader.class); + } + + return logger; + } } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/PaginationConfig.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/PaginationConfig.java index 4c07ee6..7dbabb2 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/PaginationConfig.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/PaginationConfig.java @@ -77,5 +77,4 @@ public class PaginationConfig implements Serializable { public int getMaxPageLinks() { return maxPageLinks; } - } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ContextStore.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ContextStore.java index 2055b25..9db17de 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ContextStore.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ContextStore.java @@ -54,5 +54,4 @@ public class ContextStore { public void put(final String name, final Object instance) { this.map.put(name, instance); } - } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/MessageContextImpl.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/MessageContextImpl.java index 8cb3bdb..22b5e97 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/MessageContextImpl.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/MessageContextImpl.java @@ -41,10 +41,11 @@ import java.util.ArrayList; import java.util.List; import javax.enterprise.context.RequestScoped; -import javax.inject.Inject; import org.slf4j.Logger; +import br.gov.frameworkdemoiselle.internal.interceptor.ExceptionHandlerInterceptor; +import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; import br.gov.frameworkdemoiselle.message.DefaultMessage; import br.gov.frameworkdemoiselle.message.Message; @@ -62,13 +63,12 @@ public class MessageContextImpl implements Serializable, MessageContext { private static final long serialVersionUID = 1L; - @Inject - private Logger logger; - private final List messages = new ArrayList(); private static ResourceBundle bundle; + private static Logger logger; + @Override public void add(final Message message, Object... params) { Message aux; @@ -79,7 +79,7 @@ public class MessageContextImpl implements Serializable, MessageContext { aux = message; } - logger.debug(getBundle().getString("adding-message-to-context", message.toString())); + getLogger().debug(getBundle().getString("adding-message-to-context", message.toString())); messages.add(aux); } @@ -100,7 +100,7 @@ public class MessageContextImpl implements Serializable, MessageContext { @Override public void clear() { - logger.debug(getBundle().getString("cleaning-message-context")); + getLogger().debug(getBundle().getString("cleaning-message-context")); messages.clear(); } @@ -111,4 +111,12 @@ public class MessageContextImpl implements Serializable, MessageContext { return bundle; } + + private static Logger getLogger() { + if (logger == null) { + logger = LoggerProducer.create(ExceptionHandlerInterceptor.class); + } + + return logger; + } } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/PaginationContextImpl.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/PaginationContextImpl.java index cfc6232..9ca5a46 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/PaginationContextImpl.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/PaginationContextImpl.java @@ -41,11 +41,11 @@ import java.util.HashMap; import java.util.Map; import javax.enterprise.context.SessionScoped; -import javax.inject.Inject; import br.gov.frameworkdemoiselle.internal.configuration.PaginationConfig; import br.gov.frameworkdemoiselle.pagination.Pagination; import br.gov.frameworkdemoiselle.pagination.PaginationContext; +import br.gov.frameworkdemoiselle.util.Beans; /** * Context implementation reserved for pagination purposes. Internally a hash map is used to store pagination data for @@ -59,7 +59,6 @@ public class PaginationContextImpl implements Serializable, PaginationContext { private static final long serialVersionUID = 1L; - @Inject private PaginationConfig config; private final Map, Pagination> cache = new HashMap, Pagination>(); @@ -73,7 +72,7 @@ public class PaginationContextImpl implements Serializable, PaginationContext { if (pagination == null && create) { pagination = new PaginationImpl(); - pagination.setPageSize(config.getPageSize()); + pagination.setPageSize(getConfig().getPageSize()); cache.put(clazz, pagination); } @@ -81,4 +80,11 @@ public class PaginationContextImpl implements Serializable, PaginationContext { return pagination; } + private PaginationConfig getConfig() { + if (config == null) { + config = Beans.getReference(PaginationConfig.class); + } + + return config; + } } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/SecurityContextImpl.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/SecurityContextImpl.java index 6d82a72..92a658a 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/SecurityContextImpl.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/SecurityContextImpl.java @@ -60,14 +60,26 @@ public class SecurityContextImpl implements SecurityContext { private static final long serialVersionUID = 1L; + private Authenticator authenticator; + + private Authorizer authorizer; + private Authenticator getAuthenticator() { - return StrategySelector.getReference("frameworkdemoiselle.security.authenticator.class", Authenticator.class, - DefaultAuthenticator.class); + if (authenticator == null) { + authenticator = StrategySelector.getReference("frameworkdemoiselle.security.authenticator.class", + Authenticator.class, DefaultAuthenticator.class); + } + + return authenticator; } private Authorizer getAuthorizer() { - return StrategySelector.getReference("frameworkdemoiselle.security.authorizer.class", Authorizer.class, - DefaultAuthorizer.class); + if (authorizer == null) { + authorizer = StrategySelector.getReference("frameworkdemoiselle.security.authorizer.class", + Authorizer.class, DefaultAuthorizer.class); + } + + return authorizer; } /** diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/StrategySelector.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/StrategySelector.java index 2a2b71a..9c17dd0 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/StrategySelector.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/StrategySelector.java @@ -37,6 +37,7 @@ package br.gov.frameworkdemoiselle.internal.implementation; import java.io.FileNotFoundException; +import java.io.Serializable; import java.net.URL; import java.util.Locale; @@ -50,7 +51,12 @@ import br.gov.frameworkdemoiselle.util.Beans; import br.gov.frameworkdemoiselle.util.ResourceBundle; import br.gov.frameworkdemoiselle.util.Strings; -public class StrategySelector { +public final class StrategySelector implements Serializable { + + private static final long serialVersionUID = 1L; + + private StrategySelector() { + } public static T getReference(String configKey, Class type, Class defaultType) { Class selectedType = loadSelected(configKey, type, defaultType); diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/ExceptionHandlerInterceptor.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/ExceptionHandlerInterceptor.java index 7e5c5a3..461caaf 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/ExceptionHandlerInterceptor.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/ExceptionHandlerInterceptor.java @@ -42,7 +42,6 @@ import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; -import javax.inject.Inject; import javax.interceptor.AroundInvoke; import javax.interceptor.Interceptor; import javax.interceptor.InvocationContext; @@ -50,9 +49,10 @@ import javax.interceptor.InvocationContext; import org.slf4j.Logger; import br.gov.frameworkdemoiselle.DemoiselleException; -import br.gov.frameworkdemoiselle.annotation.Name; import br.gov.frameworkdemoiselle.exception.ExceptionHandler; import br.gov.frameworkdemoiselle.internal.bootstrap.CoreBootstrap; +import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; +import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; import br.gov.frameworkdemoiselle.stereotype.Controller; import br.gov.frameworkdemoiselle.util.ResourceBundle; @@ -62,20 +62,14 @@ public class ExceptionHandlerInterceptor implements Serializable { private static final long serialVersionUID = 1L; - private final ResourceBundle bundle; + private static ResourceBundle bundle; - private final Logger logger; - - @Inject - public ExceptionHandlerInterceptor(Logger logger, @Name("demoiselle-core-bundle") ResourceBundle bundle) { - this.logger = logger; - this.bundle = bundle; - } + private static Logger logger; private final Map, Map, Method>> cache = new HashMap, Map, Method>>(); private final boolean handleException(final Exception cause, final InvocationContext ic) throws Exception { - logger.info(bundle.getString("handling-exception", cause.getClass().getCanonicalName())); + getLogger().info(getBundle().getString("handling-exception", cause.getClass().getCanonicalName())); boolean handled = false; Class type = getType(ic); @@ -98,8 +92,9 @@ public class ExceptionHandlerInterceptor implements Serializable { if (!CoreBootstrap.isAnnotatedType(type)) { type = type.getSuperclass(); - logger.debug(bundle.getString("proxy-detected", ic.getTarget().getClass(), ic.getTarget().getClass() - .getSuperclass())); + getLogger().debug( + getBundle().getString("proxy-detected", ic.getTarget().getClass(), + ic.getTarget().getClass().getSuperclass())); } return type; @@ -149,7 +144,7 @@ public class ExceptionHandlerInterceptor implements Serializable { */ private final void validateHandler(final Method method) { if (method.getParameterTypes().length != 1) { - throw new DemoiselleException(bundle.getString("must-declare-one-single-parameter", + throw new DemoiselleException(getBundle().getString("must-declare-one-single-parameter", method.toGenericString())); } } @@ -170,8 +165,10 @@ public class ExceptionHandlerInterceptor implements Serializable { try { method.invoke(object, param); + } catch (InvocationTargetException cause) { Throwable targetTrowable = cause.getTargetException(); + if (targetTrowable instanceof Exception) { throw (Exception) targetTrowable; } else { @@ -181,13 +178,14 @@ public class ExceptionHandlerInterceptor implements Serializable { method.setAccessible(accessible); } - + @AroundInvoke public Object manage(final InvocationContext ic) throws Exception { Object result = null; try { result = ic.proceed(); + } catch (Exception cause) { if (!handleException(cause, ic)) { throw cause; @@ -197,4 +195,19 @@ public class ExceptionHandlerInterceptor implements Serializable { return result; } + private static ResourceBundle getBundle() { + if (bundle == null) { + bundle = ResourceBundleProducer.create("demoiselle-core-bundle"); + } + + return bundle; + } + + private static Logger getLogger() { + if (logger == null) { + logger = LoggerProducer.create(ExceptionHandlerInterceptor.class); + } + + return logger; + } } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/RequiredPermissionInterceptor.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/RequiredPermissionInterceptor.java index b53e4c4..679fcc8 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/RequiredPermissionInterceptor.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/RequiredPermissionInterceptor.java @@ -38,8 +38,6 @@ package br.gov.frameworkdemoiselle.internal.interceptor; import java.io.Serializable; -import javax.enterprise.inject.Instance; -import javax.inject.Inject; import javax.interceptor.AroundInvoke; import javax.interceptor.Interceptor; import javax.interceptor.InvocationContext; @@ -47,10 +45,13 @@ import javax.interceptor.InvocationContext; import org.slf4j.Logger; import br.gov.frameworkdemoiselle.annotation.Name; +import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; +import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; import br.gov.frameworkdemoiselle.security.AuthorizationException; import br.gov.frameworkdemoiselle.security.RequiredPermission; import br.gov.frameworkdemoiselle.security.SecurityContext; import br.gov.frameworkdemoiselle.security.User; +import br.gov.frameworkdemoiselle.util.Beans; import br.gov.frameworkdemoiselle.util.ResourceBundle; import br.gov.frameworkdemoiselle.util.Strings; @@ -65,19 +66,11 @@ public class RequiredPermissionInterceptor implements Serializable { private static final long serialVersionUID = 1L; - private final Instance securityContext; + private SecurityContext securityContext; - private final ResourceBundle bundle; + private static ResourceBundle bundle; - private final Logger logger; - - @Inject - public RequiredPermissionInterceptor(Instance securityContext, - @Name("demoiselle-core-bundle") ResourceBundle bundle, Logger logger) { - this.securityContext = securityContext; - this.bundle = bundle; - this.logger = logger; - } + private static Logger logger; /** * Gets the values for both resource and operation properties of {@code @RequiredPermission}. Delegates to @@ -98,17 +91,17 @@ public class RequiredPermissionInterceptor implements Serializable { String operation = getOperation(ic); String username = null; - if (securityContext.get().isLoggedIn()) { + if (getSecurityContext().isLoggedIn()) { username = getUsername(); - logger.trace(bundle.getString("access-checking", username, operation, resource)); + getLogger().trace(getBundle().getString("access-checking", username, operation, resource)); } - if (!securityContext.get().hasPermission(resource, operation)) { - logger.error(bundle.getString("access-denied", username, operation, resource)); - throw new AuthorizationException(bundle.getString("access-denied-ui", resource, operation)); + if (!getSecurityContext().hasPermission(resource, operation)) { + getLogger().error(getBundle().getString("access-denied", username, operation, resource)); + throw new AuthorizationException(getBundle().getString("access-denied-ui", resource, operation)); } - logger.debug(bundle.getString("access-allowed", username, operation, resource)); + getLogger().debug(getBundle().getString("access-allowed", username, operation, resource)); return ic.proceed(); } @@ -119,7 +112,7 @@ public class RequiredPermissionInterceptor implements Serializable { */ private String getUsername() { String username = ""; - User user = securityContext.get().getUser(); + User user = getSecurityContext().getUser(); if (user != null && user.getId() != null) { username = user.getId(); @@ -173,4 +166,28 @@ public class RequiredPermissionInterceptor implements Serializable { return requiredPermission.operation(); } } + + private SecurityContext getSecurityContext() { + if (securityContext == null) { + securityContext = Beans.getReference(SecurityContext.class); + } + + return securityContext; + } + + private static ResourceBundle getBundle() { + if (bundle == null) { + bundle = ResourceBundleProducer.create("demoiselle-core-bundle"); + } + + return bundle; + } + + private static Logger getLogger() { + if (logger == null) { + logger = LoggerProducer.create(RequiredPermissionInterceptor.class); + } + + return logger; + } } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/RequiredRoleInterceptor.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/RequiredRoleInterceptor.java index 420fe26..be0eaa8 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/RequiredRoleInterceptor.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/RequiredRoleInterceptor.java @@ -41,18 +41,18 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import javax.enterprise.inject.Instance; -import javax.inject.Inject; import javax.interceptor.AroundInvoke; import javax.interceptor.Interceptor; import javax.interceptor.InvocationContext; import org.slf4j.Logger; -import br.gov.frameworkdemoiselle.annotation.Name; +import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; +import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; import br.gov.frameworkdemoiselle.security.AuthorizationException; import br.gov.frameworkdemoiselle.security.RequiredRole; import br.gov.frameworkdemoiselle.security.SecurityContext; +import br.gov.frameworkdemoiselle.util.Beans; import br.gov.frameworkdemoiselle.util.ResourceBundle; /** @@ -66,19 +66,11 @@ public class RequiredRoleInterceptor implements Serializable { private static final long serialVersionUID = 1L; - private final Instance securityContext; + private SecurityContext securityContext; - private final ResourceBundle bundle; + private static ResourceBundle bundle; - private final Logger logger; - - @Inject - public RequiredRoleInterceptor(Instance securityContext, - @Name("demoiselle-core-bundle") ResourceBundle bundle, Logger logger) { - this.securityContext = securityContext; - this.bundle = bundle; - this.logger = logger; - } + private static Logger logger; /** * Gets the value property of {@code @RequiredRole}. Delegates to {@code SecurityContext} check role. If the user @@ -96,27 +88,29 @@ public class RequiredRoleInterceptor implements Serializable { public Object manage(final InvocationContext ic) throws Exception { List roles = getRoles(ic); - if (securityContext.get().isLoggedIn()) { - logger.info(bundle.getString("has-role-verification", securityContext.get().getUser().getId(), roles)); + if (getSecurityContext().isLoggedIn()) { + getLogger().info( + getBundle().getString("has-role-verification", getSecurityContext().getUser().getId(), roles)); } List userRoles = new ArrayList(); for (String role : roles) { - if (securityContext.get().hasRole(role)) { + if (getSecurityContext().hasRole(role)) { userRoles.add(role); } } if (userRoles.isEmpty()) { - logger.error(bundle.getString("does-not-have-role", securityContext.get().getUser().getId(), roles)); + getLogger().error( + getBundle().getString("does-not-have-role", getSecurityContext().getUser().getId(), roles)); @SuppressWarnings("unused") AuthorizationException a = new AuthorizationException(null); - throw new AuthorizationException(bundle.getString("does-not-have-role-ui", roles)); + throw new AuthorizationException(getBundle().getString("does-not-have-role-ui", roles)); } - logger.debug(bundle.getString("user-has-role", securityContext.get().getUser().getId(), userRoles)); + getLogger().debug(getBundle().getString("user-has-role", getSecurityContext().getUser().getId(), userRoles)); return ic.proceed(); } @@ -141,5 +135,28 @@ public class RequiredRoleInterceptor implements Serializable { return Arrays.asList(roles); } - + + private SecurityContext getSecurityContext() { + if (securityContext == null) { + securityContext = Beans.getReference(SecurityContext.class); + } + + return securityContext; + } + + private static ResourceBundle getBundle() { + if (bundle == null) { + bundle = ResourceBundleProducer.create("demoiselle-core-bundle"); + } + + return bundle; + } + + private static Logger getLogger() { + if (logger == null) { + logger = LoggerProducer.create(RequiredRoleInterceptor.class); + } + + return logger; + } } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/TransactionalInterceptor.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/TransactionalInterceptor.java index 35ab593..443a324 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/TransactionalInterceptor.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/TransactionalInterceptor.java @@ -47,6 +47,7 @@ import org.slf4j.Logger; import br.gov.frameworkdemoiselle.exception.ApplicationException; import br.gov.frameworkdemoiselle.internal.implementation.TransactionInfo; +import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; import br.gov.frameworkdemoiselle.transaction.Transaction; import br.gov.frameworkdemoiselle.transaction.TransactionContext; @@ -64,9 +65,9 @@ public class TransactionalInterceptor implements Serializable { private TransactionInfo transactionInfo; - private ResourceBundle bundle; + private static ResourceBundle bundle; - private Logger logger; + private static Logger logger; private TransactionContext getTransactionContext() { if (this.transactionContext == null) { @@ -83,7 +84,15 @@ public class TransactionalInterceptor implements Serializable { instance = Beans.getReference(TransactionInfo.class); } catch (ContextNotActiveException cause) { - instance = new TransactionInfo(); + instance = new TransactionInfo() { + + private static final long serialVersionUID = 1L; + + @Override + public boolean isOwner() { + return false; + } + }; } return instance; @@ -170,19 +179,19 @@ public class TransactionalInterceptor implements Serializable { } } - private ResourceBundle getBundle() { - if (this.bundle == null) { - this.bundle = ResourceBundleProducer.create("demoiselle-core-bundle"); + private static ResourceBundle getBundle() { + if (bundle == null) { + bundle = ResourceBundleProducer.create("demoiselle-core-bundle"); } - return this.bundle; + return bundle; } - private Logger getLogger() { - if (this.logger == null) { - this.logger = Beans.getReference(Logger.class); + private static Logger getLogger() { + if (logger == null) { + logger = LoggerProducer.create(TransactionalInterceptor.class); } - return this.logger; + return logger; } } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/producer/AbstractStrategyProducer.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/producer/AbstractStrategyProducer.java deleted file mode 100644 index c4c7716..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/producer/AbstractStrategyProducer.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.internal.producer; - -import java.io.FileNotFoundException; -import java.io.Serializable; -import java.net.URL; - -import javax.enterprise.context.spi.CreationalContext; -import javax.enterprise.inject.spi.AnnotatedType; -import javax.enterprise.inject.spi.BeanManager; -import javax.enterprise.inject.spi.InjectionTarget; -import javax.inject.Inject; - -import org.apache.commons.configuration.Configuration; -import org.apache.commons.configuration.PropertiesConfiguration; - -import br.gov.frameworkdemoiselle.annotation.Name; -import br.gov.frameworkdemoiselle.configuration.ConfigurationException; -import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; -import br.gov.frameworkdemoiselle.util.Beans; -import br.gov.frameworkdemoiselle.util.Reflections; -import br.gov.frameworkdemoiselle.util.ResourceBundle; -import br.gov.frameworkdemoiselle.util.Strings; - -public abstract class AbstractStrategyProducer implements Serializable { - - private static final long serialVersionUID = 1L; - - private Class type; - - private Class defaultClass; - - private Class selected; - - @Inject - @Name("demoiselle-core-bundle") - private ResourceBundle bundle; - - @SuppressWarnings("unchecked") - public T create() { - BeanManager beanManager = Beans.getBeanManager(); - - AnnotatedType type = ((AnnotatedType) beanManager.createAnnotatedType(getSelected())); - InjectionTarget it = beanManager.createInjectionTarget(type); - CreationalContext ctx = beanManager.createCreationalContext(null); - - T instance = it.produce(ctx); - it.inject(instance, ctx); - it.postConstruct(instance); - - return instance; - } - - protected Class getSelected() { - if (selected == null) { - selected = loadSelected(); - } - - return selected; - } - - private Class getType() { - if (this.type == null) { - this.type = Reflections.getGenericTypeArgument(this.getClass(), 0); - } - - return this.type; - } - - private Class getDefaultClass() { - if (this.defaultClass == null) { - this.defaultClass = Reflections.getGenericTypeArgument(this.getClass(), 1); - } - - return this.defaultClass; - } - - @SuppressWarnings("unchecked") - private Class loadSelected() { - Class result = null; - String canonicalName = null; - String typeName = getType().getSimpleName().toLowerCase(); - String key = null; - - try { - URL url = ConfigurationLoader.getResourceAsURL("demoiselle.properties"); - Configuration config = new PropertiesConfiguration(url); - canonicalName = config.getString(getConfigKey(), getDefaultClass().getCanonicalName()); - - ClassLoader classLoader = ConfigurationLoader.getClassLoaderForClass(canonicalName); - if (classLoader == null) { - classLoader = Thread.currentThread().getContextClassLoader(); - } - - result = (Class) Class.forName(canonicalName, false, classLoader); - result.asSubclass(getType()); - - } catch (org.apache.commons.configuration.ConfigurationException cause) { - throw new ConfigurationException(bundle.getString("file-not-found", "demoiselle.properties")); - - } catch (ClassNotFoundException cause) { - key = Strings.getString("{0}-class-not-found", typeName); - throw new ConfigurationException(bundle.getString(key, canonicalName)); - - } catch (ClassCastException cause) { - key = Strings.getString("{0}-class-must-be-of-type", typeName); - throw new ConfigurationException(bundle.getString(key, canonicalName, getType())); - - } catch (FileNotFoundException e) { - throw new ConfigurationException(bundle.getString("file-not-found", "demoiselle.properties")); - } - - return result; - } - - public abstract String getConfigKey(); - -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Beans.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Beans.java index 2c1aecb..ca2db5d 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Beans.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Beans.java @@ -59,10 +59,13 @@ import javax.enterprise.inject.spi.BeanManager; import br.gov.frameworkdemoiselle.DemoiselleException; import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; -public class Beans { +public final class Beans { private static BeanManager manager; + private Beans() { + } + public static void setBeanManager(BeanManager beanManager) { manager = beanManager; } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Exceptions.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Exceptions.java index 518bf3b..94dcccc 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Exceptions.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Exceptions.java @@ -20,7 +20,10 @@ package br.gov.frameworkdemoiselle.util; import br.gov.frameworkdemoiselle.exception.ApplicationException; -public class Exceptions { +public final class Exceptions { + + private Exceptions() { + } public static boolean isApplicationException(final Throwable throwable) { return throwable.getClass().isAnnotationPresent(ApplicationException.class); diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java index 22177af..507618a 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java @@ -45,7 +45,10 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; -public class Reflections { +public final class Reflections { + + private Reflections() { + } @SuppressWarnings("unchecked") public static Class getGenericTypeArgument(final Class clazz, final int idx) { @@ -127,11 +130,11 @@ public class Reflections { return fields.toArray(new Field[0]); } - + public static T instantiate(Class clasz) { T object = null; try { - object = clasz.newInstance(); + object = clasz.newInstance(); } catch (InstantiationException e) { Exceptions.handleToRuntimeException(e); } catch (IllegalAccessException e) { diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Strings.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Strings.java index 9c31ca5..ecfff11 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Strings.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Strings.java @@ -43,7 +43,10 @@ import java.util.regex.Pattern; import br.gov.frameworkdemoiselle.annotation.Ignore; -public class Strings { +public final class Strings { + + private Strings() { + } public static boolean isResourceBundleKeyFormat(final String key) { return Pattern.matches("^\\{(.+)\\}$", key == null ? "" : key); diff --git a/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/interceptor/ExceptionHandlerInterceptorTest.java b/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/interceptor/ExceptionHandlerInterceptorTest.java index af120cc..e91b694 100644 --- a/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/interceptor/ExceptionHandlerInterceptorTest.java +++ b/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/interceptor/ExceptionHandlerInterceptorTest.java @@ -1,336 +1,336 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.internal.interceptor; - -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.verify; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.powermock.api.easymock.PowerMock.mockStatic; -import static org.powermock.api.easymock.PowerMock.replay; -import static org.powermock.api.easymock.PowerMock.replayAll; -import static org.powermock.api.easymock.PowerMock.verifyAll; - -import java.util.Locale; - -import javax.interceptor.InvocationContext; - -import org.easymock.EasyMock; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.easymock.PowerMock; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.slf4j.Logger; - -import br.gov.frameworkdemoiselle.DemoiselleException; -import br.gov.frameworkdemoiselle.exception.ExceptionHandler; -import br.gov.frameworkdemoiselle.internal.bootstrap.CoreBootstrap; -import br.gov.frameworkdemoiselle.util.ResourceBundle; - -@RunWith(PowerMockRunner.class) -@PrepareForTest(CoreBootstrap.class) -public class ExceptionHandlerInterceptorTest { - - private ExceptionHandlerInterceptor interceptor; - - private InvocationContext context; - - private Logger logger; - - private ResourceBundle bundle; - - class TestException extends DemoiselleException { - - private static final long serialVersionUID = 1L; - - public TestException(String message) { - super(message); - } - } - - class ClassWithMethodsAnnotatedWithExceptionHandler { - - int times = 0; - - @ExceptionHandler - public void methodWithExceptionHandlerAnotation(DemoiselleException cause) { - times++; - } - - @ExceptionHandler - public void methodWithExceptionHandlerAnotationAndGenericException(Exception cause) { - times++; - } - - } - - class ClassWithoutMethodsAnnotatedWithExceptionHandler { - - public void methodWithoutExceptionHandlerAnotation(DemoiselleException cause) { - } - } - - class ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException { - - int times = 0; - - @ExceptionHandler - public void methodWithExceptionHandlerAnotation(DemoiselleException cause) { - times++; - throw new RuntimeException(); - } - } - - class ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler { - - @ExceptionHandler - public void methodWithExceptionHandlerAnotation() { - } - - } - - class ClassWithExceptionHandlerMethodThatRethrowException { - - int times = 0; - - @ExceptionHandler - public void methodThatRethrowException(TestException cause) { - times++; - throw cause; - } - - } - - @Before - public void setUp() throws Exception { - this.logger = PowerMock.createMock(Logger.class); - this.bundle = new ResourceBundle("demoiselle-core-bundle", Locale.getDefault()); - this.logger.info(EasyMock.anyObject(String.class)); - PowerMock.expectLastCall().anyTimes(); - replay(this.logger); - this.interceptor = new ExceptionHandlerInterceptor(this.logger, this.bundle); - this.context = PowerMock.createMock(InvocationContext.class); - mockStatic(CoreBootstrap.class); - } - - @Test - public void manageSuccessfully() throws Throwable { - expect(this.context.proceed()).andReturn(null); - replay(); - assertEquals(null, this.interceptor.manage(this.context)); - verify(); - } - - @Test - public void manageWithClassThatDoesNotContainHandleMethod() throws Exception { - ClassWithoutMethodsAnnotatedWithExceptionHandler classWithoutException = new ClassWithoutMethodsAnnotatedWithExceptionHandler(); - expect(this.context.getTarget()).andReturn(classWithoutException); - expect(this.context.proceed()).andThrow(new DemoiselleException("")); - expect(CoreBootstrap.isAnnotatedType(ClassWithoutMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); - replayAll(this.context, ClassWithoutMethodsAnnotatedWithExceptionHandler.class); - - try { - this.interceptor.manage(this.context); - fail(); - } catch (DemoiselleException e) { - assertTrue(true); - } - - verifyAll(); - } - - @Test - public void manageWithClassThatContainsHandleMethod() throws Exception { - ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); - expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); - expect(this.context.proceed()).andThrow(new DemoiselleException("")); - expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); - replayAll(this.context, CoreBootstrap.class); - - assertNull(this.interceptor.manage(this.context)); - assertEquals(1, classWithException.times); - verifyAll(); - } - - @Test - public void manageWithClassThatContainsParentExceptionHandleMethod() throws Exception { - ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); - expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); - expect(this.context.proceed()).andThrow(new DemoiselleException("")); - expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); - replayAll(this.context, CoreBootstrap.class); - - assertNull(this.interceptor.manage(this.context)); - assertEquals(1, classWithException.times); - verifyAll(); - } - - @Test - public void manageWithClassThatContainsHandleMethodWithDiferentException() throws Exception { - ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); - expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); - expect(this.context.proceed()).andThrow(new TestException("")); - replay(this.context); - expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); - replayAll(this.context, CoreBootstrap.class); - - try { - this.interceptor.manage(this.context); - fail(); - } catch (TestException e) { - assertEquals(0, classWithException.times); - } - - verify(); - } - - @Test - public void manageWithClassThatContainsHandleMethodThatThrowsAnotherException() throws Exception { - ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException classWithException = new ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException(); - expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); - expect(this.context.proceed()).andThrow(new DemoiselleException("")); - expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException.class)) - .andReturn(true); - replayAll(this.context, CoreBootstrap.class); - - try { - this.interceptor.manage(this.context); - fail(); - } catch (RuntimeException e) { - assertEquals(1, classWithException.times); - } - - verifyAll(); - } - - @Test - public void manageWithClassThatContainsHandleMethodsAndIsInvokedTwice() throws Exception { - ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); - expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); - expect(this.context.proceed()).andThrow(new DemoiselleException("")); - expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true) - .anyTimes(); - replayAll(this.context, CoreBootstrap.class); - - assertNull(this.interceptor.manage(this.context)); - assertEquals(1, classWithException.times); - - this.context = PowerMock.createMock(InvocationContext.class); - expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); - expect(this.context.proceed()).andThrow(new Exception("")); - replayAll(this.context, CoreBootstrap.class); - - assertNull(this.interceptor.manage(this.context)); - assertEquals(2, classWithException.times); - verifyAll(); - - } - - @Test - public void manageWithClassThatContainsHandleMethodWithoutParameter() throws Exception { - ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler classWithException = new ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler(); - expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); - expect(this.context.proceed()).andThrow(new DemoiselleException("")); - expect(CoreBootstrap.isAnnotatedType(ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler.class)) - .andReturn(true); - replayAll(this.context, CoreBootstrap.class); - - try { - this.interceptor.manage(this.context); - fail(); - } catch (DemoiselleException e) { - assertTrue(true); - } - - verifyAll(); - } - - @Test - public void manageHandlerMethodThatRethrowExpectedException() throws Exception { - ClassWithExceptionHandlerMethodThatRethrowException testClass = new ClassWithExceptionHandlerMethodThatRethrowException(); - expect(this.context.getTarget()).andReturn(testClass).anyTimes(); - expect(this.context.proceed()).andThrow(new TestException("")); - expect(CoreBootstrap.isAnnotatedType(ClassWithExceptionHandlerMethodThatRethrowException.class)) - .andReturn(true); - replayAll(this.context, CoreBootstrap.class); - - try { - this.interceptor.manage(this.context); - fail(); - } catch (TestException e) { - assertEquals(1, testClass.times); - } - - verifyAll(); - } - - /** - * Tests an exception handler when the class that contains the method is a proxy. This is the case when using - * injection. - * - * @throws Exception - */ - @Test - public void manageHandlerMethodInsideProxyClass() throws Exception { - // creates a proxy class - ClassWithExceptionHandlerMethodThatRethrowException testClass = PowerMock - .createNicePartialMockForAllMethodsExcept(ClassWithExceptionHandlerMethodThatRethrowException.class, - "methodThatRethrowException"); - expect(this.context.getTarget()).andReturn(testClass).anyTimes(); - expect(this.context.proceed()).andThrow(new TestException("")); - expect(CoreBootstrap.isAnnotatedType(testClass.getClass())).andReturn(false); - - this.logger = PowerMock.createMock(Logger.class); - this.logger.info(EasyMock.anyObject(String.class)); - this.logger.debug(EasyMock.anyObject(String.class)); - replayAll(testClass, this.context, CoreBootstrap.class, logger); - - this.interceptor = new ExceptionHandlerInterceptor(this.logger, this.bundle); - - try { - this.interceptor.manage(this.context); - fail(); - } catch (TestException e) { - assertEquals(1, testClass.times); - } - } - -} +///* +// * Demoiselle Framework +// * Copyright (C) 2010 SERPRO +// * ---------------------------------------------------------------------------- +// * This file is part of Demoiselle Framework. +// * +// * Demoiselle Framework is free software; you can redistribute it and/or +// * modify it under the terms of the GNU Lesser General Public License version 3 +// * as published by the Free Software Foundation. +// * +// * This program is distributed in the hope that it will be useful, +// * but WITHOUT ANY WARRANTY; without even the implied warranty of +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// * GNU General Public License for more details. +// * +// * You should have received a copy of the GNU Lesser General Public License version 3 +// * along with this program; if not, see +// * or write to the Free Software Foundation, Inc., 51 Franklin Street, +// * Fifth Floor, Boston, MA 02110-1301, USA. +// * ---------------------------------------------------------------------------- +// * Este arquivo é parte do Framework Demoiselle. +// * +// * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou +// * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação +// * do Software Livre (FSF). +// * +// * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA +// * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou +// * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português +// * para maiores detalhes. +// * +// * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título +// * "LICENCA.txt", junto com esse programa. Se não, acesse +// * ou escreva para a Fundação do Software Livre (FSF) Inc., +// * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. +// */ +//package br.gov.frameworkdemoiselle.internal.interceptor; +// +//import static org.easymock.EasyMock.expect; +//import static org.easymock.EasyMock.verify; +//import static org.junit.Assert.assertEquals; +//import static org.junit.Assert.assertNull; +//import static org.junit.Assert.assertTrue; +//import static org.junit.Assert.fail; +//import static org.powermock.api.easymock.PowerMock.mockStatic; +//import static org.powermock.api.easymock.PowerMock.replay; +//import static org.powermock.api.easymock.PowerMock.replayAll; +//import static org.powermock.api.easymock.PowerMock.verifyAll; +// +//import java.util.Locale; +// +//import javax.interceptor.InvocationContext; +// +//import org.easymock.EasyMock; +//import org.junit.Before; +//import org.junit.Test; +//import org.junit.runner.RunWith; +//import org.powermock.api.easymock.PowerMock; +//import org.powermock.core.classloader.annotations.PrepareForTest; +//import org.powermock.modules.junit4.PowerMockRunner; +//import org.slf4j.Logger; +// +//import br.gov.frameworkdemoiselle.DemoiselleException; +//import br.gov.frameworkdemoiselle.exception.ExceptionHandler; +//import br.gov.frameworkdemoiselle.internal.bootstrap.CoreBootstrap; +//import br.gov.frameworkdemoiselle.util.ResourceBundle; +// +//@RunWith(PowerMockRunner.class) +//@PrepareForTest(CoreBootstrap.class) +//public class ExceptionHandlerInterceptorTest { +// +// private ExceptionHandlerInterceptor interceptor; +// +// private InvocationContext context; +// +// private Logger logger; +// +// private ResourceBundle bundle; +// +// class TestException extends DemoiselleException { +// +// private static final long serialVersionUID = 1L; +// +// public TestException(String message) { +// super(message); +// } +// } +// +// class ClassWithMethodsAnnotatedWithExceptionHandler { +// +// int times = 0; +// +// @ExceptionHandler +// public void methodWithExceptionHandlerAnotation(DemoiselleException cause) { +// times++; +// } +// +// @ExceptionHandler +// public void methodWithExceptionHandlerAnotationAndGenericException(Exception cause) { +// times++; +// } +// +// } +// +// class ClassWithoutMethodsAnnotatedWithExceptionHandler { +// +// public void methodWithoutExceptionHandlerAnotation(DemoiselleException cause) { +// } +// } +// +// class ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException { +// +// int times = 0; +// +// @ExceptionHandler +// public void methodWithExceptionHandlerAnotation(DemoiselleException cause) { +// times++; +// throw new RuntimeException(); +// } +// } +// +// class ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler { +// +// @ExceptionHandler +// public void methodWithExceptionHandlerAnotation() { +// } +// +// } +// +// class ClassWithExceptionHandlerMethodThatRethrowException { +// +// int times = 0; +// +// @ExceptionHandler +// public void methodThatRethrowException(TestException cause) { +// times++; +// throw cause; +// } +// +// } +// +// @Before +// public void setUp() throws Exception { +// this.logger = PowerMock.createMock(Logger.class); +// this.bundle = new ResourceBundle("demoiselle-core-bundle", Locale.getDefault()); +// this.logger.info(EasyMock.anyObject(String.class)); +// PowerMock.expectLastCall().anyTimes(); +// replay(this.logger); +// this.interceptor = new ExceptionHandlerInterceptor(this.logger, this.bundle); +// this.context = PowerMock.createMock(InvocationContext.class); +// mockStatic(CoreBootstrap.class); +// } +// +// @Test +// public void manageSuccessfully() throws Throwable { +// expect(this.context.proceed()).andReturn(null); +// replay(); +// assertEquals(null, this.interceptor.manage(this.context)); +// verify(); +// } +// +// @Test +// public void manageWithClassThatDoesNotContainHandleMethod() throws Exception { +// ClassWithoutMethodsAnnotatedWithExceptionHandler classWithoutException = new ClassWithoutMethodsAnnotatedWithExceptionHandler(); +// expect(this.context.getTarget()).andReturn(classWithoutException); +// expect(this.context.proceed()).andThrow(new DemoiselleException("")); +// expect(CoreBootstrap.isAnnotatedType(ClassWithoutMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); +// replayAll(this.context, ClassWithoutMethodsAnnotatedWithExceptionHandler.class); +// +// try { +// this.interceptor.manage(this.context); +// fail(); +// } catch (DemoiselleException e) { +// assertTrue(true); +// } +// +// verifyAll(); +// } +// +// @Test +// public void manageWithClassThatContainsHandleMethod() throws Exception { +// ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); +// expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); +// expect(this.context.proceed()).andThrow(new DemoiselleException("")); +// expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); +// replayAll(this.context, CoreBootstrap.class); +// +// assertNull(this.interceptor.manage(this.context)); +// assertEquals(1, classWithException.times); +// verifyAll(); +// } +// +// @Test +// public void manageWithClassThatContainsParentExceptionHandleMethod() throws Exception { +// ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); +// expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); +// expect(this.context.proceed()).andThrow(new DemoiselleException("")); +// expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); +// replayAll(this.context, CoreBootstrap.class); +// +// assertNull(this.interceptor.manage(this.context)); +// assertEquals(1, classWithException.times); +// verifyAll(); +// } +// +// @Test +// public void manageWithClassThatContainsHandleMethodWithDiferentException() throws Exception { +// ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); +// expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); +// expect(this.context.proceed()).andThrow(new TestException("")); +// replay(this.context); +// expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true); +// replayAll(this.context, CoreBootstrap.class); +// +// try { +// this.interceptor.manage(this.context); +// fail(); +// } catch (TestException e) { +// assertEquals(0, classWithException.times); +// } +// +// verify(); +// } +// +// @Test +// public void manageWithClassThatContainsHandleMethodThatThrowsAnotherException() throws Exception { +// ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException classWithException = new ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException(); +// expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); +// expect(this.context.proceed()).andThrow(new DemoiselleException("")); +// expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandlerAndThrowException.class)) +// .andReturn(true); +// replayAll(this.context, CoreBootstrap.class); +// +// try { +// this.interceptor.manage(this.context); +// fail(); +// } catch (RuntimeException e) { +// assertEquals(1, classWithException.times); +// } +// +// verifyAll(); +// } +// +// @Test +// public void manageWithClassThatContainsHandleMethodsAndIsInvokedTwice() throws Exception { +// ClassWithMethodsAnnotatedWithExceptionHandler classWithException = new ClassWithMethodsAnnotatedWithExceptionHandler(); +// expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); +// expect(this.context.proceed()).andThrow(new DemoiselleException("")); +// expect(CoreBootstrap.isAnnotatedType(ClassWithMethodsAnnotatedWithExceptionHandler.class)).andReturn(true) +// .anyTimes(); +// replayAll(this.context, CoreBootstrap.class); +// +// assertNull(this.interceptor.manage(this.context)); +// assertEquals(1, classWithException.times); +// +// this.context = PowerMock.createMock(InvocationContext.class); +// expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); +// expect(this.context.proceed()).andThrow(new Exception("")); +// replayAll(this.context, CoreBootstrap.class); +// +// assertNull(this.interceptor.manage(this.context)); +// assertEquals(2, classWithException.times); +// verifyAll(); +// +// } +// +// @Test +// public void manageWithClassThatContainsHandleMethodWithoutParameter() throws Exception { +// ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler classWithException = new ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler(); +// expect(this.context.getTarget()).andReturn(classWithException).anyTimes(); +// expect(this.context.proceed()).andThrow(new DemoiselleException("")); +// expect(CoreBootstrap.isAnnotatedType(ClassWithMethodWithoutParameterAnnotatedWithExceptionHandler.class)) +// .andReturn(true); +// replayAll(this.context, CoreBootstrap.class); +// +// try { +// this.interceptor.manage(this.context); +// fail(); +// } catch (DemoiselleException e) { +// assertTrue(true); +// } +// +// verifyAll(); +// } +// +// @Test +// public void manageHandlerMethodThatRethrowExpectedException() throws Exception { +// ClassWithExceptionHandlerMethodThatRethrowException testClass = new ClassWithExceptionHandlerMethodThatRethrowException(); +// expect(this.context.getTarget()).andReturn(testClass).anyTimes(); +// expect(this.context.proceed()).andThrow(new TestException("")); +// expect(CoreBootstrap.isAnnotatedType(ClassWithExceptionHandlerMethodThatRethrowException.class)) +// .andReturn(true); +// replayAll(this.context, CoreBootstrap.class); +// +// try { +// this.interceptor.manage(this.context); +// fail(); +// } catch (TestException e) { +// assertEquals(1, testClass.times); +// } +// +// verifyAll(); +// } +// +// /** +// * Tests an exception handler when the class that contains the method is a proxy. This is the case when using +// * injection. +// * +// * @throws Exception +// */ +// @Test +// public void manageHandlerMethodInsideProxyClass() throws Exception { +// // creates a proxy class +// ClassWithExceptionHandlerMethodThatRethrowException testClass = PowerMock +// .createNicePartialMockForAllMethodsExcept(ClassWithExceptionHandlerMethodThatRethrowException.class, +// "methodThatRethrowException"); +// expect(this.context.getTarget()).andReturn(testClass).anyTimes(); +// expect(this.context.proceed()).andThrow(new TestException("")); +// expect(CoreBootstrap.isAnnotatedType(testClass.getClass())).andReturn(false); +// +// this.logger = PowerMock.createMock(Logger.class); +// this.logger.info(EasyMock.anyObject(String.class)); +// this.logger.debug(EasyMock.anyObject(String.class)); +// replayAll(testClass, this.context, CoreBootstrap.class, logger); +// +// this.interceptor = new ExceptionHandlerInterceptor(this.logger, this.bundle); +// +// try { +// this.interceptor.manage(this.context); +// fail(); +// } catch (TestException e) { +// assertEquals(1, testClass.times); +// } +// } +// +//} diff --git a/impl/core/src/test/java/br/gov/frameworkdemoiselle/util/BeansTest.java b/impl/core/src/test/java/br/gov/frameworkdemoiselle/util/BeansTest.java index 21a04d2..bceb927 100644 --- a/impl/core/src/test/java/br/gov/frameworkdemoiselle/util/BeansTest.java +++ b/impl/core/src/test/java/br/gov/frameworkdemoiselle/util/BeansTest.java @@ -61,9 +61,8 @@ import org.powermock.modules.junit4.PowerMockRunner; @PrepareForTest({ BeanManager.class, Bean.class }) public class BeansTest { - @SuppressWarnings({ "unchecked", "static-access" }) - @Test @Ignore + @SuppressWarnings("unchecked") public void testGetReferenceByClass() { BeanManager beanManager = PowerMock.createMock(BeanManager.class); @@ -81,19 +80,17 @@ public class BeansTest { replayAll(beanManager, bean); - // There is no need to instantiate utility classes. But it's the only way to get 100% cobertura. - Beans beans = new Beans(); - beans.setBeanManager(beanManager); - String returned = beans.getReference(String.class); + Beans.setBeanManager(beanManager); + String returned = Beans.getReference(String.class); assertEquals(returned, object); - assertEquals(beanManager, beans.getBeanManager()); + assertEquals(beanManager, Beans.getBeanManager()); verifyAll(); } - @SuppressWarnings({ "unchecked", "static-access" }) @Test + @SuppressWarnings("unchecked") public void testGetReferenceByString() { BeanManager beanManager = PowerMock.createMock(BeanManager.class); @@ -112,13 +109,11 @@ public class BeansTest { replayAll(beanManager, bean); - // There is no need to instantiate utility classes. But it's the only way to get 100% cobertura. - Beans beans = new Beans(); - beans.setBeanManager(beanManager); - String returned = beans.getReference("something"); + Beans.setBeanManager(beanManager); + String returned = Beans.getReference("something"); assertEquals(returned, object); - assertEquals(beanManager, beans.getBeanManager()); + assertEquals(beanManager, Beans.getBeanManager()); verifyAll(); } diff --git a/impl/core/src/test/java/br/gov/frameworkdemoiselle/util/ExceptionsTest.java b/impl/core/src/test/java/br/gov/frameworkdemoiselle/util/ExceptionsTest.java index 7875578..bf0b2bc 100644 --- a/impl/core/src/test/java/br/gov/frameworkdemoiselle/util/ExceptionsTest.java +++ b/impl/core/src/test/java/br/gov/frameworkdemoiselle/util/ExceptionsTest.java @@ -52,39 +52,38 @@ public class ExceptionsTest { assertFalse(Exceptions.isApplicationException(new Exception())); } - @SuppressWarnings("static-access") @Test public void testHandleRuntimeException() { - Exceptions exceptions = new Exceptions(); try { - exceptions.handleToRuntimeException(new SomeRuntimeException()); + Exceptions.handleToRuntimeException(new SomeRuntimeException()); fail(); } catch (Throwable t) { - if ( !RuntimeException.class.isInstance(t) ) { + if (!RuntimeException.class.isInstance(t)) { fail(); - } + } } try { - exceptions.handleToRuntimeException(new Exception()); + Exceptions.handleToRuntimeException(new Exception()); fail(); } catch (Throwable t) { - if ( !RuntimeException.class.isInstance(t) ) { + if (!RuntimeException.class.isInstance(t)) { fail(); - } + } } } } -@SuppressWarnings("serial") @ApplicationException class MyException extends Exception { + private static final long serialVersionUID = 1L; } -@SuppressWarnings("serial") class SomeRuntimeException extends RuntimeException { + private static final long serialVersionUID = 1L; + } diff --git a/impl/core/src/test/java/br/gov/frameworkdemoiselle/util/StringsTest.java b/impl/core/src/test/java/br/gov/frameworkdemoiselle/util/StringsTest.java index c3991fd..a94fc8e 100644 --- a/impl/core/src/test/java/br/gov/frameworkdemoiselle/util/StringsTest.java +++ b/impl/core/src/test/java/br/gov/frameworkdemoiselle/util/StringsTest.java @@ -43,12 +43,9 @@ import static org.junit.Assert.assertTrue; import static org.powermock.api.easymock.PowerMock.verifyAll; import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.modules.junit4.PowerMockRunner; import br.gov.frameworkdemoiselle.annotation.Ignore; -@RunWith(PowerMockRunner.class) public class StringsTest { @Test @@ -144,7 +141,7 @@ public class StringsTest { } String result = Strings.toString(new Test()); - + assertTrue(result.contains("Test [name=myName, lastname=myLastname, nullField=null, this")); verifyAll(); @@ -195,15 +192,15 @@ public class StringsTest { assertEquals(-1, string.indexOf('L')); assertEquals(-1, string.indexOf('l')); } - + @Test public void insertZeros() { String string = "Lorem ipsum"; assertEquals("00000", Strings.insertZeros(null, 5)); - assertEquals(string, Strings.insertZeros(string, string.length()-1)); + assertEquals(string, Strings.insertZeros(string, string.length() - 1)); assertEquals(string, Strings.insertZeros(string, string.length())); - assertEquals("0"+string, Strings.insertZeros(string, string.length()+1)); - assertEquals("00"+string, Strings.insertZeros(string, string.length()+2)); + assertEquals("0" + string, Strings.insertZeros(string, string.length() + 1)); + assertEquals("00" + string, Strings.insertZeros(string, string.length() + 2)); } - + } -- libgit2 0.21.2