rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ExceptionsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 10 May 2016 04:52:05 +0200
changeset 1962 9d46ae9d4a2e
parent 1787 ea12a3bb4b33
permissions -rw-r--r--
Don't obfuscate names of fields in objects - otherwise fields provided by two modules may clash
jaroslav@1313
     1
/**
jaroslav@1313
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1787
     3
 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@1313
     4
 *
jaroslav@1313
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@1313
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@1313
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@1313
     8
 *
jaroslav@1313
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@1313
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@1313
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@1313
    12
 * GNU General Public License for more details.
jaroslav@1313
    13
 *
jaroslav@1313
    14
 * You should have received a copy of the GNU General Public License
jaroslav@1313
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1313
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@1313
    17
 */
jaroslav@1313
    18
package org.apidesign.bck2brwsr.tck;
jaroslav@1313
    19
jaroslav@1313
    20
import java.io.ByteArrayOutputStream;
jaroslav@1313
    21
import java.io.PrintStream;
jaroslav@1313
    22
import java.io.PrintWriter;
jaroslav@1313
    23
import java.io.StringWriter;
jaroslav@1313
    24
import java.io.UnsupportedEncodingException;
jaroslav@1313
    25
import org.apidesign.bck2brwsr.vmtest.Compare;
jaroslav@1313
    26
import org.apidesign.bck2brwsr.vmtest.VMTest;
jaroslav@1313
    27
import org.testng.annotations.Factory;
jaroslav@1313
    28
jaroslav@1313
    29
/**
jaroslav@1313
    30
 *
jaroslav@1313
    31
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@1313
    32
 */
jaroslav@1313
    33
public class ExceptionsTest {
jaroslav@1313
    34
    @Compare public String firstLineIsTheSame() throws UnsupportedEncodingException {
jaroslav@1313
    35
        MyException ex = new MyException("Hello");
jaroslav@1313
    36
        ByteArrayOutputStream out = new ByteArrayOutputStream();
jaroslav@1313
    37
        PrintStream ps = new PrintStream(out);
jaroslav@1313
    38
        ex.printStackTrace(ps);
jaroslav@1313
    39
        ps.flush();
jaroslav@1313
    40
        
jaroslav@1313
    41
        String s = new String(out.toByteArray(), "UTF-8");
jaroslav@1313
    42
        int newLine = s.indexOf('\n');
jaroslav@1313
    43
        return s.substring(0, newLine);
jaroslav@1313
    44
    }
jaroslav@1313
    45
jaroslav@1313
    46
    @Compare public String firstLineIsTheSameWithWriter() throws UnsupportedEncodingException {
jaroslav@1313
    47
        MyException ex = new MyException("Hello");
jaroslav@1313
    48
        StringWriter sw = new StringWriter();
jaroslav@1313
    49
        PrintWriter pw = new PrintWriter(sw);
jaroslav@1313
    50
        ex.printStackTrace(pw);
jaroslav@1313
    51
        pw.flush();
jaroslav@1313
    52
        
jaroslav@1313
    53
        String s = sw.toString();
jaroslav@1313
    54
        int newLine = s.indexOf('\n');
jaroslav@1313
    55
        return s.substring(0, newLine);
jaroslav@1313
    56
    }
jaroslav@1930
    57
jaroslav@1930
    58
    @Compare
jaroslav@1930
    59
    public String classCastMessage() throws Exception {
jaroslav@1930
    60
        Object obj = 10;
jaroslav@1930
    61
        try {
jaroslav@1930
    62
            return (String) obj;
jaroslav@1930
    63
        } catch (ClassCastException ex) {
jaroslav@1930
    64
            assert ex.getMessage().contains("cannot be cast to") : "Contains the right text: " + ex.getMessage();
jaroslav@1930
    65
            throw ex;
jaroslav@1930
    66
        }
jaroslav@1930
    67
    }
jaroslav@1313
    68
    
jaroslav@1313
    69
    static class MyException extends Exception {
jaroslav@1313
    70
        public MyException(String message) {
jaroslav@1313
    71
            super(message);
jaroslav@1313
    72
        }
jaroslav@1313
    73
    }
jaroslav@1313
    74
    
jaroslav@1313
    75
    
jaroslav@1313
    76
    @Factory public static Object[] create() {
jaroslav@1313
    77
        return VMTest.create(ExceptionsTest.class);
jaroslav@1313
    78
    }
jaroslav@1313
    79
}