rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 11 Oct 2013 09:58:03 +0200
changeset 1367 6193e735f4d1
parent 1336 804f6f982f4e
child 1382 7f4d603c46dd
permissions -rw-r--r--
If a class is not available during Ahead-Of-Time compilation it needs to be ready for dynamic loading
     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.io.UnsupportedEncodingException;
    21 import java.net.MalformedURLException;
    22 import java.net.URISyntaxException;
    23 import java.net.URL;
    24 import java.util.Locale;
    25 import org.apidesign.bck2brwsr.vmtest.Compare;
    26 import org.apidesign.bck2brwsr.vmtest.VMTest;
    27 import org.testng.annotations.Factory;
    28 
    29 /**
    30  *
    31  * @author Jaroslav Tulach <jtulach@netbeans.org>
    32  */
    33 public class CompareStringsTest {
    34     @Compare public String firstChar() {
    35         return "" + ("Hello".toCharArray()[0]);
    36     }
    37     
    38     @Compare public String classCast() {
    39         Object o = firstChar();
    40         return String.class.cast(o);
    41     }
    42 
    43     @Compare public String classCastThrown() {
    44         Object o = null;
    45         return String.class.cast(o);
    46     }
    47     
    48     @Compare public boolean equalToNull() {
    49         return "Ahoj".equals(null);
    50     }
    51     
    52     @Compare public int highByteLenght() {
    53         byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
    54         return new String(arr, 0).length();
    55     }
    56     
    57     @Compare public String highByte() {
    58         byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
    59         StringBuilder sb = new StringBuilder();
    60         sb.append("pref:");
    61         sb.append(new String(arr, 0));
    62         return sb.toString();
    63     }
    64     
    65     @Compare public static Object compareURLs() throws MalformedURLException {
    66         return new URL("http://apidesign.org:8080/wiki/").toExternalForm().toString();
    67     }
    68 
    69     @Compare public static Object compareURLsViaURIs() throws Exception {
    70         return new URL("http://apidesign.org:8080/wiki/").toURI().toString();
    71     }
    72     
    73     @Compare public String deleteLastTwoCharacters() {
    74         StringBuilder sb = new StringBuilder();
    75         sb.append("453.0");
    76         if (sb.toString().endsWith(".0")) {
    77             final int l = sb.length();
    78             sb.delete(l - 2, l);
    79         }
    80         return sb.toString().toString();
    81     }
    82     
    83     @Compare public String nameOfStringClass() throws Exception {
    84         return Class.forName("java.lang.String").getName();
    85     }
    86     @Compare public String nameOfArrayClass() throws Exception {
    87         return Class.forName("org.apidesign.bck2brwsr.tck.CompareHashTest").getName();
    88     }
    89     
    90     @Compare public String lowerHello() {
    91         return "HeLlO".toLowerCase();
    92     }
    93     
    94     @Compare public String lowerA() {
    95         return String.valueOf(Character.toLowerCase('A')).toString();
    96     }
    97     @Compare public String upperHello() {
    98         return "hello".toUpperCase();
    99     }
   100     
   101     @Compare public String upperA() {
   102         return String.valueOf(Character.toUpperCase('a')).toString();
   103     }
   104     
   105     @Compare public boolean matchRegExp() throws Exception {
   106         return "58038503".matches("\\d*");
   107     }
   108 
   109     @Compare public boolean doesNotMatchRegExp() throws Exception {
   110         return "58038503GH".matches("\\d*");
   111     }
   112 
   113     @Compare public boolean doesNotMatchRegExpFully() throws Exception {
   114         return "Hello".matches("Hell");
   115     }
   116     
   117     @Compare public String emptyCharArray() {
   118         char[] arr = new char[10];
   119         return new String(arr);
   120     }
   121     
   122     @Compare public String variousCharacterTests() throws Exception {
   123         StringBuilder sb = new StringBuilder();
   124         
   125         sb.append(Character.isUpperCase('a'));
   126         sb.append(Character.isUpperCase('A'));
   127         sb.append(Character.isLowerCase('a'));
   128         sb.append(Character.isLowerCase('A'));
   129         
   130         sb.append(Character.isLetter('A'));
   131         sb.append(Character.isLetterOrDigit('9'));
   132         sb.append(Character.isLetterOrDigit('A'));
   133         sb.append(Character.isLetter('0'));
   134         
   135         return sb.toString().toString();
   136     }
   137         
   138     @Compare
   139     public String nullFieldInitialized() {
   140         NullField nf = new NullField();
   141         return ("" + nf.name).toString();
   142     }
   143     @Compare
   144     public String toUTFString() throws UnsupportedEncodingException {
   145         byte[] arr = {
   146             (byte) -59, (byte) -67, (byte) 108, (byte) 117, (byte) -59, (byte) -91,
   147             (byte) 111, (byte) 117, (byte) -60, (byte) -115, (byte) 107, (byte) -61,
   148             (byte) -67, (byte) 32, (byte) 107, (byte) -59, (byte) -81, (byte) -59,
   149             (byte) -120
   150         };
   151         return new String(arr, "utf-8");
   152     }
   153 
   154     @Compare
   155     public int stringToBytesLenght() throws UnsupportedEncodingException {
   156         return "\u017dlu\u0165ou\u010dk\u00fd k\u016f\u0148".getBytes("utf8").length;
   157     }
   158     
   159     @Compare public String replaceSeq() {
   160         return "Hello World.".replace(".", "!");
   161     }
   162     @Compare public String replaceSeqAll() {
   163         return "Hello World! Hello World.".replace("World", "Jarda");
   164     }
   165     @Compare public String replaceSeqAA() {
   166         String res = "aaa".replace("aa", "b");
   167         assert res.equals("ba") : "Expecting ba: " + res;
   168         return res;
   169     }
   170     
   171     @Compare public String localeUS() {
   172         return Locale.US.toString();
   173     }
   174     
   175     @Compare public String localeFrench() {
   176         return Locale.FRENCH.toString();
   177     }
   178     
   179     
   180     @Compare public String formatSimple() {
   181         return String.format((Locale)null, "Hello %s!", "World");
   182     }
   183 
   184     @Compare public String replaceWithItself() {
   185         return "org.apidesign.bck2brwsr.core.JavaScriptBody".replace(".", "\\.");
   186     }
   187     
   188     @Factory
   189     public static Object[] create() {
   190         return VMTest.create(CompareStringsTest.class);
   191     }
   192 
   193     private static final class NullField {
   194 
   195         String name;
   196     }
   197 }