summaryrefslogtreecommitdiffstats
path: root/jack-tests/src/com/android/jack/test/helper/RuntimeTestHelper.java
blob: 910cffbfbe8eb408624372a9c49939a6d0e3591d (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * Copyright (C) 2014 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 com.android.jack.test.helper;

import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.io.Files;

import com.android.jack.test.TestsProperties;
import com.android.jack.test.runner.RuntimeRunner;
import com.android.jack.test.runtime.RuntimeTestInfo;
import com.android.jack.test.toolchain.AbstractTestTools;
import com.android.jack.test.toolchain.AndroidToolchain;
import com.android.jack.test.toolchain.Toolchain.SourceLevel;

import junit.framework.Assert;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
 * This class is used to write runtime tests.
 */
public class RuntimeTestHelper {

  @Nonnull
  private List<String> jUnitClasses = new ArrayList<String>(1);

  @Nonnull
  RuntimeTestInfo[] runtimeTestInfos;

  private boolean withDebugInfos = false;

  private SourceLevel level = SourceLevel.JAVA_6;

  @Nonnull
  private List<FileChecker> testExeCheckers = new ArrayList<FileChecker>(0);

  public RuntimeTestHelper(@Nonnull RuntimeTestInfo... rtTestInfos) {
    runtimeTestInfos = Arrays.copyOf(rtTestInfos, rtTestInfos.length);

    for (RuntimeTestInfo info : rtTestInfos) {
      jUnitClasses.add(info.jUnit);
    }
  }

  @Nonnull
  public RuntimeTestHelper setWithDebugInfos(boolean withDebugInfos) {
    this.withDebugInfos = withDebugInfos;
    return this;
  }

  @Nonnull
  public RuntimeTestHelper setSourceLevel(@Nonnull SourceLevel level) {
    this.level = level;
    return this;
  }

  @Nonnull
  public RuntimeTestHelper addTestExeFileChecker(@Nonnull FileChecker checker) {
    this.testExeCheckers.add(checker);
    return this;
  }

  @Nonnull
  List<File> getReferenceExtraSources() {
    List<File> result = new ArrayList<File>();
    for (RuntimeTestInfo info : runtimeTestInfos) {
      result.addAll(info.referenceExtraSources);
    }
    return result;
  }


  public void compileAndRunTest() throws Exception {
    compileAndRunTest(/* checkStructure = */ false);
  }

  public void compileAndRunTest(boolean checkStructure) throws Exception {
    Properties testProperties = new Properties();
    try {
      loadTestProperties(testProperties);
    } catch (FileNotFoundException e) {
      // No file, no pb
    }

    AndroidToolchain candidateTestTools = createCandidateToolchain();
    AndroidToolchain referenceTestTools = createReferenceToolchain();

    File[] candidateBootClasspath = candidateTestTools.getDefaultBootClasspath();
    File[] referenceBootClasspath = referenceTestTools.getDefaultBootClasspath();

    String candidateBootClasspathAsString =
        AbstractTestTools.getClasspathAsString(candidateTestTools.getDefaultBootClasspath());
    String referenceBootClasspathAsString =
        AbstractTestTools.getClasspathAsString(referenceTestTools.getDefaultBootClasspath());

    // Compile lib src
    File libLibRef = null;
    File libBinaryRef = null;
    File libLibCandidate = null;
    File[] libSources = getLibSrc();
    if (libSources.length != 0) {
      libLibRef =
          AbstractTestTools.createTempFile("lib-ref", referenceTestTools.getLibraryExtension());
      File libBinaryRefDir = AbstractTestTools.createTempDir();
      libBinaryRef = new File(libBinaryRefDir, referenceTestTools.getBinaryFileName());
      referenceTestTools.addToClasspath(referenceBootClasspath)
      .srcToLib(libLibRef, /* zipFiles = */ true, libSources);
      referenceTestTools.libToExe(libLibRef, libBinaryRefDir, /* zipFile */ false);

      libLibCandidate = AbstractTestTools.createTempFile("lib-candidate",
          candidateTestTools.getLibraryExtension());
      candidateTestTools.addToClasspath(candidateBootClasspath)
      .srcToLib(libLibCandidate, /* zipFiles = */ true, libSources);
    }

    // Compile test src
    candidateTestTools = createCandidateToolchain();

    String candidateClasspathAsString;
    String referenceClasspathAsString;
    File[] candidateClassPath;
    File[] referenceClasspath;
    if (libSources.length != 0) {
      candidateClassPath = new File[candidateBootClasspath.length + 1];
      System.arraycopy(candidateBootClasspath, 0, candidateClassPath, 0,
          candidateBootClasspath.length);
      candidateClassPath[candidateClassPath.length - 1] = libLibCandidate;
      candidateClasspathAsString = AbstractTestTools.getClasspathAsString(candidateClassPath);
      referenceClasspath = new File[referenceBootClasspath.length + 1];
      System.arraycopy(referenceBootClasspath, 0, referenceClasspath, 0,
          referenceBootClasspath.length);
      referenceClasspath[referenceClasspath.length - 1] = libLibRef;
      referenceClasspathAsString = AbstractTestTools.getClasspathAsString(referenceClasspath);
    } else {
      candidateClassPath = candidateBootClasspath;
      referenceClasspath = referenceBootClasspath;
      candidateClasspathAsString = candidateBootClasspathAsString;
      referenceClasspathAsString = referenceBootClasspathAsString;
    }

    File jarjarRules = getJarjarRules();
    List<File> proguargFlags = getProguardFlags();

    File testBinaryDir = AbstractTestTools.createTempDir();
    File testBinary = new File(testBinaryDir, candidateTestTools.getBinaryFileName());

    if (checkStructure) {
      CheckDexStructureTestHelper helper = new CheckDexStructureTestHelper(getSrcDir());
      helper.setCandidateClasspath(candidateClassPath);
      helper.setCandidateTestTools(candidateTestTools);
      if (jarjarRules != null) {
        helper.setJarjarRulesFile(jarjarRules);
      }
      helper.setProguardFlags(proguargFlags.toArray(new File[proguargFlags.size()]));
      helper.compare();
      Files.copy(helper.getCandidateDex(),
          new File(testBinaryDir, helper.getCandidateDex().getName()));
    } else {
      if (jarjarRules != null) {
        candidateTestTools.setJarjarRules(jarjarRules);
      }
      candidateTestTools.addProguardFlags(proguargFlags.toArray(new File [proguargFlags.size()]));
      candidateTestTools.addToClasspath(candidateClassPath)
      .srcToExe(testBinaryDir, /* zipFile = */ false,
          getSrcDir());
    }

    for (FileChecker checker : testExeCheckers) {
      checker.check(testBinary);
    }

    referenceTestTools = createReferenceToolchain();
    File testLib =
        AbstractTestTools.createTempFile("testRef", referenceTestTools.getLibraryExtension());
    referenceTestTools.addToClasspath(referenceClasspath)
    .srcToLib(testLib, /* zipFiles = */ true,
        getSrcDir());

    // Compile link src
    candidateTestTools = createCandidateToolchain();

    File linkBinary = null;
    if (getLinkSrc().length != 0) {
      File linkBinaryDir = AbstractTestTools.createTempDir();
      linkBinary = new File(linkBinaryDir, candidateTestTools.getBinaryFileName());
      candidateTestTools.setJarjarRules(jarjarRules);
      candidateTestTools.addProguardFlags(proguargFlags.toArray(new File [proguargFlags.size()]));
      candidateTestTools.addToClasspath(candidateBootClasspath)
      .srcToExe(linkBinaryDir, /* zipFile = */ false, getLinkSrc());
    }

    // Compile ref part src
    referenceTestTools = createReferenceToolchain();

    List<File> referenceClasspathAsList = new ArrayList<File>();
    for (File f : referenceBootClasspath) {
      referenceClasspathAsList.add(f);
    }
    if (libLibRef != null) {
      referenceClasspathAsList.add(libLibRef);
    }
    if (testLib != null) {
      referenceClasspathAsList.add(testLib);
    }

    referenceClasspath =
        referenceClasspathAsList.toArray(new File[referenceClasspathAsList.size()]);


    File [] refSources = getRefSrcDir();
    List<File> extras = getReferenceExtraSources();
    List<File> sources = new ArrayList<File>(extras.size() + refSources.length);
    Collections.addAll(sources, refSources);
    sources.addAll(extras);

    File refPartBinaryDir = AbstractTestTools.createTempDir();
    File refPartBinary = new File(refPartBinaryDir, referenceTestTools.getBinaryFileName());
    referenceTestTools.addToClasspath(referenceClasspath)
    .srcToExe(
        refPartBinaryDir,
        /* zipFile = */ false,
        sources.toArray(new File[sources.size()]));

    List<File> rtClasspath = new ArrayList<File>();
    rtClasspath.add(new File(TestsProperties.getJackRootDir(),
        "jack-tests/prebuilts/core-hostdex.jar"));
    rtClasspath.add(new File(TestsProperties.getJackRootDir(),
        "jack-tests/prebuilts/junit4-hostdex.jar"));
   if (refPartBinary != null) {
      rtClasspath.add(refPartBinary);
    }
    if (linkBinary != null) {
      rtClasspath.add(linkBinary);
    }
    if (testBinary != null) {
      rtClasspath.add(testBinary);
    }
    if (libBinaryRef != null) {
      rtClasspath.add(libBinaryRef);
    }

    // Run JUnit on runtime env(s)
    runOnRuntimeEnvironments(jUnitClasses, testProperties,
        rtClasspath.toArray(new File[rtClasspath.size()]));
  }

  @Nonnull
  private AndroidToolchain createCandidateToolchain() {
    AndroidToolchain candidateTestTools =
        AbstractTestTools.getCandidateToolchain(AndroidToolchain.class);
    candidateTestTools.setSourceLevel(level);
    candidateTestTools.setWithDebugInfos(withDebugInfos);
    return candidateTestTools;
  }

  @Nonnull
  private AndroidToolchain createReferenceToolchain() {
    AndroidToolchain referenceTestTools =
        AbstractTestTools.getReferenceToolchain(AndroidToolchain.class);
    referenceTestTools.setSourceLevel(level);
    referenceTestTools.setWithDebugInfos(withDebugInfos);
    return referenceTestTools;
  }

  private static void runOnRuntimeEnvironments(@Nonnull List<String> jUnitClasses,
      @Nonnull Properties testProperties, @Nonnull File... classpathFiles) throws Exception {
    List<RuntimeRunner> runnerList = AbstractTestTools.listRuntimeTestRunners(testProperties);
    String[] names = jUnitClasses.toArray(new String[jUnitClasses.size()]);
    for (RuntimeRunner runner : runnerList) {
      Assert.assertEquals(0, runner.runJUnit(
          getRuntimeArgs(runner.getClass().getSimpleName(), testProperties),
          AbstractTestTools.JUNIT_RUNNER_NAME, names, classpathFiles));
    }
  }

  @Nonnull
  private static final String[] getRuntimeArgs(@Nonnull String rtName,
      @Nonnull Properties properties) {

    String args = properties.getProperty("rt.args." + rtName);
    List<String> result = new ArrayList<String>(0);
    if (args != null) {
      for (String arg : Splitter.on(CharMatcher.WHITESPACE).split(args)) {
        result.add(arg);
      }
    }
    return result.toArray(new String[result.size()]);
  }

  private void loadTestProperties(@Nonnull Properties properties) throws FileNotFoundException,
      IOException {
    for (RuntimeTestInfo info : runtimeTestInfos) {
      File propertyFile = new File(info.directory, info.propertyFileName);
      if (propertyFile.exists()) {
        if (runtimeTestInfos.length > 1) {
          throw new AssertionError("Not a regression test: " + info.directory.getAbsolutePath());
        }
        properties.load(new FileInputStream(propertyFile));
        break;
      }
    }
  }

  @Nonnull
  private File[] getSrcDir() {
    List<File> result = new ArrayList<File>();
    for (RuntimeTestInfo info : runtimeTestInfos) {
      File f = new File(info.directory, info.srcDirName);
      if (f.exists()) {
        result.add(f);
      }
    }
    return result.toArray(new File[result.size()]);
  }

  @Nonnull
  private File[] getLibSrc() {
    List<File> result = new ArrayList<File>();
    for (RuntimeTestInfo info : runtimeTestInfos) {
      File f = new File(info.directory, info.libDirName);
      if (f.exists()) {
        result.add(f);
      }
    }
    return result.toArray(new File[result.size()]);
  }

  @Nonnull
  private File[] getLinkSrc() {
    List<File> result = new ArrayList<File>();
    for (RuntimeTestInfo info : runtimeTestInfos) {
      File f = new File(info.directory, info.linkDirName);
      if (f.exists()) {
        if (runtimeTestInfos.length > 1) {
          // TODO(jmhenaff): check it's necessary
          throw new AssertionError("Not a regression test: " + info.directory.getAbsolutePath());
        }
        result.add(f);
      }
    }
    return result.toArray(new File[result.size()]);
  }

  @Nonnull
  private File[] getRefSrcDir() {
    List<File> result = new ArrayList<File>();
    for (RuntimeTestInfo info : runtimeTestInfos) {
      File f = new File(info.directory, info.refDirName);
      if (f.exists()) {
        result.add(f);
      }
    }
    return result.toArray(new File[result.size()]);
  }

  @CheckForNull
  private File getJarjarRules() {
    for (RuntimeTestInfo info : runtimeTestInfos) {
      File f = new File(info.directory, info.jarjarRulesFileName);
      if (f.exists()) {
        if (runtimeTestInfos.length > 1) {
          throw new AssertionError("Not a regression test: " + info.directory.getAbsolutePath());
        }
        return f;
      }
    }
    return null;
  }

  @Nonnull
  private List<File> getProguardFlags() {
    List<File> result = new ArrayList<File>();
    for (RuntimeTestInfo info : runtimeTestInfos) {
      for (String name : info.proguardFilesNames) {
        File f = new File(info.directory, name);
        if (f.exists()) {
          if (runtimeTestInfos.length > 1) {
            throw new AssertionError("Not a regression test: " + info.directory.getAbsolutePath());
          }
          result.add(f);
        }
      }
    }
    return result;
  }

}