StringWrapperTest.java revision 31d87776c459972f311a3527694e0d630d92a84b
1/*
2 * Copyright 2013, Google Inc.
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 Google Inc. 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 */
31
32package org.jf.util;
33
34import org.junit.Assert;
35import org.junit.Test;
36
37public class StringWrapperTest {
38    @Test
39    public void testWrapString() {
40        validateResult(
41                new String[]{"abc", "abcdef", "abcdef"},
42                StringWrapper.wrapString("abc\nabcdefabcdef", 6, null));
43
44        validateResult(
45                new String[]{"abc"},
46                StringWrapper.wrapString("abc", 6, new String[3]));
47
48        validateResult(
49                new String[]{"abc"},
50                StringWrapper.wrapString("abc", 6, new String[0]));
51
52        validateResult(
53                new String[]{"abc"},
54                StringWrapper.wrapString("abc", 6, new String[1]));
55
56        validateResult(
57                new String[]{""},
58                StringWrapper.wrapString("", 6, new String[3]));
59
60        validateResult(
61                new String[]{"abcdef"},
62                StringWrapper.wrapString("abcdef", 6, new String[3]));
63
64        validateResult(
65                new String[]{"abcdef", "abcdef"},
66                StringWrapper.wrapString("abcdef\nabcdef", 6, new String[3]));
67
68        validateResult(
69                new String[]{"abc", "", "def"},
70                StringWrapper.wrapString("abc\n\ndef", 6, new String[3]));
71
72        validateResult(
73                new String[]{"", "abcdef"},
74                StringWrapper.wrapString("\nabcdef", 6, new String[3]));
75
76        validateResult(
77                new String[]{"", "", "abcdef"},
78                StringWrapper.wrapString("\n\nabcdef", 6, new String[3]));
79
80        validateResult(
81                new String[]{"", "", "abcdef"},
82                StringWrapper.wrapString("\n\nabcdef", 6, new String[4]));
83
84        validateResult(
85                new String[]{"", "", "abcdef", ""},
86                StringWrapper.wrapString("\n\nabcdef\n\n", 6, new String[4]));
87
88        validateResult(
89                new String[]{"", "", "abcdef", "a", ""},
90                StringWrapper.wrapString("\n\nabcdefa\n\n", 6, new String[4]));
91
92        validateResult(
93                new String[]{"", "", "abcdef", "a", ""},
94                StringWrapper.wrapString("\n\nabcdefa\n\n", 6, new String[0]));
95
96        validateResult(
97                new String[]{"", "", "abcdef", "a", ""},
98                StringWrapper.wrapString("\n\nabcdefa\n\n", 6, new String[5]));
99
100        validateResult(
101                new String[]{"", "", "a", "b", "c", "d", "e", "f", "a", ""},
102                StringWrapper.wrapString("\n\nabcdefa\n\n", 1, new String[5]));
103    }
104
105    public static void validateResult(String[] expected, String[] actual) {
106        Assert.assertTrue(actual.length >= expected.length);
107
108        int i;
109        for (i=0; i<actual.length; i++) {
110            if (actual[i] == null) {
111                Assert.assertTrue(i == expected.length);
112                return;
113            }
114            Assert.assertTrue(i < expected.length);
115            Assert.assertEquals(expected[i], actual[i]);
116        }
117    }
118}
119