Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: de1bbbc3abd1a3b29561189145c24757ce5b50b1 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2004, 2008 John Krasnay and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     John Krasnay - initial API and implementation
 *     Igor Jacy Lino Campista - Java 5 warnings fixed (bug 311325)
 *******************************************************************************/
package org.eclipse.wst.xml.vex.core.internal.dom;

import org.eclipse.wst.xml.vex.core.internal.dom.TextWrapper;

import junit.framework.TestCase;

/**
 * Test the TextWrapper class.
 */
public class TextWrapperTest extends TestCase {

	public void testWrap() {
		String[] results;
		String[] inputs;
		TextWrapper wrapper = new TextWrapper();

		results = wrapper.wrap(40);
		assertEquals(0, results.length);

		inputs = new String[] {
				"Here ",
				"are ",
				"some ",
				"short ",
				"words ",
				"and here are some long ones. We make sure we have some short stuff and some long stuff, just to make sure it all wraps." };

		for (String input : inputs) {
			wrapper.add(input);
		}
		results = wrapper.wrap(40);
		assertWidth(results, 40);
		assertPreserved(inputs, results);

		wrapper.clear();
		results = wrapper.wrap(40);
		assertEquals(0, results.length);

		String s1 = "yabba ";
		String s3 = "yabba yabba yabba ";
		wrapper.add(s1);
		wrapper.addNoSplit(s3);
		wrapper.addNoSplit(s3);
		wrapper.add(s1);
		results = wrapper.wrap(18);
		assertEquals(4, results.length);
		assertEquals(s1, results[0]);
		assertEquals(s3, results[1]);
		assertEquals(s3, results[2]);
		assertEquals(s1, results[3]);
	}

	/**
	 * Ensure the two string arrays represent the same run of text after all
	 * elements are concatenated.
	 */
	private void assertPreserved(String[] inputs, String[] results) {
		StringBuffer inputSB = new StringBuffer();
		StringBuffer resultSB = new StringBuffer();
		for (String input : inputs) {
			inputSB.append(input);
		}
		for (String result : results) {
			resultSB.append(result);
		}
		assertEquals(inputSB.toString(), resultSB.toString());
	}

	/**
	 * Ensure all lines fit within the given width, and that adding an extra
	 * token from the next line would blow it.
	 */
	private void assertWidth(String[] results, int width) {
		for (int i = 0; i < results.length; i++) {
			assertTrue(results[i].length() > 0);
			assertTrue(results[i].length() <= width);
			if (i < results.length - 1) {
				assertTrue(results[i].length()
						+ getToken(results[i + 1]).length() > width);
			}
		}
	}

	/**
	 * Get a token from a string.
	 */
	private String getToken(String s) {
		int i = 0;
		while (i < s.length() && !Character.isWhitespace(s.charAt(i))) {
			i++;
		}
		while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
			i++;
		}
		return s.substring(0, i);
	}

}

Back to the top