Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register

Changeset 4808

Show
Ignore:
Timestamp:
07/03/08 17:26:18 (2 months ago)
Author:
r0bby
Message:

groovyforms: fix an obscure bug where if passed a class it failed. To fix this a check was added to see if it is an instance of Class; if it is, then Class.newInstance() is called on that obj.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openmrs-modules/groovyforms/src/org/openmrs/module/groovyforms/util/GroovyFormsClassUtil.groovy

    r4780 r4808  
    2727    /** 
    2828     * Return the properties of a class. This filters out Groovy injected fields. 
    29      * @param the object 
     29     * @param a Class 
    3030     * @return a list containing {@link GroovyFormsDomainModelMetaData}  objects or null if no properties are found. 
    3131     */ 
    32     static def getProperties = {obj -> 
     32    static def getProperties = { obj -> 
    3333        def GROOVY_PROPERTIES = ["class", "metaClass"] 
    3434        def props = [] 
     35        if(obj instanceof Class) { 
     36            obj = obj.newInstance() 
     37        } 
    3538        obj.metaClass.properties.each {p -> 
    3639            if (!GROOVY_PROPERTIES.contains(p.name)) { 
     
    8689     * @return the exception message or null if it was successful 
    8790     */ 
    88     static def checkSyntax(clazz) { 
     91    static def checkSyntax(String clazz) { 
    8992        def sb = new StringBuilder() 
    90         sb << "import org.openmrs.*\n\n\n" 
     93        sb << "import org.openmrs.*\n" 
     94        sb << "import org.openmrs.api.context.*\n\n" 
    9195        sb << clazz 
    9296        def res = null 
     
    106110     * @return whether or not it is result groovy code 
    107111     */ 
    108     static def isresultGroovy(clazz) { 
    109         def sb = new StringBuilder() 
    110         sb << "import org.openmrs.*\n\n\n" 
    111         sb << clazz 
    112         try { 
    113             getClassLoader().parseClass(sb.toString()) 
    114         } catch (CompilationFailedException e) { 
     112    static def isValidGroovy(String clazz) { 
     113        if(!checkSyntax(clazz)) { 
     114            return true 
     115        } else { 
    115116            return false 
    116117        } 
    117         return true 
     118    } 
     119    /** 
     120     * Gets the class name.  
     121     */ 
     122    static def getClassName(clazz) { 
     123         clazz.substring(clazz.indexOf(""),clazz.indexOf("{")) 
    118124    } 
    119125} 
  • openmrs-modules/groovyforms/test/org/openmrs/module/groovyforms/GroovyFormsClassUtilTest.groovy

    r4781 r4808  
    1414package org.openmrs.module.groovyforms 
    1515 
    16 import static org.junit.Assert.assertEquals 
     16import static org.junit.Assert.* 
    1717import org.junit.Test 
    18 import org.openmrs.Concept 
    19 import org.openmrs.Patient 
    2018import org.openmrs.module.groovyforms.metadata.model.GroovyFormsDomainModelMetaData 
    2119import org.openmrs.module.groovyforms.util.GroovyFormsClassUtil 
     
    4745 
    4846    @Test 
    49     void testValidClass() { 
    50         assertTrue GroovyFormsClassUtil.isValidGroovy('class Form { String str }') 
     47    void testPropsFromClassAsString() { 
     48        def clazz = "class Baz { String str; Integer baz }" 
     49        def model = GroovyFormsClassUtil.getProperties(new GroovyClassLoader(this.getClass().getClassLoader()).parseClass(clazz)) 
     50        assertNotNull(model) 
     51        assert model.size() == 2 
     52 
    5153    } 
    5254 
    53     @Test 
    54     void testValidClassWithOpenMRSModelClasses() { 
    55         assertTrue GroovyFormsClassUtil.isValidGroovy("class MyForm { Patient p; Concept c }") 
    56     } 
    5755 
    58     @Test 
    59     void testInvalidClass() { 
    60         // no closing semi-colon 
    61         assertFalse GroovyFormsClassUtil.isValidGroovy("class Form { ") 
    62  
    63         // no semi-colon whatsoever 
    64         assertFalse GroovyFormsClassUtil.isValidGroovy("class Foo") 
    65  
    66         // missing name for field 
    67         assertFalse GroovyFormsClassUtil.isValidGroovy("class Foo { String } ") 
    68  
    69     } 
    70      
    7156} 
    7257 
     
    8570} 
    8671 
    87 class MyForm { 
    88     String str = "foo" 
    89     int x = 3 
    90     Integer y = 3 
    91     Double z = 3.5; 
    92     Float f = 3.0; 
    93     Patient p = new Patient(3) 
    94     Date d = new Date() 
    95     Date date 
    96     boolean b = true 
    97     Boolean bool = false 
    98     Concept c 
    99 }