Skip to content

Commit 4bc64c4

Browse files
committed
HBX-2789: Review WrapperFactory class and create only new Wrapper intances
- Add new interface method 'org.hibernate.tool.orm.jbt.api.wrp.TableWrapper#getForeignKeyIterator()' - Add implementation of above interface in 'org.hibernate.tool.orm.jbt.internal.factory.TableWrapperFactory.TableWrapperImpl#getForeignKeyIterator()' - Add additional test case 'org.hibernate.tool.orm.jbt.api.wrp.TableWrapperTest#testGetForeignKeyIterator()' Signed-off-by: Koen Aers <[email protected]>
1 parent d4da59e commit 4bc64c4

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

jbt/src/main/java/org/hibernate/tool/orm/jbt/api/wrp/TableWrapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public interface TableWrapper extends Wrapper {
1010
String getSchema();
1111
PrimaryKeyWrapper getPrimaryKey();
1212
Iterator<ColumnWrapper> getColumnIterator();
13+
Iterator<ForeignKeyWrapper> getForeignKeyIterator();
1314
String getComment();
1415
String getRowId();
1516
String getSubselect();

jbt/src/main/java/org/hibernate/tool/orm/jbt/internal/factory/TableWrapperFactory.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import java.util.Iterator;
44

55
import org.hibernate.mapping.Column;
6+
import org.hibernate.mapping.ForeignKey;
67
import org.hibernate.mapping.PrimaryKey;
78
import org.hibernate.mapping.Table;
89
import org.hibernate.mapping.Value;
910
import org.hibernate.tool.orm.jbt.api.wrp.ColumnWrapper;
11+
import org.hibernate.tool.orm.jbt.api.wrp.ForeignKeyWrapper;
1012
import org.hibernate.tool.orm.jbt.api.wrp.PrimaryKeyWrapper;
1113
import org.hibernate.tool.orm.jbt.api.wrp.TableWrapper;
1214
import org.hibernate.tool.orm.jbt.api.wrp.ValueWrapper;
@@ -77,6 +79,22 @@ public ColumnWrapper next() {
7779

7880
};
7981
}
82+
83+
@Override
84+
public Iterator<ForeignKeyWrapper> getForeignKeyIterator() {
85+
Iterator<ForeignKey> iterator = table.getForeignKeys().values().iterator();
86+
return new Iterator<ForeignKeyWrapper>() {
87+
@Override
88+
public boolean hasNext() {
89+
return iterator.hasNext();
90+
}
91+
@Override
92+
public ForeignKeyWrapper next() {
93+
return ForeignKeyWrapperFactory.createForeignKeyWrapper(iterator.next());
94+
}
95+
96+
};
97+
}
8098

8199
@Override
82100
public String getComment() {

jbt/src/test/java/org/hibernate/tool/orm/jbt/api/wrp/TableWrapperTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010

1111
import java.lang.reflect.Method;
1212
import java.util.Iterator;
13+
import java.util.List;
1314

1415
import org.hibernate.mapping.BasicValue;
1516
import org.hibernate.mapping.Column;
17+
import org.hibernate.mapping.ForeignKey;
1618
import org.hibernate.mapping.KeyValue;
1719
import org.hibernate.mapping.PrimaryKey;
1820
import org.hibernate.mapping.Table;
@@ -88,6 +90,18 @@ public void testGetColumnIterator() {
8890
assertSame(column, columnIterator.next().getWrappedObject());
8991
}
9092

93+
@Test
94+
public void testGetForeignKeyIterator() {
95+
Column column = new Column("Hibernate Tools");
96+
Iterator<ForeignKeyWrapper> foreignKeyIterator = tableWrapper.getForeignKeyIterator();
97+
assertFalse(foreignKeyIterator.hasNext());
98+
wrappedTable.createForeignKey("foo", List.of(column), "bar", "");
99+
foreignKeyIterator = tableWrapper.getForeignKeyIterator();
100+
assertTrue(foreignKeyIterator.hasNext());
101+
ForeignKeyWrapper foreignKeyWrapper = foreignKeyIterator.next();
102+
assertTrue(((ForeignKey)foreignKeyWrapper.getWrappedObject()).containsColumn(column));
103+
}
104+
91105
@Test
92106
public void testGetComment() {
93107
assertNull(tableWrapper.getComment());

0 commit comments

Comments
 (0)