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