Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  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.junit.Rule;
Base class for integration tests.

There is substantial overlap with org.apache.isis.core.specsupport.specs.CukeGlueAbstract, and it would be possible to factor out a common base class. Both delegate to an underlying org.apache.isis.core.specsupport.scenarios.ScenarioExecution, and provide a bunch of helper methods. The reason this has not been done is mostly to make it easier to see the equivalence of these two classes.

The only real differences between this class and org.apache.isis.core.specsupport.specs.CukeGlueAbstract is that this class uses JUnit rules to automatically perform transaction management and uses JUnit rules for exception handling. In org.apache.isis.core.specsupport.specs.CukeGlueAbstract these are required (by Cucumber-JVM) to be explicitly handled in the step definitions.

 
 public abstract class IntegrationTestAbstract {
 
     protected static ScenarioExecution scenarioExecution() {
         return ScenarioExecution.current();
     }
 
     // //////////////////////////////////////
 
  
    
Intended to be called whenever there is a logically distinct interaction with the system.

Each transaction has its own instances of request-scoped services, most notably the org.apache.isis.applib.services.command.Command.

(Unlike nextSession()), it is valid to hold references to objects across transactions.

 
     protected void nextTransaction() {
         scenarioExecution().endTran(true);
         scenarioExecution().beginTran();
     }

    
 
     protected void nextRequest() {
         nextTransaction();
     }

    
Completes the transaction and session, then opens another session and transaction.

Note that any references to objects must be discarded and reacquired.

    protected void nextSession() {
        scenarioExecution().endTran(true);
        scenarioExecution().closeSession();
        scenarioExecution().openSession();
        scenarioExecution().beginTran();
    }
    // //////////////////////////////////////

    
    
Convenience method
    public Object getVar(String typeString id) {
        return scenarioExecution().getVar(typeid);
    }

    
Convenience method
    public <X> X getVar(String typeString idClass<X> cls) {
        return scenarioExecution().getVar(typeid ,cls);
    }

    
Convenience method
    public void putVar(String typeString idObject value) {
        scenarioExecution().putVar(typeidvalue);
    }
    
    
Convenience method
    public void removeVar(String typeString id) {
        scenarioExecution().removeVar(typeid);
    }

    
Convenience method
    protected <T> T service(Class<T> cls) {
        return scenarioExecution().service(cls);
    }
    
    
Convenience method
    protected DomainObjectContainer container() {
        return scenarioExecution().container();
    }
    
    
Convenience method
    protected WrapperFactory wrapperFactory() {
        return scenarioExecution().wrapperFactory();
    }

    
Convenience method
    protected <T> T wrap(T obj) {
        return scenarioExecution().wrapperFactory().wrap(obj);
    }

    
Convenience method
    protected <T> T unwrap(T obj) {
        return scenarioExecution().wrapperFactory().unwrap(obj);
    }
    
    // //////////////////////////////////////

    
The order is important; this rule is outermost, and must - at a minimum - come before the expectedExceptions rule.
    @Rule
    private static class IsisTransactionRule implements MethodRule  {
        @Override
        public Statement apply(final Statement basefinal FrameworkMethod methodfinal Object target) {
            final IsisSystemForTest isft = IsisSystemForTest.get(); 
            
            return new Statement() {
                @Override
                public void evaluate() throws Throwable {
                    isft.getContainer().injectServicesInto(target);
                    isft.beginTran();
                    try {
                        base.evaluate();
                        isft.endTran();
                    } catch(Throwable e) {
                        isft.bounceSystem();
                        final List<ThrowablecausalChain = Throwables.getCausalChain(e);
                        // if underlying cause is an applib-defined exception, throw that rather than Isis' wrapper exception
                        for (Throwable cause : causalChain) {
                            if(cause instanceof RecoverableException ||
                               cause instanceof NonRecoverableException) {
                                throw cause;
                            }
                        }
                        throw e;
                    }
                }
            };
        }
    }
    // //////////////////////////////////////

    
Convenience method to avoid some boilerplate and rename (as more in keeping with the org.apache.isis.applib.fixturescripts.FixtureScript API compared to the older org.apache.isis.applib.fixtures.InstallableFixture API).
    protected static void runScript(FixtureScript... fixtureScripts) {
        scenarioExecution().install(fixtureScripts);
    }
    // //////////////////////////////////////
    @Rule
    public JUnitRuleMockery2 context = JUnitRuleMockery2.createFor(.);
    @Rule
    public ExpectedException expectedExceptions = ExpectedException.none();
New to GrepCode? Check out our FAQ X