Source Code Cross Referenced for DomainMetaDataCheck.java in  » J2EE » Jaffa » org » jaffa » metadata » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Jaffa » org.jaffa.metadata 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * ====================================================================
003:         * JAFFA - Java Application Framework For All
004:         *
005:         * Copyright (C) 2002 JAFFA Development Group
006:         *
007:         *     This library is free software; you can redistribute it and/or
008:         *     modify it under the terms of the GNU Lesser General Public
009:         *     License as published by the Free Software Foundation; either
010:         *     version 2.1 of the License, or (at your option) any later version.
011:         *
012:         *     This library is distributed in the hope that it will be useful,
013:         *     but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015:         *     Lesser General Public License for more details.
016:         *
017:         *     You should have received a copy of the GNU Lesser General Public
018:         *     License along with this library; if not, write to the Free Software
019:         *     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
020:         *
021:         * Redistribution and use of this software and associated documentation ("Software"),
022:         * with or without modification, are permitted provided that the following conditions are met:
023:         * 1.	Redistributions of source code must retain copyright statements and notices.
024:         *         Redistributions must also contain a copy of this document.
025:         * 2.	Redistributions in binary form must reproduce the above copyright notice,
026:         * 	this list of conditions and the following disclaimer in the documentation
027:         * 	and/or other materials provided with the distribution.
028:         * 3.	The name "JAFFA" must not be used to endorse or promote products derived from
029:         * 	this Software without prior written permission. For written permission,
030:         * 	please contact mail to: jaffagroup@yahoo.com.
031:         * 4.	Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032:         * 	appear in their names without prior written permission.
033:         * 5.	Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034:         *
035:         * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036:         * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037:         * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038:         * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039:         * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040:         * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042:         * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044:         * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045:         * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046:         * SUCH DAMAGE.
047:         * ====================================================================
048:         */
049:
050:        /*
051:         * DomainMetaDataCheck.java
052:         *
053:         * Created on June 22, 2001, 10:34 AM
054:         *
055:         * 1- Check if a XxxMeta.class has the methods:
056:         *       a- public static String getName()
057:         *       b- public static String[] getAttributes()
058:         *       c- public static FieldMetaData[] getFieldMetaData()
059:         *       d- public static FieldMetaData getFieldMetaData(String fieldName)
060:         * 2- Check if method-1-a returns the correct String identifying the corresponding Domain object
061:         * 3- Check if the String(s) returned by method-1-b are accessible via 'public static final String constant(s)' and vice versa
062:         * 4- Check if the FieldMetaData(s) returned by method-1-c are accessible via 'public static final FieldMetaData constant(s)' and vice versa
063:         * 5- Check if the getAttributes() & getFieldMetaData() methods return values in sync
064:         * 6- Check if method-1-d works correctly
065:         * 7- Check if each FieldMetaData has a corresponding getter/setter/updater (with the correct data-type) in the corresponding Domain object
066:         *
067:         */
068:
069:        package org.jaffa.metadata;
070:
071:        import java.io.*;
072:        import java.util.*;
073:        import java.lang.reflect.*;
074:
075:        /**
076:         *
077:         * @author  GautamJ
078:         * @version
079:         */
080:        public class DomainMetaDataCheck extends Object {
081:            private Class m_metaClass = null;
082:            private Class m_domainClass = null;
083:            private List m_errors = new ArrayList();
084:            private static final int m_maxErrors = 20;
085:            private static final String PUBLIC = "public", PRIVATE = "private",
086:                    PROTECTED = "protected", ABSTRACT = "abstract",
087:                    STATIC = "static", FINAL = "final",
088:                    SYNCHRONIZED = "synchronized";
089:            private static final String KEY1 = "key1", KEY2 = "key2";
090:
091:            public DomainMetaDataCheck(String metaClassNameWithPackage)
092:                    throws ClassNotFoundException {
093:                this (metaClassNameWithPackage, null);
094:            }
095:
096:            public DomainMetaDataCheck(String metaClassNameWithPackage,
097:                    String domainClassNameWithPackage)
098:                    throws ClassNotFoundException {
099:                m_metaClass = Class.forName(metaClassNameWithPackage);
100:
101:                if (domainClassNameWithPackage == null)
102:                    domainClassNameWithPackage = extractDomainClassName(metaClassNameWithPackage);
103:                m_domainClass = Class.forName(domainClassNameWithPackage);
104:            }
105:
106:            public void performChecks() throws DomainMetaDataCheckException {
107:                // 1- Check if a XxxMeta.class has the required methods:
108:                Method getNameMethod = getMethod(m_metaClass, "getName", null,
109:                        String.class, new int[] { Modifier.PUBLIC,
110:                                Modifier.STATIC });
111:
112:                Method getRefNameMethod = getMethod(m_metaClass, "getRefName",
113:                        null, String.class, new int[] { Modifier.PUBLIC,
114:                                Modifier.STATIC });
115:
116:                Method getAttributesMethod = getMethod(m_metaClass,
117:                        "getAttributes", null, String[].class, new int[] {
118:                                Modifier.PUBLIC, Modifier.STATIC });
119:
120:                Method getFieldMetaDataMethod = getMethod(m_metaClass,
121:                        "getFieldMetaData", null,
122:                        org.jaffa.metadata.FieldMetaData[].class, new int[] {
123:                                Modifier.PUBLIC, Modifier.STATIC });
124:
125:                Method getFieldMetaDataStringMethod = getMethod(m_metaClass,
126:                        "getFieldMetaData", new Class[] { String.class },
127:                        org.jaffa.metadata.FieldMetaData.class, new int[] {
128:                                Modifier.PUBLIC, Modifier.STATIC });
129:
130:                // 2- Check if method-1-a returns the correct String identifying
131:                // the corresponding Domain object
132:                checkGetName(getNameMethod);
133:
134:                // 3- Check if method-1-b returns the correct String identifying
135:                // the corresponding Domain Ref
136:                checkGetRefName(getRefNameMethod);
137:
138:                // 4- Check if the String(s) returned by method-1-c are accessible via
139:                // 'public static final String constant(s)' and vice versa
140:                String[] attributes = checkGetAttributes(getAttributesMethod);
141:
142:                // 5- Check if the FieldMetaData(s) returned by method-1-d are
143:                // accessible via 'public static final FieldMetaData constant(s)' and vice versa
144:                FieldMetaData[] metaFields = checkGetFieldMetaData(getFieldMetaDataMethod);
145:
146:                // 6- Check if the getAttributes() & getFieldMetaData() methods return values in sync
147:                checkAttributeAndField(attributes, metaFields);
148:
149:                // 7- Check if method-1-e works correctly
150:                checkGetFieldMetaDataString(getFieldMetaDataStringMethod,
151:                        attributes);
152:
153:                // 8- Check if each FieldMetaData has a corresponding getter/setter/updater
154:                // (with the correct data-type) in the corresponding Domain object
155:                checkMetaAndDomainSync(metaFields);
156:
157:                // Finally- throw an exception if errors exist
158:                checkErrors();
159:            }
160:
161:            private String extractDomainClassName(String metaClassName) {
162:                String domainClassName = null;
163:                int i = metaClassName.lastIndexOf("Meta");
164:                if (i > 0)
165:                    domainClassName = metaClassName.substring(0, i);
166:                return domainClassName;
167:            }
168:
169:            private String extractRefName(String domainClassName) {
170:                String refName = null;
171:                if (domainClassName != null) {
172:                    StringTokenizer st = new StringTokenizer(domainClassName,
173:                            ".");
174:                    while (st.hasMoreTokens())
175:                        refName = st.nextToken();
176:                }
177:                return refName == null ? "Ref" : refName + "Ref";
178:            }
179:
180:            private void log(String msg) throws DomainMetaDataCheckException {
181:                if (m_errors.size() >= m_maxErrors) {
182:                    // Too many errors. Throw an exception
183:                    checkErrors();
184:                } else {
185:                    m_errors.add(msg);
186:                }
187:            }
188:
189:            private void checkErrors() throws DomainMetaDataCheckException {
190:                if (!m_errors.isEmpty()) {
191:                    StringBuffer buf = new StringBuffer();
192:                    if (m_errors.size() >= m_maxErrors)
193:                        buf.append("Too many errors(" + m_maxErrors + "+) !");
194:                    else
195:                        buf.append(m_errors.size() + " error(s) found");
196:
197:                    for (Iterator itr = m_errors.iterator(); itr.hasNext();) {
198:                        buf.append('\n');
199:                        buf.append('\n');
200:                        buf.append(itr.next());
201:                    }
202:                    m_errors.clear();
203:                    throw new DomainMetaDataCheckException(buf.toString());
204:                }
205:            }
206:
207:            private Method getMethod(Class clazz, String methodName,
208:                    Class[] parameterTypes, Class returnType, int[] modifiers)
209:                    throws DomainMetaDataCheckException {
210:                // local variables
211:                Method m = null;
212:
213:                StringBuffer parametersBuf = null, modifierBuf = null;
214:
215:                boolean isAbstract = false, isFinal = false, isPrivate = false, isProtected = false, isPublic = false, isStatic = false, isSynchronized = false;
216:
217:                String returnTypeString = returnType.isArray() ? returnType
218:                        .getComponentType().getName()
219:                        + "[]" : returnType.getName();
220:
221:                String space = " ";
222:
223:                // create the parameters buffer
224:                parametersBuf = new StringBuffer();
225:                if (parameterTypes != null) {
226:                    for (int i = 0; i < parameterTypes.length; i++) {
227:                        if (i > 0)
228:                            parametersBuf.append(", ");
229:                        Class parameterClass = parameterTypes[i];
230:                        String parameter = parameterClass.isArray() ? parameterClass
231:                                .getComponentType().getName()
232:                                + "[]"
233:                                : parameterClass.getName();
234:                        parametersBuf.append(parameter);
235:                    }
236:                }
237:
238:                // create the modifiers buffer & also set the booleans
239:                if (modifiers != null) {
240:                    Arrays.sort(modifiers);
241:                    modifierBuf = new StringBuffer();
242:                    if (Arrays.binarySearch(modifiers, Modifier.PUBLIC) >= 0) {
243:                        isPublic = true;
244:                        modifierBuf.append(PUBLIC);
245:                        modifierBuf.append(space);
246:                    }
247:                    if (Arrays.binarySearch(modifiers, Modifier.PRIVATE) >= 0) {
248:                        isPrivate = true;
249:                        modifierBuf.append(PRIVATE);
250:                        modifierBuf.append(space);
251:                    }
252:                    if (Arrays.binarySearch(modifiers, Modifier.PROTECTED) >= 0) {
253:                        isProtected = true;
254:                        modifierBuf.append(PROTECTED);
255:                        modifierBuf.append(space);
256:                    }
257:                    if (Arrays.binarySearch(modifiers, Modifier.ABSTRACT) >= 0) {
258:                        isAbstract = true;
259:                        modifierBuf.append(ABSTRACT);
260:                        modifierBuf.append(space);
261:                    }
262:                    if (Arrays.binarySearch(modifiers, Modifier.STATIC) >= 0) {
263:                        isStatic = true;
264:                        modifierBuf.append(STATIC);
265:                        modifierBuf.append(space);
266:                    }
267:                    if (Arrays.binarySearch(modifiers, Modifier.FINAL) >= 0) {
268:                        isFinal = true;
269:                        modifierBuf.append(FINAL);
270:                        modifierBuf.append(space);
271:                    }
272:                    if (Arrays.binarySearch(modifiers, Modifier.SYNCHRONIZED) >= 0) {
273:                        isSynchronized = true;
274:                        modifierBuf.append(SYNCHRONIZED);
275:                        modifierBuf.append(space);
276:                    }
277:                }
278:
279:                try {
280:                    // check if method with the required signature exists
281:                    m = clazz.getDeclaredMethod(methodName, parameterTypes);
282:
283:                    // check the return-type
284:                    if (!m.getReturnType().getName().equals(
285:                            returnType.getName()))
286:                        throw new NoSuchMethodException();
287:
288:                    // check the modifier
289:                    // NOTE: This isnt an exact match !!!
290:                    // for eg. the method could very well be 'final', so long as it matches the input criteria...
291:                    // should work.. unless someone complains.
292:                    int mod = m.getModifiers();
293:                    if (isPublic && !Modifier.isPublic(mod))
294:                        throw new NoSuchMethodException();
295:                    if (isPrivate && !Modifier.isPrivate(mod))
296:                        throw new NoSuchMethodException();
297:                    if (isProtected && !Modifier.isProtected(mod))
298:                        throw new NoSuchMethodException();
299:                    if (isAbstract && !Modifier.isAbstract(mod))
300:                        throw new NoSuchMethodException();
301:                    if (isStatic && !Modifier.isStatic(mod))
302:                        throw new NoSuchMethodException();
303:                    if (isFinal && !Modifier.isFinal(mod))
304:                        throw new NoSuchMethodException();
305:                    if (isSynchronized && !Modifier.isSynchronized(mod))
306:                        throw new NoSuchMethodException();
307:                } catch (NoSuchMethodException e) {
308:                    log("Method " + clazz.getName() + ": " + modifierBuf
309:                            + returnTypeString + " " + methodName + "("
310:                            + parametersBuf + ") not found");
311:                }
312:                return m;
313:            }
314:
315:            private void checkGetName(Method m)
316:                    throws DomainMetaDataCheckException {
317:                String methodName = "String getName()";
318:                String nameFromMethod = null;
319:                String expectedName = null;
320:
321:                if (m != null) {
322:                    try {
323:                        nameFromMethod = (String) m.invoke(null, null);
324:                        expectedName = m_domainClass.getName();
325:                        if (nameFromMethod == null) {
326:                            log("Method  - " + methodName
327:                                    + " - does not return any value");
328:                        } else if (!nameFromMethod.equals(expectedName)) {
329:                            log("Method  - " + methodName
330:                                    + " - returns an invalid value '"
331:                                    + nameFromMethod
332:                                    + "', instead of the expected value '"
333:                                    + expectedName + "'");
334:                        }
335:                    } catch (Exception e) {
336:                        log("Error in invoking the method  - " + methodName);
337:                    }
338:                } else {
339:                    log("Method  - " + methodName
340:                            + " - has not been defined, hence cannot be tested");
341:                }
342:            }
343:
344:            private void checkGetRefName(Method m)
345:                    throws DomainMetaDataCheckException {
346:                String methodName = "String getRefName()";
347:                String nameFromMethod = null;
348:                String expectedName = null;
349:
350:                if (m != null) {
351:                    try {
352:                        nameFromMethod = (String) m.invoke(null, null);
353:                        expectedName = extractRefName(m_domainClass.getName());
354:                        if (nameFromMethod == null) {
355:                            log("Method  - " + methodName
356:                                    + " - does not return any value");
357:                        } else if (!nameFromMethod.equals(expectedName)) {
358:                            log("Method  - " + methodName
359:                                    + " - returns an invalid value '"
360:                                    + nameFromMethod
361:                                    + "', instead of the expected value '"
362:                                    + expectedName + "'");
363:                        }
364:                    } catch (Exception e) {
365:                        log("Error in invoking the method  - " + methodName);
366:                    }
367:                } else {
368:                    log("Method  - " + methodName
369:                            + " - has not been defined, hence cannot be tested");
370:                }
371:            }
372:
373:            private String[] checkGetAttributes(Method m)
374:                    throws DomainMetaDataCheckException {
375:                String methodName = "String[] getAttributes()";
376:                String[] attributesFromMethod = null;
377:                Field[] publicStaticFields = null;
378:                Object[] attributesFromFields = null;
379:
380:                if (m != null) {
381:                    try {
382:                        // get the attributes from the method
383:                        attributesFromMethod = (String[]) m.invoke(null, null);
384:                        if (attributesFromMethod != null)
385:                            Arrays.sort(attributesFromMethod);
386:
387:                        // get the array of 'public static String' fields
388:                        publicStaticFields = getPublicStaticFields(m_metaClass,
389:                                String.class);
390:
391:                        // get the array of objects from the 'public static String' fields
392:                        if (publicStaticFields != null)
393:                            attributesFromFields = getPublicStaticFieldValues(publicStaticFields);
394:                        if (attributesFromFields != null)
395:                            Arrays.sort(attributesFromFields);
396:
397:                        if (publicStaticFields != null
398:                                && attributesFromFields == null) {
399:                            log("Error in reading the values for the 'public static String' fields");
400:                        } else if (attributesFromMethod != null
401:                                || attributesFromFields != null) {
402:                            if (!Arrays.equals(attributesFromMethod,
403:                                    attributesFromFields)) {
404:                                StringBuffer msg = new StringBuffer();
405:                                msg
406:                                        .append("There is a mismatch between the values returned by the method '");
407:                                msg.append(methodName);
408:                                msg
409:                                        .append("' and the 'public static String' fields");
410:                                Map map = compareArrays(attributesFromMethod,
411:                                        attributesFromFields);
412:                                if (map != null) {
413:                                    Object obj1 = map.get(KEY1);
414:                                    Object obj2 = map.get(KEY2);
415:                                    if (obj1 != null) {
416:                                        msg
417:                                                .append("\nMethod returns the following extra attributes:\n");
418:                                        msg.append(obj1);
419:                                    }
420:                                    if (obj2 != null) {
421:                                        msg
422:                                                .append("\nThe following extra attributes are available from the fields:\n");
423:                                        msg.append(obj2);
424:                                    }
425:                                }
426:                                log(msg.toString());
427:                            }
428:                        }
429:                    } catch (Exception e) {
430:                        log("Error in invoking the method  - " + methodName);
431:                    }
432:                } else {
433:                    log("Method  - " + methodName
434:                            + " - has not been defined, hence cannot be tested");
435:                }
436:                return attributesFromMethod;
437:            }
438:
439:            private Field[] getPublicStaticFields(Class clazz, Class dataType) {
440:                // retrieve public fields & add the 'static String' to the list
441:                Field[] fields = clazz.getFields();
442:                List list = new ArrayList();
443:                for (int i = 0; i < fields.length; i++) {
444:                    Field field = fields[i];
445:                    int mod = field.getModifiers();
446:                    if (Modifier.isStatic(mod)
447:                            && field.getType().getName().equals(
448:                                    dataType.getName()))
449:                        list.add(field);
450:                }
451:
452:                // create an array from the list
453:                // tried the list.toArray().. but no success. maybe directly create an array instead of the list.
454:                Field[] out = null;
455:                if (!list.isEmpty()) {
456:                    out = new Field[list.size()];
457:                    int i = 0;
458:                    for (Iterator itr = list.iterator(); itr.hasNext(); i++)
459:                        out[i] = (Field) itr.next();
460:                }
461:                return out;
462:            }
463:
464:            public Object[] getPublicStaticFieldValues(Field[] fields) {
465:                Object[] out = null;
466:                if (fields != null) {
467:                    out = new Object[fields.length];
468:                    try {
469:                        for (int i = 0; i < fields.length; i++)
470:                            out[i] = fields[i].get(null);
471:                    } catch (IllegalAccessException e) {
472:                        // this should never happen since we are accessing public fields !!
473:                        out = null;
474:                    }
475:                }
476:                return out;
477:            }
478:
479:            private Map compareArrays(Object[] array1, Object[] array2) {
480:                Map map = new HashMap();
481:                StringBuffer buf1 = new StringBuffer();
482:                StringBuffer buf2 = new StringBuffer();
483:                if (array1 != null) {
484:                    for (int i = 0; i < array1.length; i++) {
485:                        if (array2 == null
486:                                || Arrays.binarySearch(array2, array1[i]) < 0) {
487:                            if (buf1.length() > 0)
488:                                buf1.append('\n');
489:                            buf1.append(array1[i].toString());
490:                        }
491:                    }
492:                }
493:
494:                if (array2 != null) {
495:                    for (int i = 0; i < array2.length; i++) {
496:                        if (array1 == null
497:                                || Arrays.binarySearch(array1, array2[i]) < 0) {
498:                            if (buf2.length() > 0)
499:                                buf2.append('\n');
500:                            buf2.append(array2[i].toString());
501:                        }
502:                    }
503:                }
504:
505:                if (buf1.length() > 0)
506:                    map.put(KEY1, buf1.toString());
507:                if (buf2.length() > 0)
508:                    map.put(KEY2, buf2.toString());
509:                return map;
510:            }
511:
512:            private FieldMetaData[] checkGetFieldMetaData(Method m)
513:                    throws DomainMetaDataCheckException {
514:                String methodName = "FieldMetaData[] getFieldMetaData()";
515:                FieldMetaData[] attributesFromMethod = null;
516:                Field[] publicStaticFields = null;
517:                Object[] attributesFromFields = null;
518:
519:                if (m != null) {
520:                    try {
521:                        // get the attributes from the method
522:                        attributesFromMethod = (FieldMetaData[]) m.invoke(null,
523:                                null);
524:                        if (attributesFromMethod != null)
525:                            Arrays.sort(attributesFromMethod);
526:
527:                        // get the array of 'public static String' fields
528:                        publicStaticFields = getPublicStaticFields(m_metaClass,
529:                                FieldMetaData.class);
530:
531:                        // get the array of objects from the 'public static String' fields
532:                        if (publicStaticFields != null)
533:                            attributesFromFields = getPublicStaticFieldValues(publicStaticFields);
534:                        if (attributesFromFields != null)
535:                            Arrays.sort(attributesFromFields);
536:
537:                        if (publicStaticFields != null
538:                                && attributesFromFields == null) {
539:                            log("Error in reading the values for the 'public static FieldMetaData' fields");
540:                        } else if (attributesFromMethod != null
541:                                || attributesFromFields != null) {
542:                            if (!Arrays.equals(attributesFromMethod,
543:                                    attributesFromFields)) {
544:                                StringBuffer msg = new StringBuffer();
545:                                msg
546:                                        .append("There is a mismatch between the values returned by the method '");
547:                                msg.append(methodName);
548:                                msg
549:                                        .append("' and the 'public static FieldMetaData' fields");
550:                                Map map = compareArrays(attributesFromMethod,
551:                                        attributesFromFields);
552:                                if (map != null) {
553:                                    Object obj1 = map.get(KEY1);
554:                                    Object obj2 = map.get(KEY2);
555:                                    if (obj1 != null) {
556:                                        msg
557:                                                .append("\nMethod returns the following extra attributes:\n");
558:                                        msg.append(obj1);
559:                                    }
560:                                    if (obj2 != null) {
561:                                        msg
562:                                                .append("\nThe following extra attributes are available from the fields:\n");
563:                                        msg.append(obj2);
564:                                    }
565:                                }
566:                                log(msg.toString());
567:                            }
568:                        }
569:                    } catch (Exception e) {
570:                        log("Error in invoking the method  - " + methodName);
571:                    }
572:                } else {
573:                    log("Method  - " + methodName
574:                            + " - has not been defined, hence cannot be tested");
575:                }
576:                return attributesFromMethod;
577:            }
578:
579:            private void checkAttributeAndField(String[] attributes,
580:                    FieldMetaData[] metaFields)
581:                    throws DomainMetaDataCheckException {
582:                if (attributes == null && metaFields == null)
583:                    return;
584:
585:                // create a String-array from the FieldMetaData-array
586:                String[] metaFieldAttributes = null;
587:                if (metaFields != null) {
588:                    metaFieldAttributes = new String[metaFields.length];
589:                    for (int i = 0; i < metaFields.length; i++)
590:                        metaFieldAttributes[i] = metaFields[i].getName();
591:                    Arrays.sort(metaFieldAttributes);
592:                }
593:
594:                if (!Arrays.equals(attributes, metaFieldAttributes)) {
595:                    StringBuffer msg = new StringBuffer();
596:                    msg
597:                            .append("There is a mismatch between the values returned by the method 'getAttributes()' and those returned by the method 'getFieldMetaData()'");
598:                    Map map = compareArrays(attributes, metaFieldAttributes);
599:                    if (map != null) {
600:                        Object obj1 = map.get(KEY1);
601:                        Object obj2 = map.get(KEY2);
602:                        if (obj1 != null) {
603:                            msg
604:                                    .append("\nMethod 'getAttributes()' returns the following extra attributes:\n");
605:                            msg.append(obj1);
606:                        }
607:                        if (obj2 != null) {
608:                            msg
609:                                    .append("\nMethod 'getFieldMetaData()' returns the following extra attributes:\n");
610:                            msg.append(obj2);
611:                        }
612:                    }
613:                    log(msg.toString());
614:                }
615:            }
616:
617:            // check if the method returns a value for each value in the string
618:            private void checkGetFieldMetaDataString(Method m,
619:                    String[] attributes) throws DomainMetaDataCheckException {
620:                String methodName = "FieldMetaData getFieldMetaData(String)";
621:
622:                if (m != null) {
623:                    try {
624:                        if (attributes != null) {
625:                            StringBuffer buf = new StringBuffer();
626:                            for (int i = 0; i < attributes.length; i++) {
627:                                Object obj = m.invoke(null,
628:                                        new Object[] { attributes[i] });
629:                                if (obj == null)
630:                                    buf.append(attributes[i]);
631:                            }
632:                            if (buf.length() > 0) {
633:                                buf
634:                                        .insert(
635:                                                0,
636:                                                "Method  - "
637:                                                        + methodName
638:                                                        + " - does not return values for the following attributes:\n");
639:                                log(buf.toString());
640:                            }
641:                        }
642:                    } catch (Exception e) {
643:                        log("Error in invoking the method  - " + methodName);
644:                    }
645:                } else {
646:                    log("Method  - " + methodName
647:                            + " - has not been defined, hence cannot be tested");
648:                }
649:            }
650:
651:            private void checkMetaAndDomainSync(FieldMetaData[] metaFields)
652:                    throws DomainMetaDataCheckException {
653:                if (metaFields != null) {
654:                    for (int i = 0; i < metaFields.length; i++) {
655:                        FieldMetaData metaField = metaFields[i];
656:                        String name = toFirstCharUpperCase(metaField.getName());
657:                        String dataType = metaField.getDataType();
658:                        Class dataTypeClass = org.jaffa.datatypes.Defaults
659:                                .getClass(dataType);
660:
661:                        // check the getter
662:                        getMethod(m_domainClass, "get" + name, null,
663:                                dataTypeClass, new int[] { Modifier.PUBLIC });
664:
665:                        // check the setter
666:                        getMethod(m_domainClass, "set" + name,
667:                                new Class[] { dataTypeClass }, void.class,
668:                                new int[] { Modifier.PUBLIC });
669:
670:                        // check the updater
671:                        getMethod(m_domainClass, "update" + name,
672:                                new Class[] { dataTypeClass }, void.class,
673:                                new int[] { Modifier.PUBLIC });
674:                    }
675:                }
676:            }
677:
678:            private String toFirstCharUpperCase(String in) {
679:                String out = null;
680:                if (in != null) {
681:                    out = "" + Character.toUpperCase(in.charAt(0))
682:                            + ((in.length() > 1) ? in.substring(1) : "");
683:                }
684:                return out;
685:            }
686:
687:            // example use
688:            public static void main(String[] args) {
689:                try {
690:                    DomainMetaDataCheck chk = null;
691:                    if (args.length == 1)
692:                        chk = new DomainMetaDataCheck(args[0]);
693:                    else if (args.length == 2)
694:                        chk = new DomainMetaDataCheck(args[0], args[1]);
695:                    else
696:                        throw new Error(
697:                                "Usage: <metaClassNameWithPackage> OR <metaClassNameWithPackage> <domainClassNameWithPackage>");
698:
699:                    System.out.println("Checking meta class : "
700:                            + chk.m_metaClass);
701:                    System.out.println("and domain class    : "
702:                            + chk.m_domainClass);
703:                    chk.performChecks();
704:                    System.out.println("Finished Checks");
705:                } catch (DomainMetaDataCheckException e) {
706:                    System.out.println(e.getMessage());
707:                } catch (Exception e) {
708:                    e.printStackTrace();
709:                }
710:            }
711:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.