diff --git a/commonbase/sourceTest/java/ch/systemsx/cisd/common/logging/MockLogger.java b/commonbase/sourceTest/java/ch/systemsx/cisd/common/logging/MockLogger.java index 9d017a66b5f31bac29392efbecb4fb8efe34629b..e8c57c759bd7a72320198ee267620d69fa7f6000 100644 --- a/commonbase/sourceTest/java/ch/systemsx/cisd/common/logging/MockLogger.java +++ b/commonbase/sourceTest/java/ch/systemsx/cisd/common/logging/MockLogger.java @@ -42,6 +42,10 @@ public final class MockLogger implements ISimpleLogger { builder.append(level).append(": ").append(message).append('\n'); messageChannel.send(message); + if (throwableOrNull != null) + { + throwableOrNull.printStackTrace(System.out); + } } public void assertNextLogMessage(String expectedMessage) diff --git a/commonbase/sourceTest/java/ch/systemsx/cisd/common/test/AssertionUtil.java b/commonbase/sourceTest/java/ch/systemsx/cisd/common/test/AssertionUtil.java index c09d1f8ef8b05a196df0c9d6dd7238e91d9876b0..04c10e3bebe17f601670478131b03b71e0bd4180 100644 --- a/commonbase/sourceTest/java/ch/systemsx/cisd/common/test/AssertionUtil.java +++ b/commonbase/sourceTest/java/ch/systemsx/cisd/common/test/AssertionUtil.java @@ -17,6 +17,7 @@ package ch.systemsx.cisd.common.test; import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertFalse; import static org.testng.AssertJUnit.assertTrue; import static org.testng.AssertJUnit.fail; @@ -64,6 +65,15 @@ public class AssertionUtil assertTrue(errorMsg, text.contains(expectedSubstring)); } + /** asserts that given text does not contain unExpectedSubstring */ + public static void assertContainsNot(String unExpectedSubstring, String text) + { + String errorMsg = + String.format("String \n'%s'\nwas expected to be NOT a substring of \n'%s'.", + unExpectedSubstring, text); + assertFalse(errorMsg, text.contains(unExpectedSubstring)); + } + /** asserts that given text contains expectedSubstring */ public static void assertSize(Collection<?> collection, int size) { diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/util/EodSqlUtils.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/util/EodSqlUtils.java index 4b5f7c522c8b5f61cf82e1511a0d2e963a7371ca..da6eb956df88be65ea67112dbf9c2117b47d9d77 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/util/EodSqlUtils.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/util/EodSqlUtils.java @@ -19,13 +19,15 @@ package ch.systemsx.cisd.openbis.generic.shared.util; import java.lang.reflect.Field; import java.sql.Connection; -import net.lemnik.eodsql.QueryTool; - import org.apache.log4j.Logger; import org.hibernate.Transaction; +import org.hibernate.engine.transaction.internal.TransactionImpl; +import org.hibernate.resource.jdbc.spi.LogicalConnectionImplementor; +import org.hibernate.resource.transaction.spi.TransactionCoordinator.TransactionDriver; import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogFactory; +import net.lemnik.eodsql.QueryTool; /** * @author pkupczyk @@ -38,10 +40,13 @@ public class EodSqlUtils // use reflection as there is no other way to get a connection try { - Field jdbcContextField = transaction.getClass().getDeclaredField("managedConnection"); - jdbcContextField.setAccessible(true); - Connection jdbcContext = (Connection) jdbcContextField.get(transaction); - QueryTool.setManagedDatabaseConnection(jdbcContext); + TransactionDriver transactionDriver = ((TransactionImpl) transaction).internalGetTransactionDriverControl(); + Class<? extends TransactionDriver> transactionDriverClass = transactionDriver.getClass(); + Field jdbcResourceTransactionField = transactionDriverClass.getDeclaredField("jdbcResourceTransaction"); + jdbcResourceTransactionField.setAccessible(true); + LogicalConnectionImplementor logicalConnectionImplementor = (LogicalConnectionImplementor) jdbcResourceTransactionField.get(transactionDriver); + Connection connection = logicalConnectionImplementor.getPhysicalConnection(); + QueryTool.setManagedDatabaseConnection(connection); } catch (NoSuchFieldException e) { // We are looking at some other kind of transaction -- log the error, but do not do anything diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/EodSqlUtilsTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/EodSqlUtilsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..7e17acda9626ef8e2e7b59bade1f19e01d645fd6 --- /dev/null +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/EodSqlUtilsTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 2018 ETH Zuerich, SIS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.generic.server.dataaccess; + +import static ch.systemsx.cisd.common.test.AssertionUtil.assertContainsNot; + +import org.testng.annotations.Test; + +import ch.systemsx.cisd.common.logging.BufferedAppender; +import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.AbstractDAOTest; +import ch.systemsx.cisd.openbis.generic.shared.util.EodSqlUtils; +import ch.systemsx.cisd.openbis.util.LogRecordingUtils; + +/** + * @author Franz-Josef Elmer + * + */ +public class EodSqlUtilsTest extends AbstractDAOTest +{ + @Test + public void test() throws Exception, IllegalAccessException + { + BufferedAppender recorder = LogRecordingUtils.createRecorder(); + + try + { + EodSqlUtils.setManagedConnection(daoFactory.getSessionFactory().getCurrentSession().getTransaction()); + } finally + { + EodSqlUtils.clearManagedConnection(); + } + + assertContainsNot("EodSql", recorder.getLogContent()); + } + +}