Android Open Source - CucumberAndroidGradle Android Object Factory






From Project

Back to project page CucumberAndroidGradle.

License

The source code is released under:

Apache License

If you think the Android project CucumberAndroidGradle listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cucumber.runtime.android;
// w w w .  j  av a2 s  . c  o m
import android.app.Instrumentation;
import android.content.Intent;
import android.test.ActivityInstrumentationTestCase2;
import android.test.AndroidTestCase;
import android.test.InstrumentationTestCase;
import cucumber.runtime.CucumberException;
import cucumber.runtime.java.ObjectFactory;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

class AndroidObjectFactory implements ObjectFactory {
    private final Instrumentation mInstrumentation;
    private final Set<Class<?>> mClasses = new HashSet<Class<?>>();
    private final Map<Class<?>, Object> mInstances = new HashMap<Class<?>, Object>();

    public AndroidObjectFactory(Instrumentation instrumentation) {
        mInstrumentation = instrumentation;
    }

    public void start() {
        // No-op
    }

    public void stop() {
        mInstances.clear();
    }

    public void addClass(Class<?> clazz) {
        mClasses.add(clazz);
    }

    public <T> T getInstance(Class<T> type) {
        if (mInstances.containsKey(type)) {
            return type.cast(mInstances.get(type));
        } else {
            return cacheNewInstance(type);
        }
    }

    private <T> T cacheNewInstance(Class<T> type) {
        try {
            Constructor<T> constructor = type.getConstructor();
            T instance = constructor.newInstance();

            if (instance instanceof ActivityInstrumentationTestCase2) {
                ((ActivityInstrumentationTestCase2) instance).injectInstrumentation(mInstrumentation);
                // This Intent prevents the ActivityInstrumentationTestCase2 to stall on
                // Intent.startActivitySync (when calling getActivity) if the activity is already running.
                Intent intent = new Intent();
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                ((ActivityInstrumentationTestCase2) instance).setActivityIntent(intent);
            } else if (instance instanceof InstrumentationTestCase) {
                ((InstrumentationTestCase) instance).injectInstrumentation(mInstrumentation);
            } else if (instance instanceof AndroidTestCase) {
                ((AndroidTestCase) instance).setContext(mInstrumentation.getTargetContext());
            }
            mInstances.put(type, instance);
            return instance;
        } catch (NoSuchMethodException e) {
            throw new CucumberException(String.format("%s doesn't have an empty constructor. If you need DI, put cucumber-picocontainer on the classpath", type), e);
        } catch (Exception e) {
            throw new CucumberException(String.format("Failed to instantiate %s", type), e);
        }
    }
}




Java Source Code List

com.example.cucumberandroid.MainActivity.java
cucumber.api.android.CucumberInstrumentationTestRunner.java
cucumber.api.android.CucumberInstrumentation.java
cucumber.api.android.RunWithCucumber.java
cucumber.runtime.android.AndroidBackend.java
cucumber.runtime.android.AndroidClasspathMethodScanner.java
cucumber.runtime.android.AndroidFormatter.java
cucumber.runtime.android.AndroidJavaStepDefinition.java
cucumber.runtime.android.AndroidMethodFormat.java
cucumber.runtime.android.AndroidObjectFactory.java
cucumber.runtime.android.AndroidResourceLoader.java
cucumber.runtime.android.AndroidResource.java
cucumber.runtime.android.JavaHookDefinition.java
cucumber.runtime.android.JavaSnippet.java
ext.com.android.internal.os.LoggingPrintStream.java
ext.com.google.android.collect.Maps.java
ext.com.google.android.collect.Sets.java