summaryrefslogtreecommitdiffstats
path: root/jack-tests/tests/com/android/jack/error/SourceErrorTest.java
blob: afee3feaeb999ffb04801385f979a7ab6fe4fc09 (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
/*
 * 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.error;

import com.android.jack.frontend.FrontendCompilationException;
import com.android.jack.test.helper.ErrorTestHelper;
import com.android.jack.test.toolchain.AbstractTestTools;
import com.android.jack.test.toolchain.IToolchain;
import com.android.jack.test.toolchain.JackApiToolchain;

import org.junit.Assert;
import org.junit.Test;

import java.io.ByteArrayOutputStream;

import javax.annotation.Nonnull;

/**
 * JUnit test checking Jack behavior with source errors.
 */
public class SourceErrorTest {

  /**
   * Checks that compilation fails because of invalid "class" keyword.
   */
  @Test
  public void testInvalidSource001() throws Exception {
    ErrorTestHelper helper = new ErrorTestHelper();

    AbstractTestTools.createFile(helper.getSourceFolder(), "jack.invalidsource", "A.java", "package jack.invalidsource;\n"
        + "public clas A {}\n");

    JackApiToolchain toolchain = AbstractTestTools.getCandidateToolchain(JackApiToolchain.class);

    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    toolchain.setErrorStream(errOut);

    try {
      compile(toolchain, helper);
      Assert.fail();
    } catch (FrontendCompilationException ex) {
      // Failure is ok since source does not compile.
    } finally {
      Assert.assertTrue(
          errOut.toString().contains("Syntax error on token \"clas\", class expected"));
    }
  }

  /**
   * Checks that compilation fails because of invalid "public" keyword.
   */
  @Test
  public void testInvalidSource002() throws Exception {
    ErrorTestHelper helper = new ErrorTestHelper();

    AbstractTestTools.createFile(helper.getSourceFolder(), "jack.invalidsource", "A.java", "package jack.invalidsource;\n"
        + "publi class A {}\n");

    JackApiToolchain toolchain = AbstractTestTools.getCandidateToolchain(JackApiToolchain.class);

    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    toolchain.setErrorStream(errOut);

    try {
      compile(toolchain, helper);
      Assert.fail();
    } catch (FrontendCompilationException ex) {
      // Failure is ok since source does not compile.
    } finally {
      Assert.assertTrue(
          errOut.toString().contains("Syntax error on token \"publi\", public expected"));
    }
  }

  /**
   * Checks that compilation fails because of a class name that does not match the file name.
   */
  @Test
  public void testInvalidSource003() throws Exception {
    ErrorTestHelper helper = new ErrorTestHelper();

    AbstractTestTools.createFile(helper.getSourceFolder(), "jack.invalidsource", "A.java", "package jack.invalidsource;\n"
        + "public class B {}\n");

    JackApiToolchain toolchain = AbstractTestTools.getCandidateToolchain(JackApiToolchain.class);

    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    toolchain.setErrorStream(errOut);

    try {
      compile(toolchain, helper);
      Assert.fail();
    } catch (FrontendCompilationException ex) {
      // Failure is ok since source does not compile.
    } finally {
      Assert.assertTrue(
          errOut.toString().contains("The public type B must be defined in its own file"));
    }
  }

  /**
   * Checks that compilation fails because of an import of a class that is not on classpath.
   */
  @Test
  public void testInvalidSource004() throws Exception {
    ErrorTestHelper helper = new ErrorTestHelper();

    AbstractTestTools.createFile(helper.getSourceFolder(), "jack.invalidsource", "A.java", "package jack.invalidsource;\n"
        + "import jack.invalidsource.B;\n"
        + "public class A {}\n");

    JackApiToolchain toolchain = AbstractTestTools.getCandidateToolchain(JackApiToolchain.class);

    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    toolchain.setErrorStream(errOut);

    try {
      compile(toolchain, helper);
      Assert.fail();
    } catch (FrontendCompilationException ex) {
      // Failure is ok since source does not compile.
    } finally {
      Assert.assertTrue(
          errOut.toString().contains("The import jack.invalidsource.B cannot be resolved"));
    }
  }

  /**
   * Checks that compilation fails because there are too many methods in a single class.
   */
  @Test
  public void testInvalidSource005() throws Exception {
    ErrorTestHelper helper = new ErrorTestHelper();

    int methodCount = 65536;
    StringBuilder content =
        new StringBuilder("public class A {");
    // -1 due to implicit init method
    for (int mthIdx = 0; mthIdx < methodCount - 1; mthIdx++) {
      content.append("public void m" + mthIdx + "() {} \n");
    }
    content.append("} \n");

    AbstractTestTools.createFile(helper.getSourceFolder(), "jack.overflow", "A.java",
        "package jack.overflow; \n" + content.toString());

    JackApiToolchain toolchain = AbstractTestTools.getCandidateToolchain(JackApiToolchain.class);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    toolchain.setOutputStream(out);
    toolchain.setErrorStream(errOut);

    try {
      compile(toolchain, helper);
      Assert.fail();
    } catch (FrontendCompilationException e) {
      // Failure is ok, since there are too many methods.
    } finally {
      Assert.assertEquals("", out.toString());
      Assert.assertTrue(
          errOut.toString().contains("Too many methods for type A. Maximum is 65535"));
    }
  }

  /**
   * Checks that compilation fails because of several source errors.
   */
  @Test
  public void testInvalidSource006() throws Exception {
    ErrorTestHelper helper = new ErrorTestHelper();

    AbstractTestTools.createFile(helper.getSourceFolder(), "jack.invalidsource", "A.java", "package jack.invalidsource;\n"
        + "public class A { private voi m() {} } \n");
    AbstractTestTools.createFile(helper.getSourceFolder(), "jack.invalidsource", "B.java", "package jack.invalidsource;\n"
        + "public class B { private void m(in a) {}; \n private void n(int a) {re}; } \n");

    JackApiToolchain toolchain = AbstractTestTools.getCandidateToolchain(JackApiToolchain.class);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    toolchain.setOutputStream(out);
    toolchain.setErrorStream(errOut);

    try {
      compile(toolchain, helper);
      Assert.fail();
    }  catch (FrontendCompilationException ex) {
      // Failure is ok since source does not compile.
    } finally {
      Assert.assertEquals("", out.toString());
      String errorString = errOut.toString();
      Assert.assertTrue(errorString.contains("in cannot be resolved to a type"));
      Assert.assertTrue(errorString.contains(
          "Syntax error, insert \"VariableDeclarators\" to complete LocalVariableDeclaration"));
      Assert.assertTrue(
          errorString.contains("Syntax error, insert \";\" to complete BlockStatements"));
      Assert.assertTrue(errorString.contains("voi cannot be resolved to a type"));
    }
  }

  /**
   * Checks that compilation fails because of a source error, with also some warnings.
   */
  @Test
  public void testInvalidSource007() throws Exception {
    ErrorTestHelper helper = new ErrorTestHelper();

    AbstractTestTools.createFile(helper.getSourceFolder(), "jack.invalidsource", "A.java", "package jack.invalidsource;\n"
        + "public class A { private void m() {} } \n");
    AbstractTestTools.createFile(helper.getSourceFolder(), "jack.invalidsource", "B.java", "package jack.invalidsource;\n"
        + "public class B { private void m(in a) {}; \n private void n(int a) {}; } \n");

    JackApiToolchain toolchain = AbstractTestTools.getCandidateToolchain(JackApiToolchain.class);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    toolchain.setOutputStream(out);
    toolchain.setErrorStream(errOut);

    try {
      compile(toolchain, helper);
      Assert.fail();
    } catch (FrontendCompilationException ex) {
      // Failure is ok since source does not compile.
    } finally {
      Assert.assertEquals("", out.toString());
      Assert.assertTrue(errOut.toString().contains("in cannot be resolved to a type"));
      Assert.assertTrue(errOut.toString().contains(
          "The method n(int) from the type B is never used locally"));
      Assert.assertTrue(
          errOut.toString().contains("The method m() from the type A is never used locally"));
    }
  }

  /**
   * Checks that compilation succeeds but prints several warnings.
   */
  @Test
  public void testInvalidSource008() throws Exception {
    ErrorTestHelper helper = new ErrorTestHelper();

    AbstractTestTools.createFile(helper.getSourceFolder(), "jack.invalidsource", "A.java", "package jack.invalidsource;\n"
        + "public class A { private void m() {} } \n");
    AbstractTestTools.createFile(helper.getSourceFolder(), "jack.invalidsource", "B.java", "package jack.invalidsource;\n"
        + "public class B { private void m(int a) {}; \n private void n(int a) {}; } \n");

    JackApiToolchain toolchain = AbstractTestTools.getCandidateToolchain(JackApiToolchain.class);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    toolchain.setOutputStream(out);
    toolchain.setErrorStream(errOut);

    try {
      compile(toolchain, helper);
    }  finally {
      Assert.assertEquals("", out.toString());
      Assert.assertTrue(errOut.toString().contains(
          "The method m(int) from the type B is never used locally"));
      Assert.assertTrue(errOut.toString().contains(
          "The method n(int) from the type B is never used locally"));
      Assert.assertTrue(
          errOut.toString().contains("The method m() from the type A is never used locally"));
    }
  }

  /**
   * Checks that compilation fails because of an invalid type.
   * undefinedTypeId = typeRelatedCategory + 2
   */
  @Test
  public void testInvalidSource009() throws Exception {
    ErrorTestHelper helper = new ErrorTestHelper();

    AbstractTestTools.createFile(helper.getSourceFolder(), "jack.invalidsource", "A.java", "package jack.invalidsource;\n"
        + "public class A { private void m(in a) {}; } \n");

    JackApiToolchain toolchain = AbstractTestTools.getCandidateToolchain(JackApiToolchain.class);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    toolchain.setOutputStream(out);
    toolchain.setErrorStream(errOut);

    try {
      compile(toolchain, helper);
      Assert.fail();
    } catch (FrontendCompilationException ex) {
      // Failure is ok since source does not compile.
    } finally {
      Assert.assertEquals("", out.toString());
      Assert.assertTrue(errOut.toString().contains("in cannot be resolved to a type"));
    }
  }

  /**
   * Checks that compilation fails because of a parsing error.
   * parsingErrorInsertToCompleteId = syntaxCategory + internalCategory + 240
   */
  @Test
  public void testInvalidSource010() throws Exception {
    ErrorTestHelper helper = new ErrorTestHelper();

    AbstractTestTools.createFile(helper.getSourceFolder(), "jack.invalidsource", "A.java", "package jack.invalidsource;\n"
        + "public class A { private void n(int a) {re;} } \n");

    JackApiToolchain toolchain = AbstractTestTools.getCandidateToolchain(JackApiToolchain.class);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    toolchain.setOutputStream(out);
    toolchain.setErrorStream(errOut);

    try {
      compile(toolchain, helper);
      Assert.fail();
    } catch (FrontendCompilationException ex) {
      // Failure is ok since source does not compile.
    } finally {
      Assert.assertEquals("", out.toString());
      Assert.assertTrue(errOut.toString().contains(
          "Syntax error, insert \"VariableDeclarators\" to complete LocalVariableDeclaration"));
    }
  }

  private void compile(@Nonnull IToolchain toolchain, @Nonnull ErrorTestHelper helper)
      throws Exception {
    toolchain.srcToExe(
        AbstractTestTools.getClasspathAsString(toolchain.getDefaultBootClasspath()),
        helper.getOutputDexFolder(),
        /* zipFile = */ false, helper.getSourceFolder());
  }

}