001 /*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2008-2017 the original author or authors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package griffon.pivot.test;
019
020 import griffon.core.GriffonApplication;
021 import griffon.core.artifact.GriffonArtifact;
022 import griffon.core.artifact.GriffonClass;
023 import griffon.core.env.Environment;
024 import griffon.core.test.TestFor;
025 import org.apache.pivot.wtk.DesktopApplicationContext;
026 import org.codehaus.griffon.runtime.core.DefaultGriffonApplication;
027 import org.codehaus.griffon.runtime.pivot.TestDesktopPivotApplication;
028 import org.junit.rules.MethodRule;
029 import org.junit.runners.model.FrameworkMethod;
030 import org.junit.runners.model.Statement;
031
032 import javax.annotation.Nonnull;
033 import javax.swing.SwingUtilities;
034 import java.lang.reflect.Field;
035 import java.util.Arrays;
036
037 import static java.util.Objects.requireNonNull;
038
039 /**
040 * @author Andres Almiray
041 * @since 2.0.0
042 */
043 public class GriffonPivotRule implements MethodRule {
044 private String[] startupArgs;
045
046 public GriffonPivotRule() {
047 this(DefaultGriffonApplication.EMPTY_ARGS);
048 }
049
050 public GriffonPivotRule(@Nonnull String[] startupArgs) {
051 requireNonNull(startupArgs, "Argument 'startupArgs' must not be null");
052 this.startupArgs = startupArgs = Arrays.copyOf(startupArgs, startupArgs.length);
053 if (!Environment.isSystemSet()) {
054 System.setProperty(Environment.KEY, Environment.TEST.getName());
055 }
056 }
057
058 @Override
059 public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
060 return new Statement() {
061 @Override
062 public void evaluate() throws Throwable {
063 TestDesktopPivotApplication.init(target);
064 SwingUtilities.invokeAndWait(new Runnable() {
065 @Override
066 public void run() {
067 DesktopApplicationContext.main(TestDesktopPivotApplication.class, startupArgs);
068 }
069 });
070 TestDesktopPivotApplication.getLatch().await();
071 GriffonApplication application = TestDesktopPivotApplication.getApplication();
072 application.getInjector().injectMembers(target);
073 handleTestForAnnotation(application, target);
074
075 before(application, target);
076 try {
077 base.evaluate();
078 } finally {
079 after(application, target);
080 }
081 }
082 };
083 }
084
085 protected void before(@Nonnull GriffonApplication application, @Nonnull Object target) throws Throwable {
086
087 }
088
089 protected void after(@Nonnull GriffonApplication application, @Nonnull Object target) {
090 application.shutdown();
091 }
092
093 private void handleTestForAnnotation(@Nonnull GriffonApplication application, @Nonnull Object target) throws Exception {
094 TestFor testFor = target.getClass().getAnnotation(TestFor.class);
095 if (testFor != null) {
096 Class artifactClass = testFor.value();
097 GriffonArtifact artifact = application.getArtifactManager().newInstance(artifactClass);
098 GriffonClass griffonClass = artifact.getGriffonClass();
099 Field artifactField = target.getClass().getDeclaredField(griffonClass.getArtifactType());
100 artifactField.setAccessible(true);
101 artifactField.set(target, artifact);
102 }
103 }
104 }
|