This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
153 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...t/java/lt/grimsa/hibernate/id/SingleGlobalTemporaryTableBulkIdStrategyRowCleanupTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package lt.grimsa.hibernate.id; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.log4j.AppenderSkeleton; | ||
import org.apache.log4j.Level; | ||
import org.apache.log4j.Logger; | ||
import org.apache.log4j.spi.LoggingEvent; | ||
import org.hibernate.Transaction; | ||
import org.hibernate.cfg.AvailableSettings; | ||
import org.hibernate.cfg.Configuration; | ||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; | ||
import org.junit.Test; | ||
|
||
import model.TestEntities.Animal; | ||
import model.TestEntities.Dog; | ||
import model.TestEntities.Human; | ||
import model.TestEntities.Mammal; | ||
import model.TestEntities.Reptile; | ||
|
||
public class SingleGlobalTemporaryTableBulkIdStrategyRowCleanupTest extends BaseCoreFunctionalTestCase { | ||
|
||
private Logger sqlLogger = Logger.getLogger("org.hibernate.SQL"); | ||
|
||
private MessageCapturingAppender sqlAppender; | ||
private boolean ddlExecuted; | ||
|
||
@Override | ||
protected void configure(Configuration configuration) { | ||
configuration.setProperty(AvailableSettings.HQL_BULK_ID_STRATEGY, SingleGlobalTemporaryTableBulkIdStrategy.class.getName()); | ||
configuration.setProperty(SingleGlobalTemporaryTableBulkIdStrategy.TABLE, "HT_TEMP_IDS"); | ||
configuration.setProperty(SingleGlobalTemporaryTableBulkIdStrategy.DISCRIMINATOR_COLUMN, "ENTITY_NAME"); | ||
configuration.setProperty(SingleGlobalTemporaryTableBulkIdStrategy.CLEAN_ROWS, "true"); | ||
} | ||
|
||
@Override | ||
protected Class<?>[] getAnnotatedClasses() { | ||
return new Class<?>[] { Animal.class, Mammal.class, Reptile.class, Human.class, Dog.class }; | ||
} | ||
|
||
@Override | ||
protected void prepareTest() throws Exception { | ||
sqlAppender = new MessageCapturingAppender(); | ||
openSession(); | ||
if (!ddlExecuted) { | ||
doInTransaction(() -> session.createSQLQuery("create global temporary table HT_TEMP_IDS (ID CHAR(36), ENTITY_NAME VARCHAR(100))").executeUpdate()); | ||
ddlExecuted = true; | ||
} | ||
sqlLogger.addAppender(sqlAppender); | ||
} | ||
|
||
@Override | ||
protected void cleanupTest() throws Exception { | ||
sqlLogger.removeAppender(sqlAppender); | ||
} | ||
|
||
@Test | ||
public void testDeleteCleansUpRows() { | ||
// given | ||
doInTransaction(() -> { | ||
session.save(new Human()); | ||
session.flush(); | ||
|
||
// when | ||
doWithLogging(() -> session.createQuery("delete from Mammal").executeUpdate()); | ||
}); | ||
|
||
// then: row delete statement was executed | ||
assertTrue(sqlAppender.log.contains("delete from HT_TEMP_IDS where ENTITY_NAME=?")); | ||
} | ||
|
||
@Test | ||
public void testUpdateCleansUpRows() { | ||
// given | ||
doInTransaction(() -> { | ||
session.save(new Human()); | ||
session.flush(); | ||
|
||
// when | ||
doWithLogging(() -> session.createQuery("update Human h set h.mammalField = 'someCoolValue'").executeUpdate()); | ||
}); | ||
|
||
// then: row delete statement was executed | ||
assertTrue(sqlAppender.log.contains("delete from HT_TEMP_IDS where ENTITY_NAME=?")); | ||
} | ||
|
||
private void doInTransaction(Runnable runnable) { | ||
Transaction transaction = session.beginTransaction(); | ||
runnable.run(); | ||
transaction.commit(); | ||
} | ||
|
||
private void doWithLogging(Runnable runnable) { | ||
sqlLogger.setLevel(Level.DEBUG); | ||
runnable.run(); | ||
sqlLogger.setLevel(Level.OFF); | ||
} | ||
|
||
private static class MessageCapturingAppender extends AppenderSkeleton { | ||
private final List<String> log = new ArrayList<>(); | ||
|
||
@Override | ||
protected void append(LoggingEvent event) { | ||
log.add(event.getRenderedMessage()); | ||
} | ||
|
||
@Override | ||
public boolean requiresLayout() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters