Commit d9a64def234f074a25071b9917dc898f2e646828
Exists in
master
Merge branch '2.3' of https://github.com/demoiselle/framework.git into 2.3
Showing
19 changed files
with
1772 additions
and
87 deletions
Show diff stats
documentation/reference/pt-BR/authorgroup.xml
@@ -38,4 +38,8 @@ | @@ -38,4 +38,8 @@ | ||
38 | <firstname>Emerson</firstname> | 38 | <firstname>Emerson</firstname> |
39 | <surname>Oliveira</surname> | 39 | <surname>Oliveira</surname> |
40 | </author> | 40 | </author> |
41 | + <author> | ||
42 | + <firstname>Luciano</firstname> | ||
43 | + <surname>Borges</surname> | ||
44 | + </author> | ||
41 | </authorgroup> | 45 | </authorgroup> |
documentation/reference/pt-BR/configuracao.xml
@@ -47,6 +47,25 @@ | @@ -47,6 +47,25 @@ | ||
47 | </section> | 47 | </section> |
48 | 48 | ||
49 | <section> | 49 | <section> |
50 | + <title>Configurando</title> | ||
51 | + <para> | ||
52 | + Para um correto funcionamento do Demoiselle é necessário inserir o interceptador de configuração no arquivo <filename>src/main/WEB-INF/beans.xml</filename>. | ||
53 | + </para> | ||
54 | + <programlisting role="XML"> | ||
55 | + <![CDATA[ | ||
56 | + <beans xmlns="http://java.sun.com/xml/ns/javaee" | ||
57 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
58 | + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee | ||
59 | + http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> | ||
60 | + <interceptors> | ||
61 | + <class>br.gov.frameworkdemoiselle.internal.interceptor.ConfigurationInterceptor</class> | ||
62 | + </interceptors> | ||
63 | + </beans> | ||
64 | + ]]> | ||
65 | + </programlisting> | ||
66 | + </section> | ||
67 | + | ||
68 | + <section> | ||
50 | <title>As classes de configuração</title> | 69 | <title>As classes de configuração</title> |
51 | <para> | 70 | <para> |
52 | A primeira etapa para a utilização do mecanismo de configuração em uma aplicação consiste em criar uma classe específica | 71 | A primeira etapa para a utilização do mecanismo de configuração em uma aplicação consiste em criar uma classe específica |
documentation/reference/pt-BR/excecao.xml
@@ -11,6 +11,25 @@ | @@ -11,6 +11,25 @@ | ||
11 | Oferecemos à você uma alternativa para resolver estes problemas, mas você estará livre para usá-la: isoladamente, | 11 | Oferecemos à você uma alternativa para resolver estes problemas, mas você estará livre para usá-la: isoladamente, |
12 | misturando com a forma verbosa ou até mesmo não usá-la. | 12 | misturando com a forma verbosa ou até mesmo não usá-la. |
13 | </para> | 13 | </para> |
14 | + | ||
15 | + <section> | ||
16 | + <title>Configurando</title> | ||
17 | + <para> | ||
18 | + Para um correto funcionamento do Demoiselle é necessário inserir o interceptador de exceção no arquivo <filename>src/main/WEB-INF/beans.xml</filename>. | ||
19 | + </para> | ||
20 | + <programlisting role="XML"> | ||
21 | + <![CDATA[ | ||
22 | + <beans xmlns="http://java.sun.com/xml/ns/javaee" | ||
23 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
24 | + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee | ||
25 | + http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> | ||
26 | + <interceptors> | ||
27 | + <class>br.gov.frameworkdemoiselle.internal.interceptor.ExceptionHandlerInterceptor</class> | ||
28 | + </interceptors> | ||
29 | + </beans> | ||
30 | + ]]> | ||
31 | + </programlisting> | ||
32 | + </section> | ||
14 | 33 | ||
15 | <section> | 34 | <section> |
16 | <title>Tratadores de exceção</title> | 35 | <title>Tratadores de exceção</title> |
documentation/reference/pt-BR/security.xml
@@ -18,6 +18,26 @@ | @@ -18,6 +18,26 @@ | ||
18 | </para> | 18 | </para> |
19 | 19 | ||
20 | <section> | 20 | <section> |
21 | + <title>Configurando</title> | ||
22 | + <para> | ||
23 | + Para um correto funcionamento do Demoiselle é necessário inserir od interceptadores de segurança no arquivo <filename>src/main/WEB-INF/beans.xml</filename>. | ||
24 | + </para> | ||
25 | + <programlisting role="XML"> | ||
26 | + <![CDATA[ | ||
27 | + <beans xmlns="http://java.sun.com/xml/ns/javaee" | ||
28 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
29 | + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee | ||
30 | + http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> | ||
31 | + <interceptors> | ||
32 | + <class>br.gov.frameworkdemoiselle.internal.interceptor.RequiredPermissionInterceptor</class> | ||
33 | + <class>br.gov.frameworkdemoiselle.internal.interceptor.RequiredRoleInterceptor</class> | ||
34 | + </interceptors> | ||
35 | + </beans> | ||
36 | + ]]> | ||
37 | + </programlisting> | ||
38 | + </section> | ||
39 | + | ||
40 | + <section> | ||
21 | <title>Autenticação</title> | 41 | <title>Autenticação</title> |
22 | <para> | 42 | <para> |
23 | O mecanismo de autenticação busca verificar a identidade do usuário de um sistema. A forma mais conhecida, e comum, | 43 | O mecanismo de autenticação busca verificar a identidade do usuário de um sistema. A forma mais conhecida, e comum, |
@@ -201,20 +221,12 @@ public class MeuAuthorizer implements Authorizer { | @@ -201,20 +221,12 @@ public class MeuAuthorizer implements Authorizer { | ||
201 | 221 | ||
202 | }]]></programlisting> | 222 | }]]></programlisting> |
203 | <para> | 223 | <para> |
204 | - Feito isso deve-se definir no arquivo META-INF/beans.xml, as classes criadas: | 224 | + Feito isso deve-se definir no arquivo <filename>demoiselle.properties</filename>, as classes criadas: |
205 | </para> | 225 | </para> |
206 | - <programlisting role="XML"><![CDATA[<?xml version="1.0"?> | ||
207 | -<beans xmlns="http://java.sun.com/xml/ns/javaee" | ||
208 | - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
209 | - xsi:schemaLocation="http://java.sun.com/xml/ns/javaee | ||
210 | - http://java.sun.com/xml/ns/javaee/beans_1_1.xsd"> | ||
211 | - | ||
212 | - <alternatives> | ||
213 | - <class>projeto.MeuAuthenticator</class> | ||
214 | - <class>projeto.MeuAuthorizer</class> | ||
215 | - </alternatives> | ||
216 | - | ||
217 | -</beans>]]></programlisting> | 226 | + <programlisting> |
227 | + frameworkdemoiselle.security.authenticator.class=projeto.MeuAuthenticator | ||
228 | + frameworkdemoiselle.security.authorizer.class=projeto.MeuAuthorizer | ||
229 | + </programlisting> | ||
218 | <para> | 230 | <para> |
219 | À partir desse momento, a aplicação já possui uma implementação de segurança definida. | 231 | À partir desse momento, a aplicação já possui uma implementação de segurança definida. |
220 | </para> | 232 | </para> |
documentation/reference/pt-BR/transacao.xml
@@ -22,6 +22,25 @@ | @@ -22,6 +22,25 @@ | ||
22 | Neste capítulo apresentaremos para você como usar a nossa solução de controle de transação, as estratégias | 22 | Neste capítulo apresentaremos para você como usar a nossa solução de controle de transação, as estratégias |
23 | prontas que oferecemos e a criação de sua própria estratégia. | 23 | prontas que oferecemos e a criação de sua própria estratégia. |
24 | </para> | 24 | </para> |
25 | + | ||
26 | + <section> | ||
27 | + <title>Configurando</title> | ||
28 | + <para> | ||
29 | + Para um correto funcionamento do Demoiselle é necessário inserir o interceptador de transação no arquivo <filename>src/main/WEB-INF/beans.xml</filename>. | ||
30 | + </para> | ||
31 | + <programlisting role="XML"> | ||
32 | + <![CDATA[ | ||
33 | + <beans xmlns="http://java.sun.com/xml/ns/javaee" | ||
34 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
35 | + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee | ||
36 | + http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> | ||
37 | + <interceptors> | ||
38 | + <class>br.gov.frameworkdemoiselle.internal.interceptor.TransactionalInterceptor</class> | ||
39 | + </interceptors> | ||
40 | + </beans> | ||
41 | + ]]> | ||
42 | + </programlisting> | ||
43 | + </section> | ||
25 | 44 | ||
26 | <section> | 45 | <section> |
27 | <title>Métodos transacionais</title> | 46 | <title>Métodos transacionais</title> |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrap.java
@@ -55,6 +55,7 @@ import javax.enterprise.inject.spi.ProcessAnnotatedType; | @@ -55,6 +55,7 @@ import javax.enterprise.inject.spi.ProcessAnnotatedType; | ||
55 | import br.gov.frameworkdemoiselle.DemoiselleException; | 55 | import br.gov.frameworkdemoiselle.DemoiselleException; |
56 | import br.gov.frameworkdemoiselle.annotation.Shutdown; | 56 | import br.gov.frameworkdemoiselle.annotation.Shutdown; |
57 | import br.gov.frameworkdemoiselle.annotation.ViewScoped; | 57 | import br.gov.frameworkdemoiselle.annotation.ViewScoped; |
58 | +import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; | ||
58 | import br.gov.frameworkdemoiselle.internal.context.CustomContext; | 59 | import br.gov.frameworkdemoiselle.internal.context.CustomContext; |
59 | import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; | 60 | import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; |
60 | import br.gov.frameworkdemoiselle.internal.processor.ShutdownProcessor; | 61 | import br.gov.frameworkdemoiselle.internal.processor.ShutdownProcessor; |
@@ -109,6 +110,8 @@ public class ShutdownBootstrap extends AbstractBootstrap { | @@ -109,6 +110,8 @@ public class ShutdownBootstrap extends AbstractBootstrap { | ||
109 | shutdown(true); | 110 | shutdown(true); |
110 | } | 111 | } |
111 | 112 | ||
113 | + private static boolean x = true; | ||
114 | + | ||
112 | /** | 115 | /** |
113 | * Before Shutdown it execute the methods annotateds with @Shutdown considering the priority order; | 116 | * Before Shutdown it execute the methods annotateds with @Shutdown considering the priority order; |
114 | */ | 117 | */ |
@@ -120,18 +123,28 @@ public class ShutdownBootstrap extends AbstractBootstrap { | @@ -120,18 +123,28 @@ public class ShutdownBootstrap extends AbstractBootstrap { | ||
120 | Collections.sort(processors); | 123 | Collections.sort(processors); |
121 | Throwable failure = null; | 124 | Throwable failure = null; |
122 | 125 | ||
123 | - for (CustomContext tempContext : tempContexts) { | ||
124 | - addContext(tempContext, abdEvent); | 126 | + if (x) { |
127 | + for (CustomContext tempContext : tempContexts) { | ||
128 | + addContext(tempContext, abdEvent); | ||
129 | + } | ||
130 | + | ||
131 | + x = false; | ||
125 | } | 132 | } |
126 | 133 | ||
127 | for (Iterator<ShutdownProcessor> iter = processors.iterator(); iter.hasNext();) { | 134 | for (Iterator<ShutdownProcessor> iter = processors.iterator(); iter.hasNext();) { |
128 | ShutdownProcessor processor = iter.next(); | 135 | ShutdownProcessor processor = iter.next(); |
129 | 136 | ||
130 | try { | 137 | try { |
131 | - processor.process(); | 138 | + ClassLoader classLoader = ConfigurationLoader.getClassLoaderForClass(processor.getAnnotatedMethod() |
139 | + .getDeclaringType().getJavaClass().getCanonicalName()); | ||
132 | 140 | ||
133 | - if (remove) { | ||
134 | - iter.remove(); | 141 | + if (Thread.currentThread().getContextClassLoader().equals(classLoader)) { |
142 | + | ||
143 | + processor.process(); | ||
144 | + | ||
145 | + if (remove) { | ||
146 | + iter.remove(); | ||
147 | + } | ||
135 | } | 148 | } |
136 | 149 | ||
137 | } catch (Throwable cause) { | 150 | } catch (Throwable cause) { |
@@ -139,7 +152,9 @@ public class ShutdownBootstrap extends AbstractBootstrap { | @@ -139,7 +152,9 @@ public class ShutdownBootstrap extends AbstractBootstrap { | ||
139 | } | 152 | } |
140 | } | 153 | } |
141 | 154 | ||
142 | - unloadTempContexts(); | 155 | + if (processors.isEmpty()) { |
156 | + unloadTempContexts(); | ||
157 | + } | ||
143 | 158 | ||
144 | if (failure != null) { | 159 | if (failure != null) { |
145 | throw new DemoiselleException(failure); | 160 | throw new DemoiselleException(failure); |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrap.java
@@ -56,6 +56,7 @@ import br.gov.frameworkdemoiselle.DemoiselleException; | @@ -56,6 +56,7 @@ import br.gov.frameworkdemoiselle.DemoiselleException; | ||
56 | import br.gov.frameworkdemoiselle.annotation.Startup; | 56 | import br.gov.frameworkdemoiselle.annotation.Startup; |
57 | import br.gov.frameworkdemoiselle.annotation.ViewScoped; | 57 | import br.gov.frameworkdemoiselle.annotation.ViewScoped; |
58 | import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; | 58 | import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; |
59 | +import br.gov.frameworkdemoiselle.internal.context.CustomContext; | ||
59 | import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; | 60 | import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; |
60 | import br.gov.frameworkdemoiselle.internal.processor.StartupProcessor; | 61 | import br.gov.frameworkdemoiselle.internal.processor.StartupProcessor; |
61 | 62 | ||
@@ -66,12 +67,14 @@ public class StartupBootstrap extends AbstractBootstrap { | @@ -66,12 +67,14 @@ public class StartupBootstrap extends AbstractBootstrap { | ||
66 | 67 | ||
67 | private static final Class<? extends Annotation> annotationClass = Startup.class; | 68 | private static final Class<? extends Annotation> annotationClass = Startup.class; |
68 | 69 | ||
69 | - private static final List<ThreadLocalContext> tempContexts = new ArrayList<ThreadLocalContext>(); | 70 | + private static final List<CustomContext> tempContexts = new ArrayList<CustomContext>(); |
70 | 71 | ||
71 | @SuppressWarnings("rawtypes") | 72 | @SuppressWarnings("rawtypes") |
72 | private static final List<StartupProcessor> processors = Collections | 73 | private static final List<StartupProcessor> processors = Collections |
73 | .synchronizedList(new ArrayList<StartupProcessor>()); | 74 | .synchronizedList(new ArrayList<StartupProcessor>()); |
74 | 75 | ||
76 | + private static AfterBeanDiscovery abdEvent; | ||
77 | + | ||
75 | /** | 78 | /** |
76 | * Observes all methods annotated with @Startup and create an instance of StartupAction for them | 79 | * Observes all methods annotated with @Startup and create an instance of StartupAction for them |
77 | * | 80 | * |
@@ -96,10 +99,7 @@ public class StartupBootstrap extends AbstractBootstrap { | @@ -96,10 +99,7 @@ public class StartupBootstrap extends AbstractBootstrap { | ||
96 | tempContexts.add(new ThreadLocalContext(SessionScoped.class)); | 99 | tempContexts.add(new ThreadLocalContext(SessionScoped.class)); |
97 | tempContexts.add(new ThreadLocalContext(ConversationScoped.class)); | 100 | tempContexts.add(new ThreadLocalContext(ConversationScoped.class)); |
98 | tempContexts.add(new ThreadLocalContext(RequestScoped.class)); | 101 | tempContexts.add(new ThreadLocalContext(RequestScoped.class)); |
99 | - | ||
100 | - for (ThreadLocalContext tempContext : tempContexts) { | ||
101 | - addContext(tempContext, event); | ||
102 | - } | 102 | + abdEvent = event; |
103 | } | 103 | } |
104 | 104 | ||
105 | /** | 105 | /** |
@@ -109,6 +109,8 @@ public class StartupBootstrap extends AbstractBootstrap { | @@ -109,6 +109,8 @@ public class StartupBootstrap extends AbstractBootstrap { | ||
109 | startup(true); | 109 | startup(true); |
110 | } | 110 | } |
111 | 111 | ||
112 | + private static boolean x = true; | ||
113 | + | ||
112 | /** | 114 | /** |
113 | * After the deployment validation it execute the methods annotateds with @Startup considering the priority order; | 115 | * After the deployment validation it execute the methods annotateds with @Startup considering the priority order; |
114 | */ | 116 | */ |
@@ -120,6 +122,14 @@ public class StartupBootstrap extends AbstractBootstrap { | @@ -120,6 +122,14 @@ public class StartupBootstrap extends AbstractBootstrap { | ||
120 | Collections.sort(processors); | 122 | Collections.sort(processors); |
121 | Throwable failure = null; | 123 | Throwable failure = null; |
122 | 124 | ||
125 | + if (x) { | ||
126 | + for (CustomContext tempContext : tempContexts) { | ||
127 | + addContext(tempContext, abdEvent); | ||
128 | + } | ||
129 | + | ||
130 | + x = false; | ||
131 | + } | ||
132 | + | ||
123 | for (Iterator<StartupProcessor> iter = processors.iterator(); iter.hasNext();) { | 133 | for (Iterator<StartupProcessor> iter = processors.iterator(); iter.hasNext();) { |
124 | StartupProcessor processor = iter.next(); | 134 | StartupProcessor processor = iter.next(); |
125 | 135 | ||
@@ -140,7 +150,7 @@ public class StartupBootstrap extends AbstractBootstrap { | @@ -140,7 +150,7 @@ public class StartupBootstrap extends AbstractBootstrap { | ||
140 | } | 150 | } |
141 | } | 151 | } |
142 | 152 | ||
143 | - if (processors.isEmpty()){ | 153 | + if (processors.isEmpty()) { |
144 | unloadTempContexts(); | 154 | unloadTempContexts(); |
145 | } | 155 | } |
146 | 156 | ||
@@ -150,7 +160,7 @@ public class StartupBootstrap extends AbstractBootstrap { | @@ -150,7 +160,7 @@ public class StartupBootstrap extends AbstractBootstrap { | ||
150 | } | 160 | } |
151 | 161 | ||
152 | private static void unloadTempContexts() { | 162 | private static void unloadTempContexts() { |
153 | - for (ThreadLocalContext tempContext : tempContexts) { | 163 | + for (CustomContext tempContext : tempContexts) { |
154 | disableContext(tempContext); | 164 | disableContext(tempContext); |
155 | } | 165 | } |
156 | } | 166 | } |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoader.java
@@ -40,6 +40,8 @@ import java.io.FileNotFoundException; | @@ -40,6 +40,8 @@ import java.io.FileNotFoundException; | ||
40 | import java.io.Serializable; | 40 | import java.io.Serializable; |
41 | import java.lang.reflect.Field; | 41 | import java.lang.reflect.Field; |
42 | import java.lang.reflect.Method; | 42 | import java.lang.reflect.Method; |
43 | +import java.lang.reflect.ParameterizedType; | ||
44 | +import java.lang.reflect.Type; | ||
43 | import java.net.URL; | 45 | import java.net.URL; |
44 | import java.util.HashSet; | 46 | import java.util.HashSet; |
45 | import java.util.Iterator; | 47 | import java.util.Iterator; |
@@ -49,8 +51,10 @@ import java.util.Set; | @@ -49,8 +51,10 @@ import java.util.Set; | ||
49 | import javax.inject.Inject; | 51 | import javax.inject.Inject; |
50 | import javax.validation.constraints.NotNull; | 52 | import javax.validation.constraints.NotNull; |
51 | 53 | ||
54 | +import org.apache.commons.configuration.DataConfiguration; | ||
52 | import org.apache.commons.configuration.PropertiesConfiguration; | 55 | import org.apache.commons.configuration.PropertiesConfiguration; |
53 | import org.apache.commons.configuration.SystemConfiguration; | 56 | import org.apache.commons.configuration.SystemConfiguration; |
57 | +import org.apache.commons.configuration.XMLConfiguration; | ||
54 | import org.slf4j.Logger; | 58 | import org.slf4j.Logger; |
55 | 59 | ||
56 | import br.gov.frameworkdemoiselle.annotation.Ignore; | 60 | import br.gov.frameworkdemoiselle.annotation.Ignore; |
@@ -109,7 +113,7 @@ public class ConfigurationLoader implements Serializable { | @@ -109,7 +113,7 @@ public class ConfigurationLoader implements Serializable { | ||
109 | org.apache.commons.configuration.Configuration config = getConfiguration(resource, type); | 113 | org.apache.commons.configuration.Configuration config = getConfiguration(resource, type); |
110 | 114 | ||
111 | String key = getKey(field, clazz, config); | 115 | String key = getKey(field, clazz, config); |
112 | - Object value = getValue(key, field.getType(), config); | 116 | + Object value = getValue(key, field, config); |
113 | 117 | ||
114 | validate(field, key, value, resource); | 118 | validate(field, key, value, resource); |
115 | setValue(field, key, object, value); | 119 | setValue(field, key, object, value); |
@@ -218,12 +222,12 @@ public class ConfigurationLoader implements Serializable { | @@ -218,12 +222,12 @@ public class ConfigurationLoader implements Serializable { | ||
218 | 222 | ||
219 | case PROPERTIES: | 223 | case PROPERTIES: |
220 | url = getResourceAsURL(resource + ".properties"); | 224 | url = getResourceAsURL(resource + ".properties"); |
221 | - config = new PropertiesConfiguration(url); | 225 | + config = new DataConfiguration(new PropertiesConfiguration(url)); |
222 | break; | 226 | break; |
223 | 227 | ||
224 | case XML: | 228 | case XML: |
225 | url = getResourceAsURL(resource + ".xml"); | 229 | url = getResourceAsURL(resource + ".xml"); |
226 | - config = new PropertiesConfiguration(url); | 230 | + config = new DataConfiguration(new XMLConfiguration(url)); |
227 | break; | 231 | break; |
228 | 232 | ||
229 | default: | 233 | default: |
@@ -239,41 +243,67 @@ public class ConfigurationLoader implements Serializable { | @@ -239,41 +243,67 @@ public class ConfigurationLoader implements Serializable { | ||
239 | return config; | 243 | return config; |
240 | } | 244 | } |
241 | 245 | ||
242 | - /** | ||
243 | - * Returns the value associated with the given configuration class and field type. | ||
244 | - * | ||
245 | - * @param name | ||
246 | - * @param config | ||
247 | - * @param fieldClass | ||
248 | - * @return the value | ||
249 | - */ | ||
250 | @SuppressWarnings("unchecked") | 246 | @SuppressWarnings("unchecked") |
251 | - private <T> T getValue(String key, Class<T> fieldClass, org.apache.commons.configuration.Configuration config) { | 247 | + private <T> T getValue(String key, Field field, org.apache.commons.configuration.Configuration config) { |
252 | Object value; | 248 | Object value; |
253 | 249 | ||
254 | - if (fieldClass.isArray() && fieldClass.getComponentType().equals(String.class)) { | ||
255 | - value = config.getStringArray(key); | ||
256 | - | 250 | + Class<?> fieldClass = (Class<?>) field.getType(); |
251 | + | ||
252 | + if (fieldClass.isArray()) { | ||
253 | + value = getArray(key, field, config); | ||
257 | } else if (fieldClass.equals(Properties.class)) { | 254 | } else if (fieldClass.equals(Properties.class)) { |
258 | value = getProperty(key, config); | 255 | value = getProperty(key, config); |
259 | 256 | ||
260 | } else { | 257 | } else { |
261 | - value = getBasic(key, fieldClass, config); | 258 | + value = getBasic(key, field, config); |
262 | } | 259 | } |
263 | 260 | ||
264 | return (T) value; | 261 | return (T) value; |
265 | } | 262 | } |
263 | + | ||
264 | + private <T> Object getArray(String key, Field field, org.apache.commons.configuration.Configuration config) { | ||
265 | + Object value = null; | ||
266 | + | ||
267 | + Class<?> fieldClass = (Class<?>) field.getType(); | ||
268 | + | ||
269 | + try { | ||
270 | + Method method; | ||
271 | + | ||
272 | + String methodName = "get"; | ||
273 | + | ||
274 | + methodName += Strings.firstToUpper(fieldClass.getSimpleName()); | ||
275 | + methodName = Strings.removeChars(methodName, '[', ']'); | ||
276 | + | ||
277 | + methodName += "Array"; | ||
278 | + | ||
279 | + method = config.getClass().getMethod(methodName, String.class); | ||
280 | + value = method.invoke(config, key); | ||
281 | + | ||
282 | + } catch (Throwable cause) { | ||
283 | + throw new ConfigurationException(bundle.getString("error-converting-to-type", fieldClass.getName()), cause); | ||
284 | + } | ||
285 | + | ||
286 | + return value; | ||
287 | + } | ||
266 | 288 | ||
267 | - private <T> Object getBasic(String key, Class<T> fieldClass, org.apache.commons.configuration.Configuration config) { | 289 | + private <T> Object getBasic(String key, Field field, org.apache.commons.configuration.Configuration config) { |
268 | Object value = null; | 290 | Object value = null; |
269 | 291 | ||
292 | + Class<?> fieldClass = (Class<?>) field.getType(); | ||
293 | + | ||
270 | try { | 294 | try { |
271 | Method method; | 295 | Method method; |
272 | - String methodName = "get" + Strings.firstToUpper(fieldClass.getSimpleName()); | 296 | + |
297 | + String methodName = "get"; | ||
298 | + | ||
299 | + methodName += discoveryGenericType(field); | ||
300 | + | ||
301 | + methodName += Strings.firstToUpper(fieldClass.getSimpleName()); | ||
273 | 302 | ||
274 | if (!fieldClass.isPrimitive()) { | 303 | if (!fieldClass.isPrimitive()) { |
275 | method = config.getClass().getMethod(methodName, String.class, fieldClass); | 304 | method = config.getClass().getMethod(methodName, String.class, fieldClass); |
276 | value = method.invoke(config, key, null); | 305 | value = method.invoke(config, key, null); |
306 | + | ||
277 | } else if (config.containsKey(key)) { | 307 | } else if (config.containsKey(key)) { |
278 | method = config.getClass().getMethod(methodName, String.class); | 308 | method = config.getClass().getMethod(methodName, String.class); |
279 | value = method.invoke(config, key); | 309 | value = method.invoke(config, key); |
@@ -286,6 +316,36 @@ public class ConfigurationLoader implements Serializable { | @@ -286,6 +316,36 @@ public class ConfigurationLoader implements Serializable { | ||
286 | return value; | 316 | return value; |
287 | } | 317 | } |
288 | 318 | ||
319 | + /** | ||
320 | + * Discovery the Generic's type. | ||
321 | + * | ||
322 | + * for example: the generic's type of List<Integer> list is an Integer type | ||
323 | + * | ||
324 | + * @param field | ||
325 | + * @return | ||
326 | + */ | ||
327 | + private String discoveryGenericType(Field field) { | ||
328 | + | ||
329 | + Type genericFieldType = field.getGenericType(); | ||
330 | + | ||
331 | + if(genericFieldType instanceof ParameterizedType){ | ||
332 | + ParameterizedType type = (ParameterizedType) genericFieldType; | ||
333 | + Type[] fieldArgumentTypes = type.getActualTypeArguments(); | ||
334 | + for(Type fieldArgumentType : fieldArgumentTypes){ | ||
335 | + @SuppressWarnings("rawtypes") | ||
336 | + Class fieldArgumentClass = (Class) fieldArgumentType; | ||
337 | + | ||
338 | + if("String".equals(fieldArgumentClass.getSimpleName())) { | ||
339 | + return ""; | ||
340 | + } | ||
341 | + | ||
342 | + return fieldArgumentClass.getSimpleName(); | ||
343 | + } | ||
344 | + } | ||
345 | + | ||
346 | + return ""; | ||
347 | + } | ||
348 | + | ||
289 | private Object getProperty(String key, org.apache.commons.configuration.Configuration config) { | 349 | private Object getProperty(String key, org.apache.commons.configuration.Configuration config) { |
290 | Object value = null; | 350 | Object value = null; |
291 | 351 |
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoaderTest.java
@@ -638,4 +638,4 @@ | @@ -638,4 +638,4 @@ | ||
638 | // assertEquals("All LowerCase", config.conventionAllLowerCase); | 638 | // assertEquals("All LowerCase", config.conventionAllLowerCase); |
639 | // } | 639 | // } |
640 | // | 640 | // |
641 | -//} | 641 | -//} |
642 | +//} | ||
642 | \ No newline at end of file | 643 | \ No newline at end of file |
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoaderWithArrayTest.java
0 → 100644
@@ -0,0 +1,520 @@ | @@ -0,0 +1,520 @@ | ||
1 | +/* | ||
2 | + * Demoiselle Framework | ||
3 | + * Copyright (C) 2010 SERPRO | ||
4 | + * ---------------------------------------------------------------------------- | ||
5 | + * This file is part of Demoiselle Framework. | ||
6 | + * | ||
7 | + * Demoiselle Framework is free software; you can redistribute it and/or | ||
8 | + * modify it under the terms of the GNU Lesser General Public License version 3 | ||
9 | + * as published by the Free Software Foundation. | ||
10 | + * | ||
11 | + * This program is distributed in the hope that it will be useful, | ||
12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | + * GNU General Public License for more details. | ||
15 | + * | ||
16 | + * You should have received a copy of the GNU Lesser General Public License version 3 | ||
17 | + * along with this program; if not, see <http://www.gnu.org/licenses/> | ||
18 | + * or write to the Free Software Foundation, Inc., 51 Franklin Street, | ||
19 | + * Fifth Floor, Boston, MA 02110-1301, USA. | ||
20 | + * ---------------------------------------------------------------------------- | ||
21 | + * Este arquivo é parte do Framework Demoiselle. | ||
22 | + * | ||
23 | + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | ||
24 | + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | ||
25 | + * do Software Livre (FSF). | ||
26 | + * | ||
27 | + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | ||
28 | + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | ||
29 | + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | ||
30 | + * para maiores detalhes. | ||
31 | + * | ||
32 | + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | ||
33 | + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | ||
34 | + * ou escreva para a Fundação do Software Livre (FSF) Inc., | ||
35 | + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | ||
36 | + */ | ||
37 | +package br.gov.frameworkdemoiselle.internal.configuration; | ||
38 | + | ||
39 | +import static org.easymock.EasyMock.expect; | ||
40 | +import static org.junit.Assert.assertEquals; | ||
41 | +import static org.powermock.api.easymock.PowerMock.mockStatic; | ||
42 | + | ||
43 | +import java.awt.Color; | ||
44 | +import java.math.BigDecimal; | ||
45 | +import java.math.BigInteger; | ||
46 | +import java.net.URL; | ||
47 | +import java.util.Calendar; | ||
48 | +import java.util.Date; | ||
49 | +import java.util.GregorianCalendar; | ||
50 | +import java.util.Locale; | ||
51 | + | ||
52 | +import org.junit.After; | ||
53 | +import org.junit.Before; | ||
54 | +import org.junit.Test; | ||
55 | +import org.junit.runner.RunWith; | ||
56 | +import org.powermock.api.easymock.PowerMock; | ||
57 | +import org.powermock.core.classloader.annotations.PrepareForTest; | ||
58 | +import org.powermock.modules.junit4.PowerMockRunner; | ||
59 | +import org.powermock.reflect.Whitebox; | ||
60 | +import org.slf4j.Logger; | ||
61 | + | ||
62 | +import br.gov.frameworkdemoiselle.configuration.ConfigType; | ||
63 | +import br.gov.frameworkdemoiselle.configuration.Configuration; | ||
64 | +import br.gov.frameworkdemoiselle.internal.bootstrap.CoreBootstrap; | ||
65 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | ||
66 | + | ||
67 | +@RunWith(PowerMockRunner.class) | ||
68 | +@PrepareForTest(CoreBootstrap.class) | ||
69 | +public class ConfigurationLoaderWithArrayTest { | ||
70 | + | ||
71 | + private ConfigurationLoader configurationLoader; | ||
72 | + | ||
73 | + @Configuration(type = ConfigType.PROPERTIES , resource = "configuration-with-array") | ||
74 | + public class ConfigurationPropertiesWithArray { | ||
75 | + | ||
76 | + /* | ||
77 | + * All methods supported by org.apache.commons.configuration.DataConfiguration class for array | ||
78 | + */ | ||
79 | + | ||
80 | + protected BigDecimal[] bigDecimalArray; | ||
81 | + protected BigInteger[] bigIntegerArray; | ||
82 | + protected boolean[] booleanArray; | ||
83 | + protected byte[] byteArray; | ||
84 | + protected Calendar[] calendarArray; | ||
85 | + protected Color[] colorArray; | ||
86 | + protected Date[] dateArray; | ||
87 | + protected double[] doubleArray; | ||
88 | + protected float[] floatArray; | ||
89 | + protected int[] integerArray; | ||
90 | + protected Locale[] localeArray; | ||
91 | + protected long[] longArray; | ||
92 | + protected short[] shortArray; | ||
93 | + protected URL[] urlArray; | ||
94 | + protected String[] stringArray; | ||
95 | + | ||
96 | + } | ||
97 | + | ||
98 | + @Configuration(type = ConfigType.XML, resource = "configuration-with-array") | ||
99 | + public class ConfigurationXMLWithArray { | ||
100 | + | ||
101 | + /* | ||
102 | + * All methods supported by org.apache.commons.configuration.DataConfiguration class for array | ||
103 | + */ | ||
104 | + | ||
105 | + protected BigDecimal[] bigDecimalArray; | ||
106 | + protected BigInteger[] bigIntegerArray; | ||
107 | + protected boolean[] booleanArray; | ||
108 | + protected byte[] byteArray; | ||
109 | + protected Calendar[] calendarArray; | ||
110 | + protected Color[] colorArray; | ||
111 | + protected Date[] dateArray; | ||
112 | + protected double[] doubleArray; | ||
113 | + protected float[] floatArray; | ||
114 | + protected int[] integerArray; | ||
115 | + protected Locale[] localeArray; | ||
116 | + protected long[] longArray; | ||
117 | + protected short[] shortArray; | ||
118 | + protected URL[] urlArray; | ||
119 | + protected String[] stringArray; | ||
120 | + | ||
121 | + } | ||
122 | + | ||
123 | + | ||
124 | + @Before | ||
125 | + public void setUp() throws Exception { | ||
126 | + Logger logger; | ||
127 | + ResourceBundle bundle; | ||
128 | + logger = PowerMock.createMock(Logger.class); | ||
129 | + bundle = new ResourceBundle("demoiselle-core-bundle", Locale.getDefault()); | ||
130 | + configurationLoader = new ConfigurationLoader(); | ||
131 | + Whitebox.setInternalState(this.configurationLoader, "bundle", bundle); | ||
132 | + Whitebox.setInternalState(this.configurationLoader, "logger", logger); | ||
133 | + } | ||
134 | + | ||
135 | + @After | ||
136 | + public void tearDown() throws Exception { | ||
137 | + } | ||
138 | + | ||
139 | + @Test | ||
140 | + public void testConfigurationPropertiesWithIntegerArray() { | ||
141 | + ConfigurationPropertiesWithArray config = prepareConfigurationPropertiesWithArray(); | ||
142 | + | ||
143 | + Integer integerValue = config.integerArray[0]; | ||
144 | + | ||
145 | + assertEquals(Integer.class, integerValue.getClass()); | ||
146 | + assertEquals(Integer.MAX_VALUE, integerValue.intValue()); | ||
147 | + assertEquals(4, config.integerArray.length); | ||
148 | + } | ||
149 | + | ||
150 | + | ||
151 | + @Test | ||
152 | + public void testConfigurationPropertiesWithShortArray() { | ||
153 | + ConfigurationPropertiesWithArray config = prepareConfigurationPropertiesWithArray(); | ||
154 | + | ||
155 | + Short shortValue = config.shortArray[0]; | ||
156 | + | ||
157 | + assertEquals(Short.class, shortValue.getClass()); | ||
158 | + assertEquals(Short.MAX_VALUE, shortValue.shortValue()); | ||
159 | + assertEquals(4, config.shortArray.length); | ||
160 | + } | ||
161 | + | ||
162 | + @Test | ||
163 | + public void testConfigurationPropertiesWithByteArray() { | ||
164 | + ConfigurationPropertiesWithArray config = prepareConfigurationPropertiesWithArray(); | ||
165 | + | ||
166 | + Byte byteValue = config.byteArray[0]; | ||
167 | + | ||
168 | + assertEquals(Byte.class, byteValue.getClass()); | ||
169 | + assertEquals(Byte.MAX_VALUE, byteValue.byteValue()); | ||
170 | + assertEquals(8, config.byteArray.length); | ||
171 | + } | ||
172 | + | ||
173 | + @Test | ||
174 | + public void testConfigurationPropertiesWithBooleanArray() { | ||
175 | + ConfigurationPropertiesWithArray config = prepareConfigurationPropertiesWithArray(); | ||
176 | + | ||
177 | + Boolean booleanValue = config.booleanArray[0]; | ||
178 | + | ||
179 | + assertEquals(Boolean.class, booleanValue.getClass()); | ||
180 | + assertEquals(2, config.booleanArray.length); | ||
181 | + } | ||
182 | + | ||
183 | + @Test | ||
184 | + public void testConfigurationPropertiesWithLongArray() { | ||
185 | + ConfigurationPropertiesWithArray config = prepareConfigurationPropertiesWithArray(); | ||
186 | + | ||
187 | + Long longValue = config.longArray[0]; | ||
188 | + | ||
189 | + assertEquals(Long.class, longValue.getClass()); | ||
190 | + assertEquals(Long.MAX_VALUE, longValue.longValue()); | ||
191 | + assertEquals(5, config.longArray.length); | ||
192 | + } | ||
193 | + | ||
194 | + @Test | ||
195 | + public void testConfigurationPropertiesWithFloatArray() { | ||
196 | + ConfigurationPropertiesWithArray config = prepareConfigurationPropertiesWithArray(); | ||
197 | + | ||
198 | + Float floatValue = config.floatArray[0]; | ||
199 | + | ||
200 | + assertEquals(Float.class, floatValue.getClass()); | ||
201 | + assertEquals(Float.MAX_VALUE, floatValue.floatValue(), 1); | ||
202 | + assertEquals(5, config.floatArray.length); | ||
203 | + } | ||
204 | + | ||
205 | + @Test | ||
206 | + public void testConfigurationPropertiesWithDoubleArray() { | ||
207 | + ConfigurationPropertiesWithArray config = prepareConfigurationPropertiesWithArray(); | ||
208 | + | ||
209 | + Double doubleValue = config.doubleArray[0]; | ||
210 | + | ||
211 | + assertEquals(Double.class, doubleValue.getClass()); | ||
212 | + assertEquals(Double.MAX_VALUE, doubleValue.doubleValue(), 1); | ||
213 | + assertEquals(3, config.doubleArray.length); | ||
214 | + } | ||
215 | + | ||
216 | + @Test | ||
217 | + public void testConfigurationPropertiesWithBigDecimalArray() { | ||
218 | + ConfigurationPropertiesWithArray config = prepareConfigurationPropertiesWithArray(); | ||
219 | + | ||
220 | + BigDecimal bigDecimalValue = config.bigDecimalArray[0]; | ||
221 | + | ||
222 | + assertEquals(BigDecimal.class, bigDecimalValue.getClass()); | ||
223 | + assertEquals(3, config.bigDecimalArray.length); | ||
224 | + } | ||
225 | + | ||
226 | + @Test | ||
227 | + public void testConfigurationPropertiesWithBigIntegerArray() { | ||
228 | + ConfigurationPropertiesWithArray config = prepareConfigurationPropertiesWithArray(); | ||
229 | + | ||
230 | + BigInteger bigIntegerValue = config.bigIntegerArray[0]; | ||
231 | + | ||
232 | + assertEquals(BigInteger.class, bigIntegerValue.getClass()); | ||
233 | + assertEquals(3, config.bigIntegerArray.length); | ||
234 | + } | ||
235 | + | ||
236 | + @Test | ||
237 | + public void testConfigurationPropertiesWithCalendarArray() { | ||
238 | + ConfigurationPropertiesWithArray config = prepareConfigurationPropertiesWithArray(); | ||
239 | + | ||
240 | + Calendar calendarValue = config.calendarArray[0]; | ||
241 | + | ||
242 | + GregorianCalendar calendar = new GregorianCalendar(2012, Calendar.JUNE, 14, 10, 10); | ||
243 | + | ||
244 | + assertEquals(Calendar.class, calendarValue.getClass().getSuperclass()); | ||
245 | + assertEquals(calendar.getTimeInMillis(), calendarValue.getTimeInMillis()); | ||
246 | + assertEquals(3, config.calendarArray.length); | ||
247 | + } | ||
248 | + | ||
249 | + @Test | ||
250 | + public void testConfigurationPropertiesWithDateArray() { | ||
251 | + ConfigurationPropertiesWithArray config = prepareConfigurationPropertiesWithArray(); | ||
252 | + | ||
253 | + Date dateValue = config.dateArray[0]; | ||
254 | + | ||
255 | + GregorianCalendar calendar = new GregorianCalendar(2012, Calendar.AUGUST, 14, 18, 10, 50); | ||
256 | + | ||
257 | + Date date = new Date(calendar.getTimeInMillis()); | ||
258 | + | ||
259 | + assertEquals(Date.class, dateValue.getClass()); | ||
260 | + assertEquals(date.getTime(), dateValue.getTime()); | ||
261 | + assertEquals(3, config.dateArray.length); | ||
262 | + } | ||
263 | + | ||
264 | + @Test | ||
265 | + public void testConfigurationPropertiesWithColorArray() { | ||
266 | + ConfigurationPropertiesWithArray config = prepareConfigurationPropertiesWithArray(); | ||
267 | + | ||
268 | + Color colorValue = config.colorArray[0]; | ||
269 | + | ||
270 | + assertEquals(Color.class, colorValue.getClass()); | ||
271 | + assertEquals(Color.gray, colorValue); | ||
272 | + assertEquals(3, config.colorArray.length); | ||
273 | + } | ||
274 | + | ||
275 | + @Test | ||
276 | + public void testConfigurationPropertiesWithLocaleArray() { | ||
277 | + ConfigurationPropertiesWithArray config = prepareConfigurationPropertiesWithArray(); | ||
278 | + | ||
279 | + Locale localeValue = config.localeArray[0]; | ||
280 | + Locale localeValue2 = config.localeArray[1]; | ||
281 | + | ||
282 | + assertEquals(Locale.class, localeValue.getClass()); | ||
283 | + assertEquals(Locale.ENGLISH, localeValue); | ||
284 | + assertEquals("BR", localeValue2.getCountry()); | ||
285 | + assertEquals(3, config.localeArray.length); | ||
286 | + } | ||
287 | + | ||
288 | + @Test | ||
289 | + public void testConfigurationPropertiesWithURLArray() { | ||
290 | + ConfigurationPropertiesWithArray config = prepareConfigurationPropertiesWithArray(); | ||
291 | + | ||
292 | + URL urlValue = config.urlArray[0]; | ||
293 | + | ||
294 | + URL otherURL = null; | ||
295 | + | ||
296 | + try { | ||
297 | + otherURL = new URL("http://www.test.com"); | ||
298 | + } | ||
299 | + catch(Exception e) { | ||
300 | + | ||
301 | + } | ||
302 | + | ||
303 | + assertEquals(URL.class, urlValue.getClass()); | ||
304 | + assertEquals(otherURL, urlValue); | ||
305 | + assertEquals(3, config.urlArray.length); | ||
306 | + } | ||
307 | + | ||
308 | + @Test | ||
309 | + public void testConfigurationPropertiesWithStringArray() { | ||
310 | + ConfigurationPropertiesWithArray config = prepareConfigurationPropertiesWithArray(); | ||
311 | + | ||
312 | + String stringValue = config.stringArray[0]; | ||
313 | + | ||
314 | + assertEquals(String.class, stringValue.getClass()); | ||
315 | + assertEquals("Test", stringValue); | ||
316 | + assertEquals(3, config.stringArray.length); | ||
317 | + } | ||
318 | + | ||
319 | + private ConfigurationPropertiesWithArray prepareConfigurationPropertiesWithArray() { | ||
320 | + ConfigurationPropertiesWithArray config = new ConfigurationPropertiesWithArray(); | ||
321 | + | ||
322 | + mockStatic(CoreBootstrap.class); | ||
323 | + expect(CoreBootstrap.isAnnotatedType(config.getClass())).andReturn(true); | ||
324 | + PowerMock.replay(CoreBootstrap.class); | ||
325 | + | ||
326 | + configurationLoader.load(config); | ||
327 | + return config; | ||
328 | + } | ||
329 | + | ||
330 | + @Test | ||
331 | + public void testConfigurationXMLWithIntegerArray() { | ||
332 | + ConfigurationXMLWithArray config = prepareConfigurationXMLWithArray(); | ||
333 | + | ||
334 | + Integer integerValue = config.integerArray[0]; | ||
335 | + | ||
336 | + assertEquals(Integer.class, integerValue.getClass()); | ||
337 | + assertEquals(Integer.MAX_VALUE, integerValue.intValue()); | ||
338 | + assertEquals(4, config.integerArray.length); | ||
339 | + } | ||
340 | + | ||
341 | + | ||
342 | + @Test | ||
343 | + public void testConfigurationXMLWithShortArray() { | ||
344 | + ConfigurationXMLWithArray config = prepareConfigurationXMLWithArray(); | ||
345 | + | ||
346 | + Short shortValue = config.shortArray[0]; | ||
347 | + | ||
348 | + assertEquals(Short.class, shortValue.getClass()); | ||
349 | + assertEquals(Short.MAX_VALUE, shortValue.shortValue()); | ||
350 | + assertEquals(4, config.shortArray.length); | ||
351 | + } | ||
352 | + | ||
353 | + @Test | ||
354 | + public void testConfigurationXMLWithByteArray() { | ||
355 | + ConfigurationXMLWithArray config = prepareConfigurationXMLWithArray(); | ||
356 | + | ||
357 | + Byte byteValue = config.byteArray[0]; | ||
358 | + | ||
359 | + assertEquals(Byte.class, byteValue.getClass()); | ||
360 | + assertEquals(Byte.MAX_VALUE, byteValue.byteValue()); | ||
361 | + assertEquals(8, config.byteArray.length); | ||
362 | + } | ||
363 | + | ||
364 | + @Test | ||
365 | + public void testConfigurationXMLWithBooleanArray() { | ||
366 | + ConfigurationXMLWithArray config = prepareConfigurationXMLWithArray(); | ||
367 | + | ||
368 | + Boolean booleanValue = config.booleanArray[0]; | ||
369 | + | ||
370 | + assertEquals(Boolean.class, booleanValue.getClass()); | ||
371 | + assertEquals(2, config.booleanArray.length); | ||
372 | + } | ||
373 | + | ||
374 | + @Test | ||
375 | + public void testConfigurationXMLWithLongArray() { | ||
376 | + ConfigurationXMLWithArray config = prepareConfigurationXMLWithArray(); | ||
377 | + | ||
378 | + Long longValue = config.longArray[0]; | ||
379 | + | ||
380 | + assertEquals(Long.class, longValue.getClass()); | ||
381 | + assertEquals(Long.MAX_VALUE, longValue.longValue()); | ||
382 | + assertEquals(5, config.longArray.length); | ||
383 | + } | ||
384 | + | ||
385 | + @Test | ||
386 | + public void testConfigurationXMLWithFloatArray() { | ||
387 | + ConfigurationXMLWithArray config = prepareConfigurationXMLWithArray(); | ||
388 | + | ||
389 | + Float floatValue = config.floatArray[0]; | ||
390 | + | ||
391 | + assertEquals(Float.class, floatValue.getClass()); | ||
392 | + assertEquals(Float.MAX_VALUE, floatValue.floatValue(), 1); | ||
393 | + assertEquals(5, config.floatArray.length); | ||
394 | + } | ||
395 | + | ||
396 | + @Test | ||
397 | + public void testConfigurationXMLWithDoubleArray() { | ||
398 | + ConfigurationXMLWithArray config = prepareConfigurationXMLWithArray(); | ||
399 | + | ||
400 | + Double doubleValue = config.doubleArray[0]; | ||
401 | + | ||
402 | + assertEquals(Double.class, doubleValue.getClass()); | ||
403 | + assertEquals(Double.MAX_VALUE, doubleValue.doubleValue(), 1); | ||
404 | + assertEquals(3, config.doubleArray.length); | ||
405 | + } | ||
406 | + | ||
407 | + @Test | ||
408 | + public void testConfigurationXMLWithBigDecimalArray() { | ||
409 | + ConfigurationXMLWithArray config = prepareConfigurationXMLWithArray(); | ||
410 | + | ||
411 | + BigDecimal bigDecimalValue = config.bigDecimalArray[0]; | ||
412 | + | ||
413 | + assertEquals(BigDecimal.class, bigDecimalValue.getClass()); | ||
414 | + assertEquals(3, config.bigDecimalArray.length); | ||
415 | + } | ||
416 | + | ||
417 | + @Test | ||
418 | + public void testConfigurationXMLWithBigIntegerArray() { | ||
419 | + ConfigurationXMLWithArray config = prepareConfigurationXMLWithArray(); | ||
420 | + | ||
421 | + BigInteger bigIntegerValue = config.bigIntegerArray[0]; | ||
422 | + | ||
423 | + assertEquals(BigInteger.class, bigIntegerValue.getClass()); | ||
424 | + assertEquals(3, config.bigIntegerArray.length); | ||
425 | + } | ||
426 | + | ||
427 | + @Test | ||
428 | + public void testConfigurationXMLWithCalendarArray() { | ||
429 | + ConfigurationXMLWithArray config = prepareConfigurationXMLWithArray(); | ||
430 | + | ||
431 | + Calendar calendarValue = config.calendarArray[0]; | ||
432 | + | ||
433 | + GregorianCalendar calendar = new GregorianCalendar(2012, Calendar.JUNE, 14, 10, 10); | ||
434 | + | ||
435 | + assertEquals(Calendar.class, calendarValue.getClass().getSuperclass()); | ||
436 | + assertEquals(calendar.getTimeInMillis(), calendarValue.getTimeInMillis()); | ||
437 | + assertEquals(3, config.calendarArray.length); | ||
438 | + } | ||
439 | + | ||
440 | + @Test | ||
441 | + public void testConfigurationXMLWithDateArray() { | ||
442 | + ConfigurationXMLWithArray config = prepareConfigurationXMLWithArray(); | ||
443 | + | ||
444 | + Date dateValue = config.dateArray[0]; | ||
445 | + | ||
446 | + GregorianCalendar calendar = new GregorianCalendar(2012, Calendar.AUGUST, 14, 18, 10, 50); | ||
447 | + | ||
448 | + Date date = new Date(calendar.getTimeInMillis()); | ||
449 | + | ||
450 | + assertEquals(Date.class, dateValue.getClass()); | ||
451 | + assertEquals(date.getTime(), dateValue.getTime()); | ||
452 | + assertEquals(3, config.dateArray.length); | ||
453 | + } | ||
454 | + | ||
455 | + @Test | ||
456 | + public void testConfigurationXMLWithColorArray() { | ||
457 | + ConfigurationXMLWithArray config = prepareConfigurationXMLWithArray(); | ||
458 | + | ||
459 | + Color colorValue = config.colorArray[0]; | ||
460 | + | ||
461 | + assertEquals(Color.class, colorValue.getClass()); | ||
462 | + assertEquals(Color.gray, colorValue); | ||
463 | + assertEquals(3, config.colorArray.length); | ||
464 | + } | ||
465 | + | ||
466 | + @Test | ||
467 | + public void testConfigurationXMLWithLocaleArray() { | ||
468 | + ConfigurationXMLWithArray config = prepareConfigurationXMLWithArray(); | ||
469 | + | ||
470 | + Locale localeValue = config.localeArray[0]; | ||
471 | + Locale localeValue2 = config.localeArray[1]; | ||
472 | + | ||
473 | + assertEquals(Locale.class, localeValue.getClass()); | ||
474 | + assertEquals(Locale.ENGLISH, localeValue); | ||
475 | + assertEquals("BR", localeValue2.getCountry()); | ||
476 | + assertEquals(3, config.localeArray.length); | ||
477 | + } | ||
478 | + | ||
479 | + @Test | ||
480 | + public void testConfigurationXMLWithURLArray() { | ||
481 | + ConfigurationXMLWithArray config = prepareConfigurationXMLWithArray(); | ||
482 | + | ||
483 | + URL urlValue = config.urlArray[0]; | ||
484 | + | ||
485 | + URL otherURL = null; | ||
486 | + | ||
487 | + try { | ||
488 | + otherURL = new URL("http://www.test.com"); | ||
489 | + } | ||
490 | + catch(Exception e) { | ||
491 | + | ||
492 | + } | ||
493 | + | ||
494 | + assertEquals(URL.class, urlValue.getClass()); | ||
495 | + assertEquals(otherURL, urlValue); | ||
496 | + assertEquals(3, config.urlArray.length); | ||
497 | + } | ||
498 | + | ||
499 | + @Test | ||
500 | + public void testConfigurationXMLWithStringArray() { | ||
501 | + ConfigurationXMLWithArray config = prepareConfigurationXMLWithArray(); | ||
502 | + | ||
503 | + String stringValue = config.stringArray[0]; | ||
504 | + | ||
505 | + assertEquals(String.class, stringValue.getClass()); | ||
506 | + assertEquals("Test", stringValue); | ||
507 | + assertEquals(3, config.stringArray.length); | ||
508 | + } | ||
509 | + | ||
510 | + private ConfigurationXMLWithArray prepareConfigurationXMLWithArray() { | ||
511 | + ConfigurationXMLWithArray config = new ConfigurationXMLWithArray(); | ||
512 | + | ||
513 | + mockStatic(CoreBootstrap.class); | ||
514 | + expect(CoreBootstrap.isAnnotatedType(config.getClass())).andReturn(true); | ||
515 | + PowerMock.replay(CoreBootstrap.class); | ||
516 | + | ||
517 | + configurationLoader.load(config); | ||
518 | + return config; | ||
519 | + } | ||
520 | +} |
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/configuration/ConfigurationLoaderWithListTest.java
0 → 100644
@@ -0,0 +1,542 @@ | @@ -0,0 +1,542 @@ | ||
1 | +/* | ||
2 | + * Demoiselle Framework | ||
3 | + * Copyright (C) 2010 SERPRO | ||
4 | + * ---------------------------------------------------------------------------- | ||
5 | + * This file is part of Demoiselle Framework. | ||
6 | + * | ||
7 | + * Demoiselle Framework is free software; you can redistribute it and/or | ||
8 | + * modify it under the terms of the GNU Lesser General Public License version 3 | ||
9 | + * as published by the Free Software Foundation. | ||
10 | + * | ||
11 | + * This program is distributed in the hope that it will be useful, | ||
12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | + * GNU General Public License for more details. | ||
15 | + * | ||
16 | + * You should have received a copy of the GNU Lesser General Public License version 3 | ||
17 | + * along with this program; if not, see <http://www.gnu.org/licenses/> | ||
18 | + * or write to the Free Software Foundation, Inc., 51 Franklin Street, | ||
19 | + * Fifth Floor, Boston, MA 02110-1301, USA. | ||
20 | + * ---------------------------------------------------------------------------- | ||
21 | + * Este arquivo é parte do Framework Demoiselle. | ||
22 | + * | ||
23 | + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | ||
24 | + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | ||
25 | + * do Software Livre (FSF). | ||
26 | + * | ||
27 | + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | ||
28 | + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | ||
29 | + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | ||
30 | + * para maiores detalhes. | ||
31 | + * | ||
32 | + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | ||
33 | + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | ||
34 | + * ou escreva para a Fundação do Software Livre (FSF) Inc., | ||
35 | + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | ||
36 | + */ | ||
37 | +package br.gov.frameworkdemoiselle.internal.configuration; | ||
38 | + | ||
39 | +import static org.easymock.EasyMock.expect; | ||
40 | +import static org.junit.Assert.assertEquals; | ||
41 | +import static org.powermock.api.easymock.PowerMock.mockStatic; | ||
42 | + | ||
43 | +import java.awt.Color; | ||
44 | +import java.math.BigDecimal; | ||
45 | +import java.math.BigInteger; | ||
46 | +import java.net.URL; | ||
47 | +import java.util.Calendar; | ||
48 | +import java.util.Date; | ||
49 | +import java.util.GregorianCalendar; | ||
50 | +import java.util.List; | ||
51 | +import java.util.Locale; | ||
52 | + | ||
53 | +import org.junit.After; | ||
54 | +import org.junit.Before; | ||
55 | +import org.junit.Test; | ||
56 | +import org.junit.runner.RunWith; | ||
57 | +import org.powermock.api.easymock.PowerMock; | ||
58 | +import org.powermock.core.classloader.annotations.PrepareForTest; | ||
59 | +import org.powermock.modules.junit4.PowerMockRunner; | ||
60 | +import org.powermock.reflect.Whitebox; | ||
61 | +import org.slf4j.Logger; | ||
62 | + | ||
63 | +import br.gov.frameworkdemoiselle.configuration.ConfigType; | ||
64 | +import br.gov.frameworkdemoiselle.configuration.Configuration; | ||
65 | +import br.gov.frameworkdemoiselle.internal.bootstrap.CoreBootstrap; | ||
66 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | ||
67 | + | ||
68 | +@RunWith(PowerMockRunner.class) | ||
69 | +@PrepareForTest(CoreBootstrap.class) | ||
70 | +public class ConfigurationLoaderWithListTest { | ||
71 | + | ||
72 | + private ConfigurationLoader configurationLoader; | ||
73 | + | ||
74 | + @Configuration(type = ConfigType.PROPERTIES, resource = "configuration-with-list") | ||
75 | + public class ConfigurationPropertiesWithList { | ||
76 | + | ||
77 | + /* | ||
78 | + * All methods supported by org.apache.commons.configuration.DataConfiguration class for List type | ||
79 | + */ | ||
80 | + | ||
81 | + protected List<BigDecimal> bigDecimalList; | ||
82 | + | ||
83 | + protected List<BigInteger> bigIntegerList; | ||
84 | + | ||
85 | + protected List<Boolean> booleanList; | ||
86 | + | ||
87 | + protected List<Byte> byteList; | ||
88 | + | ||
89 | + protected List<Calendar> calendarList; | ||
90 | + | ||
91 | + protected List<Color> colorList; | ||
92 | + | ||
93 | + protected List<Date> dateList; | ||
94 | + | ||
95 | + protected List<Double> doubleList; | ||
96 | + | ||
97 | + protected List<Float> floatList; | ||
98 | + | ||
99 | + protected List<Integer> integerList; | ||
100 | + | ||
101 | + protected List<Locale> localeList; | ||
102 | + | ||
103 | + protected List<Long> longList; | ||
104 | + | ||
105 | + protected List<Short> shortList; | ||
106 | + | ||
107 | + protected List<URL> urlList; | ||
108 | + | ||
109 | + protected List<String> stringList; | ||
110 | + } | ||
111 | + | ||
112 | + @Configuration(type = ConfigType.XML, resource = "configuration-with-list") | ||
113 | + public class ConfigurationXMLWithList { | ||
114 | + | ||
115 | + /* | ||
116 | + * All methods supported by org.apache.commons.configuration.DataConfiguration class for List type | ||
117 | + */ | ||
118 | + | ||
119 | + protected List<BigDecimal> bigDecimalList; | ||
120 | + | ||
121 | + protected List<BigInteger> bigIntegerList; | ||
122 | + | ||
123 | + protected List<Boolean> booleanList; | ||
124 | + | ||
125 | + protected List<Byte> byteList; | ||
126 | + | ||
127 | + protected List<Calendar> calendarList; | ||
128 | + | ||
129 | + protected List<Color> colorList; | ||
130 | + | ||
131 | + protected List<Date> dateList; | ||
132 | + | ||
133 | + protected List<Double> doubleList; | ||
134 | + | ||
135 | + protected List<Float> floatList; | ||
136 | + | ||
137 | + protected List<Integer> integerList; | ||
138 | + | ||
139 | + protected List<Locale> localeList; | ||
140 | + | ||
141 | + protected List<Long> longList; | ||
142 | + | ||
143 | + protected List<Short> shortList; | ||
144 | + | ||
145 | + protected List<URL> urlList; | ||
146 | + | ||
147 | + protected List<String> stringList; | ||
148 | + } | ||
149 | + | ||
150 | + @Before | ||
151 | + public void setUp() throws Exception { | ||
152 | + Logger logger; | ||
153 | + ResourceBundle bundle; | ||
154 | + logger = PowerMock.createMock(Logger.class); | ||
155 | + bundle = new ResourceBundle("demoiselle-core-bundle", Locale.getDefault()); | ||
156 | + configurationLoader = new ConfigurationLoader(); | ||
157 | + Whitebox.setInternalState(this.configurationLoader, "bundle", bundle); | ||
158 | + Whitebox.setInternalState(this.configurationLoader, "logger", logger); | ||
159 | + } | ||
160 | + | ||
161 | + @After | ||
162 | + public void tearDown() throws Exception { | ||
163 | + } | ||
164 | + | ||
165 | + @Test | ||
166 | + public void testConfigurationPropertiesWithIntegerList() { | ||
167 | + ConfigurationPropertiesWithList config = prepareConfigurationPropertiesWithList(); | ||
168 | + | ||
169 | + Integer integerValue = config.integerList.get(0); | ||
170 | + | ||
171 | + assertEquals(Integer.class, integerValue.getClass()); | ||
172 | + assertEquals(Integer.MAX_VALUE, integerValue.intValue()); | ||
173 | + assertEquals(4, config.integerList.size()); | ||
174 | + } | ||
175 | + | ||
176 | + @Test | ||
177 | + public void testConfigurationPropertiesWithShortList() { | ||
178 | + ConfigurationPropertiesWithList config = prepareConfigurationPropertiesWithList(); | ||
179 | + | ||
180 | + Short shortValue = config.shortList.get(0); | ||
181 | + | ||
182 | + assertEquals(Short.class, shortValue.getClass()); | ||
183 | + assertEquals(Short.MAX_VALUE, shortValue.shortValue()); | ||
184 | + assertEquals(4, config.shortList.size()); | ||
185 | + } | ||
186 | + | ||
187 | + @Test | ||
188 | + public void testConfigurationPropertiesWithByteList() { | ||
189 | + ConfigurationPropertiesWithList config = prepareConfigurationPropertiesWithList(); | ||
190 | + | ||
191 | + Byte byteValue = config.byteList.get(0); | ||
192 | + | ||
193 | + assertEquals(Byte.class, byteValue.getClass()); | ||
194 | + assertEquals(Byte.MAX_VALUE, byteValue.byteValue()); | ||
195 | + assertEquals(8, config.byteList.size()); | ||
196 | + } | ||
197 | + | ||
198 | + @Test | ||
199 | + public void testConfigurationPropertiesWithBooleanList() { | ||
200 | + ConfigurationPropertiesWithList config = prepareConfigurationPropertiesWithList(); | ||
201 | + | ||
202 | + Boolean booleanValue = config.booleanList.get(0); | ||
203 | + | ||
204 | + assertEquals(Boolean.class, booleanValue.getClass()); | ||
205 | + assertEquals(2, config.booleanList.size()); | ||
206 | + } | ||
207 | + | ||
208 | + @Test | ||
209 | + public void testConfigurationPropertiesWithLongList() { | ||
210 | + ConfigurationPropertiesWithList config = prepareConfigurationPropertiesWithList(); | ||
211 | + | ||
212 | + Long longValue = config.longList.get(0); | ||
213 | + | ||
214 | + assertEquals(Long.class, longValue.getClass()); | ||
215 | + assertEquals(Long.MAX_VALUE, longValue.longValue()); | ||
216 | + assertEquals(5, config.longList.size()); | ||
217 | + } | ||
218 | + | ||
219 | + @Test | ||
220 | + public void testConfigurationPropertiesWithFloatList() { | ||
221 | + ConfigurationPropertiesWithList config = prepareConfigurationPropertiesWithList(); | ||
222 | + | ||
223 | + Float floatValue = config.floatList.get(0); | ||
224 | + | ||
225 | + assertEquals(Float.class, floatValue.getClass()); | ||
226 | + assertEquals(Float.MAX_VALUE, floatValue.floatValue(), 1); | ||
227 | + assertEquals(5, config.floatList.size()); | ||
228 | + } | ||
229 | + | ||
230 | + @Test | ||
231 | + public void testConfigurationPropertiesWithDoubleList() { | ||
232 | + ConfigurationPropertiesWithList config = prepareConfigurationPropertiesWithList(); | ||
233 | + | ||
234 | + Double doubleValue = config.doubleList.get(0); | ||
235 | + | ||
236 | + assertEquals(Double.class, doubleValue.getClass()); | ||
237 | + assertEquals(Double.MAX_VALUE, doubleValue.doubleValue(), 1); | ||
238 | + assertEquals(3, config.doubleList.size()); | ||
239 | + } | ||
240 | + | ||
241 | + @Test | ||
242 | + public void testConfigurationPropertiesWithBigDecimalList() { | ||
243 | + ConfigurationPropertiesWithList config = prepareConfigurationPropertiesWithList(); | ||
244 | + | ||
245 | + BigDecimal bigDecimalValue = config.bigDecimalList.get(0); | ||
246 | + | ||
247 | + assertEquals(BigDecimal.class, bigDecimalValue.getClass()); | ||
248 | + assertEquals(3, config.bigDecimalList.size()); | ||
249 | + } | ||
250 | + | ||
251 | + @Test | ||
252 | + public void testConfigurationPropertiesWithBigIntegerList() { | ||
253 | + ConfigurationPropertiesWithList config = prepareConfigurationPropertiesWithList(); | ||
254 | + | ||
255 | + BigInteger bigIntegerValue = config.bigIntegerList.get(0); | ||
256 | + | ||
257 | + assertEquals(BigInteger.class, bigIntegerValue.getClass()); | ||
258 | + assertEquals(3, config.bigIntegerList.size()); | ||
259 | + } | ||
260 | + | ||
261 | + @Test | ||
262 | + public void testConfigurationPropertiesWithCalendarList() { | ||
263 | + ConfigurationPropertiesWithList config = prepareConfigurationPropertiesWithList(); | ||
264 | + | ||
265 | + Calendar calendarValue = config.calendarList.get(0); | ||
266 | + | ||
267 | + GregorianCalendar calendar = new GregorianCalendar(2012, Calendar.JUNE, 14, 10, 10); | ||
268 | + | ||
269 | + assertEquals(Calendar.class, calendarValue.getClass().getSuperclass()); | ||
270 | + assertEquals(calendar.getTimeInMillis(), calendarValue.getTimeInMillis()); | ||
271 | + assertEquals(3, config.calendarList.size()); | ||
272 | + } | ||
273 | + | ||
274 | + @Test | ||
275 | + public void testConfigurationPropertiesWithDateList() { | ||
276 | + ConfigurationPropertiesWithList config = prepareConfigurationPropertiesWithList(); | ||
277 | + | ||
278 | + Date dateValue = config.dateList.get(0); | ||
279 | + | ||
280 | + GregorianCalendar calendar = new GregorianCalendar(2012, Calendar.AUGUST, 14, 18, 10, 50); | ||
281 | + | ||
282 | + Date date = new Date(calendar.getTimeInMillis()); | ||
283 | + | ||
284 | + assertEquals(Date.class, dateValue.getClass()); | ||
285 | + assertEquals(date.getTime(), dateValue.getTime()); | ||
286 | + assertEquals(3, config.dateList.size()); | ||
287 | + } | ||
288 | + | ||
289 | + @Test | ||
290 | + public void testConfigurationPropertiesWithColorList() { | ||
291 | + ConfigurationPropertiesWithList config = prepareConfigurationPropertiesWithList(); | ||
292 | + | ||
293 | + Color colorValue = config.colorList.get(0); | ||
294 | + | ||
295 | + assertEquals(Color.class, colorValue.getClass()); | ||
296 | + assertEquals(Color.gray, colorValue); | ||
297 | + assertEquals(3, config.colorList.size()); | ||
298 | + } | ||
299 | + | ||
300 | + @Test | ||
301 | + public void testConfigurationPropertiesWithLocaleList() { | ||
302 | + ConfigurationPropertiesWithList config = prepareConfigurationPropertiesWithList(); | ||
303 | + | ||
304 | + Locale localeValue = config.localeList.get(0); | ||
305 | + Locale localeValue2 = config.localeList.get(1); | ||
306 | + | ||
307 | + assertEquals(Locale.class, localeValue.getClass()); | ||
308 | + assertEquals(Locale.ENGLISH, localeValue); | ||
309 | + assertEquals("BR", localeValue2.getCountry()); | ||
310 | + assertEquals(3, config.localeList.size()); | ||
311 | + } | ||
312 | + | ||
313 | + @Test | ||
314 | + public void testConfigurationPropertiesWithURLList() { | ||
315 | + ConfigurationPropertiesWithList config = prepareConfigurationPropertiesWithList(); | ||
316 | + | ||
317 | + URL urlValue = config.urlList.get(0); | ||
318 | + | ||
319 | + URL otherURL = null; | ||
320 | + | ||
321 | + try { | ||
322 | + otherURL = new URL("http://www.test.com"); | ||
323 | + } catch (Exception e) { | ||
324 | + | ||
325 | + } | ||
326 | + | ||
327 | + assertEquals(URL.class, urlValue.getClass()); | ||
328 | + assertEquals(otherURL, urlValue); | ||
329 | + assertEquals(3, config.urlList.size()); | ||
330 | + } | ||
331 | + | ||
332 | + @Test | ||
333 | + public void testConfigurationPropertiesWithStringList() { | ||
334 | + ConfigurationPropertiesWithList config = prepareConfigurationPropertiesWithList(); | ||
335 | + | ||
336 | + String stringValue = config.stringList.get(0); | ||
337 | + | ||
338 | + assertEquals(String.class, stringValue.getClass()); | ||
339 | + assertEquals("Test", stringValue); | ||
340 | + assertEquals(3, config.stringList.size()); | ||
341 | + } | ||
342 | + | ||
343 | + private ConfigurationPropertiesWithList prepareConfigurationPropertiesWithList() { | ||
344 | + ConfigurationPropertiesWithList config = new ConfigurationPropertiesWithList(); | ||
345 | + | ||
346 | + mockStatic(CoreBootstrap.class); | ||
347 | + expect(CoreBootstrap.isAnnotatedType(config.getClass())).andReturn(true); | ||
348 | + PowerMock.replay(CoreBootstrap.class); | ||
349 | + | ||
350 | + configurationLoader.load(config); | ||
351 | + return config; | ||
352 | + } | ||
353 | + | ||
354 | + @Test | ||
355 | + public void testConfigurationXMLWithIntegerList() { | ||
356 | + ConfigurationXMLWithList config = prepareConfigurationXMLWithList(); | ||
357 | + | ||
358 | + Integer integerValue = config.integerList.get(0); | ||
359 | + | ||
360 | + assertEquals(Integer.class, integerValue.getClass()); | ||
361 | + assertEquals(Integer.MAX_VALUE, integerValue.intValue()); | ||
362 | + assertEquals(4, config.integerList.size()); | ||
363 | + } | ||
364 | + | ||
365 | + @Test | ||
366 | + public void testConfigurationXMLWithShortList() { | ||
367 | + ConfigurationXMLWithList config = prepareConfigurationXMLWithList(); | ||
368 | + | ||
369 | + Short shortValue = config.shortList.get(0); | ||
370 | + | ||
371 | + assertEquals(Short.class, shortValue.getClass()); | ||
372 | + assertEquals(Short.MAX_VALUE, shortValue.shortValue()); | ||
373 | + assertEquals(4, config.shortList.size()); | ||
374 | + } | ||
375 | + | ||
376 | + @Test | ||
377 | + public void testConfigurationXMLWithByteList() { | ||
378 | + ConfigurationXMLWithList config = prepareConfigurationXMLWithList(); | ||
379 | + | ||
380 | + Byte byteValue = config.byteList.get(0); | ||
381 | + | ||
382 | + assertEquals(Byte.class, byteValue.getClass()); | ||
383 | + assertEquals(Byte.MAX_VALUE, byteValue.byteValue()); | ||
384 | + assertEquals(8, config.byteList.size()); | ||
385 | + } | ||
386 | + | ||
387 | + @Test | ||
388 | + public void testConfigurationXMLWithBooleanList() { | ||
389 | + ConfigurationXMLWithList config = prepareConfigurationXMLWithList(); | ||
390 | + | ||
391 | + Boolean booleanValue = config.booleanList.get(0); | ||
392 | + | ||
393 | + assertEquals(Boolean.class, booleanValue.getClass()); | ||
394 | + assertEquals(2, config.booleanList.size()); | ||
395 | + } | ||
396 | + | ||
397 | + @Test | ||
398 | + public void testConfigurationXMLWithLongList() { | ||
399 | + ConfigurationXMLWithList config = prepareConfigurationXMLWithList(); | ||
400 | + | ||
401 | + Long longValue = config.longList.get(0); | ||
402 | + | ||
403 | + assertEquals(Long.class, longValue.getClass()); | ||
404 | + assertEquals(Long.MAX_VALUE, longValue.longValue()); | ||
405 | + assertEquals(5, config.longList.size()); | ||
406 | + } | ||
407 | + | ||
408 | + @Test | ||
409 | + public void testConfigurationXMLWithFloatList() { | ||
410 | + ConfigurationXMLWithList config = prepareConfigurationXMLWithList(); | ||
411 | + | ||
412 | + Float floatValue = config.floatList.get(0); | ||
413 | + | ||
414 | + assertEquals(Float.class, floatValue.getClass()); | ||
415 | + assertEquals(Float.MAX_VALUE, floatValue.floatValue(), 1); | ||
416 | + assertEquals(5, config.floatList.size()); | ||
417 | + } | ||
418 | + | ||
419 | + @Test | ||
420 | + public void testConfigurationXMLWithDoubleList() { | ||
421 | + ConfigurationXMLWithList config = prepareConfigurationXMLWithList(); | ||
422 | + | ||
423 | + Double doubleValue = config.doubleList.get(0); | ||
424 | + | ||
425 | + assertEquals(Double.class, doubleValue.getClass()); | ||
426 | + assertEquals(Double.MAX_VALUE, doubleValue.doubleValue(), 1); | ||
427 | + assertEquals(3, config.doubleList.size()); | ||
428 | + } | ||
429 | + | ||
430 | + @Test | ||
431 | + public void testConfigurationXMLWithBigDecimalList() { | ||
432 | + ConfigurationXMLWithList config = prepareConfigurationXMLWithList(); | ||
433 | + | ||
434 | + BigDecimal bigDecimalValue = config.bigDecimalList.get(0); | ||
435 | + | ||
436 | + assertEquals(BigDecimal.class, bigDecimalValue.getClass()); | ||
437 | + assertEquals(3, config.bigDecimalList.size()); | ||
438 | + } | ||
439 | + | ||
440 | + @Test | ||
441 | + public void testConfigurationXMLWithBigIntegerList() { | ||
442 | + ConfigurationXMLWithList config = prepareConfigurationXMLWithList(); | ||
443 | + | ||
444 | + BigInteger bigIntegerValue = config.bigIntegerList.get(0); | ||
445 | + | ||
446 | + assertEquals(BigInteger.class, bigIntegerValue.getClass()); | ||
447 | + assertEquals(3, config.bigIntegerList.size()); | ||
448 | + } | ||
449 | + | ||
450 | + @Test | ||
451 | + public void testConfigurationXMLWithCalendarList() { | ||
452 | + ConfigurationXMLWithList config = prepareConfigurationXMLWithList(); | ||
453 | + | ||
454 | + Calendar calendarValue = config.calendarList.get(0); | ||
455 | + | ||
456 | + GregorianCalendar calendar = new GregorianCalendar(2012, Calendar.JUNE, 14, 10, 10); | ||
457 | + | ||
458 | + assertEquals(Calendar.class, calendarValue.getClass().getSuperclass()); | ||
459 | + assertEquals(calendar.getTimeInMillis(), calendarValue.getTimeInMillis()); | ||
460 | + assertEquals(3, config.calendarList.size()); | ||
461 | + } | ||
462 | + | ||
463 | + @Test | ||
464 | + public void testConfigurationXMLWithDateList() { | ||
465 | + ConfigurationXMLWithList config = prepareConfigurationXMLWithList(); | ||
466 | + | ||
467 | + Date dateValue = config.dateList.get(0); | ||
468 | + | ||
469 | + GregorianCalendar calendar = new GregorianCalendar(2012, Calendar.AUGUST, 14, 18, 10, 50); | ||
470 | + | ||
471 | + Date date = new Date(calendar.getTimeInMillis()); | ||
472 | + | ||
473 | + assertEquals(Date.class, dateValue.getClass()); | ||
474 | + assertEquals(date.getTime(), dateValue.getTime()); | ||
475 | + assertEquals(3, config.dateList.size()); | ||
476 | + } | ||
477 | + | ||
478 | + @Test | ||
479 | + public void testConfigurationXMLWithColorList() { | ||
480 | + ConfigurationXMLWithList config = prepareConfigurationXMLWithList(); | ||
481 | + | ||
482 | + Color colorValue = config.colorList.get(0); | ||
483 | + | ||
484 | + assertEquals(Color.class, colorValue.getClass()); | ||
485 | + assertEquals(Color.gray, colorValue); | ||
486 | + assertEquals(3, config.colorList.size()); | ||
487 | + } | ||
488 | + | ||
489 | + @Test | ||
490 | + public void testConfigurationXMLWithLocaleList() { | ||
491 | + ConfigurationXMLWithList config = prepareConfigurationXMLWithList(); | ||
492 | + | ||
493 | + Locale localeValue = config.localeList.get(0); | ||
494 | + Locale localeValue2 = config.localeList.get(1); | ||
495 | + | ||
496 | + assertEquals(Locale.class, localeValue.getClass()); | ||
497 | + assertEquals(Locale.ENGLISH, localeValue); | ||
498 | + assertEquals("BR", localeValue2.getCountry()); | ||
499 | + assertEquals(3, config.localeList.size()); | ||
500 | + } | ||
501 | + | ||
502 | + @Test | ||
503 | + public void testConfigurationXMLWithURLList() { | ||
504 | + ConfigurationXMLWithList config = prepareConfigurationXMLWithList(); | ||
505 | + | ||
506 | + URL urlValue = config.urlList.get(0); | ||
507 | + | ||
508 | + URL otherURL = null; | ||
509 | + | ||
510 | + try { | ||
511 | + otherURL = new URL("http://www.test.com"); | ||
512 | + } catch (Exception e) { | ||
513 | + | ||
514 | + } | ||
515 | + | ||
516 | + assertEquals(URL.class, urlValue.getClass()); | ||
517 | + assertEquals(otherURL, urlValue); | ||
518 | + assertEquals(3, config.urlList.size()); | ||
519 | + } | ||
520 | + | ||
521 | + @Test | ||
522 | + public void testConfigurationXMLWithStringList() { | ||
523 | + ConfigurationXMLWithList config = prepareConfigurationXMLWithList(); | ||
524 | + | ||
525 | + String stringValue = config.stringList.get(0); | ||
526 | + | ||
527 | + assertEquals(String.class, stringValue.getClass()); | ||
528 | + assertEquals("Test", stringValue); | ||
529 | + assertEquals(3, config.stringList.size()); | ||
530 | + } | ||
531 | + | ||
532 | + private ConfigurationXMLWithList prepareConfigurationXMLWithList() { | ||
533 | + ConfigurationXMLWithList config = new ConfigurationXMLWithList(); | ||
534 | + | ||
535 | + mockStatic(CoreBootstrap.class); | ||
536 | + expect(CoreBootstrap.isAnnotatedType(config.getClass())).andReturn(true); | ||
537 | + PowerMock.replay(CoreBootstrap.class); | ||
538 | + | ||
539 | + configurationLoader.load(config); | ||
540 | + return config; | ||
541 | + } | ||
542 | +} |
impl/core/src/test/resources/configuration-with-array.properties
0 → 100644
@@ -0,0 +1,50 @@ | @@ -0,0 +1,50 @@ | ||
1 | +# Demoiselle Framework | ||
2 | +# Copyright (C) 2010 SERPRO | ||
3 | +# ---------------------------------------------------------------------------- | ||
4 | +# This file is part of Demoiselle Framework. | ||
5 | +# | ||
6 | +# Demoiselle Framework is free software; you can redistribute it and/or | ||
7 | +# modify it under the terms of the GNU Lesser General Public License version 3 | ||
8 | +# as published by the Free Software Foundation. | ||
9 | +# | ||
10 | +# This program is distributed in the hope that it will be useful, | ||
11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | +# GNU General Public License for more details. | ||
14 | +# | ||
15 | +# You should have received a copy of the GNU Lesser General Public License version 3 | ||
16 | +# along with this program; if not, see <http://www.gnu.org/licenses/> | ||
17 | +# or write to the Free Software Foundation, Inc., 51 Franklin Street, | ||
18 | +# Fifth Floor, Boston, MA 02110-1301, USA. | ||
19 | +# ---------------------------------------------------------------------------- | ||
20 | +# Este arquivo é parte do Framework Demoiselle. | ||
21 | +# | ||
22 | +# O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | ||
23 | +# modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | ||
24 | +# do Software Livre (FSF). | ||
25 | +# | ||
26 | +# Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | ||
27 | +# GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | ||
28 | +# APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | ||
29 | +# para maiores detalhes. | ||
30 | +# | ||
31 | +# Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | ||
32 | +# "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | ||
33 | +# ou escreva para a Fundação do Software Livre (FSF) Inc., | ||
34 | +# 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | ||
35 | + | ||
36 | +integer.array = 2147483647, 10993, 12330, 129399 | ||
37 | +short.array = 32767, 123, 5512, 4212 | ||
38 | +byte.array = 127, 12, 51, 123, 11, 5, 1, -12 | ||
39 | +boolean.array = true, false | ||
40 | +long.array = 9223372036854775807, 0901920390123, 1235234523, 134230094, 66123413423 | ||
41 | +float.array = 3.4028235E38, 34.244, 2412.423444, 513.234, 10000.000 | ||
42 | +double.array = 1.7976931348623157E308, 1231234.00120349192348123, 1234123512341.9134828348238 | ||
43 | +big.decimal.array = 16578.69899, 48787.548877788, 4487787.559 | ||
44 | +big.integer.array = 1998987987897, 89789498171, 21474836475 | ||
45 | +calendar.array = 2012-06-14 10:10:00, 2012-07-14 10:10:00, 2012-06-15 18:10:00 | ||
46 | +date.array = 2012-08-14 18:10:50, 2012-07-14 10:10:00, 2012-06-15 18:10:00 | ||
47 | +color.array = #808080, #cccccc, #ABCCCC | ||
48 | +locale.array = en, pt_br, ca | ||
49 | +url.array = http://www.test.com, https://test.of.test.com, ftp://192.168.0.1 | ||
50 | +string.array = Test, One, Two |
impl/core/src/test/resources/configuration-with-array.xml
0 → 100644
@@ -0,0 +1,145 @@ | @@ -0,0 +1,145 @@ | ||
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<!-- | ||
3 | + Demoiselle Framework | ||
4 | + Copyright (C) 2010 SERPRO | ||
5 | + ============================================================================ | ||
6 | + This file is part of Demoiselle Framework. | ||
7 | + | ||
8 | + Demoiselle Framework is free software; you can redistribute it and/or | ||
9 | + modify it under the terms of the GNU Lesser General Public License version 3 | ||
10 | + as published by the Free Software Foundation. | ||
11 | + | ||
12 | + This program is distributed in the hope that it will be useful, | ||
13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | + GNU General Public License for more details. | ||
16 | + | ||
17 | + You should have received a copy of the GNU Lesser General Public License version 3 | ||
18 | + along with this program; if not, see <http://www.gnu.org/licenses/> | ||
19 | + or write to the Free Software Foundation, Inc., 51 Franklin Street, | ||
20 | + Fifth Floor, Boston, MA 02110-1301, USA. | ||
21 | + ============================================================================ | ||
22 | + Este arquivo é parte do Framework Demoiselle. | ||
23 | + | ||
24 | + O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | ||
25 | + modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | ||
26 | + do Software Livre (FSF). | ||
27 | + | ||
28 | + Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | ||
29 | + GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | ||
30 | + APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | ||
31 | + para maiores detalhes. | ||
32 | + | ||
33 | + Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | ||
34 | + "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | ||
35 | + ou escreva para a Fundação do Software Livre (FSF) Inc., | ||
36 | + 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | ||
37 | +--> | ||
38 | + | ||
39 | +<configuration> | ||
40 | + | ||
41 | + <integer> | ||
42 | + <array>2147483647</array> | ||
43 | + <array>10993</array> | ||
44 | + <array>12330</array> | ||
45 | + <array>129399</array> | ||
46 | + </integer> | ||
47 | + | ||
48 | + <short> | ||
49 | + <array>32767</array> | ||
50 | + <array>123</array> | ||
51 | + <array>5512</array> | ||
52 | + <array>4212</array> | ||
53 | + </short> | ||
54 | + | ||
55 | + <byte> | ||
56 | + <array>127</array> | ||
57 | + <array>12</array> | ||
58 | + <array>51</array> | ||
59 | + <array>123</array> | ||
60 | + <array>11</array> | ||
61 | + <array>5</array> | ||
62 | + <array>1</array> | ||
63 | + <array>-12</array> | ||
64 | + </byte> | ||
65 | + | ||
66 | + <boolean> | ||
67 | + <array>true</array> | ||
68 | + <array>false</array> | ||
69 | + </boolean> | ||
70 | + | ||
71 | + <long> | ||
72 | + <array>9223372036854775807</array> | ||
73 | + <array>0901920390123</array> | ||
74 | + <array>1235234523</array> | ||
75 | + <array>134230094</array> | ||
76 | + <array>66123413423</array> | ||
77 | + </long> | ||
78 | + | ||
79 | + <float> | ||
80 | + <array>3.4028235E38</array> | ||
81 | + <array>34.244</array> | ||
82 | + <array>2412.423444</array> | ||
83 | + <array>513.234</array> | ||
84 | + <array>10000.000</array> | ||
85 | + </float> | ||
86 | + | ||
87 | + <double> | ||
88 | + <array>1.7976931348623157E308</array> | ||
89 | + <array>1231234.00120349192348123</array> | ||
90 | + <array>1234123512341.9134828348238</array> | ||
91 | + </double> | ||
92 | + | ||
93 | + <big> | ||
94 | + <decimal> | ||
95 | + <array>16578.69899</array> | ||
96 | + <array>48787.548877788</array> | ||
97 | + <array>4487787.559</array> | ||
98 | + </decimal> | ||
99 | + </big> | ||
100 | + | ||
101 | + <big> | ||
102 | + <integer> | ||
103 | + <array>1998987987897</array> | ||
104 | + <array>89789498171</array> | ||
105 | + <array>21474836475</array> | ||
106 | + </integer> | ||
107 | + </big> | ||
108 | + | ||
109 | + <calendar> | ||
110 | + <array>2012-06-14 10:10:00</array> | ||
111 | + <array>2012-07-14 10:10:00</array> | ||
112 | + <array>2012-06-15 18:10:00</array> | ||
113 | + </calendar> | ||
114 | + | ||
115 | + <date> | ||
116 | + <array>2012-08-14 18:10:50</array> | ||
117 | + <array>2012-07-14 10:10:00</array> | ||
118 | + <array>2012-06-15 18:10:00</array> | ||
119 | + </date> | ||
120 | + | ||
121 | + <color> | ||
122 | + <array>#808080</array> | ||
123 | + <array>#cccccc</array> | ||
124 | + <array> #ABCCCC</array> | ||
125 | + </color> | ||
126 | + | ||
127 | + <locale> | ||
128 | + <array>en</array> | ||
129 | + <array>pt_br</array> | ||
130 | + <array>ca</array> | ||
131 | + </locale> | ||
132 | + | ||
133 | + <url> | ||
134 | + <array>http://www.test.com</array> | ||
135 | + <array>https://test.of.test.com</array> | ||
136 | + <array>ftp://192.168.0.1</array> | ||
137 | + </url> | ||
138 | + | ||
139 | + <string> | ||
140 | + <array>Test</array> | ||
141 | + <array>One</array> | ||
142 | + <array>Two</array> | ||
143 | + </string> | ||
144 | + | ||
145 | +</configuration> | ||
0 | \ No newline at end of file | 146 | \ No newline at end of file |
impl/core/src/test/resources/configuration-with-list.properties
0 → 100644
@@ -0,0 +1,50 @@ | @@ -0,0 +1,50 @@ | ||
1 | +# Demoiselle Framework | ||
2 | +# Copyright (C) 2010 SERPRO | ||
3 | +# ---------------------------------------------------------------------------- | ||
4 | +# This file is part of Demoiselle Framework. | ||
5 | +# | ||
6 | +# Demoiselle Framework is free software; you can redistribute it and/or | ||
7 | +# modify it under the terms of the GNU Lesser General Public License version 3 | ||
8 | +# as published by the Free Software Foundation. | ||
9 | +# | ||
10 | +# This program is distributed in the hope that it will be useful, | ||
11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | +# GNU General Public License for more details. | ||
14 | +# | ||
15 | +# You should have received a copy of the GNU Lesser General Public License version 3 | ||
16 | +# along with this program; if not, see <http://www.gnu.org/licenses/> | ||
17 | +# or write to the Free Software Foundation, Inc., 51 Franklin Street, | ||
18 | +# Fifth Floor, Boston, MA 02110-1301, USA. | ||
19 | +# ---------------------------------------------------------------------------- | ||
20 | +# Este arquivo é parte do Framework Demoiselle. | ||
21 | +# | ||
22 | +# O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | ||
23 | +# modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | ||
24 | +# do Software Livre (FSF). | ||
25 | +# | ||
26 | +# Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | ||
27 | +# GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | ||
28 | +# APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | ||
29 | +# para maiores detalhes. | ||
30 | +# | ||
31 | +# Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | ||
32 | +# "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | ||
33 | +# ou escreva para a Fundação do Software Livre (FSF) Inc., | ||
34 | +# 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | ||
35 | + | ||
36 | +integer.list = 2147483647, 10993, 12330, 129399 | ||
37 | +short.list = 32767, 123, 5512, 4212 | ||
38 | +byte.list = 127, 12, 51, 123, 11, 5, 1, -12 | ||
39 | +boolean.list = true, false | ||
40 | +long.list = 9223372036854775807, 0901920390123, 1235234523, 134230094, 66123413423 | ||
41 | +float.list = 3.4028235E38, 34.244, 2412.423444, 513.234, 10000.000 | ||
42 | +double.list = 1.7976931348623157E308, 1231234.00120349192348123, 1234123512341.9134828348238 | ||
43 | +big.decimal.list = 16578.69899, 48787.548877788, 4487787.559 | ||
44 | +big.integer.list = 1998987987897, 89789498171, 21474836475 | ||
45 | +calendar.list = 2012-06-14 10:10:00, 2012-07-14 10:10:00, 2012-06-15 18:10:00 | ||
46 | +date.list = 2012-08-14 18:10:50, 2012-07-14 10:10:00, 2012-06-15 18:10:00 | ||
47 | +color.list = #808080, #cccccc, #ABCCCC | ||
48 | +locale.list = en, pt_br, ca | ||
49 | +url.list = http://www.test.com, https://test.of.test.com, ftp://192.168.0.1 | ||
50 | +string.list = Test, One, Two |
impl/core/src/test/resources/configuration-with-list.xml
0 → 100644
@@ -0,0 +1,145 @@ | @@ -0,0 +1,145 @@ | ||
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<!-- | ||
3 | + Demoiselle Framework | ||
4 | + Copyright (C) 2010 SERPRO | ||
5 | + ============================================================================ | ||
6 | + This file is part of Demoiselle Framework. | ||
7 | + | ||
8 | + Demoiselle Framework is free software; you can redistribute it and/or | ||
9 | + modify it under the terms of the GNU Lesser General Public License version 3 | ||
10 | + as published by the Free Software Foundation. | ||
11 | + | ||
12 | + This program is distributed in the hope that it will be useful, | ||
13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | + GNU General Public License for more details. | ||
16 | + | ||
17 | + You should have received a copy of the GNU Lesser General Public License version 3 | ||
18 | + along with this program; if not, see <http://www.gnu.org/licenses/> | ||
19 | + or write to the Free Software Foundation, Inc., 51 Franklin Street, | ||
20 | + Fifth Floor, Boston, MA 02110-1301, USA. | ||
21 | + ============================================================================ | ||
22 | + Este arquivo é parte do Framework Demoiselle. | ||
23 | + | ||
24 | + O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | ||
25 | + modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | ||
26 | + do Software Livre (FSF). | ||
27 | + | ||
28 | + Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | ||
29 | + GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | ||
30 | + APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | ||
31 | + para maiores detalhes. | ||
32 | + | ||
33 | + Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | ||
34 | + "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | ||
35 | + ou escreva para a Fundação do Software Livre (FSF) Inc., | ||
36 | + 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | ||
37 | +--> | ||
38 | + | ||
39 | +<configuration> | ||
40 | + | ||
41 | + <integer> | ||
42 | + <list>2147483647</list> | ||
43 | + <list>10993</list> | ||
44 | + <list>12330</list> | ||
45 | + <list>129399</list> | ||
46 | + </integer> | ||
47 | + | ||
48 | + <short> | ||
49 | + <list>32767</list> | ||
50 | + <list>123</list> | ||
51 | + <list>5512</list> | ||
52 | + <list>4212</list> | ||
53 | + </short> | ||
54 | + | ||
55 | + <byte> | ||
56 | + <list>127</list> | ||
57 | + <list>12</list> | ||
58 | + <list>51</list> | ||
59 | + <list>123</list> | ||
60 | + <list>11</list> | ||
61 | + <list>5</list> | ||
62 | + <list>1</list> | ||
63 | + <list>-12</list> | ||
64 | + </byte> | ||
65 | + | ||
66 | + <boolean> | ||
67 | + <list>true</list> | ||
68 | + <list>false</list> | ||
69 | + </boolean> | ||
70 | + | ||
71 | + <long> | ||
72 | + <list>9223372036854775807</list> | ||
73 | + <list>0901920390123</list> | ||
74 | + <list>1235234523</list> | ||
75 | + <list>134230094</list> | ||
76 | + <list>66123413423</list> | ||
77 | + </long> | ||
78 | + | ||
79 | + <float> | ||
80 | + <list>3.4028235E38</list> | ||
81 | + <list>34.244</list> | ||
82 | + <list>2412.423444</list> | ||
83 | + <list>513.234</list> | ||
84 | + <list>10000.000</list> | ||
85 | + </float> | ||
86 | + | ||
87 | + <double> | ||
88 | + <list>1.7976931348623157E308</list> | ||
89 | + <list>1231234.00120349192348123</list> | ||
90 | + <list>1234123512341.9134828348238</list> | ||
91 | + </double> | ||
92 | + | ||
93 | + <big> | ||
94 | + <decimal> | ||
95 | + <list>16578.69899</list> | ||
96 | + <list>48787.548877788</list> | ||
97 | + <list>4487787.559</list> | ||
98 | + </decimal> | ||
99 | + </big> | ||
100 | + | ||
101 | + <big> | ||
102 | + <integer> | ||
103 | + <list>1998987987897</list> | ||
104 | + <list>89789498171</list> | ||
105 | + <list>21474836475</list> | ||
106 | + </integer> | ||
107 | + </big> | ||
108 | + | ||
109 | + <calendar> | ||
110 | + <list>2012-06-14 10:10:00</list> | ||
111 | + <list>2012-07-14 10:10:00</list> | ||
112 | + <list>2012-06-15 18:10:00</list> | ||
113 | + </calendar> | ||
114 | + | ||
115 | + <date> | ||
116 | + <list>2012-08-14 18:10:50</list> | ||
117 | + <list>2012-07-14 10:10:00</list> | ||
118 | + <list>2012-06-15 18:10:00</list> | ||
119 | + </date> | ||
120 | + | ||
121 | + <color> | ||
122 | + <list>#808080</list> | ||
123 | + <list>#cccccc</list> | ||
124 | + <list> #ABCCCC</list> | ||
125 | + </color> | ||
126 | + | ||
127 | + <locale> | ||
128 | + <list>en</list> | ||
129 | + <list>pt_br</list> | ||
130 | + <list>ca</list> | ||
131 | + </locale> | ||
132 | + | ||
133 | + <url> | ||
134 | + <list>http://www.test.com</list> | ||
135 | + <list>https://test.of.test.com</list> | ||
136 | + <list>ftp://192.168.0.1</list> | ||
137 | + </url> | ||
138 | + | ||
139 | + <string> | ||
140 | + <list>Test</list> | ||
141 | + <list>One</list> | ||
142 | + <list>Two</list> | ||
143 | + </string> | ||
144 | + | ||
145 | +</configuration> | ||
0 | \ No newline at end of file | 146 | \ No newline at end of file |
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/template/JPACrud.java
@@ -39,6 +39,8 @@ package br.gov.frameworkdemoiselle.template; | @@ -39,6 +39,8 @@ package br.gov.frameworkdemoiselle.template; | ||
39 | import java.lang.reflect.Field; | 39 | import java.lang.reflect.Field; |
40 | import java.util.ArrayList; | 40 | import java.util.ArrayList; |
41 | import java.util.List; | 41 | import java.util.List; |
42 | +import java.util.regex.Matcher; | ||
43 | +import java.util.regex.Pattern; | ||
42 | 44 | ||
43 | import javax.enterprise.inject.Instance; | 45 | import javax.enterprise.inject.Instance; |
44 | import javax.inject.Inject; | 46 | import javax.inject.Inject; |
@@ -48,6 +50,7 @@ import javax.persistence.EntityManager; | @@ -48,6 +50,7 @@ import javax.persistence.EntityManager; | ||
48 | import javax.persistence.Enumerated; | 50 | import javax.persistence.Enumerated; |
49 | import javax.persistence.Query; | 51 | import javax.persistence.Query; |
50 | import javax.persistence.TransactionRequiredException; | 52 | import javax.persistence.TransactionRequiredException; |
53 | +import javax.persistence.TypedQuery; | ||
51 | import javax.persistence.criteria.CriteriaBuilder; | 54 | import javax.persistence.criteria.CriteriaBuilder; |
52 | import javax.persistence.criteria.CriteriaQuery; | 55 | import javax.persistence.criteria.CriteriaQuery; |
53 | import javax.persistence.criteria.Predicate; | 56 | import javax.persistence.criteria.Predicate; |
@@ -115,6 +118,7 @@ public class JPACrud<T, I> implements Crud<T, I> { | @@ -115,6 +118,7 @@ public class JPACrud<T, I> implements Crud<T, I> { | ||
115 | PaginationContext context = paginationContext.get(); | 118 | PaginationContext context = paginationContext.get(); |
116 | pagination = context.getPagination(getBeanClass()); | 119 | pagination = context.getPagination(getBeanClass()); |
117 | } | 120 | } |
121 | + | ||
118 | return pagination; | 122 | return pagination; |
119 | } | 123 | } |
120 | 124 | ||
@@ -130,6 +134,7 @@ public class JPACrud<T, I> implements Crud<T, I> { | @@ -130,6 +134,7 @@ public class JPACrud<T, I> implements Crud<T, I> { | ||
130 | if (cause instanceof TransactionRequiredException) { | 134 | if (cause instanceof TransactionRequiredException) { |
131 | String message = bundle.get().getString("no-transaction-active", "frameworkdemoiselle.transaction.class", | 135 | String message = bundle.get().getString("no-transaction-active", "frameworkdemoiselle.transaction.class", |
132 | Configuration.DEFAULT_RESOURCE); | 136 | Configuration.DEFAULT_RESOURCE); |
137 | + | ||
133 | throw new DemoiselleException(message, cause); | 138 | throw new DemoiselleException(message, cause); |
134 | 139 | ||
135 | } else { | 140 | } else { |
@@ -162,31 +167,80 @@ public class JPACrud<T, I> implements Crud<T, I> { | @@ -162,31 +167,80 @@ public class JPACrud<T, I> implements Crud<T, I> { | ||
162 | } | 167 | } |
163 | 168 | ||
164 | @Override | 169 | @Override |
165 | - @SuppressWarnings("unchecked") | ||
166 | public List<T> findAll() { | 170 | public List<T> findAll() { |
167 | - final String jpql = "select this from " + getBeanClass().getSimpleName() + " this"; | ||
168 | - final Query query = getEntityManager().createQuery(jpql); | ||
169 | - | ||
170 | - final Pagination pagination = getPagination(); | ||
171 | - if (pagination != null) { | ||
172 | - pagination.setTotalResults(this.countAll().intValue()); | ||
173 | - query.setFirstResult(pagination.getFirstResult()); | ||
174 | - query.setMaxResults(pagination.getPageSize()); | 171 | + return findByJPQL("select this from " + getBeanClass().getSimpleName() + " this"); |
172 | + } | ||
173 | + | ||
174 | + /** | ||
175 | + * Search JPQL integrated into the context of paging | ||
176 | + * | ||
177 | + * @param jpql | ||
178 | + * - query in syntax JPQL | ||
179 | + * @return a list of entities | ||
180 | + */ | ||
181 | + protected List<T> findByJPQL(String jpql) { | ||
182 | + TypedQuery<T> listQuery = getEntityManager().createQuery(jpql, getBeanClass()); | ||
183 | + | ||
184 | + if (getPagination() != null) { | ||
185 | + String countQuery = createCountQuery(jpql); | ||
186 | + Query query = getEntityManager().createQuery(countQuery); | ||
187 | + Number cResults = (Number) query.getSingleResult(); | ||
188 | + getPagination().setTotalResults(cResults.intValue()); | ||
189 | + listQuery.setFirstResult(getPagination().getFirstResult()); | ||
190 | + listQuery.setMaxResults(getPagination().getPageSize()); | ||
175 | } | 191 | } |
176 | 192 | ||
177 | - List<T> lista = query.getResultList(); | ||
178 | - return lista; | 193 | + return listQuery.getResultList(); |
179 | } | 194 | } |
180 | 195 | ||
181 | /** | 196 | /** |
182 | - * Retrieves the number of persisted objects for the current class type. | 197 | + * Search CriteriaQuery integrated into the context of paging |
183 | * | 198 | * |
184 | - * @return the row count | 199 | + * @param criteriaQuery |
200 | + * - structure CriteriaQuery | ||
201 | + * @return a list of entities | ||
185 | */ | 202 | */ |
186 | - private Long countAll() { | ||
187 | - final Query query = getEntityManager().createQuery( | ||
188 | - "select count(this) from " + beanClass.getSimpleName() + " this"); | ||
189 | - return (Long) query.getSingleResult(); | 203 | + public List<T> findByCriteriaQuery(CriteriaQuery<T> criteriaQuery) { |
204 | + TypedQuery<T> listQuery = getEntityManager().createQuery(criteriaQuery); | ||
205 | + | ||
206 | + if (getPagination() != null) { | ||
207 | + CriteriaBuilder builder = getEntityManager().getCriteriaBuilder(); | ||
208 | + CriteriaQuery<Long> countQuery = builder.createQuery(Long.class); | ||
209 | + countQuery.select(builder.count(countQuery.from(getBeanClass()))); | ||
210 | + countQuery.where(criteriaQuery.getRestriction()); | ||
211 | + getEntityManager().createQuery(countQuery); | ||
212 | + getPagination().setTotalResults((int) (getEntityManager().createQuery(countQuery).getSingleResult() + 0)); | ||
213 | + listQuery.setFirstResult(getPagination().getFirstResult()); | ||
214 | + listQuery.setMaxResults(getPagination().getPageSize()); | ||
215 | + } | ||
216 | + | ||
217 | + return listQuery.getResultList(); | ||
218 | + } | ||
219 | + | ||
220 | + /** | ||
221 | + * Converts a query into a count query | ||
222 | + * | ||
223 | + * @param query | ||
224 | + * @return | ||
225 | + */ | ||
226 | + private String createCountQuery(String query) { | ||
227 | + Matcher matcher = Pattern.compile("[Ss][Ee][Ll][Ee][Cc][Tt](.+)[Ff][Rr][Oo][Mm]").matcher(query); | ||
228 | + | ||
229 | + if (matcher.find()) { | ||
230 | + String group = matcher.group(1).trim(); | ||
231 | + query = query.replaceFirst(group, "COUNT(" + group + ")"); | ||
232 | + matcher = Pattern.compile("[Oo][Rr][Dd][Ee][Rr](.+)").matcher(query); | ||
233 | + | ||
234 | + if (matcher.find()) { | ||
235 | + group = matcher.group(0); | ||
236 | + query = query.replaceFirst(group, ""); | ||
237 | + } | ||
238 | + | ||
239 | + return query; | ||
240 | + | ||
241 | + } else { | ||
242 | + throw new DemoiselleException(bundle.get().getString("malformed-jpql")); | ||
243 | + } | ||
190 | } | 244 | } |
191 | 245 | ||
192 | /** | 246 | /** |
@@ -217,7 +271,6 @@ public class JPACrud<T, I> implements Crud<T, I> { | @@ -217,7 +271,6 @@ public class JPACrud<T, I> implements Crud<T, I> { | ||
217 | * @return an instance of {@code CriteriaQuery} | 271 | * @return an instance of {@code CriteriaQuery} |
218 | */ | 272 | */ |
219 | private CriteriaQuery<T> createCriteriaByExample(final T example) { | 273 | private CriteriaQuery<T> createCriteriaByExample(final T example) { |
220 | - | ||
221 | final CriteriaBuilder builder = getCriteriaBuilder(); | 274 | final CriteriaBuilder builder = getCriteriaBuilder(); |
222 | final CriteriaQuery<T> query = builder.createQuery(getBeanClass()); | 275 | final CriteriaQuery<T> query = builder.createQuery(getBeanClass()); |
223 | final Root<T> entity = query.from(getBeanClass()); | 276 | final Root<T> entity = query.from(getBeanClass()); |
@@ -226,7 +279,6 @@ public class JPACrud<T, I> implements Crud<T, I> { | @@ -226,7 +279,6 @@ public class JPACrud<T, I> implements Crud<T, I> { | ||
226 | final Field[] fields = example.getClass().getDeclaredFields(); | 279 | final Field[] fields = example.getClass().getDeclaredFields(); |
227 | 280 | ||
228 | for (Field field : fields) { | 281 | for (Field field : fields) { |
229 | - | ||
230 | if (!field.isAnnotationPresent(Column.class) && !field.isAnnotationPresent(Basic.class) | 282 | if (!field.isAnnotationPresent(Column.class) && !field.isAnnotationPresent(Basic.class) |
231 | && !field.isAnnotationPresent(Enumerated.class)) { | 283 | && !field.isAnnotationPresent(Enumerated.class)) { |
232 | continue; | 284 | continue; |
@@ -237,10 +289,13 @@ public class JPACrud<T, I> implements Crud<T, I> { | @@ -237,10 +289,13 @@ public class JPACrud<T, I> implements Crud<T, I> { | ||
237 | try { | 289 | try { |
238 | field.setAccessible(true); | 290 | field.setAccessible(true); |
239 | value = field.get(example); | 291 | value = field.get(example); |
292 | + | ||
240 | } catch (IllegalArgumentException e) { | 293 | } catch (IllegalArgumentException e) { |
241 | continue; | 294 | continue; |
295 | + | ||
242 | } catch (IllegalAccessException e) { | 296 | } catch (IllegalAccessException e) { |
243 | continue; | 297 | continue; |
298 | + | ||
244 | } | 299 | } |
245 | 300 | ||
246 | if (value == null) { | 301 | if (value == null) { |
@@ -250,6 +305,7 @@ public class JPACrud<T, I> implements Crud<T, I> { | @@ -250,6 +305,7 @@ public class JPACrud<T, I> implements Crud<T, I> { | ||
250 | final Predicate pred = builder.equal(entity.get(field.getName()), value); | 305 | final Predicate pred = builder.equal(entity.get(field.getName()), value); |
251 | predicates.add(pred); | 306 | predicates.add(pred); |
252 | } | 307 | } |
308 | + | ||
253 | return query.where(predicates.toArray(new Predicate[0])).select(entity); | 309 | return query.where(predicates.toArray(new Predicate[0])).select(entity); |
254 | } | 310 | } |
255 | 311 |
impl/extension/jpa/src/main/resources/demoiselle-jpa-bundle.properties
@@ -42,4 +42,5 @@ can-not-get-persistence-unit-from-persistence=N\u00E3o foi poss\u00EDvel obter a | @@ -42,4 +42,5 @@ can-not-get-persistence-unit-from-persistence=N\u00E3o foi poss\u00EDvel obter a | ||
42 | more-than-one-persistence-unit-defined=Existe mais de uma unidade de persist\u00EAncia definida. Utilize @{0} no ponto de inje\u00E7\u00E3o ou defina o atributo "frameworkdemoiselle.persistence.unit.name" no arquivo demoiselle.properties. | 42 | more-than-one-persistence-unit-defined=Existe mais de uma unidade de persist\u00EAncia definida. Utilize @{0} no ponto de inje\u00E7\u00E3o ou defina o atributo "frameworkdemoiselle.persistence.unit.name" no arquivo demoiselle.properties. |
43 | persistence-unit-name-found=Unidade de persist\u00EAncia "{0}" encontrada. | 43 | persistence-unit-name-found=Unidade de persist\u00EAncia "{0}" encontrada. |
44 | entity-manager-closed=O gerenciador de entidades foi fechado. | 44 | entity-manager-closed=O gerenciador de entidades foi fechado. |
45 | -no-transaction-active=Nenhuma transa\u00E7\u00E3o est\u00E1 ativa, verifique a configura\u00E7\u00E3o "{0}" no arquivo "{1}" e defina a sua estrat\u00E9gia de transa\u00E7\u00E3o. | ||
46 | \ No newline at end of file | 45 | \ No newline at end of file |
46 | +no-transaction-active=Nenhuma transa\u00E7\u00E3o est\u00E1 ativa, verifique a configura\u00E7\u00E3o "{0}" no arquivo "{1}" e defina a sua estrat\u00E9gia de transa\u00E7\u00E3o. | ||
47 | +malformed-jpql=Consulta JPQL mal formada para pagina\u00E7\u00E3o de dados. |
impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/internal/configuration/EntityManagerConfigTest.java
@@ -70,7 +70,7 @@ public class EntityManagerConfigTest { | @@ -70,7 +70,7 @@ public class EntityManagerConfigTest { | ||
70 | @Before | 70 | @Before |
71 | public void setUp() throws Exception { | 71 | public void setUp() throws Exception { |
72 | Logger logger = PowerMock.createMock(Logger.class); | 72 | Logger logger = PowerMock.createMock(Logger.class); |
73 | - ResourceBundle bundle = new ResourceBundle("demoiselle-core-bundle", Locale.getDefault());; | 73 | + ResourceBundle bundle = new ResourceBundle("demoiselle-core-bundle", Locale.getDefault()); |
74 | 74 | ||
75 | ConfigurationLoader configurationLoader = new ConfigurationLoader(); | 75 | ConfigurationLoader configurationLoader = new ConfigurationLoader(); |
76 | 76 |
impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/template/JPACrudTest.java
@@ -129,28 +129,37 @@ public class JPACrudTest { | @@ -129,28 +129,37 @@ public class JPACrudTest { | ||
129 | this.contactDAO.delete(this.contact.getId()); | 129 | this.contactDAO.delete(this.contact.getId()); |
130 | verify(this.entityManager); | 130 | verify(this.entityManager); |
131 | } | 131 | } |
132 | - | 132 | + |
133 | + private TypedQuery<Contact> makeTypedQuery() { | ||
134 | + @SuppressWarnings("unchecked") | ||
135 | + TypedQuery<Contact> typeQuery = EasyMock.createMock(TypedQuery.class); | ||
136 | + expect(typeQuery.setFirstResult(EasyMock.anyInt())).andReturn(null); | ||
137 | + expect(typeQuery.setMaxResults(EasyMock.anyInt())).andReturn(null); | ||
138 | + expect(typeQuery.getResultList()).andReturn(createContacts(1)); | ||
139 | + return typeQuery; | ||
140 | + } | ||
141 | + | ||
133 | @Test | 142 | @Test |
134 | public void testCountAll() { | 143 | public void testCountAll() { |
135 | 144 | ||
136 | Pagination pagination = new PaginationImpl(); | 145 | Pagination pagination = new PaginationImpl(); |
146 | + pagination.setPageSize(10); | ||
137 | PaginationContext actualContext = PowerMock.createMock(PaginationContext.class); | 147 | PaginationContext actualContext = PowerMock.createMock(PaginationContext.class); |
138 | expect(actualContext.getPagination(Contact.class)).andReturn(pagination); | 148 | expect(actualContext.getPagination(Contact.class)).andReturn(pagination); |
139 | @SuppressWarnings("unchecked") | 149 | @SuppressWarnings("unchecked") |
140 | Instance<PaginationContext> paginationContext = PowerMock.createMock(Instance.class); | 150 | Instance<PaginationContext> paginationContext = PowerMock.createMock(Instance.class); |
141 | expect(paginationContext.get()).andReturn(actualContext); | 151 | expect(paginationContext.get()).andReturn(actualContext); |
142 | setInternalState(this.contactDAO, "paginationContext", paginationContext); | 152 | setInternalState(this.contactDAO, "paginationContext", paginationContext); |
153 | + | ||
154 | + TypedQuery<Contact> typeQuery = makeTypedQuery(); | ||
143 | 155 | ||
144 | Query query = EasyMock.createMock(Query.class); | 156 | Query query = EasyMock.createMock(Query.class); |
145 | - expect(query.setFirstResult(EasyMock.anyInt())).andReturn(query); | ||
146 | - expect(query.setMaxResults(EasyMock.anyInt())).andReturn(query); | ||
147 | expect(query.getSingleResult()).andReturn(10L); | 157 | expect(query.getSingleResult()).andReturn(10L); |
158 | + | ||
159 | + expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery); | ||
160 | + expect(this.entityManager.createQuery("SELECT COUNT(THIS) FROM CONTACT THIS")).andReturn(query); | ||
148 | 161 | ||
149 | - expect(this.entityManager.createQuery("select this from Contact this")).andReturn(query); | ||
150 | - expect(this.entityManager.createQuery("select count(this) from Contact this")).andReturn(query); | ||
151 | - expect(query.getResultList()).andReturn(createContacts(1)); | ||
152 | - | ||
153 | - replayAll(query, this.entityManager, paginationContext); | 162 | + replayAll(typeQuery, query, this.entityManager, paginationContext); |
154 | 163 | ||
155 | List<Contact> find = this.contactDAO.findAll(); | 164 | List<Contact> find = this.contactDAO.findAll(); |
156 | 165 | ||
@@ -171,11 +180,13 @@ public class JPACrudTest { | @@ -171,11 +180,13 @@ public class JPACrudTest { | ||
171 | expect(paginationContext.get()).andReturn(actualContext); | 180 | expect(paginationContext.get()).andReturn(actualContext); |
172 | setInternalState(this.contactDAO, "paginationContext", paginationContext); | 181 | setInternalState(this.contactDAO, "paginationContext", paginationContext); |
173 | 182 | ||
183 | + TypedQuery<Contact> typeQuery = makeTypedQuery(); | ||
184 | + | ||
174 | Query query = EasyMock.createMock(Query.class); | 185 | Query query = EasyMock.createMock(Query.class); |
175 | 186 | ||
176 | expect(query.getSingleResult()).andThrow(new DemoiselleException("")); | 187 | expect(query.getSingleResult()).andThrow(new DemoiselleException("")); |
177 | - expect(this.entityManager.createQuery("select this from Contact this")).andReturn(query); | ||
178 | - expect(this.entityManager.createQuery("select count(this) from Contact this")).andReturn(query); | 188 | + expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery); |
189 | + expect(this.entityManager.createQuery("SELECT COUNT(THIS) FROM CONTACT THIS")).andReturn(query); | ||
179 | 190 | ||
180 | replayAll(query, this.entityManager, paginationContext); | 191 | replayAll(query, this.entityManager, paginationContext); |
181 | 192 | ||
@@ -280,27 +291,31 @@ public class JPACrudTest { | @@ -280,27 +291,31 @@ public class JPACrudTest { | ||
280 | 291 | ||
281 | @Test | 292 | @Test |
282 | public void testFindAll() { | 293 | public void testFindAll() { |
283 | - | 294 | + |
295 | + Pagination pagination = new PaginationImpl(); | ||
296 | + pagination.setPageSize(10); | ||
284 | PaginationContext actualContext = PowerMock.createMock(PaginationContext.class); | 297 | PaginationContext actualContext = PowerMock.createMock(PaginationContext.class); |
285 | - expect(actualContext.getPagination(Contact.class)).andReturn(null); | 298 | + expect(actualContext.getPagination(Contact.class)).andReturn(pagination); |
286 | @SuppressWarnings("unchecked") | 299 | @SuppressWarnings("unchecked") |
287 | Instance<PaginationContext> paginationContext = PowerMock.createMock(Instance.class); | 300 | Instance<PaginationContext> paginationContext = PowerMock.createMock(Instance.class); |
288 | expect(paginationContext.get()).andReturn(actualContext); | 301 | expect(paginationContext.get()).andReturn(actualContext); |
289 | setInternalState(this.contactDAO, "paginationContext", paginationContext); | 302 | setInternalState(this.contactDAO, "paginationContext", paginationContext); |
303 | + | ||
304 | + TypedQuery<Contact> typeQuery = makeTypedQuery(); | ||
290 | 305 | ||
291 | Query query = EasyMock.createMock(Query.class); | 306 | Query query = EasyMock.createMock(Query.class); |
292 | - // expect(query.setFirstResult(EasyMock.anyInt())).andReturn(query); | ||
293 | - // expect(query.setMaxResults(EasyMock.anyInt())).andReturn(query); | ||
294 | - expect(this.entityManager.createQuery("select this from Contact this")).andReturn(query); | ||
295 | - expect(query.getResultList()).andReturn(createContacts(1)); | 307 | + expect(query.getSingleResult()).andReturn(10L); |
308 | + | ||
309 | + expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery); | ||
310 | + expect(this.entityManager.createQuery("SELECT COUNT(THIS) FROM CONTACT THIS")).andReturn(query); | ||
296 | 311 | ||
297 | - replayAll(query, this.entityManager, paginationContext); | 312 | + replayAll(typeQuery, query, this.entityManager, paginationContext); |
298 | 313 | ||
299 | List<Contact> find = this.contactDAO.findAll(); | 314 | List<Contact> find = this.contactDAO.findAll(); |
300 | 315 | ||
301 | assertEquals(1, find.size()); | 316 | assertEquals(1, find.size()); |
302 | assertTrue(find.iterator().next().getId().equals(1L)); | 317 | assertTrue(find.iterator().next().getId().equals(1L)); |
303 | - | 318 | + |
304 | verifyAll(); | 319 | verifyAll(); |
305 | } | 320 | } |
306 | 321 | ||
@@ -313,12 +328,15 @@ public class JPACrudTest { | @@ -313,12 +328,15 @@ public class JPACrudTest { | ||
313 | Instance<PaginationContext> paginationContext = PowerMock.createMock(Instance.class); | 328 | Instance<PaginationContext> paginationContext = PowerMock.createMock(Instance.class); |
314 | expect(paginationContext.get()).andReturn(actualContext); | 329 | expect(paginationContext.get()).andReturn(actualContext); |
315 | setInternalState(this.contactDAO, "paginationContext", paginationContext); | 330 | setInternalState(this.contactDAO, "paginationContext", paginationContext); |
331 | + | ||
332 | + @SuppressWarnings("unchecked") | ||
333 | + TypedQuery<Contact> typeQuery = EasyMock.createMock(TypedQuery.class); | ||
334 | + expect(typeQuery.getResultList()).andThrow(new DemoiselleException("")); | ||
316 | 335 | ||
317 | Query query = EasyMock.createMock(Query.class); | 336 | Query query = EasyMock.createMock(Query.class); |
318 | - expect(this.entityManager.createQuery("select this from Contact this")).andReturn(query); | ||
319 | - expect(query.getResultList()).andThrow(new DemoiselleException("")); | 337 | + expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery); |
320 | 338 | ||
321 | - replayAll(query, this.entityManager, paginationContext); | 339 | + replayAll(typeQuery, query, this.entityManager, paginationContext); |
322 | 340 | ||
323 | try { | 341 | try { |
324 | this.contactDAO.findAll(); | 342 | this.contactDAO.findAll(); |