This is an automated email from the ASF dual-hosted git repository. ahuber pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/master by this push: new ddfc8cd ISIS-1954: introduces IntegrationTestJupiter as base class for integration tests for the JUnit 5 Jupiter Engine ddfc8cd is described below commit ddfc8cd86b7d7d9eb52a1ec19936e838d9a895c8 Author: Andi Huber <ahu...@apache.org> AuthorDate: Thu May 31 14:17:47 2018 +0200 ISIS-1954: introduces IntegrationTestJupiter as base class for integration tests for the JUnit 5 Jupiter Engine Task-Url: https://issues.apache.org/jira/browse/ISIS-1954 --- .../integtestsupport/IntegrationTestAbstract3.java | 88 +--------------- .../integtestsupport/IntegrationTestJupiter.java | 96 +++++++++++++++++ .../apache/isis/core/integtestsupport/Util.java | 116 +++++++++++++++++++++ 3 files changed, 215 insertions(+), 85 deletions(-) diff --git a/core/integtestsupport/src/main/java/org/apache/isis/core/integtestsupport/IntegrationTestAbstract3.java b/core/integtestsupport/src/main/java/org/apache/isis/core/integtestsupport/IntegrationTestAbstract3.java index 7bd30b3..2394f74 100644 --- a/core/integtestsupport/src/main/java/org/apache/isis/core/integtestsupport/IntegrationTestAbstract3.java +++ b/core/integtestsupport/src/main/java/org/apache/isis/core/integtestsupport/IntegrationTestAbstract3.java @@ -18,16 +18,8 @@ */ package org.apache.isis.core.integtestsupport; -import java.util.List; - import org.apache.isis.applib.AppManifest; import org.apache.isis.applib.Module; -import org.apache.isis.applib.NonRecoverableException; -import org.apache.isis.applib.RecoverableException; -import org.apache.isis.applib.services.jdosupport.IsisJdoSupport; -import org.apache.isis.applib.services.xactn.TransactionService; -import org.apache.isis.commons.internal.exceptions._Exceptions; -import org.apache.isis.core.integtestsupport.components.DefaultHeadlessTransactionSupport; import org.apache.isis.core.runtime.headless.HeadlessTransactionSupport; import org.apache.isis.core.runtime.headless.HeadlessWithBootstrappingAbstract; import org.apache.isis.core.runtime.headless.IsisSystem; @@ -73,82 +65,15 @@ public abstract class IntegrationTestAbstract3 extends HeadlessWithBootstrapping base.evaluate(); final IsisSystem isft = IsisSystem.get(); isft.getService(HeadlessTransactionSupport.class).endTransaction(); - } catch(final Throwable e) { - // determine if underlying cause is an applib-defined exception, - final RecoverableException recoverableException = - determineIfRecoverableException(e); - final NonRecoverableException nonRecoverableException = - determineIfNonRecoverableException(e); - - if(recoverableException != null) { - try { - final IsisSystem isft = IsisSystem.get(); - isft.getService(TransactionService.class).flushTransaction(); // don't care if npe - isft.getService(IsisJdoSupport.class).getJdoPersistenceManager().flush(); - } catch (Exception ignore) { - // ignore - } - } - // attempt to close this - try { - final IsisSystem isft = IsisSystem.getElseNull(); - isft.closeSession(); // don't care if npe - } catch(Exception ignore) { - // ignore - } - - // attempt to start another - try { - final IsisSystem isft = IsisSystem.getElseNull(); - isft.openSession(); // don't care if npe - } catch(Exception ignore) { - // ignore - } - - - // if underlying cause is an applib-defined, then - // throw that rather than Isis' wrapper exception - if(recoverableException != null) { - throw recoverableException; - } - if(nonRecoverableException != null) { - throw nonRecoverableException; - } - - // report on the error that caused - // a problem for *this* test - throw e; - } - } - - NonRecoverableException determineIfNonRecoverableException(final Throwable e) { - NonRecoverableException nonRecoverableException = null; - final List<Throwable> causalChain2 = _Exceptions.getCausalChain(e); - for (final Throwable cause : causalChain2) { - if(cause instanceof NonRecoverableException) { - nonRecoverableException = (NonRecoverableException) cause; - break; - } + } catch(final Exception e) { + Util.handleTransactionContextException(e); } - return nonRecoverableException; } - RecoverableException determineIfRecoverableException(final Throwable e) { - RecoverableException recoverableException = null; - final List<Throwable> causalChain = _Exceptions.getCausalChain(e); - for (final Throwable cause : causalChain) { - if(cause instanceof RecoverableException) { - recoverableException = (RecoverableException) cause; - break; - } - } - return recoverableException; - } }; } } - protected IntegrationTestAbstract3(final Module module) { this(new LogConfig(Level.INFO), module); } @@ -156,16 +81,9 @@ public abstract class IntegrationTestAbstract3 extends HeadlessWithBootstrapping protected IntegrationTestAbstract3( final LogConfig logConfig, final Module module) { - super(logConfig, addHeadlessTransactionSupport(module)); + super(logConfig, Util.addHeadlessTransactionSupport(module)); } - //[ahuber] hooks into the bootstrapping, such that the - // DefaultHeadlessTransactionSupport is registered as an additional service - private static Module addHeadlessTransactionSupport(Module module) { - module.getAdditionalServices().add(DefaultHeadlessTransactionSupport.class); - return module; - } - @Override @Before public void bootstrapAndSetupIfRequired() { diff --git a/core/integtestsupport/src/main/java/org/apache/isis/core/integtestsupport/IntegrationTestJupiter.java b/core/integtestsupport/src/main/java/org/apache/isis/core/integtestsupport/IntegrationTestJupiter.java new file mode 100644 index 0000000..34314d4 --- /dev/null +++ b/core/integtestsupport/src/main/java/org/apache/isis/core/integtestsupport/IntegrationTestJupiter.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.isis.core.integtestsupport; + +import org.apache.isis.applib.AppManifest; +import org.apache.isis.applib.Module; +import org.apache.isis.core.runtime.headless.HeadlessTransactionSupport; +import org.apache.isis.core.runtime.headless.HeadlessWithBootstrappingAbstract; +import org.apache.isis.core.runtime.headless.IsisSystem; +import org.apache.isis.core.runtime.headless.logging.LogConfig; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.slf4j.event.Level; + +/** + * Base class for integration tests for the JUnit 5 Jupiter Engine, + * uses a {@link Module} to bootstrap, rather than an {@link AppManifest}. + * + * @since 2.0.0 + */ +@ExtendWith(IntegrationTestJupiter.HeadlessTransactionRule.class) +public abstract class IntegrationTestJupiter extends HeadlessWithBootstrappingAbstract { + + private static class HeadlessTransactionRule implements AfterEachCallback, BeforeEachCallback { + + @Override + public void beforeEach(ExtensionContext context) throws Exception { + final IntegrationTestJupiter testInstance = testInstance(context); + testInstance.bootstrapAndSetupIfRequired(); + } + + @Override + public void afterEach(ExtensionContext context) throws Exception { + + try { + final IsisSystem isft = IsisSystem.get(); + isft.getService(HeadlessTransactionSupport.class).endTransaction(); + } catch(final Exception e) { + Util.handleTransactionContextException(e); + } finally { + final IntegrationTestJupiter testInstance = testInstance(context); + testInstance.tearDownAllModules(); + } + } + + // -- HELPER + private IntegrationTestJupiter testInstance(ExtensionContext context) { + final IntegrationTestJupiter testInstance = (IntegrationTestJupiter) context.getTestInstance().get(); + return testInstance; + } + + } + + protected IntegrationTestJupiter(final Module module) { + this(new LogConfig(Level.INFO), module); + } + + protected IntegrationTestJupiter( + final LogConfig logConfig, + final Module module) { + super(logConfig, Util.addHeadlessTransactionSupport(module)); + } + + @Override + protected void bootstrapAndSetupIfRequired() { + + super.bootstrapAndSetupIfRequired(); + + log("### TEST: " + this.getClass().getCanonicalName()); + } + + @Override + protected void tearDownAllModules() { + + super.tearDownAllModules(); + } + +} \ No newline at end of file diff --git a/core/integtestsupport/src/main/java/org/apache/isis/core/integtestsupport/Util.java b/core/integtestsupport/src/main/java/org/apache/isis/core/integtestsupport/Util.java new file mode 100644 index 0000000..9c28bfe --- /dev/null +++ b/core/integtestsupport/src/main/java/org/apache/isis/core/integtestsupport/Util.java @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.isis.core.integtestsupport; + +import java.util.List; + +import org.apache.isis.applib.Module; +import org.apache.isis.applib.NonRecoverableException; +import org.apache.isis.applib.RecoverableException; +import org.apache.isis.applib.services.jdosupport.IsisJdoSupport; +import org.apache.isis.applib.services.xactn.TransactionService; +import org.apache.isis.commons.internal.exceptions._Exceptions; +import org.apache.isis.core.integtestsupport.components.DefaultHeadlessTransactionSupport; +import org.apache.isis.core.runtime.headless.IsisSystem; + +class Util { + + //[ahuber] hooks into the bootstrapping, such that the + // DefaultHeadlessTransactionSupport is registered as an additional service + public static Module addHeadlessTransactionSupport(Module module) { + module.getAdditionalServices().add(DefaultHeadlessTransactionSupport.class); + return module; + } + + public static void handleTransactionContextException(Exception e) throws Exception { + // determine if underlying cause is an applib-defined exception, + final RecoverableException recoverableException = + determineIfRecoverableException(e); + final NonRecoverableException nonRecoverableException = + determineIfNonRecoverableException(e); + + if(recoverableException != null) { + try { + final IsisSystem isft = IsisSystem.get(); + isft.getService(TransactionService.class).flushTransaction(); // don't care if npe + isft.getService(IsisJdoSupport.class).getJdoPersistenceManager().flush(); + } catch (Exception ignore) { + // ignore + } + } + // attempt to close this + try { + final IsisSystem isft = IsisSystem.getElseNull(); + isft.closeSession(); // don't care if npe + } catch(Exception ignore) { + // ignore + } + + // attempt to start another + try { + final IsisSystem isft = IsisSystem.getElseNull(); + isft.openSession(); // don't care if npe + } catch(Exception ignore) { + // ignore + } + + + // if underlying cause is an applib-defined, then + // throw that rather than Isis' wrapper exception + if(recoverableException != null) { + throw recoverableException; + } + if(nonRecoverableException != null) { + throw nonRecoverableException; + } + + // report on the error that caused + // a problem for *this* test + throw e; + } + + private static NonRecoverableException determineIfNonRecoverableException(final Exception e) { + NonRecoverableException nonRecoverableException = null; + final List<Throwable> causalChain2 = _Exceptions.getCausalChain(e); + for (final Throwable cause : causalChain2) { + if(cause instanceof NonRecoverableException) { + nonRecoverableException = (NonRecoverableException) cause; + break; + } + } + return nonRecoverableException; + } + + private static RecoverableException determineIfRecoverableException(final Exception e) { + RecoverableException recoverableException = null; + final List<Throwable> causalChain = _Exceptions.getCausalChain(e); + for (final Throwable cause : causalChain) { + if(cause instanceof RecoverableException) { + recoverableException = (RecoverableException) cause; + break; + } + } + return recoverableException; + } + + + + +} -- To stop receiving notification emails like this one, please contact ahu...@apache.org.