Skip to content
Snippets Groups Projects
Commit a4e8b6df authored by juanf's avatar juanf
Browse files

Merge branch 'master' of git@sissource.ethz.ch:sispub/openbis.git

parents 89276549 3f5bf212
No related branches found
No related tags found
No related merge requests found
...@@ -42,6 +42,10 @@ public final class MockLogger implements ISimpleLogger ...@@ -42,6 +42,10 @@ public final class MockLogger implements ISimpleLogger
{ {
builder.append(level).append(": ").append(message).append('\n'); builder.append(level).append(": ").append(message).append('\n');
messageChannel.send(message); messageChannel.send(message);
if (throwableOrNull != null)
{
throwableOrNull.printStackTrace(System.out);
}
} }
public void assertNextLogMessage(String expectedMessage) public void assertNextLogMessage(String expectedMessage)
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package ch.systemsx.cisd.common.test; package ch.systemsx.cisd.common.test;
import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertTrue; import static org.testng.AssertJUnit.assertTrue;
import static org.testng.AssertJUnit.fail; import static org.testng.AssertJUnit.fail;
...@@ -64,6 +65,15 @@ public class AssertionUtil ...@@ -64,6 +65,15 @@ public class AssertionUtil
assertTrue(errorMsg, text.contains(expectedSubstring)); 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 */ /** asserts that given text contains expectedSubstring */
public static void assertSize(Collection<?> collection, int size) public static void assertSize(Collection<?> collection, int size)
{ {
......
...@@ -19,13 +19,15 @@ package ch.systemsx.cisd.openbis.generic.shared.util; ...@@ -19,13 +19,15 @@ package ch.systemsx.cisd.openbis.generic.shared.util;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.sql.Connection; import java.sql.Connection;
import net.lemnik.eodsql.QueryTool;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.hibernate.Transaction; 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.LogCategory;
import ch.systemsx.cisd.common.logging.LogFactory; import ch.systemsx.cisd.common.logging.LogFactory;
import net.lemnik.eodsql.QueryTool;
/** /**
* @author pkupczyk * @author pkupczyk
...@@ -38,10 +40,13 @@ public class EodSqlUtils ...@@ -38,10 +40,13 @@ public class EodSqlUtils
// use reflection as there is no other way to get a connection // use reflection as there is no other way to get a connection
try try
{ {
Field jdbcContextField = transaction.getClass().getDeclaredField("managedConnection"); TransactionDriver transactionDriver = ((TransactionImpl) transaction).internalGetTransactionDriverControl();
jdbcContextField.setAccessible(true); Class<? extends TransactionDriver> transactionDriverClass = transactionDriver.getClass();
Connection jdbcContext = (Connection) jdbcContextField.get(transaction); Field jdbcResourceTransactionField = transactionDriverClass.getDeclaredField("jdbcResourceTransaction");
QueryTool.setManagedDatabaseConnection(jdbcContext); jdbcResourceTransactionField.setAccessible(true);
LogicalConnectionImplementor logicalConnectionImplementor = (LogicalConnectionImplementor) jdbcResourceTransactionField.get(transactionDriver);
Connection connection = logicalConnectionImplementor.getPhysicalConnection();
QueryTool.setManagedDatabaseConnection(connection);
} catch (NoSuchFieldException e) } catch (NoSuchFieldException e)
{ {
// We are looking at some other kind of transaction -- log the error, but do not do anything // 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