Commit 015950a8cdb0beac527725b51f9d7e01c3f77867

Authored by Emerson Oliveira
2 parents 40c78306 02099e78
Exists in master

Merge branch '2.4.0' of git@github.com:demoiselle/framework.git into 2.4.0

impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ContextManager.java
... ... @@ -10,6 +10,7 @@ import javax.enterprise.context.ContextNotActiveException;
10 10 import javax.enterprise.context.spi.Context;
11 11 import javax.enterprise.inject.spi.AfterBeanDiscovery;
12 12 import javax.enterprise.inject.spi.BeanManager;
  13 +import javax.enterprise.inject.spi.BeforeShutdown;
13 14  
14 15 import org.slf4j.Logger;
15 16  
... ... @@ -103,6 +104,10 @@ public class ContextManager {
103 104 }
104 105 }
105 106  
  107 + ContextManager.getLogger().trace(
  108 + ContextManager.getBundle().getString("bootstrap-context-added",
  109 + context.getClass().getCanonicalName(), context.getScope().getCanonicalName()));
  110 +
106 111 context.setActive(false);
107 112 event.addContext(context);
108 113 contexts.add(new CustomContextCounter(context));
... ... @@ -188,9 +193,13 @@ public class ContextManager {
188 193 customContextClass.getCanonicalName(), scope.getSimpleName()));
189 194 }
190 195  
  196 + /**
  197 + * <p>This method should be called when the application is shutting down, usually by observing
  198 + * the {@link BeforeShutdown} event.</p>
  199 + */
191 200 public static synchronized void shutdown() {
192 201 for (CustomContextCounter context : contexts) {
193   - context.deactivate();
  202 + context.shutdown();
194 203 }
195 204  
196 205 contexts.clear();
... ... @@ -243,14 +252,29 @@ class CustomContextCounter {
243 252 public CustomContext getInternalContext() {
244 253 return this.context;
245 254 }
  255 +
  256 + public void setInternalContext(CustomContext context) {
  257 + this.context = context;
  258 + }
246 259  
247 260 public synchronized void activate() {
248 261 try {
249 262 BeanManager beanManager = Beans.getReference(BeanManager.class);
250 263 Context c = beanManager.getContext(context.getScope());
  264 +
  265 +
251 266 if (c == context) {
252 267 activationCounter++;
253 268 }
  269 + else{
  270 + ContextManager.getLogger().trace(
  271 + ContextManager.getBundle().getString("custom-context-already-activated",
  272 + context.getClass().getCanonicalName()
  273 + ,c.getScope().getCanonicalName()
  274 + ,c.getClass().getCanonicalName()
  275 + )
  276 + );
  277 + }
254 278 } catch (ContextNotActiveException ce) {
255 279 context.setActive(true);
256 280 activationCounter++;
... ... @@ -275,4 +299,10 @@ class CustomContextCounter {
275 299 } catch (ContextNotActiveException ce) {
276 300 }
277 301 }
  302 +
  303 + public synchronized void shutdown() {
  304 + context.setActive(false);
  305 + context=null;
  306 + activationCounter=0;
  307 + }
278 308 }
... ...
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/management/Management.java
... ... @@ -67,8 +67,8 @@ import br.gov.frameworkdemoiselle.util.Beans;
67 67 import br.gov.frameworkdemoiselle.util.ResourceBundle;
68 68  
69 69 /**
70   - * Central class used by management extensions to obtain information, access properties and call operations
71   - * over discovered {@link ManagementController} classes.
  70 + * Central class used by management extensions to obtain information, access properties and call operations over
  71 + * discovered {@link ManagementController} classes.
72 72 *
73 73 * @author serpro
74 74 */
... ... @@ -83,20 +83,18 @@ public class Management {
83 83 private ResourceBundle bundle;
84 84  
85 85 private final List<ManagedType> managedTypes = new ArrayList<ManagedType>();
86   -
  86 +
87 87 private Validator validator;
88 88  
89 89 public void addManagedType(ManagedType managedType) {
90 90 managedTypes.add(managedType);
91   - logger.debug(bundle.getString("management-debug-registering-managed-type",managedType.getType().getCanonicalName()));
  91 + logger.debug(bundle.getString("management-debug-registering-managed-type", managedType.getType()
  92 + .getCanonicalName()));
92 93 }
93 94  
94 95 /**
95   - * @return List all discovered {@link ManagementController} classes.
96   - * The returned list is a shallow copy of the internal list, so you
97   - * are free to modify it.
98   - *
99   - * TODO precisamos desse clone na lista?
  96 + * @return List all discovered {@link ManagementController} classes. The returned list is a shallow copy of the
  97 + * internal list, so you are free to modify it. TODO precisamos desse clone na lista?
100 98 */
101 99 public List<ManagedType> getManagedTypes() {
102 100 ArrayList<ManagedType> cloneList = new ArrayList<ManagedType>();
... ... @@ -105,77 +103,78 @@ public class Management {
105 103 }
106 104  
107 105 /**
108   - * <p>Invoke an operation over a {@link ManagementController}.</p>
109   - *
110   - * <p>This method is not thread-safe, it's the user's responsibility to
111   - * make the operations of the managed type synchronized if necessary.</p>
  106 + * <p>
  107 + * Invoke an operation over a {@link ManagementController}.
  108 + * </p>
  109 + * <p>
  110 + * This method is not thread-safe, it's the user's responsibility to make the operations of the managed type
  111 + * synchronized if necessary.
  112 + * </p>
112 113 *
113 114 * @param managedType
114   - * A type annotated with {@link ManagementController}. This method will create
115   - * an (or obtain an already created) instance of this type and
116   - * invoke the operation over it.
  115 + * A type annotated with {@link ManagementController}. This method will create an (or obtain an already
  116 + * created) instance of this type and invoke the operation over it.
117 117 * @param actionName
118   - * Name of method to be invoked, the type must have this
119   - * operation on it's list
  118 + * Name of method to be invoked, the type must have this operation on it's list
120 119 * @param params
121   - * List of values for the operation parameters. Can be
122   - * <code>null</code> if the operation require no parameters.
123   - * @return The return value of the original invoked operation. Methods of
124   - * return type <code>void</code> will return the {@link Void} type.
  120 + * List of values for the operation parameters. Can be <code>null</code> if the operation require no
  121 + * parameters.
  122 + * @return The return value of the original invoked operation. Methods of return type <code>void</code> will return
  123 + * the {@link Void} type.
125 124 * @throws ReflectionException
126   - * In case the operation doesn't exist or have a different
127   - * signature
  125 + * In case the operation doesn't exist or have a different signature
128 126 */
129   - public Object invoke(ManagedType managedType, String actionName,
130   - Object[] params) {
131   - if ( managedTypes.contains(managedType) ) {
  127 + public Object invoke(ManagedType managedType, String actionName, Object[] params) {
  128 + if (managedTypes.contains(managedType)) {
132 129 activateContexts(managedType.getType());
133 130  
134   - Object delegate = Beans.getReference(managedType.getType());
135   -
136   - MethodDetail method = managedType.getOperationMethods().get(actionName);
137   -
138   - if (method != null) {
139   - try {
140   - logger.debug(bundle
141   - .getString("management-debug-invoking-operation",actionName,managedType.getType().getCanonicalName()));
142   - return method.getMethod().invoke(delegate, params);
143   - } catch (Exception e) {
144   - throw new DemoiselleException(bundle.getString(
145   - "management-invoke-error", actionName), e);
146   - } finally {
147   - deactivateContexts(managedType.getType());
  131 + try {
  132 + Object delegate = Beans.getReference(managedType.getType());
  133 + MethodDetail method = managedType.getOperationMethods().get(actionName);
  134 +
  135 + if (method != null) {
  136 + try {
  137 + logger.debug(bundle.getString("management-debug-invoking-operation", actionName, managedType
  138 + .getType().getCanonicalName()));
  139 + return method.getMethod().invoke(delegate, params);
  140 + } catch (Exception e) {
  141 + throw new DemoiselleException(bundle.getString("management-invoke-error", actionName), e);
  142 + }
  143 + } else {
  144 + throw new DemoiselleException(bundle.getString("management-invoke-error", actionName));
148 145 }
149   - } else {
150   - throw new DemoiselleException(bundle.getString(
151   - "management-invoke-error", actionName));
  146 + } finally {
  147 + deactivateContexts(managedType.getType());
152 148 }
153 149 } else {
154   - throw new DemoiselleException(
155   - bundle.getString("management-type-not-found"));
  150 + throw new DemoiselleException(bundle.getString("management-type-not-found"));
156 151 }
157 152 }
158 153  
159 154 /**
160   - * <p>Retrieve the current value of a property from a managed type. Properties
161   - * are attributes annotated with {@link ManagedProperty}.</p>
  155 + * <p>
  156 + * Retrieve the current value of a property from a managed type. Properties are attributes annotated with
  157 + * {@link ManagedProperty}.
  158 + * </p>
  159 + * <p>
  160 + * This method is not thread-safe, it's the user's responsibility to create the property's access methods from the
  161 + * managed type synchronized if necessary.
  162 + * </p>
162 163 *
163   - * <p>This method is not thread-safe, it's the user's responsibility to
164   - * create the property's access methods from the managed type synchronized if necessary.</p>
165   - *
166   - * @param managedType The type that has the property the client wants to know the value of.
167   - * @param propertyName The name of the property
  164 + * @param managedType
  165 + * The type that has the property the client wants to know the value of.
  166 + * @param propertyName
  167 + * The name of the property
168 168 * @return The current value of the property
169 169 */
170 170 public Object getProperty(ManagedType managedType, String propertyName) {
171   -
172   - if ( managedTypes.contains(managedType) ) {
  171 +
  172 + if (managedTypes.contains(managedType)) {
173 173 Method getterMethod = managedType.getFields().get(propertyName).getGetterMethod();
174 174  
175 175 if (getterMethod != null) {
176   - logger.debug(bundle.getString(
177   - "management-debug-acessing-property", getterMethod
178   - .getName(), managedType.getType().getCanonicalName()));
  176 + logger.debug(bundle.getString("management-debug-acessing-property", getterMethod.getName(), managedType
  177 + .getType().getCanonicalName()));
179 178  
180 179 activateContexts(managedType.getType());
181 180  
... ... @@ -184,43 +183,44 @@ public class Management {
184 183  
185 184 return getterMethod.invoke(delegate, (Object[]) null);
186 185 } catch (Exception e) {
187   - throw new DemoiselleException(bundle.getString(
188   - "management-invoke-error", getterMethod.getName()),
  186 + throw new DemoiselleException(bundle.getString("management-invoke-error", getterMethod.getName()),
189 187 e);
190 188 } finally {
191 189 deactivateContexts(managedType.getType());
192 190 }
193 191 } else {
194   - throw new DemoiselleException(bundle.getString(
195   - "management-invoke-error", propertyName));
  192 + throw new DemoiselleException(bundle.getString("management-invoke-error", propertyName));
196 193 }
197 194 } else {
198   - throw new DemoiselleException(
199   - bundle.getString("management-type-not-found"));
  195 + throw new DemoiselleException(bundle.getString("management-type-not-found"));
200 196 }
201 197 }
202   -
  198 +
203 199 /**
204   - * <p>Sets a new value for a property contained inside a managed type. A property
205   - * is an attribute annotated with {@link ManagedProperty}.</p>
  200 + * <p>
  201 + * Sets a new value for a property contained inside a managed type. A property is an attribute annotated with
  202 + * {@link ManagedProperty}.
  203 + * </p>
  204 + * <p>
  205 + * This method is not thread-safe, it's the user's responsibility to create the property's access methods from the
  206 + * managed type synchronized if necessary.
  207 + * </p>
206 208 *
207   - * <p>This method is not thread-safe, it's the user's responsibility to
208   - * create the property's access methods from the managed type synchronized if necessary.</p>
209   - *
210   - * @param managedType The type that has access to the property
211   - * @param propertyName The name of the property
212   - * @param newValue The new value of the property
  209 + * @param managedType
  210 + * The type that has access to the property
  211 + * @param propertyName
  212 + * The name of the property
  213 + * @param newValue
  214 + * The new value of the property
213 215 */
214   - public void setProperty(ManagedType managedType, String propertyName,
215   - Object newValue) {
  216 + public void setProperty(ManagedType managedType, String propertyName, Object newValue) {
216 217  
217   - if ( managedTypes.contains(managedType) ) {
  218 + if (managedTypes.contains(managedType)) {
218 219 // Procura o método set do atributo em questão
219 220 Method method = managedType.getFields().get(propertyName).getSetterMethod();
220 221 if (method != null) {
221   - logger.debug(bundle.getString(
222   - "management-debug-setting-property", method.getName(),
223   - managedType.getType().getCanonicalName()));
  222 + logger.debug(bundle.getString("management-debug-setting-property", method.getName(), managedType
  223 + .getType().getCanonicalName()));
224 224  
225 225 activateContexts(managedType.getType());
226 226 try {
... ... @@ -228,93 +228,80 @@ public class Management {
228 228 // classes
229 229 // anotadas com @ManagementController são sempre singletons.
230 230 Object delegate = Beans.getReference(managedType.getType());
231   -
232   - //Se houver um validador anexado à propriedade alterada, executa o validador sobre
233   - //o novo valor.
  231 +
  232 + // Se houver um validador anexado à propriedade alterada, executa o validador sobre
  233 + // o novo valor.
234 234 Validator validator = getDefaultValidator();
235   - if (validator!=null){
  235 + if (validator != null) {
236 236 Set<?> violations = validator.validateValue(managedType.getType(), propertyName, newValue);
237   - if (violations.size()>0){
  237 + if (violations.size() > 0) {
238 238 StringBuffer errorBuffer = new StringBuffer();
239   - for (Object objectViolation : violations){
  239 + for (Object objectViolation : violations) {
240 240 ConstraintViolation<?> violation = (ConstraintViolation<?>) objectViolation;
241 241 errorBuffer.append(violation.getMessage()).append('\r').append('\n');
242 242 }
243   -
244   - if (errorBuffer.length()>0){
  243 +
  244 + if (errorBuffer.length() > 0) {
245 245 errorBuffer.insert(0, "\r\n");
246 246 errorBuffer.insert(errorBuffer.length(), "\r\n");
247 247 }
248   -
249   - throw new DemoiselleException(
250   - bundle.getString(
251   - "management-validation-constraint-violation"
252   - ,managedType.getType().getCanonicalName()
253   - ,propertyName,errorBuffer.toString()
254   - )
255   - );
  248 +
  249 + throw new DemoiselleException(bundle.getString(
  250 + "management-validation-constraint-violation", managedType.getType()
  251 + .getCanonicalName(), propertyName, errorBuffer.toString()));
256 252 }
257   - }
258   - else{
  253 + } else {
259 254 logger.warn(bundle.getString("management-validation-validator-not-found"));
260 255 }
261 256  
262 257 Method getterMethod = managedType.getFields().get(propertyName).getGetterMethod();
263 258 Object oldValue;
264   - try{
265   - oldValue = getterMethod.invoke(delegate, (Object[])null);
266   - }
267   - catch(Exception e){
  259 + try {
  260 + oldValue = getterMethod.invoke(delegate, (Object[]) null);
  261 + } catch (Exception e) {
268 262 oldValue = null;
269 263 }
270 264  
271 265 method.invoke(delegate, new Object[] { newValue });
272 266  
273   - //Manda uma notificação de mudança de atributo
  267 + // Manda uma notificação de mudança de atributo
274 268 NotificationManager notificationManager = Beans.getReference(NotificationManager.class);
275   - Class<? extends Object> attributeType = newValue!=null ? newValue.getClass() : null;
276   -
277   - AttributeChangeNotification notification = new AttributeChangeNotification(bundle.getString("management-notification-attribute-changed",propertyName,managedType.getType().getCanonicalName())
278   - , propertyName
279   - , attributeType
280   - , oldValue
281   - , newValue);
  269 + Class<? extends Object> attributeType = newValue != null ? newValue.getClass() : null;
  270 +
  271 + AttributeChangeNotification notification = new AttributeChangeNotification(bundle.getString(
  272 + "management-notification-attribute-changed", propertyName, managedType.getType()
  273 + .getCanonicalName()), propertyName, attributeType, oldValue, newValue);
282 274 notificationManager.sendNotification(notification);
283 275  
284   - } catch (DemoiselleException de){
  276 + } catch (DemoiselleException de) {
285 277 throw de;
286 278 } catch (Exception e) {
287   - throw new DemoiselleException(bundle.getString(
288   - "management-invoke-error", method.getName()), e);
  279 + throw new DemoiselleException(bundle.getString("management-invoke-error", method.getName()), e);
289 280 } finally {
290 281 deactivateContexts(managedType.getType());
291 282 }
292 283  
293 284 } else {
294   - throw new DemoiselleException(bundle.getString(
295   - "management-invoke-error", propertyName));
  285 + throw new DemoiselleException(bundle.getString("management-invoke-error", propertyName));
296 286 }
297 287 } else {
298   - throw new DemoiselleException(
299   - bundle.getString("management-type-not-found"));
  288 + throw new DemoiselleException(bundle.getString("management-type-not-found"));
300 289 }
301 290  
302 291 }
303 292  
304 293 private void activateContexts(Class<?> managedType) {
305 294 logger.debug(bundle.getString("management-debug-starting-custom-context",
306   - ManagedContext.class.getCanonicalName(),
307   - managedType.getCanonicalName()));
308   -
309   - ContextManager.activate(ManagedContext.class,RequestScoped.class);
  295 + ManagedContext.class.getCanonicalName(), managedType.getCanonicalName()));
  296 +
  297 + ContextManager.activate(ManagedContext.class, RequestScoped.class);
310 298 }
311 299  
312 300 private void deactivateContexts(Class<?> managedType) {
313 301 logger.debug(bundle.getString("management-debug-stoping-custom-context",
314   - ManagedContext.class.getCanonicalName(),
315   - managedType.getCanonicalName()));
316   -
317   - ContextManager.deactivate(ManagedContext.class,RequestScoped.class);
  302 + ManagedContext.class.getCanonicalName(), managedType.getCanonicalName()));
  303 +
  304 + ContextManager.deactivate(ManagedContext.class, RequestScoped.class);
318 305 }
319 306  
320 307 public void shutdown(Collection<Class<? extends ManagementExtension>> monitoringExtensions) {
... ... @@ -324,8 +311,9 @@ public class Management {
324 311 ManagementExtension monitoringExtension = Beans.getReference(monitoringExtensionClass);
325 312  
326 313 monitoringExtension.shutdown(this.getManagedTypes());
327   -
328   - logger.debug( bundle.getString("management-debug-removing-management-extension",monitoringExtension.getClass().getCanonicalName()) );
  314 +
  315 + logger.debug(bundle.getString("management-debug-removing-management-extension", monitoringExtension
  316 + .getClass().getCanonicalName()));
329 317  
330 318 }
331 319  
... ... @@ -335,27 +323,26 @@ public class Management {
335 323  
336 324 for (Class<? extends ManagementExtension> monitoringExtensionClass : monitoringExtensions) {
337 325  
338   - ManagementExtension monitoringExtension = Beans
339   - .getReference(monitoringExtensionClass);
  326 + ManagementExtension monitoringExtension = Beans.getReference(monitoringExtensionClass);
340 327  
341 328 monitoringExtension.initialize(this.getManagedTypes());
342   -
343   - logger.debug( bundle.getString("management-debug-processing-management-extension",monitoringExtension.getClass().getCanonicalName()) );
  329 +
  330 + logger.debug(bundle.getString("management-debug-processing-management-extension", monitoringExtension
  331 + .getClass().getCanonicalName()));
344 332  
345 333 }
346 334  
347 335 }
348   -
349   - private Validator getDefaultValidator(){
350   - if (validator == null){
351   - try{
  336 +
  337 + private Validator getDefaultValidator() {
  338 + if (validator == null) {
  339 + try {
352 340 this.validator = Validation.buildDefaultValidatorFactory().getValidator();
353   - }
354   - catch(ValidationException e){
  341 + } catch (ValidationException e) {
355 342 this.validator = null;
356 343 }
357 344 }
358   -
  345 +
359 346 return this.validator;
360 347 }
361 348  
... ...
impl/core/src/main/resources/demoiselle-core-bundle.properties
... ... @@ -53,7 +53,8 @@ transaction-commited=Transa\u00E7\u00E3o finalizada com sucesso
53 53 transaction-rolledback=Transa\u00E7\u00E3o finalizada com rollback
54 54  
55 55 bootstrap.configuration.processing=Processando {0}
56   -bootstrap-context-already-managed=O contexto {0} para o escopo {1} j\u00E1 \u00E9 gerenciado
  56 +bootstrap-context-already-managed=O contexto {0} para o escopo {1} j\u00E1 foi adicionado
  57 +bootstrap-context-added=Adicionando o contexto {0} para o escopo {1}
57 58  
58 59 loading-configuration-class=Carregando a classe de configura\u00E7\u00E3o {0}
59 60 configuration-field-loaded=Configura\u00E7\u00E3o {0} atribu\u00EDda a {1} com o valor {2}
... ... @@ -69,6 +70,7 @@ custom-context-was-registered=O contexto {0} foi registrado
69 70 custom-context-was-unregistered=O contexto {0} foi removido
70 71 custom-context-was-activated=O contexto {0} foi ativado para o escopo {1}
71 72 custom-context-was-deactivated=O contexto {0} foi desativado para o escopo {1}
  73 +custom-context-already-activated=N\u00E3o foi poss\u00EDvel ativar o contexto {0}, o escopo {1} j\u00E1 est\u00E1 ativo no contexto {2}
72 74 custom-context-not-found=N\u00E3o foi encontrado um contexto gerenciado do tipo [{0}] para o escopo [{1}]
73 75 custom-context-manager-not-initialized=ContextManager n\u00E3o foi inicializado. Chame [initialize] ao capturar o evento [AfterBeanDiscovery] em uma extens\u00E3o CDI
74 76  
... ...
impl/core/src/test/java/message/MessageContextTest.java 0 → 100644
... ... @@ -0,0 +1,139 @@
  1 +package message;
  2 +
  3 +import static junit.framework.Assert.assertEquals;
  4 +import static junit.framework.Assert.assertTrue;
  5 +
  6 +import javax.enterprise.context.RequestScoped;
  7 +import javax.inject.Inject;
  8 +
  9 +import junit.framework.Assert;
  10 +
  11 +import org.jboss.arquillian.container.test.api.Deployment;
  12 +import org.jboss.arquillian.junit.Arquillian;
  13 +import org.jboss.shrinkwrap.api.spec.JavaArchive;
  14 +import org.junit.Test;
  15 +import org.junit.runner.RunWith;
  16 +
  17 +import resourcebundle.parameter.ResourceBundleWithParameter;
  18 +import test.Tests;
  19 +import br.gov.frameworkdemoiselle.internal.context.ContextManager;
  20 +import br.gov.frameworkdemoiselle.internal.context.ManagedContext;
  21 +import br.gov.frameworkdemoiselle.message.DefaultMessage;
  22 +import br.gov.frameworkdemoiselle.message.Message;
  23 +import br.gov.frameworkdemoiselle.message.MessageContext;
  24 +import br.gov.frameworkdemoiselle.message.SeverityType;
  25 +import br.gov.frameworkdemoiselle.util.Beans;
  26 +
  27 +@RunWith(Arquillian.class)
  28 +public class MessageContextTest {
  29 +
  30 + @Inject
  31 + private MessageContext messageContext;
  32 +
  33 + @Inject
  34 + private MessageWithResourceBundle bundleCustom;
  35 +
  36 + private static final String PATH = "src/test/resources/message/";
  37 +
  38 + @Deployment
  39 + public static JavaArchive createDeployment() {
  40 + JavaArchive deployment = Tests.createDeployment(MessageContextTest.class);
  41 + deployment.addAsResource(Tests.createFileAsset(PATH + "messages.properties"), "messages.properties");
  42 +
  43 + return deployment;
  44 + }
  45 +
  46 + @Test
  47 + public void testAddMessageWithoutParams() {
  48 + ContextManager.activate(ManagedContext.class, RequestScoped.class);
  49 + Message message = new DefaultMessage("Menssage without param");
  50 + messageContext.add(message);
  51 + assertEquals(messageContext.getMessages().size(), 1);
  52 + ContextManager.deactivate(ManagedContext.class, RequestScoped.class);
  53 + }
  54 +
  55 + @Test
  56 + public void testAddMessageWithoutParamsIfSeverityIsInfo() {
  57 + ContextManager.activate(ManagedContext.class, RequestScoped.class);
  58 + Message message = new DefaultMessage("Menssage without param");
  59 + messageContext.add(message);
  60 + assertEquals(messageContext.getMessages().get(0).getSeverity(), SeverityType.INFO);
  61 + ContextManager.deactivate(ManagedContext.class, RequestScoped.class);
  62 + }
  63 +
  64 + @Test
  65 + public void testAddMessageWitSeverityInfo() {
  66 + ContextManager.activate(ManagedContext.class, RequestScoped.class);
  67 + Message message = new DefaultMessage("Menssage without param", SeverityType.INFO);
  68 + messageContext.add(message);
  69 + assertEquals(messageContext.getMessages().get(0).getSeverity(), SeverityType.INFO);
  70 + ContextManager.deactivate(ManagedContext.class, RequestScoped.class);
  71 + }
  72 +
  73 + @Test
  74 + public void testAddMessageWitSeverityWarn() {
  75 + ContextManager.activate(ManagedContext.class, RequestScoped.class);
  76 + Message message = new DefaultMessage("Menssage without param", SeverityType.WARN);
  77 + messageContext.add(message);
  78 + assertEquals(messageContext.getMessages().get(0).getSeverity(), SeverityType.WARN);
  79 + ContextManager.deactivate(ManagedContext.class, RequestScoped.class);
  80 + }
  81 +
  82 + @Test
  83 + public void testAddMessageWitSeverityErro() {
  84 + ContextManager.activate(ManagedContext.class, RequestScoped.class);
  85 + Message message = new DefaultMessage("Menssage without param", SeverityType.ERROR);
  86 + messageContext.add(message);
  87 + assertEquals(messageContext.getMessages().get(0).getSeverity(), SeverityType.ERROR);
  88 + ContextManager.deactivate(ManagedContext.class, RequestScoped.class);
  89 + }
  90 +
  91 + @Test
  92 + public void testCleanMessageContext() {
  93 + ContextManager.activate(ManagedContext.class, RequestScoped.class);
  94 + Message message = new DefaultMessage("Menssage without param");
  95 + messageContext.add(message);
  96 + assertEquals(messageContext.getMessages().size(), 1);
  97 + messageContext.clear();
  98 + assertEquals(messageContext.getMessages().size(), 0);
  99 + ContextManager.deactivate(ManagedContext.class, RequestScoped.class);
  100 + }
  101 +
  102 + @Test
  103 + public void testRecoverMessageWithParams() {
  104 + ContextManager.activate(ManagedContext.class, RequestScoped.class);
  105 + Message message = new DefaultMessage("Message with {0} param");
  106 + messageContext.add(message, 1);
  107 + assertTrue(messageContext.getMessages().get(0).getText().equals("Message with 1 param"));
  108 + ContextManager.deactivate(ManagedContext.class, RequestScoped.class);
  109 + }
  110 +
  111 + @Test
  112 + public void testMessageWithResourceBundle() {
  113 + bundleCustom = Beans.getReference(MessageWithResourceBundle.class);
  114 + String expected = "Mensagem sem parâmetro";
  115 + String value = bundleCustom.getBundle().getString("MESSAGE_WITHOUT_PARAMETER");
  116 + Assert.assertEquals(expected, value);
  117 + }
  118 +
  119 + @Test
  120 + public void testMessageParsedText(){
  121 + ContextManager.activate(ManagedContext.class, RequestScoped.class);
  122 + Message MESSAGE_PARSED = new DefaultMessage("{MESSAGE_PARSED}");
  123 + String expected = "Message parsed";
  124 + String value = MESSAGE_PARSED.getText();
  125 + Assert.assertEquals(expected, value);
  126 + ContextManager.deactivate(ManagedContext.class, RequestScoped.class);
  127 + }
  128 +
  129 + @Test
  130 + public void testMessageIsNull(){
  131 + ContextManager.activate(ManagedContext.class, RequestScoped.class);
  132 + Message NULL_MESSAGE = new DefaultMessage(null);
  133 + String expected = null;
  134 + String value = NULL_MESSAGE.getText();
  135 + Assert.assertEquals(expected, value);
  136 + ContextManager.deactivate(ManagedContext.class, RequestScoped.class);
  137 + }
  138 +
  139 +}
... ...
impl/core/src/test/java/message/MessageWithResourceBundle.java 0 → 100644
... ... @@ -0,0 +1,16 @@
  1 +package message;
  2 +
  3 +import javax.inject.Inject;
  4 +
  5 +import br.gov.frameworkdemoiselle.util.ResourceBundle;
  6 +
  7 +public class MessageWithResourceBundle{
  8 +
  9 + @Inject
  10 + private ResourceBundle bundle;
  11 +
  12 + public ResourceBundle getBundle() {
  13 + return bundle;
  14 + }
  15 +
  16 +}
... ...
impl/core/src/test/java/security/athentication/custom/CustomAuthenticatorTest.java
... ... @@ -42,6 +42,7 @@ import static org.junit.Assert.assertNotNull;
42 42 import static org.junit.Assert.assertNull;
43 43 import static org.junit.Assert.assertTrue;
44 44  
  45 +import javax.enterprise.context.RequestScoped;
45 46 import javax.inject.Inject;
46 47  
47 48 import org.jboss.arquillian.container.test.api.Deployment;
... ... @@ -51,6 +52,8 @@ import org.junit.Test;
51 52 import org.junit.runner.RunWith;
52 53  
53 54 import test.Tests;
  55 +import br.gov.frameworkdemoiselle.internal.context.ContextManager;
  56 +import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext;
54 57 import br.gov.frameworkdemoiselle.security.SecurityContext;
55 58 import configuration.resource.ConfigurationResourceTest;
56 59  
... ... @@ -79,17 +82,25 @@ public class CustomAuthenticatorTest {
79 82  
80 83 @Test
81 84 public void loginProcess() {
  85 + ContextManager.activate(ThreadLocalContext.class, RequestScoped.class);
  86 +
82 87 context.login();
83 88 assertTrue(context.isLoggedIn());
84 89 assertNotNull(observer.getEvent());
85 90 assertEquals("demoiselle", context.getCurrentUser().getName());
  91 +
  92 + ContextManager.deactivate(ThreadLocalContext.class, RequestScoped.class);
86 93 }
87 94  
88 95 @Test
89 96 public void logoutProcess() {
  97 + ContextManager.activate(ThreadLocalContext.class, RequestScoped.class);
  98 +
90 99 context.login();
91 100 context.logout();
92 101 assertFalse(context.isLoggedIn());
93 102 assertNull(context.getCurrentUser());
  103 +
  104 + ContextManager.deactivate(ThreadLocalContext.class, RequestScoped.class);
94 105 }
95 106 }
... ...
impl/core/src/test/resources/message/messages.properties 0 → 100644
... ... @@ -0,0 +1,2 @@
  1 +MESSAGE_WITHOUT_PARAMETER=Mensagem sem parâmetro
  2 +MESSAGE_PARSED=Message parsed
0 3 \ No newline at end of file
... ...