From 170103f2c4deaa1bc4c8e719d8510797a844918c Mon Sep 17 00:00:00 2001 From: rkv Date: Wed, 28 Nov 2012 09:11:33 +0000 Subject: [PATCH] Test data are not saved now in the test database due to transactions rollbacks. User creation is fixed and appropriate test is added. --- .../org/splat/service/UserServiceImpl.java | 3 +- .../src/test/splat/common/BaseTest.java | 10 +- .../splat/dao/TestKnowledgeElementDAO.java | 21 +- .../service/TestProjectSettingsService.java | 84 ++++++- .../splat/service/TestScenarioService.java | 11 +- .../src/test/spring/ut-datasourceContext.xml | 206 +++++++++--------- 6 files changed, 219 insertions(+), 116 deletions(-) diff --git a/Workspace/Siman-Common/src/org/splat/service/UserServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/UserServiceImpl.java index 6c67231..94ba43a 100644 --- a/Workspace/Siman-Common/src/org/splat/service/UserServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/UserServiceImpl.java @@ -69,9 +69,10 @@ public class UserServiceImpl implements UserService { MultiplyDefinedException, RuntimeException { User nuser = new User(uprop); // Do merge to synchronize Role object with the current hibernate session - // and to avoid th exception: + // and to avoid the exception: // org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the // session: [org.splat.dal.bo.kernel.Role#simer] + getUserDAO().merge(nuser); getUserDAO().saveOrUpdate(nuser); //getUserDAO().create(nuser); diff --git a/Workspace/Siman-Common/src/test/splat/common/BaseTest.java b/Workspace/Siman-Common/src/test/splat/common/BaseTest.java index 519e4bc..fb7af6c 100644 --- a/Workspace/Siman-Common/src/test/splat/common/BaseTest.java +++ b/Workspace/Siman-Common/src/test/splat/common/BaseTest.java @@ -19,12 +19,12 @@ import org.splat.log.AppLogger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionStatus; +import org.springframework.transaction.support.AbstractPlatformTransactionManager; import org.springframework.transaction.support.DefaultTransactionDefinition; import org.testng.Assert; import org.testng.annotations.BeforeClass; @@ -220,8 +220,8 @@ public class BaseTest extends TestListingAndOrder { * @see test.areva.hewis.mlst2.service.gpao.rpta.TaBaseService#rollbackNestedTransaction() */ protected void startNestedTransaction() { - DataSourceTransactionManager txManager = - (DataSourceTransactionManager) applicationContext.getBean("txManager"); + AbstractPlatformTransactionManager txManager = + (AbstractPlatformTransactionManager) applicationContext.getBean("txManager"); DefaultTransactionDefinition txDefinition = new DefaultTransactionDefinition(); txDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED); @@ -234,8 +234,8 @@ public class BaseTest extends TestListingAndOrder { * @see test.areva.hewis.mlst2.service.gpao.rpta.TaBaseService#startNestedTransaction() */ protected void rollbackNestedTransaction() { - DataSourceTransactionManager txManager = - (DataSourceTransactionManager) applicationContext.getBean("txManager"); + AbstractPlatformTransactionManager txManager = + (AbstractPlatformTransactionManager) applicationContext.getBean("txManager"); txManager.rollback(_nestedTxStatus); } diff --git a/Workspace/Siman-Common/src/test/splat/dao/TestKnowledgeElementDAO.java b/Workspace/Siman-Common/src/test/splat/dao/TestKnowledgeElementDAO.java index 77ede0c..42df6a2 100644 --- a/Workspace/Siman-Common/src/test/splat/dao/TestKnowledgeElementDAO.java +++ b/Workspace/Siman-Common/src/test/splat/dao/TestKnowledgeElementDAO.java @@ -83,7 +83,8 @@ public class TestKnowledgeElementDAO extends BaseTest { public void testCreate() throws InvalidPropertyException, MissedPropertyException, MultiplyDefinedException { LOG.debug(">>>>> BEGIN testCreate()"); - + startNestedTransaction(); + KnowledgeElement aKelm = getKnowledgeElement(); // Call DAO's create method for a good transient knowledge element. Long id = _knowledgeElementDAO.create(aKelm); @@ -113,6 +114,8 @@ public class TestKnowledgeElementDAO extends BaseTest { LOG.debug("Expected exception is thrown: " + e.getClass().getSimpleName() + ": " + e.getMessage()); } + + rollbackNestedTransaction(); LOG.debug(">>>>> END testCreate()"); } @@ -149,6 +152,8 @@ public class TestKnowledgeElementDAO extends BaseTest { public void testGet() throws InvalidPropertyException, MissedPropertyException, MultiplyDefinedException { LOG.debug(">>>>> BEGIN testGet()"); + startNestedTransaction(); + KnowledgeElement aKelm = getKnowledgeElement(); // Call DAO's create method for a good transient knowledge element. Long id = _knowledgeElementDAO.create(aKelm); @@ -189,6 +194,8 @@ public class TestKnowledgeElementDAO extends BaseTest { LOG.debug("Expected exception is thrown: " + e.getClass().getSimpleName() + ": " + e.getMessage()); } + + rollbackNestedTransaction(); LOG.debug(">>>>> END testGet()"); } @@ -229,6 +236,8 @@ public class TestKnowledgeElementDAO extends BaseTest { public void testUpdate() throws InvalidPropertyException, MissedPropertyException, MultiplyDefinedException { LOG.debug(">>>>> BEGIN testUpdate()"); + startNestedTransaction(); + KnowledgeElement aKelm = getKnowledgeElement(); // Call DAO's create method for a good transient knowledge element. Long id = _knowledgeElementDAO.create(aKelm); @@ -290,6 +299,8 @@ public class TestKnowledgeElementDAO extends BaseTest { LOG.debug("Expected exception is thrown: " + e.getClass().getSimpleName() + ": " + e.getMessage()); } + + rollbackNestedTransaction(); LOG.debug(">>>>> END testUpdate()"); } @@ -326,6 +337,8 @@ public class TestKnowledgeElementDAO extends BaseTest { public void testDelete() throws InvalidPropertyException, MissedPropertyException, MultiplyDefinedException { LOG.debug(">>>>> BEGIN testDelete()"); + startNestedTransaction(); + KnowledgeElement aKelm = getKnowledgeElement(); // Call DAO's create method for a good transient knowledge element. Long id = _knowledgeElementDAO.create(aKelm); @@ -349,6 +362,8 @@ public class TestKnowledgeElementDAO extends BaseTest { LOG.debug("Expected exception is thrown: " + e.getClass().getSimpleName() + ": " + e.getMessage()); } + + rollbackNestedTransaction(); LOG.debug(">>>>> END testDelete()"); } @@ -419,8 +434,8 @@ public class TestKnowledgeElementDAO extends BaseTest { * @param anExpected * the expected object */ - private void compareObjects(KnowledgeElement anActual, - KnowledgeElement anExpected) { + private void compareObjects(final KnowledgeElement anActual, + final KnowledgeElement anExpected) { Assert.assertNotNull(anActual, "Created object is not found in the database."); Assert.assertEquals(anActual.getAuthor(), anExpected.getAuthor(), diff --git a/Workspace/Siman-Common/src/test/splat/service/TestProjectSettingsService.java b/Workspace/Siman-Common/src/test/splat/service/TestProjectSettingsService.java index cfc6fe2..d0807ae 100644 --- a/Workspace/Siman-Common/src/test/splat/service/TestProjectSettingsService.java +++ b/Workspace/Siman-Common/src/test/splat/service/TestProjectSettingsService.java @@ -21,6 +21,7 @@ import org.splat.service.technical.ProjectSettingsService; import org.splat.service.technical.ProjectSettingsService.Step; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.dao.DuplicateKeyException; import org.testng.Assert; import org.testng.annotations.Test; @@ -86,7 +87,8 @@ public class TestProjectSettingsService extends BaseTest { */ @Test public void testLoadMappings() throws IOException, SQLException { - LOG.debug(">>>>> BEGIN testGetScenarioInfo()"); + LOG.debug(">>>>> BEGIN testLoadMappings()()"); + startNestedTransaction(); // ////// Load good workflow customization /* * geometry: brep model: med loads: c3m results: med @@ -132,7 +134,8 @@ public class TestProjectSettingsService extends BaseTest { LOG.debug("Configuration file must not be found.", e); } - LOG.debug(">>>>> END testGetScenarioInfo()"); + rollbackNestedTransaction(); + LOG.debug(">>>>> END testLoadMappings()()"); } /** @@ -213,6 +216,7 @@ public class TestProjectSettingsService extends BaseTest { @Test public void testLoadDefaultDocTypes() throws IOException, SQLException { LOG.debug(">>>>> BEGIN testLoadDefaultDocTypes()"); + startNestedTransaction(); // ////// Load good workflow customization /* * geometry: brep model: med loads: c3m results: med @@ -418,6 +422,82 @@ public class TestProjectSettingsService extends BaseTest { LOG.debug("Configuration file must not be found.", e); } + rollbackNestedTransaction(); LOG.debug(">>>>> END testLoadDefaultDocTypes()"); } + + /** + * Test of repeated database configuration method (dynamic reconfiguration).
+ * The problem - duplication of the simer user admin role. Description :
+ * Load customization twice and check the result.
+ * Action :
+ * 1. call the method twice for som.xml
+ * Test data :
+ * test/som.xml
+ * + * Outcome results:
+ * + * + * + * + * @throws IOException + * if configuration loading is failed + * @throws SQLException + * if configuration loading is failed + */ + @Test + public void testConfigure() throws IOException, SQLException { + LOG.debug(">>>>> BEGIN testConfigure()"); + startNestedTransaction(); + // /////////////////////////////////////////////////// + // ////// Load good workflow customization + _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again + try { + _projectSettings.configure(ClassLoader.getSystemResource( + "test/som.xml").getPath()); + } catch (FileNotFoundException e) { + Assert.fail("Can't find configuration file: ", e); + } + + List steps = _projectSettings.getStepsOf(Scenario.class); + Assert.assertTrue(steps.size() > 0, "No steps are created."); + + // ///////////////////////////////////////////////////////// + // ////// Test reconfiguration attempt + Database.getInstance().reset(); + _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again + try { + _projectSettings.configure(ClassLoader.getSystemResource( + "test/som.xml").getPath()); + } catch (FileNotFoundException e) { + Assert.fail("Can't find configuration file: ", e); + } + steps = _projectSettings.getStepsOf(Scenario.class); + Assert.assertTrue(steps.size() > 0, "No steps are created."); + + try { + /* + * The next call to flush() must not throw the following exception: org.springframework.dao.DuplicateKeyException: a different + * object with the same identifier value was already associated with the session: [org.splat.dal.bo.kernel.Role#simer]; nested + * exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated + * with the session: [org.splat.dal.bo.kernel.Role#simer] at + * org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:662) at + * org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) at + * org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411) at + * org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) at + * org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:881) + */ + getHibernateTemplate().flush(); + } catch (DuplicateKeyException dke) { + Assert.fail( + "User creation failed during the database reconfiguration: " + + dke.getMessage(), dke); + } + + rollbackNestedTransaction(); + LOG.debug(">>>>> END testConfigure()"); + } } diff --git a/Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java b/Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java index 005ddfb..3670fa2 100644 --- a/Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java +++ b/Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java @@ -156,6 +156,8 @@ public class TestScenarioService extends BaseTest { MissedPropertyException, MultiplyDefinedException, IOException, SQLException { LOG.debug(">>>>> BEGIN testGetScenarioInfo()"); + startNestedTransaction(); + long scenarioId = createScenario(); // Call DAO's create method for a good transient knowledge element. List steps = _scenarioService.getScenarioInfo(scenarioId); @@ -234,7 +236,7 @@ public class TestScenarioService extends BaseTest { } } else { // Outdated Assert.assertEquals(file.getState(), 'O', - "File state must be actual ('O')."); + "File state must be outdated ('O')."); Assert .assertEquals(file.getProcessing(), "file-download", @@ -257,6 +259,7 @@ public class TestScenarioService extends BaseTest { LOG.debug("Expected exception is thrown: " + e.getClass().getSimpleName() + ": " + e.getMessage()); } + rollbackNestedTransaction(); LOG.debug(">>>>> END testGetScenarioInfo()"); } @@ -314,9 +317,12 @@ public class TestScenarioService extends BaseTest { MissedPropertyException, MultiplyDefinedException, IOException, SQLException, MismatchException, NotApplicableException { LOG.debug(">>>>> BEGIN testCheckin()"); + startNestedTransaction(); + _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again _projectSettings.configure(ClassLoader .getSystemResource("test/som.xml").getPath()); + getHibernateTemplate().flush(); long scenarioId = createScenario(); Scenario aScen = _scenarioDAO.get(scenarioId); User user = aScen.getAuthor(); @@ -465,7 +471,8 @@ public class TestScenarioService extends BaseTest { LOG.debug("Expected exception is thrown: " + e.getClass().getSimpleName() + ": " + e.getMessage()); } - + + rollbackNestedTransaction(); LOG.debug(">>>>> END testCheckin()"); } diff --git a/Workspace/Siman-Common/src/test/spring/ut-datasourceContext.xml b/Workspace/Siman-Common/src/test/spring/ut-datasourceContext.xml index 8b57e51..7ac2d9a 100644 --- a/Workspace/Siman-Common/src/test/spring/ut-datasourceContext.xml +++ b/Workspace/Siman-Common/src/test/spring/ut-datasourceContext.xml @@ -1,10 +1,10 @@ - + - + + + + + + - - - - - - - - + - - - - - + + + + + - - - - - - - - - - classpath:test/${hibernate.config.file} - - - + + + + + + + + + + classpath:test/${hibernate.config.file} + + + - - - - - - org/splat/dal/bo/kernel/Persistent.hbm.xml - - org/splat/dal/bo/kernel/Any.hbm.xml - org/splat/dal/bo/kernel/Entity.hbm.xml - org/splat/dal/bo/kernel/Attribute.hbm.xml - org/splat/dal/bo/kernel/Relation.hbm.xml - - org/splat/dal/bo/kernel/TextAttribute.hbm.xml - - org/splat/dal/bo/kernel/User.hbm.xml + + + + + + org/splat/dal/bo/kernel/Persistent.hbm.xml + + org/splat/dal/bo/kernel/Any.hbm.xml + org/splat/dal/bo/kernel/Entity.hbm.xml + org/splat/dal/bo/kernel/Attribute.hbm.xml + org/splat/dal/bo/kernel/Relation.hbm.xml + + org/splat/dal/bo/kernel/TextAttribute.hbm.xml + + org/splat/dal/bo/kernel/User.hbm.xml - - org/splat/dal/bo/som/ProjectElement.hbm.xml - - org/splat/dal/bo/som/Study.hbm.xml - org/splat/dal/bo/som/Scenario.hbm.xml - org/splat/dal/bo/som/Attributes.hbm.xml - org/splat/dal/bo/som/Relations.hbm.xml - org/splat/dal/bo/som/File.hbm.xml - org/splat/dal/bo/som/Document.hbm.xml - org/splat/dal/bo/som/Publication.hbm.xml - - org/splat/dal/bo/som/ValidationCycle.hbm.xml - - org/splat/dal/bo/som/Timestamp.hbm.xml - - org/splat/dal/bo/som/SimulationContext.hbm.xml - - - org/splat/dal/bo/som/KnowledgeElement.hbm.xml - - org/splat/dal/bo/som/IDBuilder.hbm.xml - - - + + org/splat/dal/bo/som/ProjectElement.hbm.xml + + org/splat/dal/bo/som/Study.hbm.xml + org/splat/dal/bo/som/Scenario.hbm.xml + org/splat/dal/bo/som/Attributes.hbm.xml + org/splat/dal/bo/som/Relations.hbm.xml + org/splat/dal/bo/som/File.hbm.xml + org/splat/dal/bo/som/Document.hbm.xml + org/splat/dal/bo/som/Publication.hbm.xml + + org/splat/dal/bo/som/ValidationCycle.hbm.xml + + org/splat/dal/bo/som/Timestamp.hbm.xml + + org/splat/dal/bo/som/SimulationContext.hbm.xml + + + org/splat/dal/bo/som/KnowledgeElement.hbm.xml + + org/splat/dal/bo/som/IDBuilder.hbm.xml + + + - - - - - - - - + + + + + + + + - - - + + + + - + -- 2.39.2