public void testForeignKeyColumnBinding() {
    GroovyClassLoader cl = new GroovyClassLoader();
    GrailsDomainClass oneClass =
        new DefaultGrailsDomainClass(
            cl.parseClass(
                "class TestOneSide {\n"
                    + "    Long id \n"
                    + "    Long version \n"
                    + "    String name \n"
                    + "    String description \n"
                    + "}"));
    GrailsDomainClass domainClass =
        new DefaultGrailsDomainClass(
            cl.parseClass(
                "class TestManySide {\n"
                    + "    Long id \n"
                    + "    Long version \n"
                    + "    String name \n"
                    + "    TestOneSide testOneSide \n"
                    + "\n"
                    + "    static mapping = {\n"
                    + "        columns {\n"
                    + "            testOneSide column:'EXPECTED_COLUMN_NAME'"
                    + "        }\n"
                    + "    }\n"
                    + "}"));

    DefaultGrailsDomainConfiguration config =
        getDomainConfig(cl, new Class[] {oneClass.getClazz(), domainClass.getClazz()});

    PersistentClass persistentClass = config.getClassMapping("TestManySide");

    Column column = (Column) persistentClass.getProperty("testOneSide").getColumnIterator().next();
    assertEquals("EXPECTED_COLUMN_NAME", column.getName());
  }
  public void testDefaultNamingStrategy() {

    GroovyClassLoader cl = new GroovyClassLoader();
    GrailsDomainClass oneClass =
        new DefaultGrailsDomainClass(
            cl.parseClass(
                "class TestOneSide {\n"
                    + "    Long id \n"
                    + "    Long version \n"
                    + "    String fooName \n"
                    + "    String barDescriPtion \n"
                    + "}"));
    GrailsDomainClass domainClass =
        new DefaultGrailsDomainClass(
            cl.parseClass(
                "class TestManySide {\n"
                    + "    Long id \n"
                    + "    Long version \n"
                    + "    TestOneSide testOneSide \n"
                    + "\n"
                    + "    static mapping = {\n"
                    + "        columns {\n"
                    + "            testOneSide column:'EXPECTED_COLUMN_NAME'"
                    + "        }\n"
                    + "    }\n"
                    + "}"));

    DefaultGrailsDomainConfiguration config =
        getDomainConfig(cl, new Class[] {oneClass.getClazz(), domainClass.getClazz()});

    PersistentClass persistentClass = config.getClassMapping("TestOneSide");
    assertEquals("test_one_side", persistentClass.getTable().getName());

    Column column = (Column) persistentClass.getProperty("id").getColumnIterator().next();
    assertEquals("id", column.getName());

    column = (Column) persistentClass.getProperty("version").getColumnIterator().next();
    assertEquals("version", column.getName());

    column = (Column) persistentClass.getProperty("fooName").getColumnIterator().next();
    assertEquals("foo_name", column.getName());

    column = (Column) persistentClass.getProperty("barDescriPtion").getColumnIterator().next();
    assertEquals("bar_descri_ption", column.getName());

    persistentClass = config.getClassMapping("TestManySide");
    assertEquals("test_many_side", persistentClass.getTable().getName());

    column = (Column) persistentClass.getProperty("id").getColumnIterator().next();
    assertEquals("id", column.getName());

    column = (Column) persistentClass.getProperty("version").getColumnIterator().next();
    assertEquals("version", column.getName());

    column = (Column) persistentClass.getProperty("testOneSide").getColumnIterator().next();
    assertEquals("EXPECTED_COLUMN_NAME", column.getName());
  }
  public void testInsertableUpdateableHibernateMapping() throws Exception {
    GroovyClassLoader cl = new GroovyClassLoader();
    GrailsDomainClass domainClass =
        new DefaultGrailsDomainClass(
            cl.parseClass(
                "class TestInsertableUpdateableDomain {\n"
                    + "    Long id \n"
                    + "    Long version \n"
                    + "    String testString1 \n"
                    + "    String testString2 \n"
                    + "\n"
                    + "    static mapping = {\n"
                    + "       testString1 insertable:false, updateable:false \n"
                    + "       testString2 updateable:false, insertable:false \n"
                    + "    }\n"
                    + "}"));

    DefaultGrailsDomainConfiguration config =
        getDomainConfig(cl, new Class[] {domainClass.getClazz()});

    PersistentClass persistentClass = config.getClassMapping("TestInsertableUpdateableDomain");

    Property property1 = persistentClass.getProperty("testString1");
    assertEquals(false, property1.isInsertable());
    assertEquals(false, property1.isUpdateable());

    Property property2 = persistentClass.getProperty("testString2");
    assertEquals(false, property2.isUpdateable());
    assertEquals(false, property2.isInsertable());
  }
 public boolean isInstance(Object obj) {
   return domainClass.getClazz().isInstance(obj);
 }
 public Class getJavaClass() {
   return domainClass.getClazz();
 }
  public void testCustomNamingStrategy() throws Exception {

    // somewhat artificial in that it doesn't test that setting the property
    // in DataSource.groovy works, but that's handled in DataSourceConfigurationTests
    GrailsDomainBinder.configureNamingStrategy(CustomNamingStrategy.class);

    GroovyClassLoader cl = new GroovyClassLoader();
    GrailsDomainClass oneClass =
        new DefaultGrailsDomainClass(
            cl.parseClass(
                "class TestOneSide {\n"
                    + "    Long id \n"
                    + "    Long version \n"
                    + "    String fooName \n"
                    + "    String barDescriPtion \n"
                    + "}"));
    GrailsDomainClass domainClass =
        new DefaultGrailsDomainClass(
            cl.parseClass(
                "class TestManySide {\n"
                    + "    Long id \n"
                    + "    Long version \n"
                    + "    TestOneSide testOneSide \n"
                    + "\n"
                    + "    static mapping = {\n"
                    + "        columns {\n"
                    + "            testOneSide column:'EXPECTED_COLUMN_NAME'"
                    + "        }\n"
                    + "    }\n"
                    + "}"));

    DefaultGrailsDomainConfiguration config =
        getDomainConfig(cl, new Class[] {oneClass.getClazz(), domainClass.getClazz()});

    PersistentClass persistentClass = config.getClassMapping("TestOneSide");
    assertEquals("table_TestOneSide", persistentClass.getTable().getName());

    Column column = (Column) persistentClass.getProperty("id").getColumnIterator().next();
    assertEquals("col_id", column.getName());

    column = (Column) persistentClass.getProperty("version").getColumnIterator().next();
    assertEquals("col_version", column.getName());

    column = (Column) persistentClass.getProperty("fooName").getColumnIterator().next();
    assertEquals("col_fooName", column.getName());

    column = (Column) persistentClass.getProperty("barDescriPtion").getColumnIterator().next();
    assertEquals("col_barDescriPtion", column.getName());

    persistentClass = config.getClassMapping("TestManySide");
    assertEquals("table_TestManySide", persistentClass.getTable().getName());

    column = (Column) persistentClass.getProperty("id").getColumnIterator().next();
    assertEquals("col_id", column.getName());

    column = (Column) persistentClass.getProperty("version").getColumnIterator().next();
    assertEquals("col_version", column.getName());

    column = (Column) persistentClass.getProperty("testOneSide").getColumnIterator().next();
    assertEquals("EXPECTED_COLUMN_NAME", column.getName());
  }
 /**
  * Is the given GrailsDomainClass a root mapped class in Compass
  *
  * @param domainClass the GrailsDomainClass
  * @param compass Compass
  * @return true if there is a root mapping for the class in Compass
  */
 public static boolean isRootMappedClass(GrailsDomainClass domainClass, Compass compass) {
   return ((InternalCompass) compass).getMapping().getRootMappingByClass(domainClass.getClazz())
       != null;
 }
 private boolean shouldMapWithGorm(GrailsDomainClass domainClass) {
   return !AnnotationDomainClassArtefactHandler.isJPADomainClass(domainClass.getClazz())
       && domainClass.getMappingStrategy().equalsIgnoreCase(GrailsDomainClass.GORM);
 }
Пример #9
0
 /**
  * Get the given domain class's searchable property value, if any
  *
  * @param grailsDomainClass the Grails domain class
  * @return the searchable property value, or null
  */
 public static Object getSearchablePropertyValue(GrailsDomainClass grailsDomainClass) {
   return GrailsClassUtils.getStaticPropertyValue(
       grailsDomainClass.getClazz(), SEARCHABLE_PROPERTY_NAME);
 }