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 package com.gargoylesoftware.base.util;
39
40 import javax.swing.text.Document;
41 import javax.swing.text.PlainDocument;
42 import junit.framework.TestCase;
43
44 /***
45 * Tests for DocumentUtil
46 *
47 * @version $Revision: 1.6 $
48 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
49 */
50 public class DocumentUtilTest extends TestCase {
51
52 /***
53 * Create a new test
54 * @param name The name of the test.
55 */
56 public DocumentUtilTest( final String name ) {
57 super(name);
58 }
59
60 /***
61 * Test upperCaseDocument() with mixed case inputs.
62 * @throws Exception If something bad happens.
63 */
64 public void testUpperCaseDocument() throws Exception {
65 final Document document
66 = DocumentUtil.upperCaseDocument( new PlainDocument() );
67
68 add( document, "aBc" );
69 assertEquals( "ABC", getText(document) );
70
71 add( document, "d" );
72 assertEquals( "ABCD", getText(document) );
73 }
74
75 /***
76 * Test upperCaseDocument() with null document.
77 * @throws Exception If something bad happens.
78 */
79 public void testUpperCaseDocument_null() throws Exception {
80 try {
81 DocumentUtil.upperCaseDocument( null );
82 fail("Expected exception for null document length");
83 }
84 catch( final DetailedNullPointerException e ) {
85 assertEquals("delegate", e.getArgumentName());
86 }
87 }
88
89 /***
90 * Test lengthLimitedDocument() with a negative length.
91 * @throws Exception If something bad happens.
92 */
93 public void testLengthLimitedDocument_negative() throws Exception {
94 try {
95 DocumentUtil.lengthLimitedDocument( new PlainDocument(), -1 );
96 fail("Expected exception for negative length");
97 }
98 catch( final IllegalArgumentException e ) {
99
100 }
101 }
102
103 /***
104 * Test lengthLimitedDocument() with a zero length.
105 * @throws Exception If something bad happens.
106 */
107 public void testLengthLimitedDocument_zero() throws Exception {
108 try {
109 DocumentUtil.lengthLimitedDocument( new PlainDocument(), -1 );
110 fail("Expected exception for zero length");
111 }
112 catch( final IllegalArgumentException e ) {
113
114 }
115 }
116
117 /***
118 * Test lengthLimitedDocument() with a null document.
119 * @throws Exception If something bad happens.
120 */
121 public void testLengthLimitedDocument_null() throws Exception {
122 try {
123 DocumentUtil.lengthLimitedDocument( null, 4 );
124 fail("Expected exception for null document length");
125 }
126 catch( final DetailedNullPointerException e ) {
127 assertEquals( "delegate", e.getArgumentName() );
128 }
129 }
130
131 /***
132 * Test lengthLimitedDocument to ensure that you can't enter more than the
133 * specified number of characters.
134 * @throws Exception If something bad happens.
135 */
136 public void testLengthLimitedDocument_4() throws Exception {
137 final Document document
138 = DocumentUtil.lengthLimitedDocument( new PlainDocument(), 4 );
139 add( document, "a" );
140 add( document, "b" );
141 add( document, "c" );
142 add( document, "d" );
143 assertEquals( "abcd", getText(document) );
144 add( document, "e" );
145 assertEquals( "abcd", getText(document) );
146 }
147
148 /***
149 * Insert the specified string at the end of the document
150 * @param document the document
151 * @param string The string to insert
152 * @throws Exception If something bad happens.
153 */
154 private void add( Document document, String string ) throws Exception {
155 document.insertString( document.getLength(), string, null );
156 }
157
158 /***
159 * Return the full text of the document.
160 * @param document The document
161 * @return The content as a string
162 * @throws Exception If something bad happens.
163 */
164 private String getText( final Document document ) throws Exception {
165 return document.getText( 0, document.getLength() );
166 }
167 }
168