Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2007 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 
     17 import junit.framework.Assert;
     18 
     19 /**
     20  * more string tests
     21  */
     22 public class Main {
     23     public static void main(String args[]) {
     24         String test = "0123456789";
     25         String test1 = new String("0123456789");    // different object
     26         String test2 = new String("0123456780");    // different value
     27         String offset = new String("xxx0123456789yyy");
     28         String sub = offset.substring(3, 13);
     29         Object blah = new Object();
     30 
     31         Assert.assertTrue(test.equals(test));
     32         Assert.assertTrue(test.equals(test1));
     33         Assert.assertFalse(test.equals(test2));
     34 
     35         Assert.assertEquals(test.compareTo(test1), 0);
     36         Assert.assertTrue(test1.compareTo(test2) > 0);
     37         Assert.assertTrue(test2.compareTo(test1) < 0);
     38 
     39         Assert.assertEquals("".compareTo(""), 0);
     40         Assert.assertTrue(test.compareTo("") > 0);
     41         Assert.assertTrue("".compareTo(test) < 0);
     42 
     43         /* compare string with a nonzero offset, in left/right side */
     44         Assert.assertEquals(test.compareTo(sub), 0);
     45         Assert.assertEquals(sub.compareTo(test), 0);
     46         Assert.assertTrue(test.equals(sub));
     47         Assert.assertTrue(sub.equals(test));
     48         /* same base, one is a substring */
     49         Assert.assertFalse(offset.equals(sub));
     50         Assert.assertFalse(sub.equals(offset));
     51         /* wrong class */
     52         Assert.assertFalse(test.equals(blah));
     53 
     54         /* null ptr - throw */
     55         try {
     56             test.compareTo(null);
     57             Assert.fail("didn't get expected npe");
     58         } catch (NullPointerException npe) {
     59             System.out.println("Got expected npe");
     60         }
     61         /* null ptr - ok */
     62         Assert.assertFalse(test.equals(null));
     63 
     64         test = test.substring(1);
     65         Assert.assertTrue(test.equals("123456789"));
     66         Assert.assertFalse(test.equals(test1));
     67 
     68         test = test.substring(1);
     69         Assert.assertTrue(test.equals("23456789"));
     70 
     71         test = test.substring(1);
     72         Assert.assertTrue(test.equals("3456789"));
     73 
     74         test = test.substring(1);
     75         Assert.assertTrue(test.equals("456789"));
     76 
     77         test = test.substring(3,5);
     78         Assert.assertTrue(test.equals("78"));
     79 
     80         test = "this/is/a/path";
     81         String[] strings = test.split("/");
     82         Assert.assertEquals(4, strings.length);
     83 
     84         Assert.assertEquals("this is a path", test.replaceAll("/", " "));
     85         Assert.assertEquals("this is a path", test.replace("/", " "));
     86     }
     87 }
     88