KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > util > test > TestTextUtil


1 /*
2  * Created on 12-Nov-06 by Peter Maloney
3  *
4  */

5 package org.planetamessenger.util.test;
6
7 import org.planetamessenger.test.TestAll;
8 import org.planetamessenger.util.*;
9
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13
14 /**
15  * Tests org.planetamessenger.util.JTextUtil
16  *
17  * Methods tested:
18  * encodeHtml(String)
19  * quote(String, char, boolean)
20  *
21  * @author Peter Maloney
22  */

23 public class TestTextUtil extends TestCase {
24
25   /**
26    *
27    */

28   public TestTextUtil() {
29     
30   }
31
32   /**
33    * @param name
34    */

35   public TestTextUtil(String JavaDoc name) {
36     super( name );
37   }
38
39   /** @return a TestSuite to test TestTextUtil */
40   public static Test suite() {
41     TestSuite suite = new TestSuite("TestTextUtil");
42     suite.addTestSuite( TestTextUtil.class );
43     
44     return suite;
45   }
46   
47   //=================================================================
48
// encodeHtml
49
//=================================================================
50
/**
51    * @param in the text to encode
52    * @param expectedResult the html result expected after encoding
53    */

54   public void encodeHtmlTester(String JavaDoc in, String JavaDoc expectedResult) {
55     String JavaDoc result = JTextUtil.encodeHtml( in );
56     
57     boolean pass = result == expectedResult || result != null && result.equals(expectedResult);
58     if( !pass ) {
59       System.err.println("in=\""+in+"\", expectedResult=\""+expectedResult+"\", result=\""+result+"\"");
60     }
61     assertTrue( pass );
62   }
63   
64   /** plain text, no special characters */
65   public void testEncodeHtml_plainText1() {
66     encodeHtmlTester( "Hello!", "Hello!" );
67   }
68   
69   /** plain text with unix newline */
70   public void testEncodeHtml_newline1() {
71     encodeHtmlTester( "Hello!\nHow are you?", "Hello!<br/>How are you?" );
72   }
73   
74   /** plain text with winblows newline */
75   public void testEncodeHtml_newlineWinblows1() {
76     encodeHtmlTester( "Hello!\r\nHow are you?", "Hello!<br/>How are you?" );
77   }
78   
79   /** plain text with mac newline */
80   public void testEncodeHtml_newlineMac1() {
81     encodeHtmlTester( "Hello!\rHow are you?", "Hello!<br/>How are you?" );
82   }
83   
84   /** plain text with mixed unix/winblows/mac newlines */
85   public void testEncodeHtml_newlineNonsense1() {
86     encodeHtmlTester(
87       "Hi.\nHello!\rHow are you?\n\rI hope you are fine.", // \n\r is not right...so it turns to 2 newlines
88
"Hi.<br/>Hello!<br/>How are you?<br/><br/>I hope you are fine."
89     );
90   }
91   
92   /** various special characters, that should be translated */
93   public void testEncodeHtml_specialCharacters() {
94     encodeHtmlTester( "Hello!\n & \" \' < >", "Hello!<br/> &amp; &quot; &#39; &lt; &gt;" );
95   }
96   
97   /** null parameter, should return null */
98   public void testEncodeHtml_nullParameter() {
99     encodeHtmlTester( null, null );
100   }
101   
102   //=================================================================
103
// quote
104
//=================================================================
105
/**
106    * Tests JTextUtil.quote(String,char,boolean)
107    * @param in the input text to quote
108    * @param inQuote the type of quote
109    * @param appendQuotes true to wrap return value in quotes
110    * @param expectedResult the expected quoted value
111    */

112   public void quoteTester(String JavaDoc in, char inQuote, boolean appendQuotes, String JavaDoc expectedResult) {
113     String JavaDoc result = JTextUtil.quote( in, inQuote, appendQuotes );
114     
115     boolean pass = result == expectedResult || result != null && result.equals(expectedResult);
116     if( !pass ) {
117       System.err.println("in=\""+in+"\", inQuote="+inQuote+", appendQuotes="+appendQuotes+", expectedResult=\""+expectedResult+"\", result=\""+result+"\"");
118     }
119     assertTrue( pass );
120   }
121   
122   /** Plain text needs no quoting, gets wrapped in quotes */
123   public void testQuote_plainText1() {
124     quoteTester("Test", '\'', true, "'Test'");
125   }
126   
127   /** Plain text needs no quoting */
128   public void testQuote_plainText2() {
129     quoteTester("Test", '\'', false, "Test");
130   }
131   
132   /** Plain text with an apostrophe */
133   public void testQuote_apostrophe() {
134     quoteTester("There's an apostrophe here.", '\'', false, "There\\\'s an apostrophe here.");
135   }
136   
137   /** single quotes in input string */
138   public void testQuote_singleQuotes() {
139     quoteTester(
140       "The rain in \'Spain\' is mainly on the \'Plane\'.",
141       '\'', false,
142       "The rain in \\\'Spain\\\' is mainly on the \\\'Plane\\\'."
143     );
144   }
145   
146   /** mixed double & single quotes, and slashes in input string */
147   public void testQuote_bothQuotesAndSlashes() {
148     quoteTester(
149       "The rain in \"Spain\" is mainly on the \'Plane\' slash: \\ 2 slashes: \\\\.",
150       '\'', false,
151       "The rain in \"Spain\" is mainly on the \\\'Plane\\\' slash: \\\\ 2 slashes: \\\\\\\\."
152     );
153   }
154   
155   /** mixed double & single quotes, and slashes in input string */
156   public void testQuote_complex() {
157     quoteTester(
158       "test \" \' ? $ * %& \' \"",
159       '\'', true,
160       "\'test \" \\\' ? $ * %& \\\' \"\'"
161     );
162   }
163   
164   
165   
166   /** tests null parameter, should return null */
167   public void testQuote_nullParameter() {
168     quoteTester(null, '"', true, null);
169   }
170   
171   /**
172    * @param args
173    */

174   public static void main(String JavaDoc[] args) {
175     junit.textui.TestRunner.run( TestTextUtil.suite() );
176   }
177
178 }
179
Popular Tags