2 * This is a common dao with basic CRUD operations and is not limited to any
3 * persistent layer implementation
5 * Copyright (C) 2008 Imran M Yousuf (imyousuf@smartitengineering.com)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 package com
.smartitengineering
.exim
.impl
;
21 import com
.smartitengineering
.exim
.AssociationConfig
;
22 import com
.smartitengineering
.exim
.EximResourceConfig
;
23 import com
.smartitengineering
.exim
.impl
.data
.IResource
;
24 import com
.smartitengineering
.exim
.impl
.data
.IResourceImpl
;
25 import com
.smartitengineering
.exim
.impl
.data
.InheritedResource
;
26 import com
.smartitengineering
.exim
.impl
.data
.MultiResourceImpl
;
27 import com
.smartitengineering
.exim
.impl
.data
.SomeResource
;
28 import com
.smartitengineering
.exim
.impl
.data
.TestClass
;
29 import com
.smartitengineering
.exim
.impl
.data
.TestResourceDomain
;
30 import com
.smartitengineering
.exim
.impl
.data
.TestResourceDomainField
;
31 import java
.util
.Collection
;
33 import junit
.framework
.TestCase
;
39 public class DefaultAnnotationConfigScannerTest
42 public DefaultAnnotationConfigScannerTest(String testName
) {
47 protected void setUp()
53 * Test of getInstance method, of class DefaultAnnotationConfigScanner.
55 public void testGetInstance() {
56 System
.out
.println("getInstance");
57 DefaultAnnotationConfigScanner expResult
=
58 DefaultAnnotationConfigScanner
.
60 DefaultAnnotationConfigScanner result
=
61 DefaultAnnotationConfigScanner
.
63 assertEquals(expResult
,
68 * Test of getConfigurations method, of class DefaultAnnotationConfigScanner.
70 public void testGetConfigurations() {
71 System
.out
.println("getConfigurations");
72 DefaultAnnotationConfigScanner instance
=
73 new DefaultAnnotationConfigScanner();
74 Map
<Class
, EximResourceConfig
> result
= instance
.getConfigurations();
77 instance
.getResourceConfigForClass(TestClass
.class);
80 assertTrue(result
.keySet().contains(TestResourceDomain
.class));
81 assertTrue(result
.keySet().contains(TestResourceDomainField
.class));
82 assertTrue(result
.keySet().contains(IResource
.class));
83 assertFalse(result
.keySet().contains(TestClass
.class));
84 assertFalse(result
.keySet().contains(SomeResource
.class));
85 instance
.getResourceConfigForClass(TestResourceDomain
.class);
88 assertTrue(result
.keySet().contains(TestResourceDomain
.class));
89 assertTrue(result
.keySet().contains(IResource
.class));
90 assertTrue(result
.keySet().contains(TestResourceDomainField
.class));
91 assertFalse(result
.keySet().contains(TestClass
.class));
92 assertFalse(result
.keySet().contains(SomeResource
.class));
96 * Test of getConfiguredResourceClasses method, of class DefaultAnnotationConfigScanner.
98 public void testGetConfiguredResourceClasses() {
99 System
.out
.println("getConfiguredResourceClasses");
100 DefaultAnnotationConfigScanner instance
=
101 new DefaultAnnotationConfigScanner();
102 Collection
<Class
> result
= instance
.getConfiguredResourceClasses();
103 assertTrue(result
.isEmpty());
104 Package resourcePackage
= Package
.getPackage(getClass().getPackage().
105 getName().concat(".data"));
106 instance
.scanPackageForResourceConfigs(resourcePackage
);
107 result
= instance
.getConfiguredResourceClasses();
110 assertTrue(result
.contains(TestResourceDomain
.class));
111 assertTrue(result
.contains(TestResourceDomainField
.class));
112 assertTrue(result
.contains(IResource
.class));
113 assertFalse(result
.contains(TestClass
.class));
114 assertFalse(result
.contains(SomeResource
.class));
118 * Test of getResourceConfigForClass method, of class DefaultAnnotationConfigScanner.
120 public void testGetResourceConfigForClass() {
121 System
.out
.println("getResourceConfigForClass");
122 DefaultAnnotationConfigScanner instance
=
123 new DefaultAnnotationConfigScanner();
125 EximResourceConfig result
;
126 Map
<String
, AssociationConfig
> asssocConfigs
;
127 AssociationConfig associationConfig
;
130 * Test TestResourceDomain's config
132 resourceClass
= TestResourceDomain
.class;
133 result
= instance
.getResourceConfigForClass(resourceClass
);
134 assertNotNull(result
);
135 assertTrue(result
.isAccessByPropertyEnabled());
136 assertFalse(result
.isAssociateExportPolicyAsUri());
137 assertFalse(result
.isIdentityCustomizerImplemented());
138 assertFalse(result
.isImporterImplemented());
139 assertTrue(result
.isExporterImplemented());
140 assertEquals(TestResourceDomain
.class, result
.getDomainClass());
141 assertEquals(TestResourceDomain
.ID_PREFIX
, result
.getIdPrefix());
142 assertEquals(TestResourceDomain
.ID
, result
.getIdPropertyName());
143 assertEquals(result
.getDomainClass().getName(), result
.getName());
144 assertEquals(TestResourceDomain
.PATH
, result
.getPathToResource());
145 assertEquals(0, result
.getPriority());
146 assertFalse(result
.isExportBasicTypesInTypeElementEnabled());
147 asssocConfigs
= result
.getAssociationConfigs();
148 assertNotNull(asssocConfigs
);
149 assertFalse(asssocConfigs
.isEmpty());
151 asssocConfigs
.size());
153 assertTrue(asssocConfigs
.containsKey(TestResourceDomain
.ID
));
154 associationConfig
= asssocConfigs
.get(TestResourceDomain
.ID
);
155 assertEquals("getId", associationConfig
.getAccessorName());
156 assertEquals(AssociationConfig
.AssociationType
.TYPE_INTEGER
,
157 associationConfig
.getAssociationType());
158 assertEquals(TestResourceDomain
.ID
, associationConfig
.getName());
159 assertFalse(associationConfig
.isEagerSet());
160 assertFalse(associationConfig
.isItToBeExportedAsUri());
161 assertFalse(associationConfig
.isStringProviderImplemented());
162 assertFalse(associationConfig
.isTransient());
164 assertTrue(asssocConfigs
.containsKey(TestResourceDomain
.SOME_PROP
));
165 associationConfig
= asssocConfigs
.get(TestResourceDomain
.SOME_PROP
);
166 assertEquals("getSomeProp", associationConfig
.getAccessorName());
167 assertEquals(AssociationConfig
.AssociationType
.TYPE_STRING
,
168 associationConfig
.getAssociationType());
169 assertEquals(TestResourceDomain
.SOME_PROP_NAME
, associationConfig
.
171 assertFalse(associationConfig
.isEagerSet());
172 assertTrue(associationConfig
.isItToBeExportedAsUri());
173 assertFalse(associationConfig
.isStringProviderImplemented());
174 assertFalse(associationConfig
.isTransient());
176 assertTrue(asssocConfigs
.containsKey(TestResourceDomain
.SOME_BOOL_PROP
));
177 associationConfig
= asssocConfigs
.get(TestResourceDomain
.SOME_BOOL_PROP
);
178 assertEquals("hasSomeBoolProp", associationConfig
.getAccessorName());
179 assertEquals(AssociationConfig
.AssociationType
.TYPE_BOOLEAN
,
180 associationConfig
.getAssociationType());
181 assertEquals(TestResourceDomain
.SOME_BOOL_PROP
, associationConfig
.
183 assertTrue(associationConfig
.isEagerSet());
184 assertFalse(associationConfig
.isItToBeExportedAsUri());
185 assertFalse(associationConfig
.isStringProviderImplemented());
186 assertFalse(associationConfig
.isTransient());
187 //Some transient prop
188 assertTrue(asssocConfigs
.containsKey(
189 TestResourceDomain
.SOME_TRANSIENT_PROP
));
190 associationConfig
= asssocConfigs
.get(
191 TestResourceDomain
.SOME_TRANSIENT_PROP
);
192 assertEquals("getSomeTransientProp", associationConfig
.getAccessorName());
193 assertEquals(AssociationConfig
.AssociationType
.TYPE_STRING
,
194 associationConfig
.getAssociationType());
195 assertEquals(TestResourceDomain
.SOME_TRANSIENT_PROP
, associationConfig
.
197 assertFalse(associationConfig
.isEagerSet());
198 assertFalse(associationConfig
.isItToBeExportedAsUri());
199 assertFalse(associationConfig
.isStringProviderImplemented());
200 assertTrue(associationConfig
.isTransient());
203 * Test TestResourceDomainField's config
205 resourceClass
= TestResourceDomainField
.class;
206 result
= instance
.getResourceConfigForClass(resourceClass
);
207 assertNotNull(result
);
208 assertFalse(result
.isAccessByPropertyEnabled());
209 assertTrue(result
.isAssociateExportPolicyAsUri());
210 assertTrue(result
.isIdentityCustomizerImplemented());
211 assertTrue(result
.isImporterImplemented());
212 assertFalse(result
.isExporterImplemented());
213 assertEquals(TestResourceDomainField
.class, result
.getDomainClass());
214 assertEquals(null, result
.getIdPrefix());
215 assertEquals(null, result
.getIdPropertyName());
216 assertEquals(TestResourceDomainField
.NAME
, result
.getName());
217 assertEquals("", result
.getPathToResource());
218 assertEquals(0, result
.getPriority());
219 assertFalse(result
.isExportBasicTypesInTypeElementEnabled());
220 asssocConfigs
= result
.getAssociationConfigs();
221 assertNotNull(asssocConfigs
);
222 assertFalse(asssocConfigs
.isEmpty());
223 assertEquals(2, asssocConfigs
.size());
225 assertTrue(asssocConfigs
.containsKey(
226 TestResourceDomainField
.SOME_STR_PROP
));
227 associationConfig
= asssocConfigs
.get(
228 TestResourceDomainField
.SOME_STR_PROP
);
229 assertEquals(TestResourceDomainField
.SOME_STR_PROP
,
230 associationConfig
.getAccessorName());
231 assertEquals(AssociationConfig
.AssociationType
.TYPE_STRING
,
232 associationConfig
.getAssociationType());
233 assertEquals(TestResourceDomainField
.SOME_STR_PROP
, associationConfig
.
235 assertFalse(associationConfig
.isEagerSet());
236 assertFalse(associationConfig
.isItToBeExportedAsUri());
237 assertFalse(associationConfig
.isStringProviderImplemented());
238 assertFalse(associationConfig
.isTransient());
239 //someTransientStrProp
240 assertTrue(asssocConfigs
.containsKey(
241 TestResourceDomainField
.SOME_TRANSIENT_STR_PROP
));
242 associationConfig
= asssocConfigs
.get(
243 TestResourceDomainField
.SOME_TRANSIENT_STR_PROP
);
244 assertEquals(TestResourceDomainField
.SOME_TRANSIENT_STR_PROP
,
245 associationConfig
.getAccessorName());
246 assertEquals(AssociationConfig
.AssociationType
.TYPE_STRING
,
247 associationConfig
.getAssociationType());
248 assertEquals(TestResourceDomainField
.SOME_TRANSIENT_STR_PROP
,
249 associationConfig
.getName());
250 assertFalse(associationConfig
.isEagerSet());
251 assertFalse(associationConfig
.isItToBeExportedAsUri());
252 assertFalse(associationConfig
.isStringProviderImplemented());
253 assertTrue(associationConfig
.isTransient());
256 * Test IResource's config
258 resourceClass
= IResource
.class;
259 result
= instance
.getResourceConfigForClass(resourceClass
);
260 assertNotNull(result
);
261 assertTrue(result
.isAccessByPropertyEnabled());
262 assertTrue(result
.isAssociateExportPolicyAsUri());
263 assertFalse(result
.isIdentityCustomizerImplemented());
264 assertFalse(result
.isImporterImplemented());
265 assertFalse(result
.isExporterImplemented());
266 assertEquals(IResource
.class, result
.getDomainClass());
267 assertEquals("", result
.getIdPrefix());
268 assertEquals(IResource
.RESOURCE_ID
, result
.getIdPropertyName());
269 assertEquals(result
.getDomainClass().getName(), result
.getName());
270 assertEquals("", result
.getPathToResource());
271 assertEquals(IResource
.PRIORITY
, result
.getPriority());
272 assertTrue(result
.isExportBasicTypesInTypeElementEnabled());
273 asssocConfigs
= result
.getAssociationConfigs();
274 assertNotNull(asssocConfigs
);
275 assertFalse(asssocConfigs
.isEmpty());
276 assertEquals(7, asssocConfigs
.size());
278 assertTrue(asssocConfigs
.containsKey(IResource
.RESOURCE_ID
));
279 associationConfig
= asssocConfigs
.get(IResource
.RESOURCE_ID
);
280 assertEquals(AssociationConfig
.AssociationType
.TYPE_INTEGER
,
281 associationConfig
.getAssociationType());
283 assertTrue(asssocConfigs
.containsKey(IResource
.LIST
));
284 associationConfig
= asssocConfigs
.get(IResource
.LIST
);
285 assertEquals(AssociationConfig
.AssociationType
.TYPE_LIST
,
286 associationConfig
.getAssociationType());
288 assertTrue(asssocConfigs
.containsKey(IResource
.SET
));
289 associationConfig
= asssocConfigs
.get(IResource
.SET
);
290 assertEquals(AssociationConfig
.AssociationType
.TYPE_SET
,
291 associationConfig
.getAssociationType());
292 assertEquals(IResource
.SET_NAME
, associationConfig
.getName());
294 assertTrue(asssocConfigs
.containsKey(IResource
.COLLECTION
));
295 associationConfig
= asssocConfigs
.get(IResource
.COLLECTION
);
296 assertEquals(AssociationConfig
.AssociationType
.TYPE_COLLECTION
,
297 associationConfig
.getAssociationType());
299 assertTrue(asssocConfigs
.containsKey(IResource
.MAP
));
300 associationConfig
= asssocConfigs
.get(IResource
.MAP
);
301 assertEquals(AssociationConfig
.AssociationType
.TYPE_MAP
,
302 associationConfig
.getAssociationType());
303 assertTrue(associationConfig
.isEagerSet());
305 assertTrue(asssocConfigs
.containsKey(IResource
.ARRAY
));
306 associationConfig
= asssocConfigs
.get(IResource
.ARRAY
);
307 assertEquals(AssociationConfig
.AssociationType
.TYPE_ARRAY
,
308 associationConfig
.getAssociationType());
310 assertTrue(asssocConfigs
.containsKey(IResource
.VALID
));
311 associationConfig
= asssocConfigs
.get(IResource
.VALID
);
312 assertEquals(AssociationConfig
.AssociationType
.TYPE_BOOLEAN
,
313 associationConfig
.getAssociationType());
318 * Test of scanPackageForResourceConfigs method, of class DefaultAnnotationConfigScanner.
320 public void testScanPackageForResourceConfigs() {
321 System
.out
.println("scanPackageForResourceConfigs");
322 Package resourcePackage
= Package
.getPackage(getClass().getPackage().
323 getName().concat(".data"));
324 DefaultAnnotationConfigScanner instance
=
325 new DefaultAnnotationConfigScanner();
326 Map
<Class
, EximResourceConfig
> result
= instance
.getConfigurations();
327 assertEquals(0, result
.size());
328 instance
.scanPackageForResourceConfigs(resourcePackage
);
329 assertEquals(4, result
.size());
330 assertTrue(result
.keySet().contains(TestResourceDomain
.class));
331 assertTrue(result
.keySet().contains(TestResourceDomainField
.class));
332 assertTrue(result
.keySet().contains(IResource
.class));
333 assertFalse(result
.keySet().contains(TestClass
.class));
334 assertFalse(result
.keySet().contains(SomeResource
.class));
337 public void testGetConfigForInheritence() {
338 DefaultAnnotationConfigScanner instance
=
339 new DefaultAnnotationConfigScanner();
340 Package resourcePackage
= Package
.getPackage(getClass().getPackage().
341 getName().concat(".data"));
342 instance
.scanPackageForResourceConfigs(resourcePackage
);
343 EximResourceConfig config
= instance
.getResourceConfigForClass(
344 IResourceImpl
.class);
345 assertNotNull(config
);
346 assertEquals(IResource
.class, config
.getDomainClass());
347 config
= instance
.getResourceConfigForClass(InheritedResource
.class);
348 assertNotNull(config
);
349 assertEquals(IResource
.class, config
.getDomainClass());
350 config
= instance
.getResourceConfigForClass(MultiResourceImpl
.class);
351 assertNotNull(config
);
352 assertEquals(IResource
.class, config
.getDomainClass());
356 * Test of getPropertyNameFromMethodName method, of class DefaultAnnotationConfigScanner.
358 public void testGetPropertyNameFromMethodName() {
359 System
.out
.println("getPropertyNameFromMethodName");
360 String methodName
= "getPropertyNameFromMethodName";
361 DefaultAnnotationConfigScanner instance
=
362 new DefaultAnnotationConfigScanner();
363 String expResult
= "propertyNameFromMethodName";
364 String result
= instance
.getPropertyNameFromMethodName(methodName
);
365 assertEquals(expResult
, result
);
366 methodName
= "isPropertyNameFromMethodName";
367 result
= instance
.getPropertyNameFromMethodName(methodName
);
368 assertEquals(expResult
, result
);
369 methodName
= "hasPropertyNameFromMethodName";
370 result
= instance
.getPropertyNameFromMethodName(methodName
);
371 assertEquals(expResult
, result
);
372 methodName
= "somePropertyNameFromMethodName";
374 result
= instance
.getPropertyNameFromMethodName(methodName
);
375 fail("should not pass!");
377 catch (IllegalArgumentException ex
) {
381 result
= instance
.getPropertyNameFromMethodName(methodName
);
382 fail("should not pass!");
384 catch (NullPointerException ex
) {