|
23 | 23 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
24 | 24 |
|
25 | 25 | import java.io.File; |
26 | | -import java.sql.SQLException; |
27 | 26 | import java.util.ArrayList; |
28 | 27 | import java.util.List; |
| 28 | +import java.util.Objects; |
29 | 29 |
|
30 | 30 | import org.hibernate.boot.Metadata; |
31 | 31 | import org.hibernate.mapping.PersistentClass; |
|
42 | 42 | import org.hibernate.tools.test.util.JdbcUtil; |
43 | 43 | import org.junit.jupiter.api.AfterEach; |
44 | 44 | import org.junit.jupiter.api.BeforeEach; |
45 | | -import org.junit.jupiter.api.Disabled; |
46 | 45 | import org.junit.jupiter.api.Test; |
47 | 46 | import org.junit.jupiter.api.io.TempDir; |
48 | 47 |
|
|
51 | 50 | * @author koen |
52 | 51 | */ |
53 | 52 | public class TestCase { |
54 | | - |
55 | | - @TempDir |
56 | | - public File outputFolder = new File("output"); |
57 | | - |
58 | | - private MetadataDescriptor metadataDescriptor = null; |
59 | 53 |
|
60 | | - @BeforeEach |
61 | | - public void setUp() { |
62 | | - JdbcUtil.createDatabase(this); |
63 | | - AbstractStrategy c = new AbstractStrategy() { |
64 | | - public List<SchemaSelection> getSchemaSelections() { |
65 | | - List<SchemaSelection> selections = new ArrayList<SchemaSelection>(); |
66 | | - selections.add(createSchemaSelection(null, "HTT", null)); |
67 | | - selections.add(createSchemaSelection(null, "OTHERSCHEMA", null)); |
68 | | - selections.add(createSchemaSelection(null, "THIRDSCHEMA", null)); |
69 | | - return selections; |
70 | | - } |
71 | | - }; |
72 | | - metadataDescriptor = MetadataDescriptorFactory |
73 | | - .createReverseEngineeringDescriptor(c, null); |
74 | | - } |
| 54 | + @TempDir |
| 55 | + public File outputFolder = new File("output"); |
75 | 56 |
|
76 | | - @AfterEach |
77 | | - public void tearDown() { |
78 | | - JdbcUtil.dropDatabase(this); |
79 | | - } |
| 57 | + private MetadataDescriptor metadataDescriptor = null; |
80 | 58 |
|
81 | | - // TODO Investigate the ignored test: HBX-1410 |
82 | | - @Disabled |
83 | | - @Test |
84 | | - public void testTernaryModel() throws SQLException { |
85 | | - assertMultiSchema(metadataDescriptor.createMetadata()); |
86 | | - } |
| 59 | + @BeforeEach |
| 60 | + public void setUp() { |
| 61 | + JdbcUtil.createDatabase(this); |
| 62 | + AbstractStrategy c = new AbstractStrategy() { |
| 63 | + public List<SchemaSelection> getSchemaSelections() { |
| 64 | + List<SchemaSelection> selections = new ArrayList<>(); |
| 65 | + selections.add(createSchemaSelection("PUBLIC")); |
| 66 | + selections.add(createSchemaSelection("OTHERSCHEMA")); |
| 67 | + selections.add(createSchemaSelection("THIRDSCHEMA")); |
| 68 | + return selections; |
| 69 | + } |
| 70 | + }; |
| 71 | + metadataDescriptor = MetadataDescriptorFactory |
| 72 | + .createReverseEngineeringDescriptor(c, null); |
| 73 | + } |
87 | 74 |
|
88 | | - // TODO Investigate the ignored test: HBX-1410 |
89 | | - @Disabled |
90 | | - @Test |
91 | | - public void testGeneration() { |
92 | | - HbmExporter hme = new HbmExporter(); |
93 | | - hme.getProperties().put(ExporterConstants.METADATA_DESCRIPTOR, metadataDescriptor); |
94 | | - hme.getProperties().put(ExporterConstants.DESTINATION_FOLDER, outputFolder); |
95 | | - hme.start(); |
96 | | - JUnitUtil.assertIsNonEmptyFile( new File(outputFolder, "Role.hbm.xml") ); |
97 | | - JUnitUtil.assertIsNonEmptyFile( new File(outputFolder, "User.hbm.xml") ); |
98 | | - JUnitUtil.assertIsNonEmptyFile( new File(outputFolder, "Plainrole.hbm.xml") ); |
99 | | - assertEquals(3, outputFolder.listFiles().length); |
100 | | - File[] files = new File[3]; |
101 | | - files[0] = new File(outputFolder, "Role.hbm.xml"); |
102 | | - files[0] = new File(outputFolder, "User.hbm.xml"); |
103 | | - files[0] = new File(outputFolder, "Plainrole.hbm.xml"); |
104 | | - assertMultiSchema(MetadataDescriptorFactory |
105 | | - .createNativeDescriptor(null, files, null) |
106 | | - .createMetadata()); |
107 | | - } |
108 | | - |
109 | | - private void assertMultiSchema(Metadata metadata) { |
110 | | - JUnitUtil.assertIteratorContainsExactly( |
111 | | - "There should be five tables!", |
112 | | - metadata.getEntityBindings().iterator(), |
113 | | - 5); |
114 | | - final PersistentClass role = metadata.getEntityBinding("Role"); |
115 | | - assertNotNull(role); |
116 | | - PersistentClass userroles = metadata.getEntityBinding("Userroles"); |
117 | | - assertNotNull(userroles); |
118 | | - PersistentClass user = metadata.getEntityBinding("User"); |
119 | | - assertNotNull(user); |
120 | | - PersistentClass plainRole = metadata.getEntityBinding("Plainrole"); |
121 | | - assertNotNull(plainRole); |
122 | | - Property property = role.getProperty("users"); |
123 | | - assertEquals(role.getTable().getSchema(), "OTHERSCHEMA"); |
124 | | - assertNotNull(property); |
125 | | - property.getValue().accept(new DefaultValueVisitor(true) { |
126 | | - public Object accept(Set o) { |
127 | | - assertEquals(o.getCollectionTable().getSchema(), "THIRDSCHEMA"); |
128 | | - return null; |
129 | | - } |
130 | | - }); |
131 | | - property = plainRole.getProperty("users"); |
132 | | - assertEquals(role.getTable().getSchema(), "OTHERSCHEMA"); |
133 | | - assertNotNull(property); |
134 | | - property.getValue().accept(new DefaultValueVisitor(true) { |
135 | | - public Object accept(Set o) { |
136 | | - assertEquals(o.getCollectionTable().getSchema(), null); |
137 | | - return null; |
138 | | - } |
139 | | - }); |
140 | | - } |
141 | | - |
142 | | - private SchemaSelection createSchemaSelection(String matchCatalog, String matchSchema, String matchTable) { |
143 | | - return new SchemaSelection() { |
144 | | - @Override |
145 | | - public String getMatchCatalog() { |
146 | | - return matchCatalog; |
147 | | - } |
148 | | - @Override |
149 | | - public String getMatchSchema() { |
150 | | - return matchSchema; |
151 | | - } |
152 | | - @Override |
153 | | - public String getMatchTable() { |
154 | | - return matchTable; |
155 | | - } |
156 | | - }; |
157 | | - } |
| 75 | + @AfterEach |
| 76 | + public void tearDown() { |
| 77 | + JdbcUtil.dropDatabase(this); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testTernaryModel() { |
| 82 | + assertMultiSchema(metadataDescriptor.createMetadata()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testGeneration() { |
| 87 | + HbmExporter hme = new HbmExporter(); |
| 88 | + hme.getProperties().put(ExporterConstants.METADATA_DESCRIPTOR, metadataDescriptor); |
| 89 | + hme.getProperties().put(ExporterConstants.DESTINATION_FOLDER, outputFolder); |
| 90 | + hme.start(); |
| 91 | + JUnitUtil.assertIsNonEmptyFile( new File(outputFolder, "Role.hbm.xml") ); |
| 92 | + JUnitUtil.assertIsNonEmptyFile( new File(outputFolder, "Member.hbm.xml") ); |
| 93 | + JUnitUtil.assertIsNonEmptyFile( new File(outputFolder, "Plainrole.hbm.xml") ); |
| 94 | + assertEquals(3, Objects.requireNonNull( outputFolder.listFiles() ).length); |
| 95 | + File[] files = new File[3]; |
| 96 | + files[0] = new File(outputFolder, "Role.hbm.xml"); |
| 97 | + files[1] = new File(outputFolder, "Member.hbm.xml"); |
| 98 | + files[2] = new File(outputFolder, "Plainrole.hbm.xml"); |
| 99 | + assertMultiSchema(MetadataDescriptorFactory |
| 100 | + .createNativeDescriptor(null, files, null) |
| 101 | + .createMetadata()); |
| 102 | + } |
| 103 | + |
| 104 | + private void assertMultiSchema(Metadata metadata) { |
| 105 | + JUnitUtil.assertIteratorContainsExactly( |
| 106 | + "There should be three entities!", |
| 107 | + metadata.getEntityBindings().iterator(), |
| 108 | + 3); |
| 109 | + final PersistentClass role = metadata.getEntityBinding("Role"); |
| 110 | + assertNotNull(role); |
| 111 | + PersistentClass member = metadata.getEntityBinding("Member"); |
| 112 | + assertNotNull(member); |
| 113 | + PersistentClass plainRole = metadata.getEntityBinding("Plainrole"); |
| 114 | + assertNotNull(plainRole); |
| 115 | + Property property = role.getProperty("members"); |
| 116 | + assertEquals( "OTHERSCHEMA", role.getTable().getSchema() ); |
| 117 | + assertNotNull(property); |
| 118 | + property.getValue().accept(new DefaultValueVisitor(true) { |
| 119 | + public Object accept(Set o) { |
| 120 | + assertEquals( "THIRDSCHEMA", o.getCollectionTable().getSchema() ); |
| 121 | + return null; |
| 122 | + } |
| 123 | + }); |
| 124 | + property = plainRole.getProperty("members"); |
| 125 | + assertEquals( "OTHERSCHEMA", role.getTable().getSchema() ); |
| 126 | + assertNotNull(property); |
| 127 | + property.getValue().accept(new DefaultValueVisitor(true) { |
| 128 | + public Object accept(Set o) { |
| 129 | + assertEquals("PUBLIC", o.getCollectionTable().getSchema() ); |
| 130 | + return null; |
| 131 | + } |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + private SchemaSelection createSchemaSelection(String matchSchema) { |
| 136 | + return new SchemaSelection() { |
| 137 | + @Override |
| 138 | + public String getMatchCatalog() { |
| 139 | + return null; |
| 140 | + } |
| 141 | + @Override |
| 142 | + public String getMatchSchema() { |
| 143 | + return matchSchema; |
| 144 | + } |
| 145 | + @Override |
| 146 | + public String getMatchTable() { |
| 147 | + return null; |
| 148 | + } |
| 149 | + }; |
| 150 | + } |
158 | 151 | } |
0 commit comments