PageRenderTime 23ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/src/test/java/com/notnoop/apns/utils/StringTruncationTest.java

http://github.com/notnoop/java-apns
Java | 89 lines | 48 code | 10 blank | 31 comment | 0 complexity | 469c2b10c487d212c4d1dcbb1f3ea83f MD5 | raw file
 1/*
 2 *  Copyright 2009, Mahmood Ali.
 3 *  All rights reserved.
 4 *
 5 *  Redistribution and use in source and binary forms, with or without
 6 *  modification, are permitted provided that the following conditions are
 7 *  met:
 8 *
 9 *    * Redistributions of source code must retain the above copyright
10 *      notice, this list of conditions and the following disclaimer.
11 *    * Redistributions in binary form must reproduce the above
12 *      copyright notice, this list of conditions and the following disclaimer
13 *      in the documentation and/or other materials provided with the
14 *      distribution.
15 *    * Neither the name of Mahmood Ali. nor the names of its
16 *      contributors may be used to endorse or promote products derived from
17 *      this software without specific prior written permission.
18 *
19 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31package com.notnoop.apns.utils;
32
33import org.junit.Assert;
34
35import org.junit.experimental.theories.DataPoints;
36import org.junit.experimental.theories.Theories;
37import org.junit.experimental.theories.Theory;
38import org.junit.runner.RunWith;
39
40import com.notnoop.apns.internal.Utilities;
41
42// Test inspired by http://stackoverflow.com/questions/119328/how-do-i-truncate-a-java-string-to-fit-in-a-given-number-of-bytes-once-utf-8-enc
43@RunWith(Theories.class)
44public class StringTruncationTest {
45    @DataPoints public static Object[][] dataPoints = {
46            {"abcd", 0, 0},
47            {"abcd", 1, 1},
48            {"abcd", 2, 2},
49            {"abcd", 3, 3},
50            {"abcd", 4, 4},
51            {"abcd", 5, 4},
52
53            {"a\u0080b", 0, 0},
54            {"a\u0080b", 1, 1},
55            {"a\u0080b", 2, 1},
56            {"a\u0080b", 3, 3},
57            {"a\u0080b", 4, 4},
58            {"a\u0080b", 5, 4},
59
60            {"a\u0800b", 0, 0},
61            {"a\u0800b", 1, 1},
62            {"a\u0800b", 2, 1},
63            {"a\u0800b", 3, 1},
64            {"a\u0800b", 4, 4},
65            {"a\u0800b", 5, 5},
66            {"a\u0800b", 6, 5},
67    };
68
69    @DataPoints public static Object[][] surrogatePairs = {
70            {"\uD834\uDD1E", 0, 0},
71            {"\uD834\uDD1E", 1, 0},
72            {"\uD834\uDD1E", 2, 0},
73            {"\uD834\uDD1E", 3, 0},
74            {"\uD834\uDD1E", 4, 4},
75            {"\uD834\uDD1E", 5, 4}
76    };
77
78    @Theory
79    public void truncateUTF8Properly(Object[] p) {
80        String str = (String)p[0];
81        int maxBytes = (Integer)p[1];
82        int expectedBytes = (Integer)p[2];
83
84        String result = Utilities.truncateWhenUTF8(str, maxBytes);
85        byte[] utf8 = Utilities.toUTF8Bytes(result);
86
87        Assert.assertEquals(expectedBytes, utf8.length);
88    }
89}