Skip to content
Snippets Groups Projects
Commit 3f5bf212 authored by felmer's avatar felmer
Browse files

SSDM-6958: Fixing and testing EodSqlUtils because of changes in Hibernate internal classes

parent c769b1d1
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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)
{
......
......@@ -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
......
/*
* 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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment