details: https://code.openbravo.com/erp/devel/pi/rev/bdf1796ee012 changeset: 25875:bdf1796ee012 user: Asier Lostalé <asier.lostale <at> openbravo.com> date: Thu Feb 05 17:30:09 2015 +0100 summary: fixed bug 28876: OBBaseTest rolls back all test executions
Added a test watcher to determine wether the test failed or succeeded and do the commit/rollback based on that. details: https://code.openbravo.com/erp/devel/pi/rev/0c0527b41e73 changeset: 25876:0c0527b41e73 user: Asier Lostalé <asier.lostale <at> openbravo.com> date: Thu Feb 05 17:30:40 2015 +0100 summary: related to bug 28876: added test case diffstat: src-test/src/org/openbravo/test/AllAntTaskTests.java | 11 +- src-test/src/org/openbravo/test/AllQuickAntTaskTests.java | 9 +- src-test/src/org/openbravo/test/AntTaskTests.java | 6 +- src-test/src/org/openbravo/test/base/OBBaseTest.java | 99 +++++---- src-test/src/org/openbravo/test/system/TestInfrastructure.java | 93 +++++++++ 5 files changed, 161 insertions(+), 57 deletions(-) diffs (truncated from 347 to 300 lines): diff -r c201d8c9afdb -r 0c0527b41e73 src-test/src/org/openbravo/test/AllAntTaskTests.java --- a/src-test/src/org/openbravo/test/AllAntTaskTests.java Thu Feb 05 14:41:35 2015 +0000 +++ b/src-test/src/org/openbravo/test/AllAntTaskTests.java Thu Feb 05 17:30:40 2015 +0100 @@ -11,7 +11,7 @@ * under the License. * The Original Code is Openbravo ERP. * The Initial Developer of the Original Code is Openbravo SLU - * All portions are Copyright (C) 2009-2014 Openbravo SLU + * All portions are Copyright (C) 2009-2015 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************ @@ -23,7 +23,6 @@ import org.junit.runners.Suite; import org.openbravo.erpCommon.info.ClassicSelectorTest; import org.openbravo.test.accounting.RecordID2Test; -import org.openbravo.test.costing.TestCosting; import org.openbravo.test.dal.AdminContextTest; import org.openbravo.test.dal.ComputedColumnsTest; import org.openbravo.test.dal.DalComplexQueryRequisitionTest; @@ -64,6 +63,7 @@ import org.openbravo.test.system.ErrorTextParserTest; import org.openbravo.test.system.SystemServiceTest; import org.openbravo.test.system.SystemValidatorTest; +import org.openbravo.test.system.TestInfrastructure; import org.openbravo.test.xml.ClientExportImportTest; import org.openbravo.test.xml.DatasetExportTest; import org.openbravo.test.xml.EntityXMLImportTestBusinessObject; @@ -132,7 +132,8 @@ // system SystemServiceTest.class, // SystemValidatorTest.class, // - ErrorTextParserTest.class, + ErrorTextParserTest.class, // + TestInfrastructure.class, // // xml ClientExportImportTest.class, // @@ -149,8 +150,6 @@ ClassicSelectorTest.class, // Accounting - RecordID2Test.class }) - - + RecordID2Test.class }) public class AllAntTaskTests { } diff -r c201d8c9afdb -r 0c0527b41e73 src-test/src/org/openbravo/test/AllQuickAntTaskTests.java --- a/src-test/src/org/openbravo/test/AllQuickAntTaskTests.java Thu Feb 05 14:41:35 2015 +0000 +++ b/src-test/src/org/openbravo/test/AllQuickAntTaskTests.java Thu Feb 05 17:30:40 2015 +0100 @@ -11,7 +11,7 @@ * under the License. * The Original Code is Openbravo ERP. * The Initial Developer of the Original Code is Openbravo SLU - * All portions are Copyright (C) 2009-2014 Openbravo SLU + * All portions are Copyright (C) 2009-2015 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************ @@ -42,6 +42,7 @@ import org.openbravo.test.system.ErrorTextParserTest; import org.openbravo.test.system.SystemServiceTest; import org.openbravo.test.system.SystemValidatorTest; +import org.openbravo.test.system.TestInfrastructure; import org.openbravo.test.xml.EntityXMLImportTestBusinessObject; import org.openbravo.test.xml.EntityXMLImportTestReference; import org.openbravo.test.xml.EntityXMLImportTestSingle; @@ -85,8 +86,10 @@ EntityAccessTest.class, WritableReadableOrganizationClientTest.class, // system - SystemServiceTest.class, SystemValidatorTest.class, // - ErrorTextParserTest.class, + SystemServiceTest.class, // + SystemValidatorTest.class, // + ErrorTextParserTest.class, // + TestInfrastructure.class, // // xml EntityXMLImportTestBusinessObject.class, EntityXMLImportTestReference.class, // diff -r c201d8c9afdb -r 0c0527b41e73 src-test/src/org/openbravo/test/AntTaskTests.java --- a/src-test/src/org/openbravo/test/AntTaskTests.java Thu Feb 05 14:41:35 2015 +0000 +++ b/src-test/src/org/openbravo/test/AntTaskTests.java Thu Feb 05 17:30:40 2015 +0100 @@ -11,7 +11,7 @@ * under the License. * The Original Code is Openbravo ERP. * The Initial Developer of the Original Code is Openbravo SLU - * All portions are Copyright (C) 2009-2014 Openbravo SLU + * All portions are Copyright (C) 2009-2015 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************ @@ -56,6 +56,7 @@ import org.openbravo.test.system.ErrorTextParserTest; import org.openbravo.test.system.SystemServiceTest; import org.openbravo.test.system.SystemValidatorTest; +import org.openbravo.test.system.TestInfrastructure; import org.openbravo.test.xml.DatasetExportTest; import org.openbravo.test.xml.EntityXMLImportTestBusinessObject; import org.openbravo.test.xml.EntityXMLImportTestReference; @@ -120,7 +121,8 @@ // system SystemServiceTest.class, // SystemValidatorTest.class, // - ErrorTextParserTest.class, + ErrorTextParserTest.class, // + TestInfrastructure.class, // // xml EntityXMLImportTestBusinessObject.class, // diff -r c201d8c9afdb -r 0c0527b41e73 src-test/src/org/openbravo/test/base/OBBaseTest.java --- a/src-test/src/org/openbravo/test/base/OBBaseTest.java Thu Feb 05 14:41:35 2015 +0000 +++ b/src-test/src/org/openbravo/test/base/OBBaseTest.java Thu Feb 05 17:30:40 2015 +0100 @@ -11,7 +11,7 @@ * under the License. * The Original Code is Openbravo ERP. * The Initial Developer of the Original Code is Openbravo SLU - * All portions are Copyright (C) 2014 Openbravo SLU + * All portions are Copyright (C) 2014-2015 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************ @@ -29,9 +29,11 @@ import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.hibernate.criterion.Restrictions; -import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; import org.openbravo.base.exception.OBException; import org.openbravo.base.model.Entity; import org.openbravo.base.model.ModelProvider; @@ -57,6 +59,54 @@ public class OBBaseTest { + /** + * Add a TestWatcher rule to be able to catch test failures allowing them to fail. + * + * This will be used to commit or rollback DAL session after on test finalization. Note failed + * method is invoked after invoking any method annotated with @After, that's why commit/rollback + * is performed on this rule's finished method which is invoked after failed. + */ + @Rule + public TestWatcher watchFailures = new TestWatcher() { + @Override + protected void failed(Throwable e, Description description) { + errorOccured = true; + } + + @Override + protected void finished(Description description) { + // if not an administrator but still admin mode set throw an exception + if (!OBContext.getOBContext().getUser().getId().equals("0") + && !OBContext.getOBContext().getRole().getId().equals("0") + && OBContext.getOBContext().isInAdministratorMode()) { + throw new IllegalStateException( + "Test case should take care of reseting admin mode correctly in a finally block, use OBContext.restorePreviousMode"); + } + try { + if (SessionHandler.isSessionHandlerPresent()) { + if (SessionHandler.getInstance().getDoRollback()) { + SessionHandler.getInstance().rollback(); + } else if (isErrorOccured()) { + SessionHandler.getInstance().rollback(); + } else if (SessionHandler.getInstance().getSession().getTransaction().isActive()) { + SessionHandler.getInstance().commitAndClose(); + } else { + SessionHandler.getInstance().getSession().close(); + } + } + } catch (final Exception e) { + SessionHandler.getInstance().rollback(); + reportException(e); + throw new OBException(e); + } finally { + SessionHandler.deleteSessionHandler(); + OBContext.setOBContext((OBContext) null); + } + + super.finished(description); + } + }; + private static final Logger log = Logger.getLogger(OBBaseTest.class); private boolean errorOccured = false; @@ -174,8 +224,7 @@ public void setUp() throws Exception { // clear the session otherwise it keeps the old model setTestUserContext(); - // be negative is set back to false at the end of a successfull test. - errorOccured = true; + errorOccured = false; } /** @@ -279,48 +328,6 @@ return userIds.get(r.nextInt(userIds.size())); } - /* - * public TestResult run() { // TODO Auto-generated method stub return super.run(); } - * - * /** Overridden to keep track if an exception was thrown, if not then errorOccurred is set to - * false, signaling to tearDown to commit the transaction. - *//* - * public void runTest() throws Throwable { super.runTest(); errorOccured = false; } - * - * /** Performs rolling back of a transaction (in case setTestCompleted was not called by the - * subclass), or commits the transaction if the testcase passed without exception. - */ - @After - public void tearDown() throws Exception { - // if not an administrator but still admin mode set throw an exception - if (!OBContext.getOBContext().getUser().getId().equals("0") - && !OBContext.getOBContext().getRole().getId().equals("0") - && OBContext.getOBContext().isInAdministratorMode()) { - throw new IllegalStateException( - "Test case should take care of reseting admin mode correctly in a finally block, use OBContext.restorePreviousMode"); - } - try { - if (SessionHandler.isSessionHandlerPresent()) { - if (SessionHandler.getInstance().getDoRollback()) { - SessionHandler.getInstance().rollback(); - } else if (isErrorOccured()) { - SessionHandler.getInstance().rollback(); - } else if (SessionHandler.getInstance().getSession().getTransaction().isActive()) { - SessionHandler.getInstance().commitAndClose(); - } else { - SessionHandler.getInstance().getSession().close(); - } - } - } catch (final Exception e) { - SessionHandler.getInstance().rollback(); - reportException(e); - throw e; - } finally { - SessionHandler.deleteSessionHandler(); - OBContext.setOBContext((OBContext) null); - } - } - /** * Prints the stacktrace of the exception to System.err. Handles the case that the exception is a * SQLException which has the real causing exception in the diff -r c201d8c9afdb -r 0c0527b41e73 src-test/src/org/openbravo/test/system/TestInfrastructure.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src-test/src/org/openbravo/test/system/TestInfrastructure.java Thu Feb 05 17:30:40 2015 +0100 @@ -0,0 +1,93 @@ +/* + ************************************************************************* + * The contents of this file are subject to the Openbravo Public License + * Version 1.1 (the "License"), being the Mozilla Public License + * Version 1.1 with a permitted attribution clause; you may not use this + * file except in compliance with the License. You may obtain a copy of + * the License at http://www.openbravo.com/legal/license.html + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the + * License for the specific language governing rights and limitations + * under the License. + * The Original Code is Openbravo ERP. + * The Initial Developer of the Original Code is Openbravo SLU + * All portions are Copyright (C) 2015 Openbravo SLU + * All Rights Reserved. + * Contributor(s): ______________________________________. + ************************************************************************ + */ + +package org.openbravo.test.system; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertThat; + +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; +import org.openbravo.base.provider.OBProvider; +import org.openbravo.dal.service.OBDal; +import org.openbravo.model.common.uom.UOM; +import org.openbravo.test.base.OBBaseTest; + +/** + * Test cases to verify test case infrastructure + * + * @author alostale + * + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestInfrastructure extends OBBaseTest { + private static final String NEW_UOM_ID = "1000"; + + /** Creates a BaseOBObject */ + @Test ------------------------------------------------------------------------------ Dive into the World of Parallel Programming. The Go Parallel Website, sponsored by Intel and developed in partnership with Slashdot Media, is your hub for all things parallel software development, from weekly thought leadership blogs to news, videos, case studies, tutorials and more. Take a look and join the conversation now. http://goparallel.sourceforge.net/ _______________________________________________ Openbravo-commits mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openbravo-commits
