View Javadoc
1   package org.apache.maven.plugins.pdf;
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.File;
23  import java.io.IOException;
24  import java.io.Reader;
25  
26  import org.apache.maven.doxia.document.DocumentAuthor;
27  import org.apache.maven.doxia.document.DocumentCover;
28  import org.apache.maven.doxia.document.DocumentMeta;
29  import org.apache.maven.doxia.document.DocumentModel;
30  import org.apache.maven.doxia.document.DocumentTOC;
31  import org.apache.maven.doxia.site.decoration.DecorationModel;
32  import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Reader;
33  import org.apache.maven.plugins.pdf.stubs.ModelBuilderMavenProjectStub;
34  
35  import org.codehaus.plexus.PlexusTestCase;
36  import org.codehaus.plexus.util.ReaderFactory;
37  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
38  
39  /**
40   *
41   * @author ltheussl
42   */
43  public class DocumentModelBuilderTest
44          extends PlexusTestCase
45  {
46      /**
47       * Test of getDocumentModel method, of class DocumentModelBuilder.
48       */
49      public void testEmptyDocumentModel()
50      {
51          DocumentModel model = new DocumentModelBuilder( null ).getDocumentModel();
52  
53          assertNotNull( model );
54          assertNull( model.getModelEncoding() );
55          assertEquals( "unnamed", model.getOutputName() );
56          assertNotNull( model.getCover() );
57          assertNotNull( model.getMeta() );
58          assertNotNull( model.getToc() );
59      }
60  
61      /**
62       * Test of getDocumentModel method, of class DocumentModelBuilder.
63       */
64      public void testGetDocumentModel()
65      {
66          DocumentModel model = new DocumentModelBuilder( new ModelBuilderMavenProjectStub() ).getDocumentModel();
67  
68          assertEquals( "UTF-8", model.getModelEncoding() );
69          assertEquals( "Test ArtifactId", model.getOutputName() );
70  
71          DocumentCover cover = model.getCover();
72          assertEquals( "Test Version", cover.getCoverVersion() );
73          assertEquals( "Test Name", cover.getProjectName() );
74          assertEquals( "Test Name", cover.getCoverTitle() );
75          assertEquals( "v. Test Version", cover.getCoverSubTitle() );
76          assertEquals( "Test Organization", cover.getCompanyName() );
77          assertEquals( 2, cover.getAuthors().size() );
78          assertFirstDocumentAuthor(cover.getAuthors().get( 0 ));
79  
80          DocumentMeta meta = model.getMeta();
81          assertEquals( "Test Description", meta.getDescription() );
82          assertEquals( 2, meta.getAuthors().size() );
83          assertFirstDocumentAuthor(meta.getAuthors().get( 0 ));
84          assertEquals( "Test Name", meta.getSubject() );
85          assertEquals( "Test Name", meta.getTitle() );
86  
87          DocumentTOC toc = model.getToc();
88          assertEquals( 0, toc.getItems().size() );
89      }
90  
91      /**
92       * Test of getDocumentModel method, of class DocumentModelBuilder.
93       * @throws Exception if something happens.
94       */
95      public void testGetDocumentModelWithSiteDescriptor()
96              throws Exception
97      {
98          File descriptorFile = new File( testBaseDir() + "src/site/", "model_builder_site.xml" );
99          DecorationModel dModel = getDecorationModelFromFile( descriptorFile );
100         DocumentModel model =
101                 new DocumentModelBuilder( new ModelBuilderMavenProjectStub(), dModel ).getDocumentModel();
102 
103         DocumentTOC toc = model.getToc();
104         assertEquals( 1, toc.getItems().size() );
105         assertEquals( "Intro", toc.getItems().get( 0 ).getName() );
106         assertEquals( "index.html", toc.getItems().get( 0 ).getRef() );
107     }
108 
109     private void assertFirstDocumentAuthor( DocumentAuthor author )
110     {
111         assertEquals( "dev name", author.getName() );
112         assertEquals( "dev@email", author.getEmail() );
113         assertEquals( "dev broetchengeber", author.getCompanyName() );
114         assertEquals( "dev main role, dev second role", author.getPosition() );
115     }
116 
117     private DecorationModel getDecorationModelFromFile( File descriptorFile )
118         throws IOException, XmlPullParserException
119     {
120         try ( Reader reader = ReaderFactory.newXmlReader( descriptorFile ) )
121         {
122             return new DecorationXpp3Reader().read( reader );
123         }
124     }
125 
126     private String testBaseDir()
127     {
128         return getBasedir() + "/src/test/resources/unit/pdf/";
129     }
130 }