001package org.apache.maven.doxia.document; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.File; 023import java.io.IOException; 024import java.io.Reader; 025import java.io.Writer; 026import java.util.Date; 027import java.util.List; 028 029import org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Reader; 030import org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Writer; 031 032import org.codehaus.plexus.PlexusTestCase; 033import org.codehaus.plexus.util.IOUtil; 034import org.codehaus.plexus.util.ReaderFactory; 035import org.codehaus.plexus.util.WriterFactory; 036import org.codehaus.plexus.util.xml.pull.XmlPullParserException; 037 038/** 039 * Test DocumentModel. 040 * 041 * @author ltheussl 042 */ 043public class DocumentModelTest 044 extends PlexusTestCase 045{ 046 /** ISO 8601 date format, i.e. <code>yyyy-MM-dd</code> **/ 047 private static final java.text.DateFormat ISO_8601_FORMAT = 048 new java.text.SimpleDateFormat( "yyyy-MM-dd", java.util.Locale.ENGLISH ); 049 050 /** 051 * Test DocumentModel. 052 * 053 * @throws Exception if any. 054 */ 055 public void testDocumentModel() 056 throws Exception 057 { 058 DocumentModel model = getModel(); 059 verifyModel( model ); 060 061 DocumentModel copy = writeAndRecover( model ); 062 verifyModel( copy ); 063 assertTrue( copy.equals( model ) ); 064 } 065 066 private DocumentModel getModel() 067 { 068 DocumentModel model = new DocumentModel(); 069 070 model.setOutputName( "outputName" ); 071 model.setModelEncoding( "ISO-8859-1" ); 072 model.setCover( getDocumentCover() ); 073 model.setToc( getDocumentToc() ); 074 model.setMeta( getDocumentMeta() ); 075 076 return model ; 077 } 078 079 private void verifyModel( DocumentModel model ) 080 { 081 assertNotNull( model ); 082 assertTrue( model.equals( model ) ); 083 assertTrue ( model.hashCode() != 0 ); 084 assertTrue( model.toString().length() > 0 ); 085 086 assertEquals( "outputName", model.getOutputName() ); 087 assertEquals( "ISO-8859-1", model.getModelEncoding() ); 088 verifyDocumentCover( model.getCover() ); 089 verifyDocumentTOC( model.getToc() ); 090 verifyDocumentMeta( model.getMeta() ); 091 } 092 093 private DocumentAuthor getAuthor( int i ) 094 { 095 DocumentAuthor author = new DocumentAuthor(); 096 097 author.setCity( "city" + i ); 098 author.setCompanyName( "companyName" + i ); 099 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}