View Javadoc

1   package org.apache.maven.doxia.document;
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  import java.io.Writer;
26  import java.util.Date;
27  import java.util.List;
28  
29  import org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Reader;
30  import org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Writer;
31  
32  import org.codehaus.plexus.PlexusTestCase;
33  import org.codehaus.plexus.util.IOUtil;
34  import org.codehaus.plexus.util.ReaderFactory;
35  import org.codehaus.plexus.util.WriterFactory;
36  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
37  
38  /**
39   * Test DocumentModel.
40   *
41   * @author ltheussl
42   */
43  public class DocumentModelTest
44      extends PlexusTestCase
45  {
46      /** ISO 8601 date format, i.e. <code>yyyy-MM-dd</code> **/
47      private static final java.text.DateFormat ISO_8601_FORMAT =
48              new java.text.SimpleDateFormat( "yyyy-MM-dd", java.util.Locale.ENGLISH );
49  
50      /**
51       * Test DocumentModel.
52       *
53       * @throws Exception if any.
54       */
55      public void testDocumentModel()
56              throws Exception
57      {
58          DocumentModel model = getModel();
59          verifyModel( model );
60  
61          DocumentModel copy = writeAndRecover( model );
62          verifyModel( copy );
63          assertTrue( copy.equals( model ) );
64      }
65  
66      private DocumentModel getModel()
67      {
68          DocumentModel model = new DocumentModel();
69  
70          model.setOutputName( "outputName" );
71          model.setModelEncoding( "ISO-8859-1" );
72          model.setCover( getDocumentCover() );
73          model.setToc( getDocumentToc() );
74          model.setMeta( getDocumentMeta() );
75  
76          return model ;
77      }
78  
79      private void verifyModel( DocumentModel model )
80      {
81          assertNotNull( model );
82          assertTrue( model.equals( model ) );
83          assertTrue ( model.hashCode() != 0 );
84          assertTrue( model.toString().length() > 0 );
85  
86          assertEquals( "outputName", model.getOutputName() );
87          assertEquals( "ISO-8859-1", model.getModelEncoding() );
88          verifyDocumentCover( model.getCover() );
89          verifyDocumentTOC( model.getToc() );
90          verifyDocumentMeta( model.getMeta() );
91      }
92  
93      private DocumentAuthor getAuthor( int i )
94      {
95          DocumentAuthor author = new DocumentAuthor();
96  
97          author.setCity( "city" + i );
98          author.setCompanyName( "companyName" + i );
99          author.setCountry( "country" + i );
100         author.setEmail( "email" + i );
101         author.setFaxNumber( "faxNumber" + i );
102         author.setName( "name" + i );
103         author.setFirstName( "firstName" + i );
104         author.setInitials( "initials" + i );
105         author.setLastName( "lastName" + i );
106         author.setPhoneNumber( "phoneNumber" + i );
107         author.setPosition( "position" + i );
108         author.setPostalCode( "postalCode" + i );
109         author.setState( "state" + i );
110         author.setStreet( "street" + i );
111         author.setTitle( "title" + i );
112 
113         return author;
114     }
115 
116     private void verifyAuthor( DocumentAuthor documentAuthor, int i )
117     {
118         assertEquals( "city" + i, documentAuthor.getCity() );
119         assertEquals( "companyName" + i, documentAuthor.getCompanyName() );
120         assertEquals( "country" + i, documentAuthor.getCountry() );
121         assertEquals( "email" + i, documentAuthor.getEmail() );
122         assertEquals( "faxNumber" + i, documentAuthor.getFaxNumber() );
123         assertEquals( "name" + i, documentAuthor.getName() );
124         assertEquals( "firstName" + i, documentAuthor.getFirstName() );
125         assertEquals( "initials" + i, documentAuthor.getInitials() );
126         assertEquals( "lastName" + i, documentAuthor.getLastName() );
127         assertEquals( "phoneNumber" + i, documentAuthor.getPhoneNumber() );
128         assertEquals( "position" + i, documentAuthor.getPosition() );
129         assertEquals( "postalCode" + i, documentAuthor.getPostalCode() );
130         assertEquals( "state" + i, documentAuthor.getState() );
131         assertEquals( "street" + i, documentAuthor.getStreet() );
132         assertEquals( "title" + i, documentAuthor.getTitle() );
133     }
134 
135     private DocumentCover getDocumentCover()
136     {
137         DocumentCover cover = new DocumentCover();
138         cover.addAuthor( getAuthor( 1 ) );
139         cover.setAuthor( "Author" );
140         cover.setCompanyLogo( "companyLogo" );
141         cover.setCompanyName( "companyName" );
142         cover.setCoverDate( new Date( 0L ) );
143         cover.setCoverSubTitle( "coverSubTitle" );
144         cover.setCoverTitle( "coverTitle" );
145         cover.setCoverType( "coverType" );
146         cover.setCoverVersion( "coverVersion" );
147         cover.setProjectLogo( "projectLogo" );
148         cover.setProjectName( "projectName" );
149 
150         return cover;
151     }
152 
153     private void verifyDocumentCover( DocumentCover cover )
154     {
155         List<DocumentAuthor> authors = cover.getAuthors();
156         assertEquals( 1, authors.size() );
157         verifyAuthor( authors.get( 0 ), 1 );
158 
159         assertEquals( "Author", cover.getAuthor() );
160         assertEquals( "companyLogo", cover.getCompanyLogo() );
161         assertEquals( "companyName", cover.getCompanyName() );
162         assertEquals( 0L, cover.getCoverDate().getTime() );
163         // the actual String depends on the timezone you're in
164         assertEquals( ISO_8601_FORMAT.format( new Date( 0L ) ), cover.getCoverdate() );
165         assertEquals( "coverSubTitle", cover.getCoverSubTitle() );
166         assertEquals( "coverTitle", cover.getCoverTitle() );
167         assertEquals( "coverType", cover.getCoverType() );
168         assertEquals( "coverVersion", cover.getCoverVersion() );
169         assertEquals( "projectLogo", cover.getProjectLogo() );
170         assertEquals( "projectName", cover.getProjectName() );
171     }
172 
173     private DocumentStatistic getDocumentStatistic()
174     {
175         DocumentStatistic statistic = new DocumentStatistic();
176 
177         statistic.setCharacterCount( 2L );
178         statistic.setDrawCount( 3L );
179         statistic.setFrameCount( 4L );
180         statistic.setImageCount( 5L );
181         statistic.setNonWhitespaceCharacterCount( 6L );
182         statistic.setObjectCount( 1L );
183         statistic.setOleObjectCount( 8L );
184         statistic.setPageCount( 7L );
185         statistic.setParagraphCount( 5L );
186         statistic.setRowCount( 6L );
187         statistic.setSentenceCount( 3L );
188         statistic.setSyllableCount( 10L );
189         statistic.setTableCount( 2L );
190         statistic.setWordCount( 11L );
191 
192         return statistic;
193     }
194 
195     private void verifyDocumentStatistic( DocumentStatistic documentStatistic )
196     {
197         assertEquals( 2L, documentStatistic.getCharacterCount() );
198         assertEquals( 3L, documentStatistic.getDrawCount() );
199         assertEquals( 4L, documentStatistic.getFrameCount() );
200         assertEquals( 5L, documentStatistic.getImageCount() );
201         assertEquals( 6L, documentStatistic.getNonWhitespaceCharacterCount() );
202         assertEquals( 1L, documentStatistic.getObjectCount() );
203         assertEquals( 8L, documentStatistic.getOleObjectCount() );
204         assertEquals( 7L, documentStatistic.getPageCount() );
205         assertEquals( 5L, documentStatistic.getParagraphCount() );
206         assertEquals( 6L, documentStatistic.getRowCount() );
207         assertEquals( 3L, documentStatistic.getSentenceCount() );
208         assertEquals( 10L, documentStatistic.getSyllableCount() );
209         assertEquals( 2L, documentStatistic.getTableCount() );
210         assertEquals( 11L, documentStatistic.getWordCount() );
211     }
212 
213     private DocumentHyperlinkBehaviour getDocumentHyperlinkBehaviour()
214     {
215         DocumentHyperlinkBehaviour hylink = new DocumentHyperlinkBehaviour();
216 
217         hylink.setTargetFrame( "targetFrame" );
218 
219         return hylink;
220     }
221 
222     private void verifyDocumentHyperlinkBehaviour( DocumentHyperlinkBehaviour hyperlinkBehaviour )
223     {
224         assertEquals( "targetFrame", hyperlinkBehaviour.getTargetFrame() );
225     }
226 
227     private DocumentMeta getDocumentMeta()
228     {
229         DocumentMeta meta = new DocumentMeta();
230 
231         meta.setAuthor( "author" );
232         meta.addAuthor( getAuthor( 2 ) );
233         meta.setConfidential( true );
234         meta.setCreationDate( new Date( 1L ) );
235         meta.setCreator( "creator" );
236         meta.setDate( new Date( 2L ) );
237         meta.setDescription( "description" );
238         meta.setDocumentStatistic( getDocumentStatistic() );
239         meta.setDraft( true );
240         meta.setEditingCycles( 15L );
241         meta.setEditingDuration( 3L );
242         meta.setGenerator( "generator" );
243         meta.setHyperlinkBehaviour( getDocumentHyperlinkBehaviour() );
244         meta.setInitialCreator( "initialCreator" );
245         meta.addKeyWord( "keyword1" );
246         meta.addKeyWord( "keyword2" );
247         meta.setLanguage( "language" );
248         meta.setPageSize( "pageSize" );
249         meta.setPrintDate( new Date( 4L ) );
250         meta.setPrintedBy( "printedBy" );
251         meta.setSubject( "subject" );
252         meta.setTemplate( getDocumentTemplate() );
253         meta.setTitle( "title" );
254 
255         return meta;
256     }
257 
258     private void verifyDocumentMeta( DocumentMeta meta )
259     {
260         assertEquals( "author", meta.getAuthor() );
261         List<DocumentAuthor> authors = meta.getAuthors();
262         assertEquals( 1, authors.size() );
263         verifyAuthor( authors.get( 0 ), 2 );
264 
265         assertTrue( meta.isConfidential() );
266         assertEquals( 1L, meta.getCreationDate().getTime() );
267         assertEquals( "creator", meta.getCreator() );
268         assertEquals( 2L, meta.getDate().getTime() );
269         assertEquals( "description", meta.getDescription() );
270         verifyDocumentStatistic( meta.getDocumentStatistic() );
271         assertTrue( meta.isDraft() );
272         assertEquals( 15L, meta.getEditingCycles() );
273         assertEquals( 3L, meta.getEditingDuration() );
274         assertEquals( "generator", meta.getGenerator() );
275         verifyDocumentHyperlinkBehaviour( meta.getHyperlinkBehaviour() );
276         assertEquals( "initialCreator", meta.getInitialCreator() );
277         assertEquals( "keyword1, keyword2", meta.getAllKeyWords() );
278         assertEquals( "language", meta.getLanguage() );
279         assertEquals( "pageSize", meta.getPageSize() );
280         assertEquals( 4L, meta.getPrintDate().getTime() );
281         assertEquals( "printedBy", meta.getPrintedBy() );
282         assertEquals( "subject", meta.getSubject() );
283         verifyDocumentTemplate( meta.getTemplate() );
284         assertEquals( "title", meta.getTitle() );
285     }
286 
287     private DocumentTemplate getDocumentTemplate()
288     {
289         DocumentTemplate template = new DocumentTemplate();
290 
291         template.setDate( new Date( 3L ) );
292         template.setHref( "href" );
293         template.setTitle( "title" );
294 
295         return template;
296     }
297 
298     private void verifyDocumentTemplate( DocumentTemplate template )
299     {
300         assertEquals( 3L, template.getDate().getTime() );
301         assertEquals( "href", template.getHref() );
302         assertEquals( "title", template.getTitle() );
303     }
304 
305     private DocumentTOC getDocumentToc()
306     {
307         DocumentTOCItem item1 = new DocumentTOCItem();
308         item1.setName( "First document" );
309         item1.setRef( "doc1.apt" );
310 
311         DocumentTOCItem item2 = new DocumentTOCItem();
312         item2.setName( "Second document" );
313         item2.setRef( "doc2.xml" );
314 
315         DocumentTOC toc = new DocumentTOC();
316         toc.setName( "name" );
317         toc.addItem( item1 );
318         toc.addItem( item2 );
319 
320         return toc;
321     }
322 
323     private void verifyDocumentTOC( DocumentTOC toc )
324     {
325         assertEquals( "name", toc.getName() );
326     }
327 
328     private DocumentModel writeAndRecover( DocumentModel model )
329             throws IOException
330     {
331         File dir = getTestFile( "target/test-output/xpp3/" );
332 
333         if ( !dir.exists() )
334         {
335             dir.mkdirs();
336         }
337 
338         File testFile = getTestFile( dir.getAbsolutePath(), "testModel.xml" );
339         Writer w = null;
340 
341         try
342         {
343             w = WriterFactory.newXmlWriter( testFile );
344             new DocumentXpp3Writer().write( w, model );
345         }
346         finally
347         {
348             IOUtil.close( w );
349         }
350 
351         DocumentModel documentModel;
352 
353         Reader reader = null;
354 
355         try
356         {
357             reader = ReaderFactory.newXmlReader( testFile );
358             documentModel = new DocumentXpp3Reader().read( reader );
359         }
360         catch ( XmlPullParserException e )
361         {
362             throw (IOException) new IOException( "Error parsing document descriptor" ).initCause( e );
363         }
364         finally
365         {
366             IOUtil.close( reader );
367         }
368 
369         return documentModel;
370     }
371 }