Commit 363c533e072fa7adf5be7e4947aface2410e1c11

Authored by Dancovich
1 parent 69773da3
Exists in master

Testes de módulo de gerenciamento.

impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/MonitoringManager.java
... ... @@ -63,7 +63,10 @@ public class MonitoringManager {
63 63 }
64 64  
65 65 /**
66   - * Invoke an operation over a managed type.
  66 + * <p>Invoke an operation over a managed type.</p>
  67 + *
  68 + * <p>This method is not thread-safe, it's the user's responsibility to
  69 + * make the operations of the managed type synchronized if necessary.</p>
67 70 *
68 71 * @param managedType
69 72 * A type annotated with {@link Managed}. This method will create
... ... @@ -81,7 +84,7 @@ public class MonitoringManager {
81 84 * In case the operation doesn't exist or have a different
82 85 * signature
83 86 */
84   - public synchronized Object invoke(ManagedType managedType, String actionName,
  87 + public Object invoke(ManagedType managedType, String actionName,
85 88 Object[] params) {
86 89 if ( managedTypes.contains(managedType) ) {
87 90 activateContexts(managedType.getType());
... ... @@ -112,14 +115,17 @@ public class MonitoringManager {
112 115 }
113 116  
114 117 /**
115   - * Retrieve the current value of a property from a managed type. Properties
116   - * are attributes annotated with {@link Property}.
  118 + * <p>Retrieve the current value of a property from a managed type. Properties
  119 + * are attributes annotated with {@link Property}.</p>
  120 + *
  121 + * <p>This method is not thread-safe, it's the user's responsibility to
  122 + * create the property's access methods from the managed type synchronized if necessary.</p>
117 123 *
118 124 * @param managedType The type that has the property the client wants to know the value of.
119 125 * @param propertyName The name of the property
120 126 * @return The current value of the property
121 127 */
122   - public synchronized Object getProperty(ManagedType managedType, String propertyName) {
  128 + public Object getProperty(ManagedType managedType, String propertyName) {
123 129  
124 130 if ( managedTypes.contains(managedType) ) {
125 131 Method getterMethod = managedType.getFields().get(propertyName).getGetterMethod();
... ... @@ -153,14 +159,17 @@ public class MonitoringManager {
153 159 }
154 160  
155 161 /**
156   - * Sets a new value for a property contained inside a managed type. A property
157   - * is an attribute annotated with {@link Property}.
  162 + * <p>Sets a new value for a property contained inside a managed type. A property
  163 + * is an attribute annotated with {@link Property}.</p>
  164 + *
  165 + * <p>This method is not thread-safe, it's the user's responsibility to
  166 + * create the property's access methods from the managed type synchronized if necessary.</p>
158 167 *
159 168 * @param managedType The type that has access to the property
160 169 * @param propertyName The name of the property
161 170 * @param newValue The new value of the property
162 171 */
163   - public synchronized void setProperty(ManagedType managedType, String propertyName,
  172 + public void setProperty(ManagedType managedType, String propertyName,
164 173 Object newValue) {
165 174  
166 175 if ( managedTypes.contains(managedType) ) {
... ...
impl/core/src/test/java/management/ManagedClassStore.java
... ... @@ -1,25 +0,0 @@
1   -package management;
2   -
3   -import java.util.ArrayList;
4   -import java.util.Collection;
5   -import java.util.List;
6   -
7   -import javax.enterprise.context.ApplicationScoped;
8   -
9   -import br.gov.frameworkdemoiselle.management.internal.ManagedType;
10   -
11   -@ApplicationScoped
12   -public class ManagedClassStore {
13   -
14   - private List<ManagedType> managedTypes = new ArrayList<ManagedType>();
15   -
16   -
17   - public List<ManagedType> getManagedTypes() {
18   - return managedTypes;
19   - }
20   -
21   - public void addManagedTypes(Collection<ManagedType> managedTypes){
22   - this.managedTypes.addAll(managedTypes);
23   - }
24   -
25   -}
impl/core/src/test/java/management/ManagementBootstrapTestCase.java
... ... @@ -6,6 +6,7 @@ import java.util.List;
6 6 import management.testclasses.DummyManagedClass;
7 7 import management.testclasses.DummyManagedClassPropertyError;
8 8 import management.testclasses.DummyManagementExtension;
  9 +import management.testclasses.ManagedClassStore;
9 10  
10 11 import org.jboss.arquillian.container.test.api.Deployer;
11 12 import org.jboss.arquillian.container.test.api.Deployment;
... ... @@ -44,7 +45,7 @@ public class ManagementBootstrapTestCase {
44 45 new File("src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension"),
45 46 "services/javax.enterprise.inject.spi.Extension")
46 47 .addPackages(false, ManagementBootstrapTestCase.class.getPackage())
47   - .addClasses(DummyManagementExtension.class,DummyManagedClass.class);
  48 + .addClasses(DummyManagementExtension.class,DummyManagedClass.class,ManagedClassStore.class);
48 49 }
49 50  
50 51 /**
... ... @@ -63,7 +64,7 @@ public class ManagementBootstrapTestCase {
63 64 new File("src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension"),
64 65 "services/javax.enterprise.inject.spi.Extension")
65 66 .addPackages(false, ManagementBootstrapTestCase.class.getPackage())
66   - .addClasses(DummyManagementExtension.class,DummyManagedClassPropertyError.class);
  67 + .addClasses(DummyManagementExtension.class,DummyManagedClassPropertyError.class,ManagedClassStore.class);
67 68 }
68 69  
69 70 /**
... ...
impl/core/src/test/java/management/ManagementTestCase.java 0 → 100644
... ... @@ -0,0 +1,128 @@
  1 +package management;
  2 +
  3 +import java.io.File;
  4 +
  5 +import junit.framework.Assert;
  6 +import management.testclasses.DummyManagedClass;
  7 +import management.testclasses.DummyManagementExtension;
  8 +import management.testclasses.ManagedClassStore;
  9 +
  10 +import org.jboss.arquillian.container.test.api.Deployment;
  11 +import org.jboss.arquillian.junit.Arquillian;
  12 +import org.jboss.shrinkwrap.api.ShrinkWrap;
  13 +import org.jboss.shrinkwrap.api.asset.FileAsset;
  14 +import org.jboss.shrinkwrap.api.spec.JavaArchive;
  15 +import org.junit.Test;
  16 +import org.junit.runner.RunWith;
  17 +
  18 +import test.LocaleProducer;
  19 +import br.gov.frameworkdemoiselle.DemoiselleException;
  20 +import br.gov.frameworkdemoiselle.util.Beans;
  21 +
  22 +/**
  23 + * Test case that simulates a management extension and tests if properties and operations on a managed class can be
  24 + * easily accessed and invoked.
  25 + *
  26 + * @author serpro
  27 + */
  28 +@RunWith(Arquillian.class)
  29 +public class ManagementTestCase {
  30 +
  31 + @Deployment
  32 + public static JavaArchive createMultithreadedDeployment() {
  33 + return ShrinkWrap
  34 + .create(JavaArchive.class)
  35 + .addClass(LocaleProducer.class)
  36 + .addPackages(true, "br")
  37 + .addAsResource(new FileAsset(new File("src/test/resources/test/beans.xml")), "beans.xml")
  38 + .addAsManifestResource(
  39 + new File("src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension"),
  40 + "services/javax.enterprise.inject.spi.Extension")
  41 + .addPackages(false, ManagementTestCase.class.getPackage())
  42 + .addClasses(DummyManagementExtension.class, DummyManagedClass.class, ManagedClassStore.class);
  43 + }
  44 +
  45 + @Test
  46 + public void testReadProperty() {
  47 + DummyManagedClass managedClass = Beans.getReference(DummyManagedClass.class);
  48 + managedClass.setName("Test Name");
  49 +
  50 + // store é nossa extensão de gerenciamento falsa, então estamos testando um "cliente" acessando
  51 + // nosso tipo gerenciado DummyManagedClass remotamente.
  52 + ManagedClassStore store = Beans.getReference(ManagedClassStore.class);
  53 + Object name = store.getProperty(DummyManagedClass.class, "name");
  54 + Assert.assertEquals("Test Name", name);
  55 + }
  56 +
  57 + @Test
  58 + public void testWriteProperty() {
  59 + // store é nossa extensão de gerenciamento falsa, então estamos testando um "cliente" definindo
  60 + // um novo valor em uma propriedade de nosso tipo gerenciado DummyManagedClass remotamente.
  61 + ManagedClassStore store = Beans.getReference(ManagedClassStore.class);
  62 + store.setProperty(DummyManagedClass.class, "name", "Test Name");
  63 +
  64 + DummyManagedClass managedClass = Beans.getReference(DummyManagedClass.class);
  65 + Assert.assertEquals("Test Name", managedClass.getName());
  66 + }
  67 +
  68 + @Test
  69 + public void testReadAWriteOnly() {
  70 +
  71 + ManagedClassStore store = Beans.getReference(ManagedClassStore.class);
  72 +
  73 + try {
  74 + store.getProperty(DummyManagedClass.class, "writeOnlyProperty");
  75 + Assert.fail();
  76 + } catch (DemoiselleException de) {
  77 + // SUCCESS
  78 + }
  79 +
  80 + }
  81 +
  82 + @Test
  83 + public void testWriteAReadOnly() {
  84 +
  85 + ManagedClassStore store = Beans.getReference(ManagedClassStore.class);
  86 +
  87 + try {
  88 + store.setProperty(DummyManagedClass.class, "readOnlyProperty", "New Value");
  89 + Assert.fail();
  90 + } catch (DemoiselleException de) {
  91 + // SUCCESS
  92 + }
  93 +
  94 + }
  95 +
  96 + @Test
  97 + public void testInvokeOperation() {
  98 +
  99 + ManagedClassStore store = Beans.getReference(ManagedClassStore.class);
  100 +
  101 + try {
  102 + store.setProperty(DummyManagedClass.class, "firstFactor", new Integer(10));
  103 + store.setProperty(DummyManagedClass.class, "secondFactor", new Integer(15));
  104 + Integer response = (Integer) store.invoke(DummyManagedClass.class, "sumFactors");
  105 + Assert.assertEquals(new Integer(25), response);
  106 + } catch (DemoiselleException de) {
  107 + Assert.fail(de.getMessage());
  108 + }
  109 +
  110 + }
  111 +
  112 + @Test
  113 + public void testInvokeNonAnnotatedOperation() {
  114 +
  115 + ManagedClassStore store = Beans.getReference(ManagedClassStore.class);
  116 +
  117 + try {
  118 + // O método "nonOperationAnnotatedMethod" existe na classe DummyManagedClass, mas não está anotado como
  119 + // "@Operation", então
  120 + // ela não pode ser exposta para extensões.
  121 + store.invoke(DummyManagedClass.class, "nonOperationAnnotatedMethod");
  122 + Assert.fail();
  123 + } catch (DemoiselleException de) {
  124 + // SUCCESS
  125 + }
  126 +
  127 + }
  128 +}
... ...
impl/core/src/test/java/management/NotificationTestCase.java
... ... @@ -53,7 +53,7 @@ public class NotificationTestCase {
53 53 .addAsManifestResource(
54 54 new File("src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension"),
55 55 "services/javax.enterprise.inject.spi.Extension")
56   - .addPackages(false, ManagementBootstrapTestCase.class.getPackage())
  56 + .addPackages(false, NotificationTestCase.class.getPackage())
57 57 .addClasses(DummyNotificationListener.class,DummyManagedClass.class);
58 58 }
59 59  
... ...
impl/core/src/test/java/management/testclasses/DummyManagedClass.java
... ... @@ -12,15 +12,24 @@ import br.gov.frameworkdemoiselle.management.annotation.validation.AllowedValues
12 12 public class DummyManagedClass {
13 13  
14 14 @Property
  15 + private String name;
  16 +
  17 + @Property
15 18 @AllowedValues(allows={"f","m","F","M"},valueType=ValueType.INTEGER)
16 19 private Integer id;
17 20  
18 21 @Property
  22 + private Integer firstFactor , secondFactor;
  23 +
  24 + @Property
19 25 private String uuid;
20 26  
21 27 @Property
22 28 private String writeOnlyProperty;
23 29  
  30 + @Property
  31 + private String readOnlyProperty = "Default Value";
  32 +
24 33 /**
25 34 * Propriedade para testar detecção de métodos GET e SET quando propriedade tem apenas uma letra.
26 35 */
... ... @@ -71,4 +80,87 @@ public class DummyManagedClass {
71 80 this.uuid = UUID.randomUUID().toString();
72 81 return this.uuid;
73 82 }
  83 +
  84 +
  85 + public String getName() {
  86 + return name;
  87 + }
  88 +
  89 +
  90 + public void setName(String name) {
  91 + this.name = name;
  92 + }
  93 +
  94 +
  95 + public String getReadOnlyProperty() {
  96 + return readOnlyProperty;
  97 + }
  98 +
  99 +
  100 + public Integer getFirstFactor() {
  101 + return firstFactor;
  102 + }
  103 +
  104 +
  105 + public void setFirstFactor(Integer firstFactor) {
  106 + this.firstFactor = firstFactor;
  107 + }
  108 +
  109 +
  110 + public Integer getSecondFactor() {
  111 + return secondFactor;
  112 + }
  113 +
  114 +
  115 + public void setSecondFactor(Integer secondFactor) {
  116 + this.secondFactor = secondFactor;
  117 + }
  118 +
  119 + @Operation
  120 + public Integer calculateFactorsNonSynchronized(Integer firstFactor , Integer secondFactor){
  121 + setFirstFactor(firstFactor);
  122 + setSecondFactor(secondFactor);
  123 +
  124 + try {
  125 + int temp = firstFactor + secondFactor;
  126 + Thread.sleep( (long)(Math.random() * 100));
  127 +
  128 + temp = temp + firstFactor;
  129 + Thread.sleep( (long)(Math.random() * 100));
  130 +
  131 + temp = temp + secondFactor;
  132 + Thread.sleep( (long)(Math.random() * 100));
  133 +
  134 + return temp;
  135 + } catch (InterruptedException e) {
  136 + throw new RuntimeException(e);
  137 + }
  138 + }
  139 +
  140 + @Operation
  141 + public synchronized Integer calculateFactorsSynchronized(Integer firstFactor , Integer secondFactor){
  142 + setFirstFactor(firstFactor);
  143 + setSecondFactor(secondFactor);
  144 +
  145 + try {
  146 + int temp = firstFactor + secondFactor;
  147 + Thread.sleep( (long)(Math.random() * 100));
  148 +
  149 + temp = temp + firstFactor;
  150 + Thread.sleep( (long)(Math.random() * 100));
  151 +
  152 + temp = temp + secondFactor;
  153 + Thread.sleep( (long)(Math.random() * 100));
  154 +
  155 + return temp;
  156 + } catch (InterruptedException e) {
  157 + throw new RuntimeException(e);
  158 + }
  159 + }
  160 +
  161 + public void nonOperationAnnotatedMethod(){
  162 + System.out.println("Test");
  163 + }
  164 +
  165 +
74 166 }
... ...
impl/core/src/test/java/management/testclasses/DummyManagementExtension.java
... ... @@ -4,7 +4,6 @@ import java.util.List;
4 4  
5 5 import javax.inject.Inject;
6 6  
7   -import management.ManagedClassStore;
8 7  
9 8 import br.gov.frameworkdemoiselle.management.extension.ManagementExtension;
10 9 import br.gov.frameworkdemoiselle.management.internal.ManagedType;
... ...
impl/core/src/test/java/management/testclasses/ManagedClassStore.java 0 → 100644
... ... @@ -0,0 +1,66 @@
  1 +package management.testclasses;
  2 +
  3 +import java.util.ArrayList;
  4 +import java.util.Collection;
  5 +import java.util.List;
  6 +
  7 +import javax.enterprise.context.ApplicationScoped;
  8 +
  9 +import br.gov.frameworkdemoiselle.management.internal.ManagedType;
  10 +import br.gov.frameworkdemoiselle.management.internal.MonitoringManager;
  11 +import br.gov.frameworkdemoiselle.util.Beans;
  12 +
  13 +/**
  14 + * Dummy class that stores managed types detected by the management bootstrap
  15 + * and can read/write properties and invoke operations on them, simulating a management
  16 + * extension like JMX or SNMP.
  17 + *
  18 + * @author serpro
  19 + *
  20 + */
  21 +@ApplicationScoped
  22 +public class ManagedClassStore {
  23 +
  24 + private List<ManagedType> managedTypes = new ArrayList<ManagedType>();
  25 +
  26 +
  27 + public List<ManagedType> getManagedTypes() {
  28 + return managedTypes;
  29 + }
  30 +
  31 + public void addManagedTypes(Collection<ManagedType> managedTypes){
  32 + this.managedTypes.addAll(managedTypes);
  33 + }
  34 +
  35 + public void setProperty(Class<?> managedClass , String attributeName , Object newValue){
  36 + MonitoringManager manager = Beans.getReference(MonitoringManager.class);
  37 + for (ManagedType type : manager.getManagedTypes()){
  38 + if (type.getType().equals(managedClass)){
  39 + manager.setProperty(type, attributeName, newValue);
  40 + break;
  41 + }
  42 + }
  43 + }
  44 +
  45 + public Object getProperty(Class<?> managedClass , String attributeName ){
  46 + MonitoringManager manager = Beans.getReference(MonitoringManager.class);
  47 + for (ManagedType type : manager.getManagedTypes()){
  48 + if (type.getType().equals(managedClass)){
  49 + return manager.getProperty(type, attributeName);
  50 + }
  51 + }
  52 +
  53 + return null;
  54 + }
  55 +
  56 + public Object invoke(Class<?> managedClass , String operation , Object... params){
  57 + MonitoringManager manager = Beans.getReference(MonitoringManager.class);
  58 + for (ManagedType type : manager.getManagedTypes()){
  59 + if (type.getType().equals(managedClass)){
  60 + return manager.invoke(type, operation, params);
  61 + }
  62 + }
  63 +
  64 + return null;
  65 + }
  66 +}
... ...