View Javadoc
1   package org.codehaus.plexus.util.xml;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.FileOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.io.Reader;
26  import java.io.StringWriter;
27  import java.io.Writer;
28  
29  import junit.framework.TestCase;
30  
31  import org.codehaus.plexus.util.IOUtil;
32  import org.codehaus.plexus.util.ReaderFactory;
33  import org.codehaus.plexus.util.StringUtils;
34  import org.codehaus.plexus.util.WriterFactory;
35  
36  /**
37   * Test the {@link XmlUtil} class.
38   *
39   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
40   * @version $Id$
41   */
42  public class XmlUtilTest
43      extends TestCase
44  {
45      private String basedir;
46  
47      public final String getBasedir()
48      {
49          if ( null == basedir )
50          {
51              basedir = System.getProperty( "basedir", new File( "" ).getAbsolutePath() );
52          }
53          return basedir;
54      }
55  
56      private File getTestOutputFile( String relPath )
57          throws IOException
58      {
59          final File file = new File( getBasedir(), relPath );
60          final File parentFile = file.getParentFile();
61          if ( !parentFile.isDirectory() && !parentFile.mkdirs() )
62          {
63              throw new IOException( "Could not create test directory " + parentFile );
64          }
65          return file;
66      }
67  
68      /** {@inheritDoc} */
69      protected void setUp()
70          throws Exception
71      {
72          super.setUp();
73      }
74  
75      /** {@inheritDoc} */
76      protected void tearDown()
77          throws Exception
78      {
79          super.tearDown();
80      }
81  
82      public void testPrettyFormatInputStreamOutputStream()
83          throws Exception
84      {
85          File testDocument = new File( getBasedir(), "src/test/resources/testDocument.xhtml" );
86          assertTrue( testDocument.exists() );
87  
88          InputStream is = null;
89          OutputStream os = null;
90          try
91          {
92              is = new FileInputStream( testDocument );
93              os = new FileOutputStream( getTestOutputFile( "target/test/prettyFormatTestDocumentOutputStream.xml" ) );
94  
95              assertNotNull( is );
96              assertNotNull( os );
97  
98              XmlUtil.prettyFormat( is, os );
99          }
100         finally
101         {
102             IOUtil.close( is );
103             IOUtil.close( os );
104         }
105     }
106 
107     public void testPrettyFormatReaderWriter()
108         throws Exception
109     {
110         File testDocument = new File( getBasedir(), "src/test/resources/testDocument.xhtml" );
111         assertTrue( testDocument.exists() );
112 
113         Reader reader = null;
114         Writer writer = null;
115         try
116         {
117             reader = ReaderFactory.newXmlReader( testDocument );
118             writer =
119                 WriterFactory.newXmlWriter( getTestOutputFile( "target/test/prettyFormatTestDocumentWriter.xml" ) );
120 
121             assertNotNull( reader );
122             assertNotNull( writer );
123 
124             XmlUtil.prettyFormat( reader, writer );
125         }
126         finally
127         {
128             IOUtil.close( reader );
129             IOUtil.close( writer );
130         }
131     }
132 
133     public void testPrettyFormatString()
134         throws Exception
135     {
136         File testDocument = new File( getBasedir(), "src/test/resources/testDocument.xhtml" );
137         assertTrue( testDocument.exists() );
138 
139         Reader reader = null;
140         Writer writer = null;
141         String content;
142         try
143         {
144             reader = ReaderFactory.newXmlReader( testDocument );
145             content = IOUtil.toString( reader );
146 
147             reader = ReaderFactory.newXmlReader( testDocument );
148             writer = new StringWriter();
149             XmlUtil.prettyFormat( reader, writer );
150         }
151         finally
152         {
153             IOUtil.close( reader );
154             IOUtil.close( writer );
155         }
156 
157         assertNotNull( content );
158 
159         int countEOL = StringUtils.countMatches( content, XmlUtil.DEFAULT_LINE_SEPARATOR );
160         assertTrue( countEOL < StringUtils.countMatches( writer.toString(), XmlUtil.DEFAULT_LINE_SEPARATOR ) );
161     }
162 
163     public void testPrettyFormatReaderWriter2()
164         throws Exception
165     {
166         File testDocument = new File( getBasedir(), "src/test/resources/test.xdoc.xhtml" );
167         assertTrue( testDocument.exists() );
168 
169         Reader reader = null;
170         Writer writer = null;
171         try
172         {
173             reader = ReaderFactory.newXmlReader( testDocument );
174             writer = WriterFactory.newXmlWriter( getTestOutputFile( "target/test/prettyFormatTestXdocWriter.xml" ) );
175 
176             assertNotNull( reader );
177             assertNotNull( writer );
178 
179             XmlUtil.prettyFormat( reader, writer );
180         }
181         finally
182         {
183             IOUtil.close( reader );
184             IOUtil.close( writer );
185         }
186     }
187 }