1
2 package org.apache.maven.doxia.site.decoration.inheritance;
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.net.URI;
24
25 import junit.framework.TestCase;
26
27
28
29
30
31
32
33 public class URIPathDescriptorTest
34 extends TestCase
35 {
36 private static final String BASE_URL = "http://maven.apache.org/";
37
38
39
40
41
42
43 public void testConstructor()
44 throws Exception
45 {
46 final String expected = BASE_URL + "doxia";
47
48 final URIPathDescriptor path = new URIPathDescriptor( BASE_URL, "doxia" );
49 assertEquals( expected, path.toString() );
50 assertEquals( BASE_URL, path.getBaseURI().toString() );
51 assertEquals( "doxia", path.getLink().toString() );
52
53 URIPathDescriptor compare = new URIPathDescriptor( "http://maven.apache.org", "/doxia" );
54 assertEquals( expected, compare.toString() );
55
56 compare = new URIPathDescriptor( "http://maven.apache.org/./doxia/../", "/sub/./sub/../../doxia" );
57 assertEquals( expected, compare.toString() );
58
59 compare = new URIPathDescriptor( "http://maven.apache.org/doxia", "" );
60 assertEquals( expected + "/", compare.toString() );
61
62 compare = new URIPathDescriptor( "file:///C:\\Foo\\bar1", "" );
63 assertEquals( "file:///C:/Foo/bar1/", compare.getBaseURI().toString() );
64
65 assertEquals( "file:/C:/Foo/bar1/", compare.toString() );
66
67 compare = new URIPathDescriptor( "file:///C:/Documents%20and%20Settings/foo/", "bar" );
68 assertEquals( "file:/C:/Documents%20and%20Settings/foo/bar", compare.toString() );
69
70 compare = new URIPathDescriptor( "file:////Users/", "user" );
71 assertEquals( "file:/Users/user", compare.toString() );
72
73 compare = new URIPathDescriptor( "file:/C:/Documents%20and%20Settings/foo/", "bar" );
74 assertEquals( "file:/C:/Documents%20and%20Settings/foo/bar", compare.toString() );
75
76 compare = new URIPathDescriptor( "file://C:/Documents%20and%20Settings/foo/", "bar" );
77
78 assertEquals( "file://C/Documents%20and%20Settings/foo/bar", compare.toString() );
79
80 compare = new URIPathDescriptor( "file://C:8080/Documents%20and%20Settings/foo/", "bar" );
81 assertEquals( "file://C:8080/Documents%20and%20Settings/foo/bar", compare.toString() );
82
83 compare = new URIPathDescriptor( "C:\\Foo\\bar", "bar" );
84 assertEquals( "C:/Foo/bar/bar", compare.toString() );
85
86 assertFailure( "/doxia", BASE_URL );
87 assertFailure( "file:///C:/Documents and Settings/foo/", "bar" );
88 }
89
90
91
92
93
94
95 public void testResolveLink()
96 throws Exception
97 {
98 final String expected = BASE_URL + "source";
99
100 URIPathDescriptor oldPath = new URIPathDescriptor( BASE_URL, "source" );
101 assertEquals( expected, oldPath.resolveLink().toString() );
102
103 oldPath = new URIPathDescriptor( BASE_URL, "source/" );
104 assertEquals( expected + "/", oldPath.resolveLink().toString() );
105
106 oldPath = new URIPathDescriptor( BASE_URL, "/source" );
107 assertEquals( expected, oldPath.resolveLink().toString() );
108
109 oldPath = new URIPathDescriptor( "http://maven.apache.org", "source" );
110 assertEquals( expected, oldPath.resolveLink().toString() );
111
112 oldPath = new URIPathDescriptor( BASE_URL, "source/index.html" );
113 assertEquals( expected + "/index.html", oldPath.resolveLink().toString() );
114
115 oldPath = new URIPathDescriptor( BASE_URL, "source/index.html?var=foo&var2=bar" );
116 assertEquals( expected + "/index.html?var=foo&var2=bar", oldPath.resolveLink().toString() );
117
118 oldPath = new URIPathDescriptor( "file:////Users/", "user" );
119 assertEquals( "file:/Users/user", oldPath.resolveLink().toString() );
120
121 oldPath = new URIPathDescriptor( "file:///C:/Documents%20and%20Settings/", "source" );
122
123 assertEquals( "file:/C:/Documents%20and%20Settings/source", oldPath.resolveLink().toString() );
124
125 oldPath = new URIPathDescriptor( "file://C:/Documents%20and%20Settings/", "source" );
126
127 assertEquals( "file://C/Documents%20and%20Settings/source", oldPath.resolveLink().toString() );
128
129 oldPath = new URIPathDescriptor( "file:/C:/Documents%20and%20Settings/", "source" );
130 assertEquals( "file:/C:/Documents%20and%20Settings/source", oldPath.resolveLink().toString() );
131 }
132
133
134
135
136
137
138 public void testRebaseLink()
139 throws Exception
140 {
141 URIPathDescriptor oldPath = new URIPathDescriptor( BASE_URL, "source" );
142 assertEquals( "../source", oldPath.rebaseLink( "http://maven.apache.org/doxia/" ).toString() );
143 assertEquals( "http://maven.apache.org/source", oldPath.rebaseLink( null ).toString() );
144 assertEquals( "http://maven.apache.org/source", oldPath.rebaseLink( "C:/Documents and Settings/" ).toString() );
145
146 oldPath = new URIPathDescriptor( BASE_URL, "./" );
147 assertEquals( "", oldPath.rebaseLink( "http://maven.apache.org/" ).toString() );
148
149 oldPath = new URIPathDescriptor( BASE_URL, "" );
150 assertEquals( "", oldPath.rebaseLink( "http://maven.apache.org/" ).toString() );
151
152 oldPath = new URIPathDescriptor( BASE_URL, "source/index.html" );
153 assertEquals( "../source/index.html", oldPath.rebaseLink( "http://maven.apache.org/doxia/" ).toString() );
154
155 oldPath = new URIPathDescriptor( BASE_URL, "source/index.html?var=foo&var2=bar" );
156 assertEquals( "../source/index.html?var=foo&var2=bar",
157 oldPath.rebaseLink( "http://maven.apache.org/doxia/" ).toString() );
158
159 oldPath = new URIPathDescriptor( "scp://people.apache.org/", "source" );
160 assertEquals( "../source", oldPath.rebaseLink( "scp://people.apache.org/doxia" ).toString() );
161
162 oldPath = new URIPathDescriptor( BASE_URL, "banner/left" );
163 assertEquals( "../banner/left", oldPath.rebaseLink( "http://maven.apache.org/doxia/" ).toString() );
164
165 oldPath = new URIPathDescriptor( BASE_URL, "index.html?var=foo&var2=bar" );
166 assertEquals( "../index.html?var=foo&var2=bar",
167 oldPath.rebaseLink( "http://maven.apache.org/doxia/" ).toString() );
168
169 oldPath = new URIPathDescriptor( "http://jakarta.apache.org/", "banner/left" );
170 assertEquals( "http://jakarta.apache.org/banner/left", oldPath.rebaseLink( BASE_URL ).toString() );
171
172 oldPath = new URIPathDescriptor( "file:////Users/", "user" );
173 assertEquals( "../user", oldPath.rebaseLink( "file:////Users/target" ).toString() );
174 assertEquals( "../user", oldPath.rebaseLink( "file:/Users/target" ).toString() );
175
176 oldPath = new URIPathDescriptor( "file:///C:/Documents%20and%20Settings/", "source" );
177 assertEquals( "../source", oldPath.rebaseLink( "file:///C:/Documents%20and%20Settings/target" ).toString() );
178
179 oldPath = new URIPathDescriptor( "file://C:/Documents%20and%20Settings/", "source" );
180 assertEquals( "../source", oldPath.rebaseLink( "file://C:/Documents%20and%20Settings/target" ).toString() );
181
182 oldPath = new URIPathDescriptor( "file:/C:/Documents%20and%20Settings/", "source" );
183 assertEquals( "../source", oldPath.rebaseLink( "file:/C:/Documents%20and%20Settings/target" ).toString() );
184 }
185
186
187
188
189
190
191 public void testRelativizeLink()
192 throws Exception
193 {
194 URIPathDescriptor path = new URIPathDescriptor( BASE_URL, "source" );
195 assertEquals( "source", path.relativizeLink().toString() );
196
197 path = new URIPathDescriptor( BASE_URL, "http://maven.apache.org/source" );
198 assertEquals( "source", path.relativizeLink().toString() );
199
200 path = new URIPathDescriptor( BASE_URL, "http://maven.apache.org/" );
201 assertEquals( "./", path.relativizeLink().toString() );
202
203 path = new URIPathDescriptor( BASE_URL, "http://maven.apache.org" );
204 assertEquals( "./", path.relativizeLink().toString() );
205
206 path = new URIPathDescriptor( "http://maven.apache.org", BASE_URL );
207 assertEquals( "./", path.relativizeLink().toString() );
208
209 path = new URIPathDescriptor( "http://maven.apache.org", "http://maven.apache.org" );
210 assertEquals( "./", path.relativizeLink().toString() );
211
212 path = new URIPathDescriptor( "http://maven.apache.org/doxia/", "http://maven.apache.org/source/" );
213 assertEquals( "../source/", path.relativizeLink().toString() );
214
215 path = new URIPathDescriptor( "http://maven.apache.org/doxia", "http://maven.apache.org/source" );
216 assertEquals( "../source", path.relativizeLink().toString() );
217
218 path = new URIPathDescriptor( BASE_URL, "http://maven.apache.org/index.html" );
219 assertEquals( "index.html", path.relativizeLink().toString() );
220
221 path = new URIPathDescriptor( BASE_URL, "http://maven.apache.org/index.html?var=foo&var2=bar" );
222 assertEquals( "index.html?var=foo&var2=bar", path.relativizeLink().toString() );
223
224 path = new URIPathDescriptor( "file:////Users/", "index.html" );
225 assertEquals( "index.html", path.relativizeLink().toString() );
226
227 path = new URIPathDescriptor( "file:///C:/Documents%20and%20Settings/", "index.html" );
228 assertEquals( "index.html", path.relativizeLink().toString() );
229
230 path = new URIPathDescriptor( "file://C:/Documents%20and%20Settings/", "index.html" );
231 assertEquals( "index.html", path.relativizeLink().toString() );
232
233 path = new URIPathDescriptor( "file:/C:/Documents%20and%20Settings/", "index.html" );
234 assertEquals( "index.html", path.relativizeLink().toString() );
235 }
236
237
238
239
240
241
242 public void testSameSite()
243 throws Exception
244 {
245 final URIPathDescriptor path = new URIPathDescriptor( BASE_URL, "doxia" );
246
247 assertTrue( path.sameSite( new URI( "http://maven.apache.org/" ) ) );
248 assertTrue( path.sameSite( new URI( "http://maven.apache.org" ) ) );
249 assertTrue( path.sameSite( new URI( "HTTP://maven.apache.org/" ) ) );
250 assertTrue( path.sameSite( new URI( "http://MAVEN.apache.org/" ) ) );
251 assertTrue( path.sameSite( new URI( "http://maven.apache.org/wagon/index.html" ) ) );
252
253 assertFalse( path.sameSite( null ) );
254 assertFalse( path.sameSite( new URI( "https://maven.apache.org/" ) ) );
255 assertFalse( path.sameSite( new URI( "http://ant.apache.org/" ) ) );
256 assertFalse( path.sameSite( new URI( "http://maven.apache.org:80" ) ) );
257 assertFalse( path.sameSite( new URI( "/usr/share/bin/" ) ) );
258 assertFalse( path.sameSite( new URI( "http:///maven.apache.org/" ) ) );
259
260 final URIPathDescriptor nullHost = new URIPathDescriptor( "http:///maven.apache.org/", "doxia" );
261 assertTrue( nullHost.sameSite( new URI( "http:///maven.apache.org/" ) ) );
262 assertFalse( nullHost.sameSite( new URI( "http://maven.apache.org/" ) ) );
263
264 URIPathDescriptor newPath = new URIPathDescriptor( "file:///C:/Documents%20and%20Settings/", "source" );
265 assertTrue( newPath.sameSite( new URI( "file:///C:/Documents%20and%20Settings/" ) ) );
266 assertFalse( newPath.sameSite( new URI( "file://C:/Documents%20and%20Settings/" ) ) );
267
268 assertTrue( newPath.sameSite( new URI( "file:/C:/Documents%20and%20Settings/" ) ) );
269 }
270
271 private static void assertFailure( final String base, final String link )
272 {
273 try
274 {
275 final URIPathDescriptor test = new URIPathDescriptor( base, link );
276 fail( "Should fail: " + test.toString() );
277 }
278 catch ( IllegalArgumentException ex )
279 {
280 assertNotNull( ex );
281 }
282 }
283 }