vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 21 Jan 2013 12:53:05 +0100
changeset 501 dc07c9001184
parent 456 f2f769bafeef
child 608 6e9328ca3462
permissions -rw-r--r--
String.equals(null) should not yield exception
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.tck;
    19 
    20 import java.net.MalformedURLException;
    21 import java.net.URL;
    22 import org.apidesign.bck2brwsr.vmtest.Compare;
    23 import org.apidesign.bck2brwsr.vmtest.VMTest;
    24 import org.testng.annotations.Factory;
    25 
    26 /**
    27  *
    28  * @author Jaroslav Tulach <jtulach@netbeans.org>
    29  */
    30 public class CompareStringsTest {
    31     @Compare public String firstChar() {
    32         return "" + ("Hello".toCharArray()[0]);
    33     }
    34     
    35     @Compare public String classCast() {
    36         Object o = firstChar();
    37         return String.class.cast(o);
    38     }
    39 
    40     @Compare public String classCastThrown() {
    41         Object o = null;
    42         return String.class.cast(o);
    43     }
    44     
    45     @Compare public boolean equalToNull() {
    46         return "Ahoj".equals(null);
    47     }
    48     
    49     @Compare public static Object compareURLs() throws MalformedURLException {
    50         return new URL("http://apidesign.org:8080/wiki/").toExternalForm().toString();
    51     }
    52     
    53     @Compare public String deleteLastTwoCharacters() {
    54         StringBuilder sb = new StringBuilder();
    55         sb.append("453.0");
    56         if (sb.toString().endsWith(".0")) {
    57             final int l = sb.length();
    58             sb.delete(l - 2, l);
    59         }
    60         return sb.toString().toString();
    61     }
    62     
    63     @Compare public String nameOfStringClass() throws Exception {
    64         return Class.forName("java.lang.String").getName();
    65     }
    66     @Compare public String nameOfArrayClass() throws Exception {
    67         return Class.forName("org.apidesign.bck2brwsr.tck.CompareHashTest").getName();
    68     }
    69     
    70     @Compare public String lowerHello() {
    71         return "HeLlO".toLowerCase();
    72     }
    73     
    74     @Compare public String lowerA() {
    75         return String.valueOf(Character.toLowerCase('A')).toString();
    76     }
    77     @Compare public String upperHello() {
    78         return "hello".toUpperCase();
    79     }
    80     
    81     @Compare public String upperA() {
    82         return String.valueOf(Character.toUpperCase('a')).toString();
    83     }
    84     
    85     @Compare public boolean matchRegExp() throws Exception {
    86         return "58038503".matches("\\d*");
    87     }
    88 
    89     @Compare public boolean doesNotMatchRegExp() throws Exception {
    90         return "58038503GH".matches("\\d*");
    91     }
    92 
    93     @Compare public boolean doesNotMatchRegExpFully() throws Exception {
    94         return "Hello".matches("Hell");
    95     }
    96     
    97     @Compare public String emptyCharArray() {
    98         char[] arr = new char[10];
    99         return new String(arr);
   100     }
   101     
   102     @Compare public String variousCharacterTests() throws Exception {
   103         StringBuilder sb = new StringBuilder();
   104         
   105         sb.append(Character.isUpperCase('a'));
   106         sb.append(Character.isUpperCase('A'));
   107         sb.append(Character.isLowerCase('a'));
   108         sb.append(Character.isLowerCase('A'));
   109         
   110         sb.append(Character.isLetter('A'));
   111         sb.append(Character.isLetterOrDigit('9'));
   112         sb.append(Character.isLetterOrDigit('A'));
   113         sb.append(Character.isLetter('0'));
   114         
   115         return sb.toString().toString();
   116     }
   117         
   118     @Compare
   119     public String nullFieldInitialized() {
   120         NullField nf = new NullField();
   121         return ("" + nf.name).toString();
   122     }
   123 
   124     @Factory
   125     public static Object[] create() {
   126         return VMTest.create(CompareStringsTest.class);
   127     }
   128 
   129     private static final class NullField {
   130 
   131         String name;
   132     }
   133 }