summaryrefslogtreecommitdiffstats
path: root/luni/src/test/etc/loading-test-jar/TestMethods.java
blob: bc22dc39ca0a4710b352b91d558528c786e29c84 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package test;

import test2.Target2;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Class used as part of the class loading tests. This class uses
 * other classes, some of which should have come from the same jar/dex
 * file and others of which should have come from a different jar/dex
 * file. Each test method in this class is called from the same-named
 * method in {@code DexClassLoaderTest}.
 */
public class TestMethods {
    /*
     * The following are all simple utility methods which, under
     * normal circumstances, would be part of other libraries
     * (specifically, JUnit and the Android libcore support library).
     *
     * However, this file gets compiled as an independent unit without
     * any dependencies. We could theoretically add dependencies on other
     * libraries, but that would make things much more complicated and
     * fragile, and for very little benefit.
     */

    /**
     * Simple sameness assertion checker.
     */
    public static void assertSame(Object expected, Object actual) {
        if (expected != actual) {
            throw new RuntimeException(
                "EXPECTED: " + expected + "; ACTUAL: " + actual);
        }
    }

    /**
     * Simple sameness assertion checker.
     */
    public static void assertSame(int expected, int actual) {
        if (expected != actual) {
            throw new RuntimeException(
                "EXPECTED: " + expected + "; ACTUAL: " + actual);
        }
    }

    /**
     * Fully read the contents of the given stream.
     */
    public static byte[] readFully(InputStream in) throws IOException {
        // This is a copy of the same-named method in libcore.io.Streams.
        byte[] buffer = new byte[1024];
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        while (true) {
            int byteCount = in.read(buffer);
            if (byteCount == -1) {
                return bytes.toByteArray();
            }
            bytes.write(buffer, 0, byteCount);
        }
    }

    /*
     * Test methods that use another class from the same dex/jar file
     */

    /**
     * Test that an instance of a sibling class can be constructed.
     */
    public static void test_constructor() {
        new Target();
    }

    /**
     * Test calling a static method on a sibling class.
     */
    public static void test_callStaticMethod() {
        assertSame("blort", Target.blort());
    }

    /**
     * Test getting a static variable of a sibling class.
     */
    public static void test_getStaticVariable() {
        Target.setStaticVariable(22);
        assertSame(22, Target.staticVariable);
    }

    /**
     * Test calling an instance method on a sibling class.
     */
    public static void test_callInstanceMethod() {
        Target target = new Target();
        assertSame("zorch", target.zorch());
    }

    /**
     * Test getting an instance variable of a sibling class.
     */
    public static void test_getInstanceVariable() {
        Target target = new Target();
        target.setInstanceVariable(10098);
        assertSame(10098, target.instanceVariable);
    }

    /**
     * Test getting a resource which should be in the same jar
     * file as this class.
     */
    public static void test_getResourceAsStream() throws IOException {
        ClassLoader cl = TestMethods.class.getClassLoader();
        InputStream in = cl.getResourceAsStream("test/Resource1.txt");
        byte[] contents = readFully(in);
        String s = new String(contents, "UTF-8");

        assertSame("Muffins are tasty!\n", s.intern());
    }

    /*
     * Test methods that use a class from a different dex/jar file
     */

    /**
     * Test that an instance of a cousin class can be constructed.
     */
    public static void test_diff_constructor() {
        new Target2();
    }

    /**
     * Test calling a static method on a cousin class.
     */
    public static void test_diff_callStaticMethod() {
        assertSame("frotz", Target2.frotz());
    }

    /**
     * Test getting a static variable of a cousin class.
     */
    public static void test_diff_getStaticVariable() {
        Target2.setStaticIgram(220);
        assertSame(220, Target2.staticIgram);
    }

    /**
     * Test calling an instance method on a cousin class.
     */
    public static void test_diff_callInstanceMethod() {
        Target2 target = new Target2();
        assertSame("fizmo", target.fizmo());
    }

    /**
     * Test getting an instance variable of a cousin class.
     */
    public static void test_diff_getInstanceVariable() {
        Target2 target = new Target2();
        target.setInstanceMagri(10098);
        assertSame(10098, target.instanceMagri);
    }

    /**
     * Test getting a resource which should be in a different jar
     * file as this class.
     */
    public static void test_diff_getResourceAsStream() throws IOException {
        ClassLoader cl = TestMethods.class.getClassLoader();
        InputStream in = cl.getResourceAsStream("test2/Resource2.txt");
        byte[] contents = readFully(in);
        String s = new String(contents, "UTF-8");

        assertSame("Who doesn't like a good biscuit?\n", s.intern());
    }
}