aboutsummaryrefslogtreecommitdiffstats
path: root/test/java/org/apache/fop/text/linebreak/LineBreakStatusTest.java
blob: f832d089676ad947568220bc2d1a3a4bfedf93b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop.text.linebreak;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * JUnit test case for the LineBreakStatus class
 */
public class LineBreakStatusTest extends TestCase {

    /*
     * These symbols are used to indicate the break action returned
     * by the paragraph breaking. Their meaning is as per Unicode
     * <a href="http://unicode.org/reports/tr14/#PairBasedImplementation">technical 
     * report #14</a>.
     */
    private static final String BREAK_ACTION = "_%#@^!";

    /**
     * Creates the test with the given name.
     * @param testName The name for this test.
     */
    public LineBreakStatusTest(String testName) {
        super(testName);
    }

    /**
     * Returns an TestSuite constructed from this class.
     * @return the TestSuite
     * @see junit.framework.TestSuite#TestSuite(class)
     */
    public static Test suite() {
        TestSuite suite = new TestSuite(LineBreakStatusTest.class);
        
        return suite;
    }

    /**
     * Test of reset method, of class org.apache.commons.text.linebreak.LineBreakStatus.
     */
    public void testReset() {
        System.out.println("testReset");
        // TODO
    }

    /**
     * Test of nextChar method, of class org.apache.commons.text.linebreak.LineBreakStatus.
     * Runs tests for most of the Line Breaking Properties defined in the Unicode standard.
     */
    public void testNextChar() {
        System.out.println("testNextChar");
        
        // AL -- Ordinary Alphabetic and Symbol Characters (XP)
        assertTrue(testBreak(
            "Nobreak", 
            "^^^^^^^"
            ));
        
        // BA -- Break Opportunity After (A)
        assertTrue(testBreak(
            "Thin Space" + "\u2009" + "break", 
            "^^^^^%^^^^" + "^"      + "_^^^^"
            ));

        assertTrue(testBreak(
            "Shy" + "\u00AD" + "break", 
            "^^^" + "^"      + "_^^^^"
            ));


        // BB -- Break opportunites before characters (B)
        assertTrue(testBreak(
            "Acute Accent" + "\u00B4" + "break", 
            "^^^^^^%^^^^^" + "_"      + "^^^^^"
            ));

        // B2 -- Break Opportunity Before and After (B/A/XP)
        assertTrue(testBreak(
            "Em Dash" + "\u2014" + "break", 
            "^^^%^^^" + "_"      + "_^^^^"
            ));

        assertTrue(testBreak(
            "Em Dash Dash" + "\u2014" + "\u2014" + "break", 
            "^^^%^^^^%^^^" + "_"      + "^"      + "_^^^^"
            ));

        // BK Mandatory Break (A) -- normative
        assertTrue(testBreak(
            "Form Feed" + "\u000C" + "break", 
            "^^^^^%^^^" + "^"      + "!^^^^"
            ));

        assertTrue(testBreak(
            "Line Separator" + "\u2028" + "break", 
            "^^^^^%^^^^^^^^" + "^"      + "!^^^^"
            ));

        assertTrue(testBreak(
            "Paragraph Separator" + "\u2029" + "break", 
            "^^^^^^^^^^%^^^^^^^^" + "^"      + "!^^^^"
            ));

        // CB Contingent Break Opportunity (B/A) -- normative
        // TODO Don't know quite what to do here
        
        // CL -- Closing Punctuation (XB)
        assertTrue(testBreak(
            "Right Parenthesis ) break", 
            "^^^^^^%^^^^^^^^^^^^^%^^^^"
            ));
        
        // CM -- Attached Characters and Combining Marks (XB) -- normative
        assertTrue(testBreak(
            "Grave Accent" + "\u0300" + " break", 
            "^^^^^^%^^^^^" + "^"      + "^%^^^^"
            ));
        
        // CR -- Carriage Return (A) -- normative
        assertTrue(testBreak(
            "CR" + "\r" + "break", 
            "^^" + "^"  + "!^^^^"
            ));
        
        assertTrue(testBreak(
            "CRLF" + "\r\n" + "break", 
            "^^^^" + "^^"   + "!^^^^"
            ));
        
        // EX -- Exclamation / interrogation (XB)
        assertTrue(testBreak(
            "EX CL ! ) break", 
            "^^^%^^^^^^%^^^^"
            ));
        
        assertTrue(testBreak(
            "EX Wave Dash ! " + "\u301C" + " break", 
            "^^^%^^^^%^^^^^^" + "%"      + "^_^^^^"
            ));
        
        // GL -- Non-breaking ("Glue") (XB/XA) -- normative
        assertTrue(testBreak(
            "No" + "\u00a0" + "break", 
            "^^" + "^"      + "^^^^^"
            ));
        
        assertTrue(testBreak(
            "Non" + "\u2011" + " Hyphen", 
            "^^^" + "^"      + "^%^^^^^"
            ));
        
        // H2 -- Hangul LVT Syllable (B/A)
        // TODO

        // H3 -- Hangul LVT Syllable (B/A)
        // TODO

        // HY -- Hyphen Minus
        assertTrue(testBreak(
            "Normal-Hyphen", 
            "^^^^^^^_^^^^^"
            ));

        assertTrue(testBreak(
            "Normal - Hyphen", 
            "^^^^^^^%^_^^^^^"
            ));

        assertTrue(testBreak(
            "123-456", 
            "^^^^^^^"
            ));

        assertTrue(testBreak(
            "123 - 456", 
            "^^^^%^%^^"
            ));

        // ID -- Ideographic (B/A)
        assertTrue(testBreak(
            "\u4E00" + "\u3000" + "\u4E02", 
            "^"      + "_"      + "_"
            ));

        // IN -- Inseperable characters (XP)
        assertTrue(testBreak(
            "IN " + "\u2024" + "\u2025" + "\u2026", 
            "^^^" + "%"      + "^"      + "^"
            ));

        // IS -- Numeric Separator (Infix) (XB)
        assertTrue(testBreak(
            "123,456.00 12:59", 
            "^^^^^^^^^^^%^^^^"
            ));

        // JL -- Hangul L Jamo (B)
        // TODO

        // JT -- Hangul T Jamo (A)
        // TODO

        // JV -- Hangul V Jamo (XA/XB)
        // TODO

        // LF -- Line Feed (A) -- normative
        assertTrue(testBreak(
            "Simple" + "\n" + "\n" + "break", 
            "^^^^^^" + "^" + "!"  + "!^^^^"
            ));

        // NL -- Next Line (A) -- normative
        assertTrue(testBreak(
            "NL" + "\u0085" + "break", 
            "^^" + "^"      + "!^^^^"
            ));

        // NS -- Non-starters (XB)
        // TODO

        // NU -- Numeric (XP)
        // Tested as part of IS

        // OP -- Opening Punctuation (XA)
        assertTrue(testBreak(
            "[ Bracket ( Parenthesis", 
            "^^^^^^^^^^%^^^^^^^^^^^^"
            ));
        
        // PO -- Postfix (Numeric) (XB)
        assertTrue(testBreak(
            "(12.00)%", 
            "^^^^^^^^"
            ));

        // PR -- Prefix (Numeric) (XA)
        assertTrue(testBreak(
            "$1000.00", 
            "^^^^^^^^"
            ));

        // QU -- Ambiguous Quotation (XB/XA)
        assertTrue(testBreak(
            "'In Quotes'", 
            "^^^^%^^^^^^"
            ));

        assertTrue(testBreak(
            "' (In Quotes) '", 
            "^^^^^^%^^^^^^^%"
            ));

        // SA -- Complex-context Dependent Characters (South East Asian) (P)
        // TODO
        
        // SP -- Space (A) -- normative
        assertTrue(testBreak(
            "Simple break", 
            "^^^^^^^%^^^^"
            ));

        assertTrue(testBreak(
            "Simple    break2", 
            "^^^^^^^^^^%^^^^^"
            ));

        // SY -- Symbols Allowing Break After (A)
        assertTrue(testBreak(
            "http://xmlgraphics.apache.org/fop", 
            "^^^^^^^_^^^^^^^^^^^^^^^^^^^^^^_^^"
            ));

        assertTrue(testBreak(
            "1/2 31/10/2005", 
            "^^^^%^^^^^^^^^"
            ));

        // WJ -- Word Joiner (XA/XB) -- (normative)
        assertTrue(testBreak(
            "http://" + "\u2060" + "xmlgraphics.apache.org/" + "\uFEFF" + "fop", 
            "^^^^^^^" + "^"      + "^^^^^^^^^^^^^^^^^^^^^^^" + "^"      + "^^^"
            ));

        assertTrue(testBreak(
            "Simple " + "\u2060" + "break", 
            "^^^^^^^" + "^"      + "^^^^^"
            ));

        assertTrue(testBreak(
            "Simple" + "\u200B" + "\u2060" + "break", 
            "^^^^^^" + "^"      + "_"      + "^^^^^"
            ));

        // XX -- Unknown (XP)
        // TODO
        
        // ZW -- Zero Width Space (A) -- (normative)
        assertTrue(testBreak(
            "Simple" + "\u200B" + "break", 
            "^^^^^^" + "^"      + "_^^^^"
            ));

    }

    /**
     * Tests the paragraph break status (break actions) returned from calling
     * LineBreakStatus.nextChar() on each character of paragraph against
     * the expected break actions. There must be a positional match between
     * the characters in paragraph and characters in breakAction.
     * @param paragraph The text to be analysed for line breaks
     * @param breakActions The symbolic representation of the break actions
     * expected to be returned.
     */
    private boolean testBreak(String paragraph, String breakActions) {
        boolean result = true;
        int length = paragraph.length();
        LineBreakStatus lbs = new LineBreakStatus();
        for (int i = 0; i < length; i++) {
            byte breakAction = lbs.nextChar(paragraph.charAt(i));
            if (BREAK_ACTION.charAt(breakAction) != breakActions.charAt(i)) {
                System.err.println(paragraph);
                System.err.println(breakActions);
                System.err.println("pos = " + i 
                    + " expected '" + breakActions.charAt(i) 
                    + "' got '" + BREAK_ACTION.charAt(breakAction) + "'");
                result = false;
            }
        }
        return result;
    }
}