Changeset 4808
- Timestamp:
- 07/03/08 17:26:18 (2 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openmrs-modules/groovyforms/src/org/openmrs/module/groovyforms/util/GroovyFormsClassUtil.groovy
r4780 r4808 27 27 /** 28 28 * Return the properties of a class. This filters out Groovy injected fields. 29 * @param the object29 * @param a Class 30 30 * @return a list containing {@link GroovyFormsDomainModelMetaData} objects or null if no properties are found. 31 31 */ 32 static def getProperties = { obj ->32 static def getProperties = { obj -> 33 33 def GROOVY_PROPERTIES = ["class", "metaClass"] 34 34 def props = [] 35 if(obj instanceof Class) { 36 obj = obj.newInstance() 37 } 35 38 obj.metaClass.properties.each {p -> 36 39 if (!GROOVY_PROPERTIES.contains(p.name)) { … … 86 89 * @return the exception message or null if it was successful 87 90 */ 88 static def checkSyntax( clazz) {91 static def checkSyntax(String clazz) { 89 92 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" 91 95 sb << clazz 92 96 def res = null … … 106 110 * @return whether or not it is result groovy code 107 111 */ 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 { 115 116 return false 116 117 } 117 return true 118 } 119 /** 120 * Gets the class name. 121 */ 122 static def getClassName(clazz) { 123 clazz.substring(clazz.indexOf(""),clazz.indexOf("{")) 118 124 } 119 125 } openmrs-modules/groovyforms/test/org/openmrs/module/groovyforms/GroovyFormsClassUtilTest.groovy
r4781 r4808 14 14 package org.openmrs.module.groovyforms 15 15 16 import static org.junit.Assert. assertEquals16 import static org.junit.Assert.* 17 17 import org.junit.Test 18 import org.openmrs.Concept19 import org.openmrs.Patient20 18 import org.openmrs.module.groovyforms.metadata.model.GroovyFormsDomainModelMetaData 21 19 import org.openmrs.module.groovyforms.util.GroovyFormsClassUtil … … 47 45 48 46 @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 51 53 } 52 54 53 @Test54 void testValidClassWithOpenMRSModelClasses() {55 assertTrue GroovyFormsClassUtil.isValidGroovy("class MyForm { Patient p; Concept c }")56 }57 55 58 @Test59 void testInvalidClass() {60 // no closing semi-colon61 assertFalse GroovyFormsClassUtil.isValidGroovy("class Form { ")62 63 // no semi-colon whatsoever64 assertFalse GroovyFormsClassUtil.isValidGroovy("class Foo")65 66 // missing name for field67 assertFalse GroovyFormsClassUtil.isValidGroovy("class Foo { String } ")68 69 }70 71 56 } 72 57 … … 85 70 } 86 71 87 class MyForm {88 String str = "foo"89 int x = 390 Integer y = 391 Double z = 3.5;92 Float f = 3.0;93 Patient p = new Patient(3)94 Date d = new Date()95 Date date96 boolean b = true97 Boolean bool = false98 Concept c99 }