rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 31 Oct 2013 15:01:35 +0100
changeset 1402 e896bc687984
parent 1382 7f4d603c46dd
permissions -rw-r--r--
Implementing String's intern()
     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 boolean internIsSame() {
    53         return new String("Ahoj").intern() == another();
    54     }
    55     
    56     private static String another() {
    57         return new String("Ahoj").intern();
    58     }
    59     
    60     @Compare public int highByteLenght() {
    61         byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
    62         return new String(arr, 0).length();
    63     }
    64     
    65     @Compare public String highByte() {
    66         byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
    67         StringBuilder sb = new StringBuilder();
    68         sb.append("pref:");
    69         sb.append(new String(arr, 0));
    70         return sb.toString();
    71     }
    72     
    73     @Compare public static Object compareURLs() throws MalformedURLException {
    74         return new URL("http://apidesign.org:8080/wiki/").toExternalForm().toString();
    75     }
    76 
    77     @Compare public static Object compareURLsViaURIs() throws Exception {
    78         return new URL("http://apidesign.org:8080/wiki/").toURI().toString();
    79     }
    80     
    81     @Compare public String deleteLastTwoCharacters() {
    82         StringBuilder sb = new StringBuilder();
    83         sb.append("453.0");
    84         if (sb.toString().endsWith(".0")) {
    85             final int l = sb.length();
    86             sb.delete(l - 2, l);
    87         }
    88         return sb.toString().toString();
    89     }
    90     
    91     @Compare public String nameOfStringClass() throws Exception {
    92         return Class.forName("java.lang.String").getName();
    93     }
    94     @Compare public String nameOfArrayClass() throws Exception {
    95         return Class.forName("org.apidesign.bck2brwsr.tck.CompareHashTest").getName();
    96     }
    97     
    98     @Compare public String lowerHello() {
    99         return "HeLlO".toLowerCase();
   100     }
   101     
   102     @Compare public String lowerA() {
   103         return String.valueOf(Character.toLowerCase('A')).toString();
   104     }
   105     @Compare public String upperHello() {
   106         return "hello".toUpperCase();
   107     }
   108     
   109     @Compare public String upperA() {
   110         return String.valueOf(Character.toUpperCase('a')).toString();
   111     }
   112     
   113     @Compare public boolean matchRegExp() throws Exception {
   114         return "58038503".matches("\\d*");
   115     }
   116 
   117     @Compare public boolean doesNotMatchRegExp() throws Exception {
   118         return "58038503GH".matches("\\d*");
   119     }
   120 
   121     @Compare public boolean doesNotMatchRegExpFully() throws Exception {
   122         return "Hello".matches("Hell");
   123     }
   124     
   125     @Compare public String emptyCharArray() {
   126         char[] arr = new char[10];
   127         return new String(arr);
   128     }
   129     
   130     @Compare public String variousCharacterTests() throws Exception {
   131         StringBuilder sb = new StringBuilder();
   132         
   133         sb.append(Character.isUpperCase('a'));
   134         sb.append(Character.isUpperCase('A'));
   135         sb.append(Character.isLowerCase('a'));
   136         sb.append(Character.isLowerCase('A'));
   137         
   138         sb.append(Character.isLetter('A'));
   139         sb.append(Character.isLetterOrDigit('9'));
   140         sb.append(Character.isLetterOrDigit('A'));
   141         sb.append(Character.isLetter('0'));
   142         
   143         return sb.toString().toString();
   144     }
   145         
   146     @Compare
   147     public String nullFieldInitialized() {
   148         NullField nf = new NullField();
   149         return ("" + nf.name).toString();
   150     }
   151     @Compare
   152     public String toUTFString() throws UnsupportedEncodingException {
   153         byte[] arr = {
   154             (byte) -59, (byte) -67, (byte) 108, (byte) 117, (byte) -59, (byte) -91,
   155             (byte) 111, (byte) 117, (byte) -60, (byte) -115, (byte) 107, (byte) -61,
   156             (byte) -67, (byte) 32, (byte) 107, (byte) -59, (byte) -81, (byte) -59,
   157             (byte) -120
   158         };
   159         return new String(arr, "utf-8");
   160     }
   161 
   162     @Compare
   163     public int stringToBytesLenght() throws UnsupportedEncodingException {
   164         return "\u017dlu\u0165ou\u010dk\u00fd k\u016f\u0148".getBytes("utf8").length;
   165     }
   166     
   167     @Compare public String replaceSeq() {
   168         return "Hello World.".replace(".", "!");
   169     }
   170     @Compare public String replaceSeqAll() {
   171         return "Hello World! Hello World.".replace("World", "Jarda");
   172     }
   173     @Compare public String replaceSeqAA() {
   174         String res = "aaa".replace("aa", "b");
   175         assert res.equals("ba") : "Expecting ba: " + res;
   176         return res;
   177     }
   178     
   179     @Compare public String localeUS() {
   180         return Locale.US.toString();
   181     }
   182     
   183     @Compare public String localeFrench() {
   184         return Locale.FRENCH.toString();
   185     }
   186     
   187     
   188     @Compare public String formatSimple() {
   189         return String.format((Locale)null, "Hello %s!", "World");
   190     }
   191 
   192     @Compare public String replaceWithItself() {
   193         return "org.apidesign.bck2brwsr.core.JavaScriptBody".replace(".", "\\.");
   194     }
   195     
   196     @Compare public boolean matchWithComplicatedRegExp() {
   197         return "Activates this model instance.".matches("(?sm).*^\\s*@deprecated( |$).*");
   198     }
   199     
   200     @Factory
   201     public static Object[] create() {
   202         return VMTest.create(CompareStringsTest.class);
   203     }
   204 
   205     private static final class NullField {
   206 
   207         String name;
   208     }
   209 }