Yohann Roussel | 5cd1569 | 2016-05-19 12:07:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.framework.multidexlegacycorrupteddex; |
| 17 | |
| 18 | import android.test.ActivityInstrumentationTestCase2; |
| 19 | |
| 20 | import java.io.ByteArrayOutputStream; |
| 21 | import java.io.PrintStream; |
| 22 | |
| 23 | /** |
| 24 | * Run the tests with: <code>adb shell am instrument -w |
| 25 | com.android.framework.multidexlegacycorrupteddex/android.test.InstrumentationTestRunner |
| 26 | </code> |
| 27 | */ |
| 28 | public class CorruptedDexTest extends ActivityInstrumentationTestCase2<MainActivity> |
| 29 | { |
| 30 | |
| 31 | public CorruptedDexTest() { |
| 32 | super(MainActivity.class); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Tests that when a {@link ClassNotFoundException} is thrown, the message also contains |
| 37 | * something about the suppressed IOException. |
| 38 | */ |
| 39 | public void testSupressedExceptions() |
| 40 | { |
| 41 | try { |
| 42 | Class.forName("notapackage.NotAClass"); |
| 43 | throw new AssertionError(); |
| 44 | } catch (ClassNotFoundException e) { |
| 45 | // expected |
| 46 | |
| 47 | // This the check we should do but API is not yet available in 19 |
| 48 | // Throwable[] suppressed = e.getSuppressed(); |
| 49 | // assertTrue(suppressed.length > 0); |
| 50 | // boolean ioFound = false; |
| 51 | // for (Throwable throwable : suppressed) { |
| 52 | // if (throwable instanceof IOException) { |
| 53 | // ioFound = true; |
| 54 | // break; |
| 55 | // } |
| 56 | // } |
| 57 | // assertTrue(ioFound); |
| 58 | |
| 59 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 60 | PrintStream ps = new PrintStream(baos); |
| 61 | e.printStackTrace(ps); |
| 62 | ps.close(); |
| 63 | assertTrue("Exception message should mention IOException but is not: " |
| 64 | + baos.toString(), |
| 65 | baos.toString().contains("IOException")); |
| 66 | } |
| 67 | } |
| 68 | } |