summaryrefslogtreecommitdiffstats
path: root/jack-tests/src/com/android/jack/test/helper/IncrementalTestHelper.java
blob: a69c4daf6699df35eb10806125cdbbcf2f4296fc (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
/*
 * 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.android.jack.library.FileType;
import com.android.jack.library.InputJackLibrary;
import com.android.jack.library.LibraryIOException;
import com.android.jack.test.runner.AbstractRuntimeRunner;
import com.android.jack.test.runner.RuntimeRunner;
import com.android.jack.test.toolchain.AbstractTestTools;
import com.android.jack.test.toolchain.IToolchain;
import com.android.jack.test.toolchain.JackApiToolchainBase;
import com.android.jack.test.toolchain.JackBasedToolchain.MultiDexKind;
import com.android.jack.test.toolchain.JackCliToolchain;
import com.android.jack.test.toolchain.LegacyJillToolchain;
import com.android.sched.vfs.InputVFile;
import com.android.sched.vfs.VPath;

import junit.framework.Assert;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

/**
 * This class is used to write tests on incremental compilation.
 */
public class IncrementalTestHelper {

  @Nonnull
  private final File testingFolder;
  @Nonnull
  private final File sourceFolder;
  @Nonnull
  private final File dexOutDir;
  @Nonnull
  private final File dexFile;
  @Nonnull
  private final File compilerStateFolder;
  @Nonnull
  private final Set<File> javaFiles = new HashSet<File>();
  @Nonnull
  private final Map<VPath, Long> fileModificationDate = new HashMap<VPath, Long>();
  @Nonnull
  private OutputStream out = System.out;
  @Nonnull
  private OutputStream err = System.err;

  private boolean isApiTest = false;

  public IncrementalTestHelper(@Nonnull File testingFolder) throws IOException {
    this.testingFolder = testingFolder;
    this.sourceFolder = new File(testingFolder, "src");
    if (!this.sourceFolder.mkdirs()) {
      throw new IOException("Failed to create folder " + this.sourceFolder.getAbsolutePath());
    }
    compilerStateFolder = new File(testingFolder, "compileState");
    if (!compilerStateFolder.exists() && !compilerStateFolder.mkdir()) {
      throw new IOException("Failed to create folder " + compilerStateFolder.getAbsolutePath());
    }

    dexOutDir = new File(testingFolder, "outputBinary");
    if (!dexOutDir.exists() && !dexOutDir.mkdir()) {
      throw new IOException("Failed to create folder " + dexOutDir.getAbsolutePath());
    }
    dexFile = new File(dexOutDir, "classes.dex");
  }

  public void setOut(OutputStream out) {
    this.out = out;
  }

  public void setErr(OutputStream err) {
    this.err = err;
  }

  public void setIsApiTest() {
    isApiTest = true;
  }

  @Nonnull
  public File addJavaFile(@Nonnull String packageName, @Nonnull String fileName,
      @Nonnull String fileContent) throws IOException {
    File file = AbstractTestTools.createFile(sourceFolder, packageName, fileName, fileContent);
    javaFiles.add(file);
    return file;
  }

  public void deleteJavaFile(@Nonnull File javaFile)
      throws IOException {
    AbstractTestTools.deleteFile(javaFile);
    javaFiles.remove(javaFile);
  }

  @Nonnull
  public File getCompilerStateFolder() {
    return compilerStateFolder;
  }

  public void cleanSnapshot() {
    fileModificationDate.clear();
  }

  public void snapshotJackFilesModificationDate() throws LibraryIOException {
    InputJackLibrary compilerStateLib = null;
    try {
      compilerStateLib = AbstractTestTools.getInputJackLibrary(compilerStateFolder);
      Iterator<InputVFile> jayceIter = compilerStateLib.iterator(FileType.JAYCE);
      while (jayceIter.hasNext()) {
        InputVFile jayceFile = jayceIter.next();
        fileModificationDate.put(jayceFile.getPathFromRoot(),
            Long.valueOf(jayceFile.getLastModified()));
      }
    } finally {
      if (compilerStateLib != null) {
        compilerStateLib.close();
      }
    }
  }

  @Nonnull
  public List<String> getFQNOfRebuiltTypes() throws LibraryIOException {
    assert !fileModificationDate.isEmpty();

    List<String> fqnOfRebuiltTypes = new ArrayList<String>();
    InputJackLibrary compilerStateLib = null;
    try {
      compilerStateLib = AbstractTestTools.getInputJackLibrary(compilerStateFolder);
      Iterator<InputVFile> jayceIter = compilerStateLib.iterator(FileType.JAYCE);
      while (jayceIter.hasNext()) {
        InputVFile jayceFile = jayceIter.next();
        VPath path = jayceFile.getPathFromRoot();
        Long previousDate = fileModificationDate.get(path);
        if (previousDate == null || jayceFile.getLastModified() > previousDate.longValue()) {
          String fqnWithExtension = path.getPathAsString('.');
          String fqn = fqnWithExtension.substring(0,
              fqnWithExtension.lastIndexOf(FileType.JAYCE.getFileExtension()));
          fqnOfRebuiltTypes.add(fqn);
        }
      }
    } finally {
      if (compilerStateLib != null) {
        compilerStateLib.close();
      }
    }

    return fqnOfRebuiltTypes;
  }

  public void incrementalBuildFromFolder() throws Exception {
    incrementalBuildFromFolder(null, Collections.<File>emptyList());
  }

  public void incrementalBuildFromFolder(@Nonnull File[] classpath) throws Exception {
    incrementalBuildFromFolder(classpath, Collections.<File>emptyList());
  }

  public void incrementalBuildFromFolder(@CheckForNull File[] classpath,
      @Nonnull List<File> imports) throws Exception {
    incrementalBuildFromFolder(classpath, imports, MultiDexKind.NONE);
  }

  public void incrementalBuildFromFolder(@CheckForNull File[] classpath,
      @Nonnull List<File> imports, @Nonnull MultiDexKind multiDexKind) throws Exception {

    List<Class<? extends IToolchain>> excludeList = new ArrayList<Class<? extends IToolchain>>(1);
    excludeList.add(LegacyJillToolchain.class);
    if (isApiTest) {
      excludeList.add(JackCliToolchain.class);
    }

    JackApiToolchainBase jackToolchain =
        AbstractTestTools.getCandidateToolchain(JackApiToolchainBase.class, excludeList);
    jackToolchain.setIncrementalFolder(getCompilerStateFolder());
    jackToolchain.addStaticLibs(imports.toArray(new File[imports.size()]));
    jackToolchain.setMultiDexKind(multiDexKind);

    jackToolchain.setOutputStream(out);
    jackToolchain.setErrorStream(err);

    File[] bootclasspath = jackToolchain.getDefaultBootClasspath();

    jackToolchain.addToClasspath(bootclasspath);
    if (classpath != null) {
      jackToolchain.addToClasspath(classpath);
    }

    jackToolchain.srcToExe(dexOutDir, /* zipFile = */ false, sourceFolder);

    Thread.sleep(1000);
  }

  @Nonnull
  public void run(@Nonnull String mainClass, @Nonnull String expected) throws Exception {
    List<RuntimeRunner> runnerList = AbstractTestTools.listRuntimeTestRunners(null);
    for (RuntimeRunner runner : runnerList) {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      ((AbstractRuntimeRunner) runner).setOutputStream(out);
      Assert.assertEquals(0, runner.run(new String[0], mainClass, dexFile));
      Assert.assertEquals(expected, out.toString());
    }
  }

  @Nonnull
  public File getDexFile() {
    return dexFile;
  }

  @Nonnull
  public int getJayceCount() throws LibraryIOException {
    int size = 0;
    InputJackLibrary compilerStateLib = null;
    try {
      compilerStateLib = AbstractTestTools.getInputJackLibrary(compilerStateFolder);
      Iterator<InputVFile> jayceIter = compilerStateLib.iterator(FileType.JAYCE);
      while (jayceIter.hasNext()) {
        size++;
        jayceIter.next();
      }
    } finally {
      if (compilerStateLib != null) {
        compilerStateLib.close();
      }
    }
    return size;
  }
}