Ejemplo n.º 1
0
  public void testDomainClassBinding() {
    GroovyClassLoader cl = new GroovyClassLoader();
    GrailsDomainClass domainClass =
        new DefaultGrailsDomainClass(
            cl.parseClass(
                "public class BinderTestClass {\n"
                    + "    Long id; \n"
                    + "    Long version; \n"
                    + "\n"
                    + "    String firstName; \n"
                    + "    String lastName; \n"
                    + "    String comment; \n"
                    + "    Integer age;\n"
                    + "    boolean active = true"
                    + "\n"
                    + "    static constraints = {\n"
                    + "        firstName(nullable:true,size:4..15)\n"
                    + "        lastName(nullable:false)\n"
                    + "        age(nullable:true)\n"
                    + "    }\n"
                    + "}"));

    DefaultGrailsDomainConfiguration config = getDomainConfig(cl, cl.getLoadedClasses());

    // Test database mappings
    PersistentClass persistentClass = config.getClassMapping("BinderTestClass");
    assertTrue(
        "Property [firstName] must be optional in db mapping",
        persistentClass.getProperty("firstName").isOptional());
    assertFalse(
        "Property [lastName] must be required in db mapping",
        persistentClass.getProperty("lastName").isOptional());
    // Property must be required by default
    assertFalse(
        "Property [comment] must be required in db mapping",
        persistentClass.getProperty("comment").isOptional());

    // Test properties
    assertTrue(
        "Property [firstName] must be optional",
        domainClass.getPropertyByName("firstName").isOptional());
    assertFalse(
        "Property [lastName] must be optional",
        domainClass.getPropertyByName("lastName").isOptional());
    assertFalse(
        "Property [comment] must be required",
        domainClass.getPropertyByName("comment").isOptional());
    assertTrue(
        "Property [age] must be optional", domainClass.getPropertyByName("age").isOptional());
  }
Ejemplo n.º 2
0
 /**
  * Returns the class type of the searchable property
  *
  * @param grailsDomainClass
  * @param propertyName
  * @param searchableGrailsDomainClasses
  * @return
  */
 public static Class getSearchablePropertyAssociatedClass(
     GrailsDomainClass grailsDomainClass,
     String propertyName,
     Collection searchableGrailsDomainClasses) {
   Assert.notNull(grailsDomainClass, "grailsDomainClass cannot be null");
   Assert.notNull(propertyName, "propertyName cannot be null");
   return getSearchablePropertyAssociatedClass(
       grailsDomainClass.getPropertyByName(propertyName), searchableGrailsDomainClasses);
 }
Ejemplo n.º 3
0
 private Class<?> getReferencedTypeForCollection(String name, Object target) {
   final GrailsApplication grailsApplication = ApplicationHolder.getApplication();
   if (grailsApplication != null) {
     GrailsDomainClass dc =
         (GrailsDomainClass)
             grailsApplication.getArtefact(
                 DomainClassArtefactHandler.TYPE, target.getClass().getName());
     if (dc != null) {
       GrailsDomainClassProperty domainProperty = dc.getPropertyByName(name);
       if (domainProperty != null) {
         return domainProperty.getReferencedPropertyType();
       }
     }
   }
   return null;
 }