aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/androidTest/java/org/wordpress/android/mocks/XMLRPCFactoryTest.java
blob: 60852f11c9730fc04297ff924de9505718e7293a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package org.wordpress.android.mocks;

import android.content.Context;

import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.AppLog.T;
import org.xmlrpc.android.XMLRPCClientInterface;
import org.xmlrpc.android.XMLRPCFactoryAbstract;

import java.net.URI;
import java.util.HashSet;
import java.util.Set;

public class XMLRPCFactoryTest implements XMLRPCFactoryAbstract {
    public static String sPrefix = "default";
    public static Context sContext;
    public static Mode sMode = Mode.EMPTY;
    public static Set<XMLRPCClientCustomizableMockAbstract> sInstances =
            new HashSet<XMLRPCClientCustomizableMockAbstract>();

    public static void setContextAllInstances(Context context) {
        sContext = context;
        if (sMode != Mode.CUSTOMIZABLE_JSON && sMode != Mode.CUSTOMIZABLE_XML) {
            AppLog.e(T.TESTS, "You tried to change context on a non-customizable XMLRPCClient mock");
        }
        for (XMLRPCClientCustomizableMockAbstract client : sInstances) {
            client.setContext(context);
        }
    }

    public static void setPrefixAllInstances(String prefix) {
        sPrefix = prefix;
        if (sMode != Mode.CUSTOMIZABLE_JSON && sMode != Mode.CUSTOMIZABLE_XML) {
            AppLog.e(T.TESTS, "You tried to change prefix on a non-customizable XMLRPCClient mock");
        }
        for (XMLRPCClientCustomizableMockAbstract client : sInstances) {
            client.setPrefix(prefix);
        }
    }

    public XMLRPCClientInterface make(URI uri, String httpUser, String httpPassword) {
        switch (sMode) {
            case CUSTOMIZABLE_JSON:
                XMLRPCClientCustomizableJSONMock clientJSONMock = new XMLRPCClientCustomizableJSONMock(uri, httpUser,
                        httpPassword);
                if (sContext != null) {
                    clientJSONMock.setContextAndPrefix(sContext, sPrefix);
                } else {
                    AppLog.e(T.TESTS, "You have to set XMLRPCFactoryTest.sContext field before running tests");
                    throw new IllegalStateException();
                }
                AppLog.v(T.TESTS, "make: XMLRPCClientCustomizableJSONMock");
                sInstances.add(clientJSONMock);
                return clientJSONMock;
            case CUSTOMIZABLE_XML:
                XMLRPCClientCustomizableXMLMock clientXMLMock = new XMLRPCClientCustomizableXMLMock(uri, httpUser,
                        httpPassword);
                if (sContext != null) {
                    clientXMLMock.setContextAndPrefix(sContext, sPrefix);
                } else {
                    AppLog.e(T.TESTS, "You have to set XMLRPCFactoryTest.sContext field before running tests");
                    throw new IllegalStateException();
                }
                AppLog.v(T.TESTS, "make: XMLRPCClientCustomizableXMLMock");
                sInstances.add(clientXMLMock);
                return clientXMLMock;
            case EMPTY:
            default:
                AppLog.v(T.TESTS, "make: XMLRPCClientEmptyMock");
                return new XMLRPCClientEmptyMock(uri, httpUser, httpPassword);
        }
    }

    public enum Mode {EMPTY, CUSTOMIZABLE_JSON, CUSTOMIZABLE_XML}
}