KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > valueuri > tests > ShortenStringURIHandlerTest


1 /**
2  * $Id: ShortenStringURIHandlerTest.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2005 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The GNU LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.valueuri.tests;
30
31 import junit.framework.TestSuite;
32
33 import com.idaremedia.antx.ValueURIHandler;
34 import com.idaremedia.antx.valueuri.ShortenStringURIHandler;
35
36 /**
37  * Testsuite for {@linkplain ShortenStringValueURIHandler}.
38  *
39  * @since JWare/AntX 0.5
40  * @author ssmc, &copy;2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
41  * @version 0.5
42  * @.safety single
43  * @.group impl,test
44  **/

45
46 public final class ShortenStringURIHandlerTest extends ValueURIHandlerTestSkeleton
47 {
48     private final static int MAXLEN = ShortenStringURIHandler.MAXLEN;
49     private final static String JavaDoc ABC_ETC = "abcdefghijklmnopqrstuvwxyz?";
50     private final String JavaDoc EXACTSTRING;
51     private final String JavaDoc OVERSTRING;
52     private final String JavaDoc UNDERSTRING;
53     private final String JavaDoc EXACTLEFT, EXACTRIGHT;
54
55
56     /**
57      * Initializes a new test case for named method.
58      * @param methodname test case method's name (non-null)
59      **/

60     public ShortenStringURIHandlerTest(String JavaDoc methodname)
61     {
62         super("ShortenStringURIHandler::",methodname);
63         EXACTSTRING = xstring(MAXLEN);
64         OVERSTRING = xstring(MAXLEN+5);
65         UNDERSTRING = xstring(Math.max(0,MAXLEN-5));
66         EXACTLEFT = "..."+xstring(MAXLEN-3);
67         EXACTRIGHT = OVERSTRING.substring(0,MAXLEN-3)+"...";
68     }
69
70
71     /**
72      * Returns full test suite for XDefCheckValueURIHandler.
73      **/

74     public static TestSuite suite()
75     {
76         return new TestSuite(ShortenStringURIHandlerTest.class);
77     }
78
79 // ---------------------------------------------------------------------------------------------------------
80
// --------------------------------------- [ Misc Factory Methods ] ----------------------------------------
81
// ---------------------------------------------------------------------------------------------------------
82

83     protected ValueURIHandler newOUT()
84     {
85         return new ShortenStringURIHandler();
86     }
87     
88     private String JavaDoc xstring(int length)
89     {
90         final String JavaDoc L = "0123456789";
91         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(Math.max(length,100));
92         while (--length>=0) {
93             sb.append(L.charAt(length % 10));
94         }
95         return sb.substring(0);
96     }
97
98 // ---------------------------------------------------------------------------------------------------------
99
// ------------------------------------------- [ The Test Cases ] ------------------------------------------
100
// ---------------------------------------------------------------------------------------------------------
101

102     /**
103      * Verify a blank uri fragment is evaluated ok.
104      * @since JWare/AntX 0.5
105      **/

106     public void testBaseline_AntX05()
107     {
108         checkBaseline();
109         System.out.println("EXACTSTRING:"+EXACTSTRING);
110         System.out.println("OVERSTRING:"+OVERSTRING);
111         ValueURIHandler out = newOUT();
112         String JavaDoc result = out.valueFrom("","$shorten:",m_rqlink);
113         assertEqual(result,"","$shorten:");
114     }
115
116
117     /**
118      * Verify an all blank options frag is handled properly.
119      * @since JWare/AntX 0.5
120      **/

121     public void testAllBlankOptionsParsedOK_AntX05()
122     {
123         ValueURIHandler out = newOUT();
124         String JavaDoc result = out.valueFrom("?,,,,","$shorten:?,,,,",m_rqlink);
125         assertEqual(result,"","'$shorten:?,,,,'");
126
127         final String JavaDoc BLANK= "@(blank)?@(blank),,@(blank),,";
128         final String JavaDoc BLANKURI="$shorten:"+BLANK;
129
130         getProject().setProperty("blank","");
131         result = out.valueFrom(BLANK,BLANKURI,m_rqlink);
132         assertEqual(result,"",BLANKURI);
133         result = out.valueFrom(" "+BLANK,"$shorten: "+BLANK,m_rqlink);
134         assertEqual(result," ","'$shorten: "+BLANK+"'");
135     }
136
137
138     /**
139      * Verify that exact and under length strings are returned
140      * as-is regardless of the incoming options.
141      * @since JWare/AntX 0.5
142      **/

143     public void testStringsUnderAndExactMaxLenNotChanged_AntX05()
144     {
145         ValueURIHandler out = newOUT();
146         String JavaDoc result = out.valueFrom(EXACTSTRING,"$shorten:"+EXACTSTRING,m_rqlink);
147         assertIdent(result,EXACTSTRING,"$shorten:EXACTSTRING");
148         result = out.valueFrom(UNDERSTRING,"$shorten:"+UNDERSTRING,m_rqlink);
149         assertIdent(result,UNDERSTRING,"$shorten:UNDERSTRING");
150         
151         String JavaDoc urifragment = EXACTSTRING+"?"+MAXLEN+",,right";
152         result = out.valueFrom(urifragment,"$shorten:"+urifragment,m_rqlink);
153         assertEqual(result,EXACTSTRING,"$shorten:EXACTSTRING?MAXLEN,,right");
154         
155         urifragment = UNDERSTRING+"?"+MAXLEN+",,left,,ERROR!";
156         result = out.valueFrom(urifragment,"$shorten:"+urifragment,m_rqlink);
157         assertEqual(result,UNDERSTRING,"$shorten:UNDERSTRING?MAXLEN,,left,,ERROR!");
158     }
159
160
161     /**
162      * Verify that strings over the max length are shortened as
163      * expected.
164      * @since JWare/AntX 0.5
165      **/

166     public void testStringsOverMaxLengthShortenedDDD_AntX05()
167     {
168         ValueURIHandler out = newOUT();
169         String JavaDoc result = out.valueFrom(OVERSTRING,"$shorten:"+OVERSTRING,m_rqlink);
170         assertEqual(result,EXACTLEFT,"$shorten:OVERSTRING");
171         result = out.valueFrom(OVERSTRING+"?,,right","$shorten:"+OVERSTRING+"?,,right",m_rqlink);
172         assertEqual(result,EXACTRIGHT,"$shorten:OVERSTRING?,,right");
173         result = out.valueFrom("abcdefgh?5,,left","$shorten:abcdefgh?5,,left",m_rqlink);
174         assertEqual(result,"...gh","$shorten:abcdefgh?5,,left");
175         result = out.valueFrom("abcdefgh?5,,right","$shorten:abcdefgh?5,,right",m_rqlink);
176         assertEqual(result,"ab...","$shorten:abcdefgh?5,,right");
177     }
178
179
180     /**
181      * Verify can specify custom ellipses both left and right.
182      * @since JWare/AntX 0.5
183      **/

184     public void testCustomEllipsesUsed_AntX05()
185     {
186         ValueURIHandler out = newOUT();
187         String JavaDoc urifragment = ABC_ETC+"20,,left,,[snip]...";
188         String JavaDoc result = out.valueFrom(urifragment,"$shorten:"+urifragment,m_rqlink);
189         assertEqual(result,"[snip]...pqrstuvwxyz","$shorten:"+urifragment);
190         urifragment = ABC_ETC+"25,,right,,..blablabla";
191         result = out.valueFrom(urifragment,"$shorten:"+urifragment,m_rqlink);
192         assertEqual(result,"abcdefghijklmn..blablabla","$shorten:"+urifragment);
193     }
194
195
196     /**
197      * Verify that a custom ellipses string that is too long (will
198      * always force final string over maxlength) is ignored and
199      * replaced with "&#8230;" always.
200      * @since JWare/AntX 0.5
201      **/

202     public void testTooLongCustomEllipsesIgnored_AntX05()
203     {
204         ValueURIHandler out = newOUT();
205         String JavaDoc urifragment = "abcdefgh?5,,left,,[abbreviated]";
206         String JavaDoc result = out.valueFrom(urifragment,"$shorten:"+urifragment,m_rqlink);
207         assertEqual(result,"...gh","$shorten:"+urifragment);
208
209         final String JavaDoc ellipses="[abbreviated]";
210         final int maxlen= ellipses.length()-1;//12(=maxlength)
211
urifragment = "abcdefghijklmnopqrstuvwxyz?"+maxlen+",,right,,"+ellipses;
212         result= out.valueFrom(urifragment,"$shorten:"+urifragment,m_rqlink);
213         assertEqual(result,"abcdefghi...","$shorten:"+urifragment);
214
215         urifragment = "abcdefghijklmnopqrstuvwxyz?"+maxlen+",,left,,"+ellipses;
216         result= out.valueFrom(urifragment,"$shorten:"+urifragment,m_rqlink);
217         assertEqual(result,"...rstuvwxyz","$shorten:"+urifragment);
218     }
219 }
220
221 /* end-of-ShortenStringURIHandlerTest.java */
Popular Tags