001 /*
002 * Copyright 2008-2014 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.core.test;
017
018 import griffon.core.ApplicationBootstrapper;
019 import griffon.core.GriffonApplication;
020 import griffon.core.artifact.GriffonArtifact;
021 import griffon.core.artifact.GriffonClass;
022 import griffon.core.env.Environment;
023 import org.codehaus.griffon.runtime.core.DefaultGriffonApplication;
024 import org.codehaus.griffon.runtime.core.TestApplicationBootstrapper;
025 import org.junit.rules.MethodRule;
026 import org.junit.runners.model.FrameworkMethod;
027 import org.junit.runners.model.Statement;
028
029 import javax.annotation.Nonnull;
030 import java.lang.reflect.Constructor;
031 import java.lang.reflect.Field;
032
033 import static java.util.Objects.requireNonNull;
034
035 /**
036 * @author Andres Almiray
037 * @since 2.0.0
038 */
039 public class GriffonUnitRule implements MethodRule {
040 private String[] startupArgs;
041 private Class<? extends GriffonApplication> applicationClass;
042 private Class<? extends ApplicationBootstrapper> applicationBootstrapper;
043
044 public GriffonUnitRule() {
045 this(DefaultGriffonApplication.EMPTY_ARGS, DefaultGriffonApplication.class, TestApplicationBootstrapper.class);
046 }
047
048 public GriffonUnitRule(@Nonnull Class<? extends GriffonApplication> applicationClass) {
049 this(DefaultGriffonApplication.EMPTY_ARGS, applicationClass, TestApplicationBootstrapper.class);
050 }
051
052 public GriffonUnitRule(@Nonnull Class<? extends GriffonApplication> applicationClass, @Nonnull Class<? extends ApplicationBootstrapper> applicationBootstrapper) {
053 this(DefaultGriffonApplication.EMPTY_ARGS, applicationClass, applicationBootstrapper);
054 }
055
056 public GriffonUnitRule(@Nonnull String[] startupArgs) {
057 this(startupArgs, DefaultGriffonApplication.class, TestApplicationBootstrapper.class);
058 }
059
060 public GriffonUnitRule(@Nonnull String[] startupArgs, @Nonnull Class<? extends GriffonApplication> applicationClass) {
061 this(startupArgs, applicationClass, TestApplicationBootstrapper.class);
062 }
063
064 public GriffonUnitRule(@Nonnull String[] startupArgs, @Nonnull Class<? extends GriffonApplication> applicationClass, @Nonnull Class<? extends ApplicationBootstrapper> applicationBootstrapper) {
065 requireNonNull(startupArgs, "Argument 'startupArgs' must not be null");
066 this.startupArgs = new String[startupArgs.length];
067 System.arraycopy(startupArgs, 0, this.startupArgs, 0, startupArgs.length);
068 this.applicationClass = requireNonNull(applicationClass, "Argument 'applicationClass' must not be null");
069 this.applicationBootstrapper = requireNonNull(applicationBootstrapper, "Argument 'applicationBootstrapper' must not be null");
070 if (!Environment.isSystemSet()) {
071 System.setProperty(Environment.KEY, Environment.TEST.getName());
072 }
073 }
074
075 @Override
076 public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
077 return new Statement() {
078 @Override
079 public void evaluate() throws Throwable {
080 GriffonApplication application = instantiateApplication();
081 ApplicationBootstrapper bootstrapper = instantiateApplicationBootstrapper(application, target);
082
083 bootstrapper.bootstrap();
084 application.initialize();
085 application.getInjector().injectMembers(target);
086 handleTestForAnnotation(application, target);
087
088 before(application, target);
089 try {
090 base.evaluate();
091 } finally {
092 after(application, target);
093 }
094 }
095 };
096 }
097
098 protected void before(@Nonnull GriffonApplication application, @Nonnull Object target) throws Throwable {
099
100 }
101
102 protected void after(@Nonnull GriffonApplication application, @Nonnull Object target) {
103 application.shutdown();
104 }
105
106 @Nonnull
107 private GriffonApplication instantiateApplication() throws Exception {
108 String[] array = new String[0];
109 Constructor<? extends GriffonApplication> ctor = applicationClass.getDeclaredConstructor(array.getClass());
110 return ctor.newInstance(new Object[]{startupArgs});
111 }
112
113 @Nonnull
114 private ApplicationBootstrapper instantiateApplicationBootstrapper(@Nonnull GriffonApplication application, @Nonnull Object testCase) throws Exception {
115 Constructor<? extends ApplicationBootstrapper> constructor = applicationBootstrapper.getDeclaredConstructor(GriffonApplication.class);
116 ApplicationBootstrapper bootstrapper = constructor.newInstance(application);
117 if (bootstrapper instanceof TestCaseAware) {
118 ((TestCaseAware) bootstrapper).setTestCase(testCase);
119 }
120 return bootstrapper;
121 }
122
123 private void handleTestForAnnotation(@Nonnull GriffonApplication application, @Nonnull Object target) throws Exception {
124 TestFor testFor = target.getClass().getAnnotation(TestFor.class);
125 if (testFor != null) {
126 Class artifactClass = testFor.value();
127 GriffonArtifact artifact = application.getArtifactManager().newInstance(artifactClass);
128 GriffonClass griffonClass = artifact.getGriffonClass();
129 Field artifactField = target.getClass().getDeclaredField(griffonClass.getArtifactType());
130 artifactField.setAccessible(true);
131 artifactField.set(target, artifact);
132 }
133 }
134 }
|