Updated Branches: refs/heads/master 2d44e7d29 -> aafdf365b
DELTASPIKE-515 MockedJsfTestContainerAdapter Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/aafdf365 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/aafdf365 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/aafdf365 Branch: refs/heads/master Commit: aafdf365b2c8a16f3e8d868faf98a9a3ffbeb4c7 Parents: 2d44e7d Author: gpetracek <[email protected]> Authored: Thu Jan 30 11:21:27 2014 +0100 Committer: gpetracek <[email protected]> Committed: Thu Jan 30 11:23:15 2014 +0100 ---------------------------------------------------------------------- .../impl/jsf/MockedJsfTestContainerAdapter.java | 111 +++++++++++++++++++ 1 file changed, 111 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/aafdf365/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsfTestContainerAdapter.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsfTestContainerAdapter.java b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsfTestContainerAdapter.java new file mode 100644 index 0000000..7cffdc5 --- /dev/null +++ b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsfTestContainerAdapter.java @@ -0,0 +1,111 @@ +/* + * 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.deltaspike.testcontrol.impl.jsf; + +import org.apache.deltaspike.core.util.ClassUtils; +import org.apache.deltaspike.core.util.ExceptionUtils; +import org.apache.deltaspike.testcontrol.spi.ExternalContainer; + +import javax.enterprise.context.RequestScoped; +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; + +/** + * Optional adapter for MockedJsfTestContainer + * Requires MyFaces-Test v1.0.6 or higher + */ +//TODO use MockedJsfTestContainer without reflection once v1.0.6 of myfaces-test is released +public class MockedJsfTestContainerAdapter implements ExternalContainer +{ + private final Object mockedMyFacesTestContainer; + + private final Method containerStartMethod; + private final Method containerStopMethod; + + public MockedJsfTestContainerAdapter() + { + this.mockedMyFacesTestContainer = + ClassUtils.tryToInstantiateClassForName("org.apache.myfaces.test.mock.MockedJsfTestContainer"); + + if (this.mockedMyFacesTestContainer == null) + { + throw new IllegalStateException("This adapter requires MyFaces-Test v1.0.6 or higher."); + } + + try + { + this.containerStartMethod = this.mockedMyFacesTestContainer.getClass().getDeclaredMethod("setUp"); + this.containerStopMethod = this.mockedMyFacesTestContainer.getClass().getDeclaredMethod("tearDown"); + } + catch (NoSuchMethodException e) + { + throw ExceptionUtils.throwAsRuntimeException(e); + } + } + + public void boot() + { + //MockedJsfTestContainer needs to be bootstrapped for every request + } + + @Override + public void startScope(Class<? extends Annotation> scopeClass) + { + if (RequestScoped.class.equals(scopeClass)) + { + try + { + //see the comment at #boot + this.containerStartMethod.invoke(this.mockedMyFacesTestContainer); + } + catch (Exception e) + { + throw ExceptionUtils.throwAsRuntimeException(e); + } + } + } + + @Override + public void stopScope(Class<? extends Annotation> scopeClass) + { + if (RequestScoped.class.equals(scopeClass)) + { + try + { + //see the comment at #shutdown + this.containerStopMethod.invoke(this.mockedMyFacesTestContainer); + } + catch (Exception e) + { + throw ExceptionUtils.throwAsRuntimeException(e); + } + } + } + + public void shutdown() + { + //MockedJsfTestContainer needs to be stopped after every request + } + + @Override + public int getOrdinal() + { + return 1000; //default in ds + } +}
