Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2016 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 package libcore.java.util;
     18 
     19 import junit.framework.TestCase;
     20 
     21 import java.util.StringJoiner;
     22 
     23 public class StringJoinerTest extends TestCase {
     24 
     25     private static final String[] WEEKDAYS = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
     26     private static final String EXPECTED = "[Mon,Tue,Wed,Thu,Fri,Sat,Sun]";
     27 
     28     public void testConstructorNull() {
     29         try {
     30             new StringJoiner(null);
     31             fail();
     32         } catch (NullPointerException expected) {}
     33 
     34         try {
     35             new StringJoiner(null, "[", "]");
     36             fail();
     37         } catch (NullPointerException expected) {}
     38 
     39         try {
     40             new StringJoiner(",", null, null);
     41             fail();
     42         } catch (NullPointerException expected) {}
     43     }
     44 
     45     public void testAddString() {
     46         StringJoiner sj = new StringJoiner(",", "[", "]");
     47 
     48         for (int i = 0; i < WEEKDAYS.length; ++i) {
     49             sj.add(WEEKDAYS[i]);
     50             int expectedLength = 2 /* prefix and postfix */
     51                     + 3 * (i + 1) /* length of elements */
     52                     + i /* length of separators, one less than number of elements */;
     53             assertEquals(expectedLength, sj.length());
     54         }
     55         assertEquals(EXPECTED, sj.toString());
     56     }
     57 
     58     public void testAddStringBuilder() {
     59         StringJoiner sj = new StringJoiner(",", "[", "]");
     60 
     61         for (int i = 0; i < WEEKDAYS.length; ++i) {
     62             sj.add(new StringBuilder(WEEKDAYS[i]));
     63             int expectedLength = 2 /* prefix and postfix */
     64                     + 3 * (i + 1) /* length of elements */
     65                     + i /* length of separators, one less than number of elements */;
     66             assertEquals(expectedLength, sj.length());
     67         }
     68         assertEquals(EXPECTED, sj.toString());
     69     }
     70 
     71     public void testAddNone() {
     72         StringJoiner sj = new StringJoiner(",", "[", "]");
     73         assertEquals("[]", sj.toString());
     74         assertEquals(2, sj.length()); // len("[]")
     75     }
     76 
     77     public void testAddNull() {
     78         StringJoiner sj = new StringJoiner("|");
     79         sj.add(null).add(null);
     80         assertEquals("null|null", sj.toString());
     81     }
     82 
     83     public void testMerge() {
     84         StringJoiner sj1 = new StringJoiner(" ", "[", "]").add("Hello").add("world");
     85         StringJoiner sj2 = new StringJoiner("", "{", "}").add("Foo").add("Bar");
     86         StringJoiner sj3 = new StringJoiner("!", "<", ">").add("a").add("b");
     87         assertEquals("[Hello world FooBar a!b]", sj1.merge(sj2).merge(sj3).toString());
     88     }
     89 
     90     public void testMergeEmpty() {
     91         StringJoiner sj1 = new StringJoiner(",").add("");
     92         StringJoiner sj2 = new StringJoiner(".");
     93         assertEquals("", sj1.merge(sj2).toString());
     94     }
     95 
     96     public void testMergeSelf() {
     97         StringJoiner sj = new StringJoiner(" ", "|", "|").add("Hello").add("world");
     98         assertEquals("|Hello world Hello world|", sj.merge(sj).toString());
     99     }
    100 
    101     public void testMergeNull() {
    102         try {
    103             new StringJoiner(" ").merge(null);
    104             fail();
    105         } catch (NullPointerException expected) {}
    106     }
    107 
    108     public void testSetEmptyValue() {
    109         StringJoiner sj = new StringJoiner(",", "[", "]").setEmptyValue("EMPTY");
    110         assertEquals("EMPTY", sj.toString());
    111     }
    112 
    113     public void testSetEmptyValuePopulated() {
    114         StringJoiner sj = new StringJoiner(",", "[", "]").add("FOOBAR").setEmptyValue("");
    115         assertEquals("[FOOBAR]", sj.toString());
    116     }
    117 
    118     public void testSetEmptyValuePopulated2() {
    119         StringJoiner sj = new StringJoiner(",").add("").setEmptyValue("FOOBAR");
    120         assertEquals("", sj.toString());
    121     }
    122 
    123     public void testSetEmptyValueEmpty() {
    124         StringJoiner sj = new StringJoiner(",", "[", "]").setEmptyValue("");
    125         assertEquals("", sj.toString());
    126     }
    127 
    128     public void testSetEmptyValueNull() {
    129         try {
    130             StringJoiner sj = new StringJoiner(",", "[", "]").setEmptyValue(null);
    131             fail();
    132         } catch (NullPointerException expected) {}
    133     }
    134 }
    135