1 package org.apache.maven.doxia.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.text.ParseException;
23
24 import java.util.Date;
25 import java.util.GregorianCalendar;
26
27 import org.codehaus.plexus.PlexusTestCase;
28
29
30
31
32
33
34
35 public class DoxiaUtilsTest
36 extends PlexusTestCase
37 {
38
39
40
41 public void testIsInternalLink()
42 {
43 String link = "#anchor";
44 assertTrue( "Should be an internal link: " + link,
45 DoxiaUtils.isInternalLink( link ) );
46
47 link = "http://maven.apache.org/index.html#anchor";
48 assertFalse( "Should NOT be an internal link: " + link,
49 DoxiaUtils.isInternalLink( link ) );
50
51 link = "./index.html";
52 assertFalse( "Should NOT be an internal link: " + link,
53 DoxiaUtils.isInternalLink( link ) );
54 }
55
56
57
58
59 public void testIsExternalLink()
60 {
61 String link = "http://maven.apache.org/";
62 assertTrue( "Should be an external link: " + link,
63 DoxiaUtils.isExternalLink( link ) );
64
65 link = "https://maven.apache.org/";
66 assertTrue( "Should be an external link: " + link,
67 DoxiaUtils.isExternalLink( link ) );
68
69 link = "HTTPS://MAVEN.APACHE.ORG/";
70 assertTrue( "Should be an external link: " + link,
71 DoxiaUtils.isExternalLink( link ) );
72
73 link = "ftp:/maven.apache.org/";
74 assertTrue( "Should be an external link: " + link,
75 DoxiaUtils.isExternalLink( link ) );
76
77 link = "mailto:maven@apache.org";
78 assertTrue( "Should be an external link: " + link,
79 DoxiaUtils.isExternalLink( link ) );
80
81 link = "file:/index.html";
82 assertTrue( "Should be an external link: " + link,
83 DoxiaUtils.isExternalLink( link ) );
84
85 link = "resource_type://domain:port/filepathname?query_string#anchor";
86 assertTrue( "Should be an external link: " + link,
87 DoxiaUtils.isExternalLink( link ) );
88
89 link = "index.html";
90 assertFalse( "Should NOT be an external link: " + link,
91 DoxiaUtils.isExternalLink( link ) );
92
93 link = "example.pdf";
94 assertFalse( "Should NOT be an external link: " + link,
95 DoxiaUtils.isExternalLink( link ) );
96
97 link = "./index.html";
98 assertFalse( "Should NOT be an external link: " + link,
99 DoxiaUtils.isExternalLink( link ) );
100
101 link = "../index.html";
102 assertFalse( "Should NOT be an external link: " + link,
103 DoxiaUtils.isExternalLink( link ) );
104
105
106
107 link = "file:\\index.html";
108 assertFalse( "Should NOT be an external link: " + link,
109 DoxiaUtils.isExternalLink( link ) );
110
111 link = ".\\index.html";
112 assertFalse( "Should NOT be an external link: " + link,
113 DoxiaUtils.isExternalLink( link ) );
114
115 link = "..\\index.html";
116 assertFalse( "Should NOT be an external link: " + link,
117 DoxiaUtils.isExternalLink( link ) );
118 }
119
120
121
122
123 public void testIsLocalLink()
124 {
125 String link = "index.html";
126 assertTrue( "Should be a local link: " + link,
127 DoxiaUtils.isLocalLink( link ) );
128
129 link = "./index.html";
130 assertTrue( "Should be a local link: " + link,
131 DoxiaUtils.isLocalLink( link ) );
132
133 link = "../index.html";
134 assertTrue( "Should be a local link: " + link,
135 DoxiaUtils.isLocalLink( link ) );
136
137 link = "#anchor";
138 assertFalse( "Should NOT be a local link: " + link,
139 DoxiaUtils.isLocalLink( link ) );
140
141 link = "http://maven.apache.org/";
142 assertFalse( "Should NOT be a local link: " + link,
143 DoxiaUtils.isLocalLink( link ) );
144
145 }
146
147
148
149
150 public void testEncodeId()
151 {
152 assertEquals( DoxiaUtils.encodeId( null ), null );
153 assertEquals( DoxiaUtils.encodeId( "" ), "a" );
154 assertEquals( DoxiaUtils.encodeId( " " ), "a" );
155 assertEquals( DoxiaUtils.encodeId( " _ " ), "a_" );
156 assertEquals( DoxiaUtils.encodeId( "1" ), "a1" );
157 assertEquals( DoxiaUtils.encodeId( "1anchor" ), "a1anchor" );
158 assertEquals( DoxiaUtils.encodeId( "_anchor" ), "a_anchor" );
159 assertEquals( DoxiaUtils.encodeId( "a b-c123 " ), "a_b-c123" );
160 assertEquals( DoxiaUtils.encodeId( " anchor" ), "anchor" );
161 assertEquals( DoxiaUtils.encodeId( "myAnchor" ), "myAnchor" );
162 assertEquals( DoxiaUtils.encodeId( "my&Anchor" ), "my%26Anchor" );
163 assertEquals( DoxiaUtils.encodeId( "H\u00E5kon" ), "H%c3%a5kon" );
164 assertEquals( DoxiaUtils.encodeId( "H\u00E5kon", true ), "Hkon" );
165 assertEquals( DoxiaUtils.encodeId( "Theu\u00DFl" ), "Theu%c3%9fl" );
166 assertEquals( DoxiaUtils.encodeId( "Theu\u00DFl", true ), "Theul" );
167 }
168
169
170
171
172 public void testIsValidId()
173 {
174 assertFalse( DoxiaUtils.isValidId( null ) );
175 assertFalse( DoxiaUtils.isValidId( "" ) );
176 assertFalse( DoxiaUtils.isValidId( " " ) );
177 assertFalse( DoxiaUtils.isValidId( " _ " ) );
178 assertFalse( DoxiaUtils.isValidId( "1" ) );
179 assertFalse( DoxiaUtils.isValidId( "1anchor" ) );
180 assertFalse( DoxiaUtils.isValidId( "_anchor" ) );
181 assertFalse( DoxiaUtils.isValidId( "a b-c123 " ) );
182 assertFalse( DoxiaUtils.isValidId( " anchor" ) );
183 assertFalse( DoxiaUtils.isValidId( "my&Anchor" ) );
184 assertTrue( DoxiaUtils.isValidId( "myAnchor" ) );
185 assertTrue( DoxiaUtils.isValidId( "a_" ) );
186 assertTrue( DoxiaUtils.isValidId( "a-" ) );
187 assertTrue( DoxiaUtils.isValidId( "a:" ) );
188 assertTrue( DoxiaUtils.isValidId( "a." ) );
189 assertTrue( DoxiaUtils.isValidId( "index.html" ) );
190 assertFalse( DoxiaUtils.isValidId( "Theu\u00DFl" ) );
191 }
192
193
194
195
196 public void testParseDate()
197 {
198 final int year = 1973;
199 final int month = 1;
200 final int day = 27;
201
202 try
203 {
204 final Date feb27 = new GregorianCalendar( year, month, day ).getTime();
205 assertEquals( feb27, DoxiaUtils.parseDate( "27.02.1973" ) );
206 assertEquals( feb27, DoxiaUtils.parseDate( "27. 02. 1973" ) );
207 assertEquals( feb27, DoxiaUtils.parseDate( "1973-02-27" ) );
208 assertEquals( feb27, DoxiaUtils.parseDate( "1973/02/27" ) );
209 assertEquals( feb27, DoxiaUtils.parseDate( "27 Feb 1973" ) );
210 assertEquals( feb27, DoxiaUtils.parseDate( "27 Feb. 1973" ) );
211 assertEquals( feb27, DoxiaUtils.parseDate( "Feb. 27, 1973" ) );
212 assertEquals( feb27, DoxiaUtils.parseDate( "Feb 27, '73" ) );
213 assertEquals( feb27, DoxiaUtils.parseDate( "February 27, 1973" ) );
214 assertEquals( feb27, DoxiaUtils.parseDate( "19730227" ) );
215
216 assertEquals( new GregorianCalendar( year, 0, 1 ).getTime(), DoxiaUtils.parseDate( "1973" ) );
217
218 final Date feb1 = new GregorianCalendar( year, 1, 1 ).getTime();
219 assertEquals( feb1, DoxiaUtils.parseDate( "February 1973" ) );
220 assertEquals( feb1, DoxiaUtils.parseDate( "Feb. 1973" ) );
221 assertEquals( feb1, DoxiaUtils.parseDate( "February '73" ) );
222 assertEquals( feb1, DoxiaUtils.parseDate( "Feb. '73" ) );
223
224 assertNotNull( DoxiaUtils.parseDate( "Today" ) );
225 assertNotNull( DoxiaUtils.parseDate( "NOW" ) );
226 }
227 catch ( ParseException ex )
228 {
229 fail( ex.getMessage() );
230 }
231
232 try
233 {
234 DoxiaUtils.parseDate( "yesterday" ).getTime();
235 fail();
236 }
237 catch ( ParseException ex )
238 {
239 assertNotNull( ex );
240 }
241 }
242 }