diff options
author | mikaelpeltier <mikaelpeltier@google.com> | 2014-05-23 13:58:28 +0200 |
---|---|---|
committer | mikaelpeltier <mikaelpeltier@google.com> | 2014-05-23 16:22:51 +0200 |
commit | b4cf5c8110ad0d81be3aad1c8f6d3457291e0cc9 (patch) | |
tree | c3d031c9fe3e46a365bd4d3072629f2906eaf98c | |
parent | e21f2262a582dc762176d2ec7f649f6b2c7ee0b5 (diff) | |
download | toolchain_jack-b4cf5c8110ad0d81be3aad1c8f6d3457291e0cc9.zip toolchain_jack-b4cf5c8110ad0d81be3aad1c8f6d3457291e0cc9.tar.gz toolchain_jack-b4cf5c8110ad0d81be3aad1c8f6d3457291e0cc9.tar.bz2 |
Add tests on command line errors
Change-Id: I839fe804a3cc7bf5bef0a519af5682e4d5221469
6 files changed, 162 insertions, 16 deletions
diff --git a/jack/src/com/android/jack/Options.java b/jack/src/com/android/jack/Options.java index 49b8877..4ac8645 100644 --- a/jack/src/com/android/jack/Options.java +++ b/jack/src/com/android/jack/Options.java @@ -613,10 +613,9 @@ public class Options { if (!compiler.proceed) { throw new NothingToDoException(); } + compiler.getLibraryAccess().cleanup(); } catch (IllegalArgumentException e) { throw new IllegalOptionsException(e.getMessage(), e); - } finally { - compiler.getLibraryAccess().cleanup(); } } diff --git a/jack/tests/com/android/jack/errorhandling/CommandLineErrorTest.java b/jack/tests/com/android/jack/errorhandling/CommandLineErrorTest.java new file mode 100644 index 0000000..aa000f9 --- /dev/null +++ b/jack/tests/com/android/jack/errorhandling/CommandLineErrorTest.java @@ -0,0 +1,122 @@ +/* + * 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.errorhandling; + +import com.android.jack.IllegalOptionsException; +import com.android.jack.Main; +import com.android.jack.NothingToDoException; +import com.android.jack.Options; +import com.android.jack.category.KnownBugs; +import com.android.jack.frontend.FrontendCompilationException; + +import junit.framework.Assert; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * JUnit test checking Jack behavior on exceptions. + */ +public class CommandLineErrorTest { + + @BeforeClass + public static void setUpClass() { + Main.class.getClassLoader().setDefaultAssertionStatus(true); + } + + /** + * Checks that compilation fails correctly when an unsupported options is passed to ecj. + */ + @Test + public void testCommandLineError001() throws Exception { + TestingEnvironment ite = new TestingEnvironment(); + + Options options = new Options(); + List<String> ecjArgs = new ArrayList<String>(); + ecjArgs.add("-unsupported"); + ecjArgs.add(ite.getTestingFolder().getAbsolutePath()); + options.setEcjArguments(ecjArgs); + + try { + ite.startErrRedirection(); + ite.compile(options); + Assert.fail(); + } catch (IllegalOptionsException e) { + // Failure is ok since a bad options is passed to ecj. + } finally { + Assert.assertEquals("", ite.endErrRedirection()); + } + } + + /** + * Checks that compilation fails correctly when no source files are passed to ecj. + */ + @Test + public void testCommandLineError002() throws Exception { + TestingEnvironment ite = new TestingEnvironment(); + + Options options = new Options(); + options.setEcjArguments(new ArrayList<String>()); + + try { + ite.startErrRedirection(); + ite.startOutRedirection(); + ite.compile(options); + Assert.fail(); + } catch (NothingToDoException e) { + // Failure is ok since there is no source files. + } finally { + Assert.assertEquals("", ite.endErrRedirection()); + Assert.assertTrue(ite.endOutRedirection().contains("Usage:")); + } + } + + /** + * Checks that compilation fails correctly when java.lang.Object does not exist on classpath. + */ + @Test + @Category(KnownBugs.class) + public void testCommandLineError003() throws Exception { + TestingEnvironment ite = new TestingEnvironment(); + + File sourceFile = ite.addFile(ite.getSourceFolder(),"jack.incremental", "A.java", + "package jack.incremental; \n"+ + "public class A {} \n"); + + Options options = new Options(); + List<String> ecjArgs = new ArrayList<String>(); + ecjArgs.add(sourceFile.getAbsolutePath()); + options.setEcjArguments(ecjArgs); + + try { + ite.startErrRedirection(); + ite.startOutRedirection(); + ite.compile(options); + Assert.fail(); + } catch (FrontendCompilationException e) { + // Failure is ok, since java.lang.Object does not exists. + } finally { + Assert.assertEquals("", ite.endOutRedirection()); + Assert.assertTrue(!ite.endErrRedirection().contains("file")); + } + } +} diff --git a/jack/tests/com/android/jack/errorhandling/ErrorHandlingAllTests.java b/jack/tests/com/android/jack/errorhandling/ErrorHandlingAllTests.java index f5cc5b7..0a7df8e 100644 --- a/jack/tests/com/android/jack/errorhandling/ErrorHandlingAllTests.java +++ b/jack/tests/com/android/jack/errorhandling/ErrorHandlingAllTests.java @@ -20,6 +20,7 @@ import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) -@SuiteClasses(value = {JackFormatErrorTest.class, FileAccessErrorTest.class}) +@SuiteClasses( + value = {JackFormatErrorTest.class, FileAccessErrorTest.class, CommandLineErrorTest.class}) public class ErrorHandlingAllTests { }
\ No newline at end of file diff --git a/jack/tests/com/android/jack/errorhandling/FileAccessErrorTest.java b/jack/tests/com/android/jack/errorhandling/FileAccessErrorTest.java index 12cdf70..dd3f205 100644 --- a/jack/tests/com/android/jack/errorhandling/FileAccessErrorTest.java +++ b/jack/tests/com/android/jack/errorhandling/FileAccessErrorTest.java @@ -121,7 +121,7 @@ public class FileAccessErrorTest { ite.compile(options); Assert.fail(); } finally { - Assert.assertEquals("", ite.endErrorRedirection()); + Assert.assertEquals("", ite.endErrRedirection()); if (!jackOutputFile.setReadable(true)) { Assert.fail("Fails to change file permissions of " + jackOutputFile.getAbsolutePath()); } @@ -204,7 +204,7 @@ public class FileAccessErrorTest { } catch (JackIOException e) { // Failure is ok since jack file is not readable } finally { - Assert.assertEquals("", ite.endErrorRedirection()); + Assert.assertEquals("", ite.endErrRedirection()); for (File jackFile : ite.getJackFiles(ite.getJackFolder())) { if (!jackFile.setReadable(true)) { Assert.fail("Fails to change file permissions of " + jackFile.getAbsolutePath()); diff --git a/jack/tests/com/android/jack/errorhandling/JackFormatErrorTest.java b/jack/tests/com/android/jack/errorhandling/JackFormatErrorTest.java index bd55edc..8be9944 100644 --- a/jack/tests/com/android/jack/errorhandling/JackFormatErrorTest.java +++ b/jack/tests/com/android/jack/errorhandling/JackFormatErrorTest.java @@ -71,7 +71,7 @@ public class JackFormatErrorTest { } catch (JayceFormatException e) { // Failure is ok since jack file is corrupted. } finally { - Assert.assertEquals("", ite.endErrorRedirection()); + Assert.assertEquals("", ite.endErrRedirection()); } } @@ -103,7 +103,7 @@ public class JackFormatErrorTest { } catch (JayceFormatException e) { // Failure is ok since jack file header is corrupted. } finally { - Assert.assertEquals("", ite.endErrorRedirection()); + Assert.assertEquals("", ite.endErrRedirection()); } } @@ -135,7 +135,7 @@ public class JackFormatErrorTest { } catch (JayceVersionException e) { // Failure is ok since jack file header is corrupted. } finally { - Assert.assertEquals("", ite.endErrorRedirection()); + Assert.assertEquals("", ite.endErrRedirection()); } } } diff --git a/jack/tests/com/android/jack/errorhandling/TestingEnvironment.java b/jack/tests/com/android/jack/errorhandling/TestingEnvironment.java index 25464d2..5c7cc14 100644 --- a/jack/tests/com/android/jack/errorhandling/TestingEnvironment.java +++ b/jack/tests/com/android/jack/errorhandling/TestingEnvironment.java @@ -31,6 +31,7 @@ import com.android.sched.util.config.ConfigurationException; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileDescriptor; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; @@ -43,7 +44,13 @@ import javax.annotation.Nonnull; public class TestingEnvironment { @CheckForNull - ByteArrayOutputStream baos = null; + ByteArrayOutputStream baosOut = null; + + @CheckForNull + PrintStream outRedirectStream = null; + + @CheckForNull + ByteArrayOutputStream baosErr = null; @CheckForNull PrintStream errRedirectStream = null; @@ -131,19 +138,36 @@ public class TestingEnvironment { } } + public void startErrRedirection() { + baosErr = new ByteArrayOutputStream(); + errRedirectStream = new PrintStream(baosErr); + System.setErr(errRedirectStream); + } + @Nonnull - public String endErrorRedirection() { - assert baos != null; - String err = baos.toString(); + public String endErrRedirection() { + assert baosErr != null; + String err = baosErr.toString(); assert errRedirectStream != null; errRedirectStream.close(); + System.setErr(new PrintStream(new FileOutputStream(FileDescriptor.err))); return err; } - public void startErrRedirection() { - baos = new ByteArrayOutputStream(); - errRedirectStream = new PrintStream(baos); - System.setErr(errRedirectStream); + public void startOutRedirection() { + baosOut = new ByteArrayOutputStream(); + outRedirectStream = new PrintStream(baosOut); + System.setOut(outRedirectStream); + } + + @Nonnull + public String endOutRedirection() { + assert baosOut != null; + String out = baosOut.toString(); + assert outRedirectStream != null; + outRedirectStream.close(); + System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); + return out; } @Nonnull |