View Javadoc
1   package org.apache.maven.doxia.site.decoration.inheritance;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.io.Reader;
24  
25  import java.util.List;
26  
27  import org.apache.maven.doxia.site.decoration.Banner;
28  import org.apache.maven.doxia.site.decoration.Body;
29  import org.apache.maven.doxia.site.decoration.DecorationModel;
30  import org.apache.maven.doxia.site.decoration.LinkItem;
31  import org.apache.maven.doxia.site.decoration.Logo;
32  import org.apache.maven.doxia.site.decoration.Menu;
33  import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Reader;
34  import org.codehaus.plexus.util.IOUtil;
35  import org.codehaus.plexus.util.ReaderFactory;
36  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
37  import org.junit.Test;
38  
39  import static org.junit.Assert.*;
40  
41  /**
42   * Test the inheritance assembler.
43   *
44   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
45   */
46  public class DecorationModelInheritanceAssemblerTest
47  {
48      private DecorationModelInheritanceAssembler assembler = new DefaultDecorationModelInheritanceAssembler();
49  
50      private static final String NAME = "Name";
51  
52      /**
53       *
54       * @throws IOException
55       * @throws XmlPullParserException
56       */
57      @Test
58      public void testInheritance()
59          throws IOException, XmlPullParserException
60      {
61          DecorationModel childModel = readModel( "inheritance-child.xml" );
62          DecorationModel parentModel = readModel( "inheritance-parent.xml" );
63  
64          assembler.assembleModelInheritance( NAME, childModel, parentModel, "http://maven.apache.org/doxia",
65                                              "http://maven.apache.org" );
66          DecorationModel expectedModel = readModel( "inheritance-expected.xml" );
67  
68          assertEquals( "Check result", expectedModel, childModel );
69  
70          // same with scp url, DOXIASITETOOLS-47
71          childModel = readModel( "inheritance-child.xml" );
72          assembler.assembleModelInheritance( NAME, childModel, parentModel, "scp://people.apache.org/doxia",
73                                              "scp://people.apache.org" );
74          assertEquals( "Check scp result", expectedModel, childModel );
75  
76          assertEquals( "Modified parent!", readModel( "inheritance-parent.xml" ), parentModel );
77  
78          // late inheritance in links can't be rebased: check friendly message
79          parentModel.getBannerLeft().setHref( "${project.url}" );
80          childModel = readModel( "inheritance-child.xml" );
81          try
82          {
83              assembler.assembleModelInheritance( NAME, childModel, parentModel, "scp://people.apache.org/doxia",
84                                                  "scp://people.apache.org" );
85              fail( "late interpolation in link should cause IllegalArgumentException" );
86          }
87          catch ( IllegalArgumentException iae )
88          {
89              assertTrue( iae.getMessage().startsWith( "site.xml late interpolation" ) );
90          }
91      }
92  
93      /**
94       *
95       * @throws IOException
96       * @throws XmlPullParserException
97       */
98      @Test
99      public void testSuppressedInheritance()
100             throws IOException, XmlPullParserException
101     {
102         DecorationModel unassembledChildModel = readModel( "inheritance-child-no-inheritance.xml" );
103         DecorationModel childModel = readModel( "inheritance-child-no-inheritance.xml" );
104         DecorationModel parentModel = readModel( "inheritance-parent.xml" );
105         assembler.assembleModelInheritance( NAME, childModel, parentModel, "http://maven.apache.org/doxia",
106                                             "http://maven.apache.org" );
107         assertEquals( "Check result", unassembledChildModel, childModel );
108 
109         // 2 levels of inheritance
110         DecorationModel childOfchildModel = new DecorationModel();
111         assembler.assembleModelInheritance( "Child of Child", childOfchildModel, childModel,
112                                             "http://maven.apache.org/doxia/child", "http://maven.apache.org/doxia" );
113         assembler.assembleModelInheritance( NAME, childOfchildModel, parentModel, "http://maven.apache.org/doxia",
114                                             "http://maven.apache.org" );
115         // check that the 3 breadcrumb items from parent.xml are not inherited
116         assertEquals( "child of child no inheritance: breadcrumbs count", 0,
117                 childOfchildModel.getBody().getBreadcrumbs().size() );
118     }
119 
120     /**
121      *
122      * @throws IOException
123      * @throws XmlPullParserException
124      */
125     @Test
126     public void testPathsResolvedWhenEmpty()
127         throws IOException, XmlPullParserException
128     {
129         // Test an empty model avoids NPEs
130         DecorationModel childModel = readModel( "empty.xml" );
131         DecorationModel parentModel = readModel( "empty.xml" );
132 
133         assembler.assembleModelInheritance( NAME, childModel, parentModel, "http://maven.apache.org/doxia",
134                                             "http://maven.apache.org" );
135         DecorationModel mergedModel = readModel( "empty.xml" );
136 
137         assertEquals( "Check result", mergedModel, childModel );
138 
139         // same with scp url, DOXIASITETOOLS-47
140         childModel = readModel( "empty.xml" );
141         assembler.assembleModelInheritance( NAME, childModel, parentModel, "scp://people.apache.org/doxia",
142                                             "scp://people.apache.org" );
143         assertEquals( "Check scp result", mergedModel, childModel );
144 
145         assertEquals( "Modified parent!", readModel( "empty.xml" ), parentModel );
146     }
147 
148     /**
149      *
150      * @throws IOException
151      * @throws XmlPullParserException
152      */
153     @Test
154     public void testPathsNotResolvedForExternalUrls()
155         throws IOException, XmlPullParserException
156     {
157         DecorationModel parentModel = readModel( "external-urls.xml" );
158         DecorationModel childModel = readModel( "empty.xml" );
159 
160         assembler.assembleModelInheritance( NAME, childModel, parentModel, "http://maven.apache.org/doxia",
161                                             "http://maven.apache.org" );
162         assertPathsNotResolvedForExternalUrls( childModel );
163 
164         // same with scp url, DOXIASITETOOLS-47
165         childModel = readModel( "empty.xml" );
166         assembler.assembleModelInheritance( NAME, childModel, parentModel, "scp://people.apache.org/doxia",
167                                             "scp://people.apache.org" );
168         assertPathsNotResolvedForExternalUrls( childModel );
169 
170         assertEquals( "Modified parent!", readModel( "external-urls.xml" ), parentModel );
171     }
172 
173     private static void assertPathsNotResolvedForExternalUrls( final DecorationModel childModel )
174     {
175         assertEquals( "check left banner href", "http://jakarta.apache.org/",
176                 childModel.getBannerLeft().getHref() );
177         assertEquals( "check left banner image", "http://jakarta.apache.org/images/jakarta-logo.gif",
178                 childModel.getBannerLeft().getSrc() );
179 
180         assertEquals( "check right banner href", "http://jakarta.apache.org/commons/sandbox",
181                 childModel.getBannerRight().getHref() );
182         assertEquals( "check right banner image", "http://jakarta.apache.org/commons/images/logo.png",
183                 childModel.getBannerRight().getSrc() );
184 
185         Logo poweredBy = childModel.getPoweredBy().get( 0 );
186         assertEquals( "check powered by logo href", "http://tomcat.apache.org/", poweredBy.getHref() );
187         assertEquals( "check powered by logo image", "http://tomcat.apache.org/logo.gif", poweredBy.getImg() );
188 
189         LinkItem breadcrumb = childModel.getBody().getBreadcrumbs().get( 0 );
190         assertEquals( "check breadcrumb href", "http://www.apache.org/", breadcrumb.getHref() );
191 
192         LinkItem link = childModel.getBody().getLinks().get( 0 );
193         assertEquals( "check link href", "http://www.bouncycastle.org", link.getHref() );
194 
195         Menu menu = childModel.getBody().getMenus().get( 0 );
196         LinkItem menuItem = menu.getItems().get( 0 );
197         assertEquals( "check menu item href", "http://www.apache.org/special/", menuItem.getHref() );
198     }
199 
200     /**
201      *
202      * @throws IOException
203      * @throws XmlPullParserException
204      */
205     @Test
206     public void testPathsResolvedForRelativeUrls()
207         throws IOException, XmlPullParserException
208     {
209         DecorationModel parentModel = readModel( "relative-urls.xml" );
210         DecorationModel childModel = readModel( "empty.xml" );
211 
212         assembler.assembleModelInheritance( NAME, childModel, parentModel, "http://maven.apache.org/doxia/",
213                                             "http://maven.apache.org" );
214         assertPathsResolvedForRelativeUrls( childModel );
215 
216         // same with scp url, DOXIASITETOOLS-47
217         childModel = readModel( "empty.xml" );
218         assembler.assembleModelInheritance( NAME, childModel, parentModel, "scp://people.apache.org/doxia",
219                                             "scp://people.apache.org" );
220         assertPathsResolvedForRelativeUrls( childModel );
221 
222         assertEquals( "Modified parent!", readModel( "relative-urls.xml" ), parentModel );
223     }
224 
225     private static void assertPathsResolvedForRelativeUrls( final DecorationModel childModel )
226     {
227         assertEquals( "check left banner href", "../banner/left", childModel.getBannerLeft().getHref() );
228         assertEquals( "check left banner image", "../images/jakarta-logo.gif",
229                 childModel.getBannerLeft().getSrc() );
230 
231         assertEquals( "check right banner href", "../banner/right/", childModel.getBannerRight().getHref() );
232         assertEquals( "check right banner image", "../commons/images/logo.png",
233                 childModel.getBannerRight().getSrc() );
234 
235         Logo poweredBy = childModel.getPoweredBy().get( 0 );
236         assertEquals( "check powered by logo href", "../tomcat", poweredBy.getHref() );
237         assertEquals( "check powered by logo image", "../tomcat/logo.gif", poweredBy.getImg() );
238 
239         LinkItem breadcrumb = childModel.getBody().getBreadcrumbs().get( 0 );
240         assertEquals( "check breadcrumb href", "../apache", breadcrumb.getHref() );
241 
242         LinkItem link = childModel.getBody().getLinks().get( 0 );
243         assertEquals( "check link href", "../bouncycastle/", link.getHref() );
244 
245         Menu menu = childModel.getBody().getMenus().get( 0 );
246         LinkItem menuItem = menu.getItems().get( 0 );
247         assertEquals( "check menu item href", "../special/", menuItem.getHref() );
248     }
249 
250     /**
251      *
252      * @throws IOException
253      * @throws XmlPullParserException
254      */
255     @Test
256     public void testPathsResolvedForSubsiteUrls()
257         throws IOException, XmlPullParserException
258     {
259         DecorationModel parentModel = readModel( "subsite-urls.xml" );
260         DecorationModel childModel = readModel( "empty.xml" );
261 
262         assembler.assembleModelInheritance( NAME, childModel, parentModel, "http://maven.apache.org/doxia/",
263                                             "http://maven.apache.org" );
264         assembler.resolvePaths( childModel, "http://maven.apache.org/doxia" );
265 
266         assertPathsResolvedForSubsiteUrls( childModel );
267 
268         // same with scp url, DOXIASITETOOLS-47
269         childModel = readModel( "empty.xml" );
270         assembler.assembleModelInheritance( NAME, childModel, parentModel, "scp://people.apache.org/doxia",
271                                             "scp://people.apache.org" );
272         assembler.resolvePaths( childModel, "http://maven.apache.org/doxia" );
273         assertPathsResolvedForSubsiteUrls( childModel );
274 
275         assertEquals( "Modified parent!", readModel( "subsite-urls.xml" ), parentModel );
276     }
277 
278     private static void assertPathsResolvedForSubsiteUrls( final DecorationModel childModel )
279     {
280         assertEquals( "check left banner href", "../banner/left", childModel.getBannerLeft().getHref() );
281         assertEquals( "check left banner image", "../images/jakarta-logo.gif",
282                 childModel.getBannerLeft().getSrc() );
283 
284         assertEquals( "check right banner href", "../banner/right/", childModel.getBannerRight().getHref() );
285         assertEquals( "check right banner image", "../commons/images/logo.png",
286                 childModel.getBannerRight().getSrc() );
287 
288         Logo poweredBy = childModel.getPoweredBy().get( 0 );
289         assertEquals( "check powered by logo href", "../tomcat", poweredBy.getHref() );
290         assertEquals( "check powered by logo image", "../tomcat/logo.gif", poweredBy.getImg() );
291 
292         LinkItem breadcrumb = childModel.getBody().getBreadcrumbs().get( 0 );
293         assertEquals( "check breadcrumb href", "../apache", breadcrumb.getHref() );
294 
295         LinkItem link = childModel.getBody().getLinks().get( 0 );
296         assertEquals( "check link href", "../bouncycastle/", link.getHref() );
297 
298         Menu menu = childModel.getBody().getMenus().get( 0 );
299         LinkItem menuItem = menu.getItems().get( 0 );
300         assertEquals( "check menu item href", "../special/", menuItem.getHref() );
301     }
302 
303     /**
304      *
305      * @throws IOException
306      * @throws XmlPullParserException
307      */
308     @Test
309     public void testPathsResolvedForRelativeUrlsDepthOfTwo()
310         throws IOException, XmlPullParserException
311     {
312         DecorationModel parentModel = readModel( "relative-urls.xml" );
313         DecorationModel childModel = readModel( "empty.xml" );
314 
315         assembler.assembleModelInheritance( NAME, childModel, parentModel, "http://maven.apache.org/doxia/core",
316                                             "http://maven.apache.org" );
317         assertPathsResolvedForRelativeUrlsDepthOfTwo( childModel );
318 
319         // same with scp url, DOXIASITETOOLS-47
320         childModel = readModel( "empty.xml" );
321         assembler.assembleModelInheritance( NAME, childModel, parentModel, "scp://people.apache.org/doxia/core",
322                                             "scp://people.apache.org" );
323         assertPathsResolvedForRelativeUrlsDepthOfTwo( childModel );
324 
325         assertEquals( "Modified parent!", readModel( "relative-urls.xml" ), parentModel );
326     }
327 
328     private static void assertPathsResolvedForRelativeUrlsDepthOfTwo( final DecorationModel childModel )
329     {
330         assertEquals( "check left banner href", "../../banner/left", childModel.getBannerLeft().getHref() );
331         assertEquals( "check left banner image", "../../images/jakarta-logo.gif",
332                 childModel.getBannerLeft().getSrc() );
333 
334         assertEquals( "check right banner href", "../../banner/right/", childModel.getBannerRight().getHref() );
335         assertEquals( "check right banner image", "../../commons/images/logo.png",
336                 childModel.getBannerRight().getSrc() );
337 
338         Logo poweredBy = childModel.getPoweredBy().get( 0 );
339         assertEquals( "check powered by logo href", "../../tomcat", poweredBy.getHref() );
340         assertEquals( "check powered by logo image", "../../tomcat/logo.gif", poweredBy.getImg() );
341 
342         LinkItem breadcrumb = childModel.getBody().getBreadcrumbs().get( 0 );
343         assertEquals( "check breadcrumb href", "../../apache", breadcrumb.getHref() );
344 
345         LinkItem link = childModel.getBody().getLinks().get( 0 );
346         assertEquals( "check link href", "../../bouncycastle/", link.getHref() );
347 
348         Menu menu = childModel.getBody().getMenus().get( 0 );
349         LinkItem menuItem = menu.getItems().get( 0 );
350         assertEquals( "check menu item href", "../../special/", menuItem.getHref() );
351     }
352 
353     /**
354      *
355      * @throws IOException
356      * @throws XmlPullParserException
357      */
358     @Test
359     public void testPathsResolvedForReverseRelativeUrls()
360         throws IOException, XmlPullParserException
361     {
362         DecorationModel parentModel = readModel( "relative-urls.xml" );
363         DecorationModel childModel = readModel( "empty.xml" );
364 
365         assembler.assembleModelInheritance( NAME, childModel, parentModel, "http://maven.apache.org/",
366                                             "http://maven.apache.org/doxia/" );
367         assertPathsResolvedForReverseRelativeUrls( childModel );
368 
369         // same with scp url, DOXIASITETOOLS-47
370         childModel = readModel( "empty.xml" );
371         assembler.assembleModelInheritance( NAME, childModel, parentModel, "scp://people.apache.org/",
372                                             "scp://people.apache.org/doxia/" );
373         assertPathsResolvedForReverseRelativeUrls( childModel );
374 
375         assertEquals( "Modified parent!", readModel( "relative-urls.xml" ), parentModel );
376     }
377 
378     private static void assertPathsResolvedForReverseRelativeUrls( final DecorationModel childModel )
379     {
380         assertEquals( "check left banner href", "doxia/banner/left", childModel.getBannerLeft().getHref() );
381         assertEquals( "check left banner image", "doxia/images/jakarta-logo.gif",
382                 childModel.getBannerLeft().getSrc() );
383 
384         assertEquals( "check right banner href", "doxia/banner/right/", childModel.getBannerRight().getHref() );
385         assertEquals( "check right banner image", "doxia/commons/images/logo.png",
386                 childModel.getBannerRight().getSrc() );
387 
388         Logo poweredBy = childModel.getPoweredBy().get( 0 );
389         assertEquals( "check powered by logo href", "doxia/tomcat", poweredBy.getHref() );
390         assertEquals( "check powered by logo image", "doxia/tomcat/logo.gif", poweredBy.getImg() );
391 
392         LinkItem breadcrumb = childModel.getBody().getBreadcrumbs().get( 0 );
393         assertEquals( "check breadcrumb href", "doxia/apache", breadcrumb.getHref() );
394 
395         LinkItem link = childModel.getBody().getLinks().get( 0 );
396         assertEquals( "check link href", "doxia/bouncycastle/", link.getHref() );
397 
398         Menu menu = childModel.getBody().getMenus().get( 0 );
399         LinkItem menuItem = menu.getItems().get( 0 );
400         assertEquals( "check menu item href", "doxia/special/", menuItem.getHref() );
401     }
402 
403     /**
404      *
405      * @throws IOException
406      * @throws XmlPullParserException
407      */
408     @Test
409     public void testPathsResolvedForReverseRelativeUrlsDepthOfTwo()
410         throws IOException, XmlPullParserException
411     {
412         DecorationModel parentModel = readModel( "relative-urls.xml" );
413         DecorationModel childModel = readModel( "empty.xml" );
414 
415         assembler.assembleModelInheritance( NAME, childModel, parentModel, "http://maven.apache.org/",
416                                             "http://maven.apache.org/doxia/core/" );
417         assertPathsResolvedForReverseRelativeUrlsDepthOfTwo( childModel );
418 
419         // same with scp url, DOXIASITETOOLS-47
420         childModel = readModel( "empty.xml" );
421         assembler.assembleModelInheritance( NAME, childModel, parentModel, "scp://people.apache.org/",
422                                             "scp://people.apache.org/doxia/core/" );
423         assertPathsResolvedForReverseRelativeUrlsDepthOfTwo( childModel );
424 
425         assertEquals( "Modified parent!", readModel( "relative-urls.xml" ), parentModel );
426     }
427 
428     private static void assertPathsResolvedForReverseRelativeUrlsDepthOfTwo( final DecorationModel childModel )
429     {
430         assertEquals( "check left banner href", "doxia/core/banner/left", childModel.getBannerLeft().getHref() );
431         assertEquals( "check left banner image", "doxia/core/images/jakarta-logo.gif",
432                 childModel.getBannerLeft().getSrc() );
433 
434         assertEquals( "check right banner href", "doxia/core/banner/right/",
435                 childModel.getBannerRight().getHref() );
436         assertEquals( "check right banner image", "doxia/core/commons/images/logo.png",
437                 childModel.getBannerRight().getSrc() );
438 
439         Logo poweredBy = childModel.getPoweredBy().get( 0 );
440         assertEquals( "check powered by logo href", "doxia/core/tomcat", poweredBy.getHref() );
441         assertEquals( "check powered by logo image", "doxia/core/tomcat/logo.gif", poweredBy.getImg() );
442 
443         LinkItem breadcrumb = childModel.getBody().getBreadcrumbs().get( 0 );
444         assertEquals( "check breadcrumb href", "doxia/core/apache", breadcrumb.getHref() );
445 
446         LinkItem link = childModel.getBody().getLinks().get( 0 );
447         assertEquals( "check link href", "doxia/core/bouncycastle/", link.getHref() );
448 
449         Menu menu = childModel.getBody().getMenus().get( 0 );
450         LinkItem menuItem = menu.getItems().get( 0 );
451         assertEquals( "check menu item href", "doxia/core/special/", menuItem.getHref() );
452     }
453 
454     /**
455      *
456      * @throws IOException
457      * @throws XmlPullParserException
458      */
459     @Test
460     public void testPathsResolvedForUnrelatedRelativeUrls()
461         throws IOException, XmlPullParserException
462     {
463         DecorationModel parentModel = readModel( "relative-urls.xml" );
464         DecorationModel childModel = readModel( "empty.xml" );
465 
466         assembler.assembleModelInheritance( NAME, childModel, parentModel, "http://maven.apache.org",
467                                             "http://jakarta.apache.org" );
468         assertPathsResolvedForUnrelatedRelativeUrls( childModel );
469 
470         // same with scp url, DOXIASITETOOLS-47
471         childModel = readModel( "empty.xml" );
472         assembler.assembleModelInheritance( NAME, childModel, parentModel, "scp://people.apache.org/",
473                                             "http://jakarta.apache.org" );
474         assertPathsResolvedForUnrelatedRelativeUrls( childModel );
475 
476         assertEquals( "Modified parent!", readModel( "relative-urls.xml" ), parentModel );
477     }
478 
479     private static void assertPathsResolvedForUnrelatedRelativeUrls( final DecorationModel childModel )
480     {
481         assertEquals( "check left banner href", "http://jakarta.apache.org/banner/left",
482                 childModel.getBannerLeft().getHref() );
483         assertEquals( "check left banner image", "http://jakarta.apache.org/images/jakarta-logo.gif",
484                 childModel.getBannerLeft().getSrc() );
485 
486         assertEquals( "check right banner href", "http://jakarta.apache.org/banner/right/",
487                 childModel.getBannerRight().getHref() );
488         assertEquals( "check right banner image", "http://jakarta.apache.org/commons/images/logo.png",
489                 childModel.getBannerRight().getSrc() );
490 
491         Logo poweredBy = childModel.getPoweredBy().get( 0 );
492         assertEquals( "check powered by logo href", "http://jakarta.apache.org/tomcat", poweredBy.getHref() );
493         assertEquals( "check powered by logo image", "http://jakarta.apache.org/tomcat/logo.gif",
494                 poweredBy.getImg() );
495 
496         LinkItem breadcrumb = childModel.getBody().getBreadcrumbs().get( 0 );
497         assertEquals( "check breadcrumb href", "http://jakarta.apache.org/apache", breadcrumb.getHref() );
498 
499         LinkItem link = childModel.getBody().getLinks().get( 0 );
500         assertEquals( "check link href", "http://jakarta.apache.org/bouncycastle/", link.getHref() );
501 
502         Menu menu = childModel.getBody().getMenus().get( 0 );
503         LinkItem menuItem = menu.getItems().get( 0 );
504         assertEquals( "check menu item href", "http://jakarta.apache.org/special/", menuItem.getHref() );
505     }
506 
507     /**
508      *
509      * @throws IOException
510      * @throws XmlPullParserException
511      */
512     @Test
513     public void testNullParent()
514         throws IOException, XmlPullParserException
515     {
516         DecorationModel childModel = readModel( "empty.xml" );
517 
518         assembler.assembleModelInheritance( NAME, childModel, null, "http://maven.apache.org/doxia",
519                                             "http://maven.apache.org" );
520         DecorationModel mergedModel = readModel( "empty.xml" );
521 
522         assertEquals( "Check result", mergedModel, childModel );
523 
524         // same with scp url, DOXIASITETOOLS-47
525         childModel = readModel( "empty.xml" );
526         assembler.assembleModelInheritance( NAME, childModel, null, "scp://people.apache.org/doxia",
527                                             "scp://people.apache.org" );
528         assertEquals( "Check scp result", mergedModel, childModel );
529     }
530 
531     /**
532      *
533      * @throws IOException
534      * @throws XmlPullParserException
535      */
536     @Test
537     public void testFullyPopulatedChild()
538         throws IOException, XmlPullParserException
539     {
540         DecorationModel childModel = readModel( "fully-populated-child.xml" );
541         DecorationModel parentModel = readModel( "fully-populated-child.xml" );
542 
543         assembler.assembleModelInheritance( NAME, childModel, parentModel, "http://foo.apache.org/doxia",
544                                             "http://foo.apache.org" );
545         DecorationModel mergedModel = readModel( "fully-populated-child.xml" );
546 
547         assertEquals( "Check result", mergedModel, childModel );
548 
549         // same with scp url, DOXIASITETOOLS-47
550         childModel = readModel( "fully-populated-child.xml" );
551         assembler.assembleModelInheritance( NAME, childModel, parentModel, "scp://foo.apache.org/doxia",
552                                             "scp://foo.apache.org" );
553         assertEquals( "Check scp result", mergedModel, childModel );
554 
555         assertEquals( "Modified parent!", readModel( "fully-populated-child.xml" ), parentModel );
556     }
557 
558     /**
559      *
560      * @throws IOException
561      * @throws XmlPullParserException
562      */
563     @Test
564     public void testFullyPopulatedParentAndEmptyChild()
565         throws IOException, XmlPullParserException
566     {
567         DecorationModel childModel = readModel( "empty.xml" );
568         DecorationModel parentModel = readModel( "fully-populated-child.xml" );
569 
570         assembler.assembleModelInheritance( NAME, childModel, parentModel, "http://maven.apache.org/doxia",
571                                             "http://maven.apache.org" );
572 
573         DecorationModel unresolvedModel = readModel( "fully-populated-unresolved.xml" );
574         assertEquals( "Check result", unresolvedModel, childModel );
575 
576         assembler.resolvePaths( childModel, "http://maven.apache.org/doxia" );
577         DecorationModel mergedModel = readModel( "fully-populated-merged.xml" );
578 
579         assertEquals( "Check result", mergedModel, childModel );
580 
581         // same with scp url, DOXIASITETOOLS-47
582         childModel = readModel( "empty.xml" );
583         assembler.assembleModelInheritance( NAME, childModel, parentModel, "scp://maven.apache.org/doxia",
584                                             "scp://maven.apache.org" );
585         assembler.resolvePaths( childModel, "http://maven.apache.org/doxia" );
586         assertEquals( "Check scp result", mergedModel, childModel );
587 
588         assertEquals( "Modified parent!", readModel( "fully-populated-child.xml" ), parentModel );
589     }
590 
591     /**
592      *
593      * @throws IOException
594      * @throws XmlPullParserException
595      */
596     @Test
597     public void testResolvingAllExternalUrls()
598         throws IOException, XmlPullParserException
599     {
600         DecorationModel model = readModel( "external-urls.xml" );
601 
602         assembler.resolvePaths( model, "http://foo.com/" );
603         DecorationModel mergedModel = readModel( "external-urls.xml" );
604 
605         assertEquals( "Check result", mergedModel, model );
606     }
607 
608     /**
609      *
610      * @throws IOException
611      * @throws XmlPullParserException
612      */
613     @Test
614     public void testResolvingAllRelativeUrls()
615         throws IOException, XmlPullParserException
616     {
617         DecorationModel model = readModel( "relative-urls.xml" );
618 
619         assembler.resolvePaths( model, "http://foo.com/" );
620 
621         DecorationModel resolvedModel = readModel( "relative-urls-resolved.xml" );
622 
623         assertEquals( "Check result", resolvedModel, model );
624     }
625 
626     /**
627      *
628      * @throws IOException
629      * @throws XmlPullParserException
630      */
631     @Test
632     public void testResolvingAllSiteUrls()
633         throws IOException, XmlPullParserException
634     {
635         DecorationModel model = readModel( "subsite-urls.xml" );
636 
637         assembler.resolvePaths( model, "http://maven.apache.org/" );
638 
639         DecorationModel resolvedModel = readModel( "relative-urls-resolved.xml" );
640         assertEquals( "Check result", resolvedModel, model );
641     }
642 
643 /* [MSITE-62] This is to test the ../ relative paths, which I am inclined not to use
644     public void testResolvingAllSiteChildUrls()
645         throws IOException, XmlPullParserException
646     {
647         DecorationModel model = readModel( "subsite-urls.xml" );
648 
649         assembler.resolvePaths( model, "http://maven.apache.org/foo" );
650 
651         DecorationModel resolvedModel = readModel( "subsite-relative-urls-resolved.xml" );
652         assertEquals( "Check result", resolvedModel, model );
653     }
654 
655     public void testResolvingAllSiteChildUrlsMultipleLevels()
656         throws IOException, XmlPullParserException
657     {
658         DecorationModel model = readModel( "subsite-urls.xml" );
659 
660         assembler.resolvePaths( model, "http://maven.apache.org/banner/right" );
661 
662         DecorationModel resolvedModel = readModel( "subsite-relative-urls-multiple-resolved.xml" );
663         assertEquals( "Check result", resolvedModel, model );
664     }
665 
666     public void testResolvingAllSiteChildFilesystemUrls()
667         throws IOException, XmlPullParserException
668     {
669         DecorationModel model = readModel( "subsite-urls-file.xml" );
670 
671         assembler.resolvePaths( model, "file://localhost/www/maven.apache.org/foo" );
672 
673         DecorationModel resolvedModel = readModel( "subsite-relative-urls-resolved.xml" );
674         assertEquals( "Check result", resolvedModel, model );
675     }
676 
677 */
678 
679     /**
680      *
681      * @throws IOException
682      * @throws XmlPullParserException
683      */
684     @Test
685     public void testResolvingEmptyDescriptor()
686         throws IOException, XmlPullParserException
687     {
688         DecorationModel model = readModel( "empty.xml" );
689         assembler.resolvePaths( model, "http://maven.apache.org" );
690         DecorationModel mergedModel = readModel( "empty.xml" );
691 
692         assertEquals( "Check result", mergedModel, model );
693     }
694 
695     /**
696      *
697      */
698     @Test
699     public void testDuplicateParentElements()
700     {
701         DecorationModel model = new DecorationModel();
702         model.setBody( new Body() );
703         model.getBody().addLink( createLinkItem( "Foo", "http://foo.apache.org" ) );
704         model.getBody().addLink( createLinkItem( "Foo", "http://foo.apache.org" ) );
705 
706         model.addPoweredBy( createLogo( "Foo", "http://foo.apache.org", "http://foo.apache.org/foo.jpg" ) );
707         model.addPoweredBy( createLogo( "Foo", "http://foo.apache.org", "http://foo.apache.org/foo.jpg" ) );
708 
709         DecorationModel child = new DecorationModel();
710         assembler.assembleModelInheritance( NAME, child, model, "http://maven.apache.org/doxia",
711                                             "http://maven.apache.org" );
712 
713         assertEquals( "Check size", 1, child.getBody().getLinks().size() );
714         assertEquals( "Check item", createLinkItem( "Foo", "http://foo.apache.org" ),
715                 child.getBody().getLinks().get( 0 ) );
716 
717         assertEquals( "Check size", 1, child.getPoweredBy().size() );
718         assertEquals( "Check item",
719                 createLogo( "Foo", "http://foo.apache.org", "http://foo.apache.org/foo.jpg" ),
720                 child.getPoweredBy().get( 0 ) );
721     }
722 
723     /**
724      *
725      */
726     @Test
727     public void testDuplicateChildElements()
728     {
729         DecorationModel model = new DecorationModel();
730         model.setBody( new Body() );
731         model.getBody().addLink( createLinkItem( "Foo", "http://foo.apache.org" ) );
732         model.getBody().addLink( createLinkItem( "Foo", "http://foo.apache.org" ) );
733 
734         model.addPoweredBy( createLogo( "Foo", "http://foo.apache.org", "http://foo.apache.org/foo.jpg" ) );
735         model.addPoweredBy( createLogo( "Foo", "http://foo.apache.org", "http://foo.apache.org/foo.jpg" ) );
736 
737         DecorationModel parent = new DecorationModel();
738         assembler.assembleModelInheritance( NAME, model, parent, "http://maven.apache.org/doxia",
739                                             "http://maven.apache.org" );
740 
741         assertEquals( "Check size", 1, model.getBody().getLinks().size() );
742         assertEquals( "Check item", createLinkItem( "Foo", "http://foo.apache.org" ),
743                 model.getBody().getLinks().get( 0 ) );
744 
745         assertEquals( "Check size", 1, model.getPoweredBy().size() );
746         assertEquals( "Check item",
747                 createLogo( "Foo", "http://foo.apache.org", "http://foo.apache.org/foo.jpg" ),
748                 model.getPoweredBy().get( 0 ) );
749 
750         assertEquals( "Modified parent!", new DecorationModel(), parent );
751     }
752 
753     /**
754      *
755      */
756     @Test
757     public void testBadHref()
758     {
759         final DecorationModel model = new DecorationModel();
760         model.setBody( new Body() );
761         model.getBody().addBreadcrumb( createLinkItem( "Foo", "http://foo.apache.org/${property}" ) );
762         assembler.resolvePaths( model, "http://foo.apache.org" );
763         assertEquals( "Check size", 1, model.getBody().getBreadcrumbs().size() );
764         assertEquals( "Check item", createLinkItem( "Foo", "http://foo.apache.org/${property}" ),
765                 model.getBody().getBreadcrumbs().get( 0 ) );
766     }
767 
768     /**
769      *
770      */
771     @Test
772     public void testBreadcrumbWithoutHref()
773     {
774         DecorationModel model = new DecorationModel();
775         model.setBody( new Body() );
776         model.getBody().addBreadcrumb( createLinkItem( "Foo", null ) );
777         assembler.resolvePaths( model, "http://foo.apache.org" );
778         assertEquals( "Check size", 1, model.getBody().getBreadcrumbs().size() );
779         assertEquals( "Check item", createLinkItem( "Foo", null ), model.getBody().getBreadcrumbs().get( 0 ) );
780     }
781 
782     /**
783      *
784      */
785     @Test
786     public void testBreadcrumbs()
787     {
788         String parentHref = "http://parent.com/index.html";
789 
790         final DecorationModel parent = new DecorationModel();
791         parent.setBody( new Body() );
792         parent.getBody().addBreadcrumb( createLinkItem( "Parent", parentHref ) );
793 
794         DecorationModel child = new DecorationModel();
795         assembler.assembleModelInheritance( "childName", child, parent,
796                 "http://parent.com/child", "http://parent.com" );
797         assertBreadcrumbsCorrect( child.getBody().getBreadcrumbs(), "childName", parentHref );
798 
799 
800         // same with trailing slash
801         child = new DecorationModel();
802         assembler.assembleModelInheritance( "childName", child, parent,
803                 "http://parent.com/child/", "http://parent.com/" );
804         assertBreadcrumbsCorrect( child.getBody().getBreadcrumbs(), "childName", parentHref );
805 
806         // now mixed
807         child = new DecorationModel();
808         assembler.assembleModelInheritance( "childName", child, parent,
809                 "http://parent.com/child/", "http://parent.com" );
810         assertBreadcrumbsCorrect( child.getBody().getBreadcrumbs(), "childName", parentHref );
811 
812         // and other way round
813         child = new DecorationModel();
814         assembler.assembleModelInheritance( "childName", child, parent,
815                 "http://parent.com/child", "http://parent.com/" );
816         assertBreadcrumbsCorrect( child.getBody().getBreadcrumbs(), "childName", parentHref );
817 
818 
819         // now with child breadcrumb
820         child = new DecorationModel();
821         child.setBody( new Body() );
822         child.getBody().addBreadcrumb( createLinkItem( "Child", "index.html" ) );
823         assembler.assembleModelInheritance( "childName", child, parent,
824                 "http://parent.com/child/", "http://parent.com/" );
825         assertBreadcrumbsCorrect( child.getBody().getBreadcrumbs(), "Child", parentHref );
826 
827 
828         // now with file url
829         parentHref = "file://parent.com/index.html";
830         ( parent.getBody().getBreadcrumbs().get( 0 ) ).setHref( parentHref );
831         child = new DecorationModel();
832         assembler.assembleModelInheritance( "childName", child, parent,
833                 "file://parent.com/child/", "file://parent.com/" );
834         assertBreadcrumbsCorrect( child.getBody().getBreadcrumbs(), "childName", parentHref );
835 
836 
837         // now with scp url
838         parentHref = "scp://parent.com/index.html";
839         ( parent.getBody().getBreadcrumbs().get( 0 ) ).setHref( parentHref );
840         child = new DecorationModel();
841         assembler.assembleModelInheritance( "childName", child, parent,
842                 "scp://parent.com/child/", "scp://parent.com/" );
843         assertBreadcrumbsCorrect( child.getBody().getBreadcrumbs(), "childName", parentHref );
844     }
845 
846     private static void assertBreadcrumbsCorrect( final List<LinkItem> breadcrumbs, final String childName,
847             final String parentHref )
848     {
849         assertEquals( "Check size", 2, breadcrumbs.size() );
850         assertEquals( "Check parent item", createLinkItem( "Parent", parentHref ), breadcrumbs.get( 0 ) );
851         assertEquals( "Check child item", createLinkItem( childName, "index.html" ), breadcrumbs.get( 1 ) );
852     }
853 
854     /**
855      * https://issues.apache.org/jira/browse/DOXIASITETOOLS-62
856      */
857     @Test
858     public void testBreadcrumbCutParentAfterDuplicate()
859     {
860         DecorationModel child = new DecorationModel(); // B > E
861         child.setBody( new Body() );
862         child.getBody().addBreadcrumb( createLinkItem( "B", null ) );
863         child.getBody().addBreadcrumb( createLinkItem( "E", null ) );
864 
865         DecorationModel parent = new DecorationModel(); // A > B > C > D
866         parent.setBody( new Body() );
867         parent.getBody().addBreadcrumb( createLinkItem( "A", null ) );
868         parent.getBody().addBreadcrumb( createLinkItem( "B", null ) );
869         parent.getBody().addBreadcrumb( createLinkItem( "C", null ) );
870         parent.getBody().addBreadcrumb( createLinkItem( "D", null ) );
871 
872         assembler.assembleModelInheritance( NAME, child, parent, "http://maven.apache.org/doxia",
873                                             "http://maven.apache.org" );
874 
875         final List<LinkItem> breadcrumbs = child.getBody().getBreadcrumbs(); // expected: A > B > E
876         assertEquals( "Check size", 3, breadcrumbs.size() );
877         assertEquals( "Check item", createLinkItem( "A", null ), breadcrumbs.get( 0 ) );
878         assertEquals( "Check item", createLinkItem( "B", null ), breadcrumbs.get( 1 ) );
879         assertEquals( "Check item", createLinkItem( "E", null ), breadcrumbs.get( 2 ) );
880     }
881 
882     /**
883      *
884      */
885     @Test
886     public void testBannerWithoutHref()
887     {
888         DecorationModel model = new DecorationModel();
889         model.setBody( new Body() );
890 
891         Banner banner = createBanner( "Left", null, "/images/src.gif", "alt" );
892 
893         model.setBannerLeft( banner );
894 
895         assembler.resolvePaths( model, "http://foo.apache.org" );
896 
897         assertEquals( "Check banner", createBanner( "Left", null, "images/src.gif", "alt" ),
898                 model.getBannerLeft() );
899     }
900 
901     /**
902      *
903      */
904     @Test
905     public void testLogoWithoutImage()
906     {
907         // This should actually be validated in the model, it doesn't really make sense
908         DecorationModel model = new DecorationModel();
909         model.setBody( new Body() );
910         model.addPoweredBy( createLogo( "Foo", "http://foo.apache.org", null ) );
911         assembler.resolvePaths( model, "http://foo.apache.org" );
912         assertEquals( "Check size", 1, model.getPoweredBy().size() );
913         assertEquals( "Check item", createLogo( "Foo", "./", null ), model.getPoweredBy().get( 0 ) );
914     }
915 
916     private static Banner createBanner( String name, String href, String src, String alt )
917     {
918         Banner banner = new Banner();
919         banner.setName( name );
920         banner.setHref( href );
921         banner.setSrc( src );
922         banner.setAlt( alt );
923         return banner;
924     }
925 
926     private Logo createLogo( String name, String href, String img )
927     {
928         Logo logo = new Logo();
929         logo.setHref( href );
930         logo.setImg( img );
931         logo.setName( name );
932         return logo;
933     }
934 
935     private static LinkItem createLinkItem( String name, String href )
936     {
937         LinkItem item = new LinkItem();
938         item.setName( name );
939         item.setHref( href );
940         return item;
941     }
942 
943     private DecorationModel readModel( String name )
944         throws IOException, XmlPullParserException
945     {
946         Reader reader = null;
947         try
948         {
949             reader = ReaderFactory.newXmlReader( getClass().getResourceAsStream( "/" + name ) );
950             return new DecorationXpp3Reader().read( reader );
951         }
952         finally
953         {
954             IOUtil.close( reader );
955         }
956     }
957 }