### Question:
ForeignKeyDescFactoryImpl implements ForeignKeyDescFactory { public ForeignKeyDesc getForeignKeyDesc(EntityMeta entityMeta, PropertyMeta propertyMeta) { if (!propertyMeta.isRelationship() || propertyMeta.getMappedBy() != null) { return null; } ReferentialConstraint referentialConstraint = propertyMeta.getField() .getAnnotation(ReferentialConstraint.class); if (referentialConstraint == null) { if (!regardRelationshipAsFk) { return null; } } else { if (!referentialConstraint.enable()) { return null; } } ForeignKeyDesc foreignKeyDesc = new ForeignKeyDesc(); doColumn(entityMeta, propertyMeta, foreignKeyDesc); doTable(entityMeta, propertyMeta, foreignKeyDesc); doReferetialAction(entityMeta, propertyMeta, foreignKeyDesc); return foreignKeyDesc; } ForeignKeyDescFactoryImpl(GenDialect dialect,
            EntityMetaFactory entityMetaFactory, boolean regardRelationshipAsFk); ForeignKeyDesc getForeignKeyDesc(EntityMeta entityMeta,
            PropertyMeta propertyMeta);  }### Answer:
@Test public void testSingleForeignKey() throws Exception { EntityMeta entityMeta = entityMetaFactory.getEntityMeta(Aaa.class); ForeignKeyDesc foreignKeyDesc = foreignKeyDescFactory .getForeignKeyDesc(entityMeta, entityMeta .getPropertyMeta("bbb")); assertNotNull(foreignKeyDesc); assertEquals(1, foreignKeyDesc.getColumnNameList().size()); assertEquals("BBB_ID", foreignKeyDesc.getColumnNameList().get(0)); assertEquals("hoge", foreignKeyDesc.getReferencedCatalogName()); assertEquals("foo", foreignKeyDesc.getReferencedSchemaName()); assertEquals("BBB", foreignKeyDesc.getReferencedTableName()); assertEquals(1, foreignKeyDesc.getReferencedColumnNameList().size()); assertEquals("ID", foreignKeyDesc.getReferencedColumnNameList().get(0)); }
@Test public void testCompositeForeignKey() throws Exception { EntityMeta entityMeta = entityMetaFactory.getEntityMeta(Aaa.class); ForeignKeyDesc foreignKeyDesc = foreignKeyDescFactory .getForeignKeyDesc(entityMeta, entityMeta .getPropertyMeta("ccc")); assertNotNull(foreignKeyDesc); assertEquals(2, foreignKeyDesc.getColumnNameList().size()); assertEquals("CCC_ID1", foreignKeyDesc.getColumnNameList().get(0)); assertEquals("CCC_ID2", foreignKeyDesc.getColumnNameList().get(1)); assertEquals("hoge", foreignKeyDesc.getReferencedCatalogName()); assertEquals("foo", foreignKeyDesc.getReferencedSchemaName()); assertEquals("CCC", foreignKeyDesc.getReferencedTableName()); assertEquals(2, foreignKeyDesc.getReferencedColumnNameList().size()); assertEquals("ID1", foreignKeyDesc.getReferencedColumnNameList().get(0)); assertEquals("ID2", foreignKeyDesc.getReferencedColumnNameList().get(1)); }
@Test public void testReferentialConstraint_true() throws Exception { EntityMeta entityMeta = entityMetaFactory.getEntityMeta(Eee.class); foreignKeyDescFactory = new ForeignKeyDescFactoryImpl( new StandardGenDialect(), entityMetaFactory, false); ForeignKeyDesc foreignKeyDesc = foreignKeyDescFactory .getForeignKeyDesc(entityMeta, entityMeta .getPropertyMeta("bbb")); assertNotNull(foreignKeyDesc); }
@Test public void testReferentialConstraint_false() throws Exception { EntityMeta entityMeta = entityMetaFactory.getEntityMeta(Ddd.class); ForeignKeyDesc foreignKeyDesc = foreignKeyDescFactory .getForeignKeyDesc(entityMeta, entityMeta .getPropertyMeta("bbb")); assertNull(foreignKeyDesc); }
@Test public void testNoForeignKey() throws Exception { EntityMeta entityMeta = entityMetaFactory.getEntityMeta(Aaa.class); ForeignKeyDesc foreignKeyDesc = foreignKeyDescFactory .getForeignKeyDesc(entityMeta, entityMeta.getPropertyMeta("id")); assertNull(foreignKeyDesc); }
@Test public void testInverseRelationship() throws Exception { EntityMeta entityMeta = entityMetaFactory.getEntityMeta(Bbb.class); ForeignKeyDesc foreignKeyDesc = foreignKeyDescFactory .getForeignKeyDesc(entityMeta, entityMeta .getPropertyMeta("aaas")); assertNull(foreignKeyDesc); }