1   /*
2    * Copyright (c) 1998, 2005 Gargoyle Software Inc. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    *
7    * 1. Redistributions of source code must retain the above copyright notice,
8    *    this list of conditions and the following disclaimer.
9    * 2. Redistributions in binary form must reproduce the above copyright notice,
10   *    this list of conditions and the following disclaimer in the documentation
11   *    and/or other materials provided with the distribution.
12   * 3. The end-user documentation included with the redistribution, if any, must
13   *    include the following acknowledgment:
14   *
15   *       "This product includes software developed by Gargoyle Software Inc.
16   *        (http://www.GargoyleSoftware.com/)."
17   *
18   *    Alternately, this acknowledgment may appear in the software itself, if
19   *    and wherever such third-party acknowledgments normally appear.
20   * 4. The name "Gargoyle Software" must not be used to endorse or promote
21   *    products derived from this software without prior written permission.
22   *    For written permission, please contact info@GargoyleSoftware.com.
23   * 5. Products derived from this software may not be called "GSBase", nor may
24   *    "GSBase" appear in their name, without prior written permission of
25   *    Gargoyle Software Inc.
26   *
27   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30   * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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              // Expected path
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             // Expected path
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