View Javadoc

1   /*
2    *   Copyright (C) Christian Schulte, 2005-206
3    *   All rights reserved.
4    *
5    *   Redistribution and use in source and binary forms, with or without
6    *   modification, are permitted provided that the following conditions
7    *   are met:
8    *
9    *     o Redistributions of source code must retain the above copyright
10   *       notice, this list of conditions and the following disclaimer.
11   *
12   *     o Redistributions in binary form must reproduce the above copyright
13   *       notice, this list of conditions and the following disclaimer in
14   *       the documentation and/or other materials provided with the
15   *       distribution.
16   *
17   *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18   *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19   *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20   *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21   *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   *
28   *   $JOMC: SectionEditorTest.java 3934 2011-11-12 00:42:08Z schulte2005 $
29   *
30   */
31  package org.jomc.util.test;
32  
33  import java.io.BufferedReader;
34  import java.io.IOException;
35  import java.io.InputStream;
36  import java.io.StringReader;
37  import org.apache.commons.io.IOUtils;
38  import org.jomc.util.LineEditor;
39  import org.jomc.util.Section;
40  import org.jomc.util.SectionEditor;
41  import org.junit.Test;
42  import static org.junit.Assert.assertEquals;
43  import static org.junit.Assert.assertFalse;
44  import static org.junit.Assert.assertNotNull;
45  import static org.junit.Assert.assertTrue;
46  import static org.junit.Assert.fail;
47  
48  /**
49   * Test cases for class {@code org.jomc.util.SectionEditor}.
50   *
51   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
52   * @version $JOMC: SectionEditorTest.java 3934 2011-11-12 00:42:08Z schulte2005 $
53   */
54  public class SectionEditorTest extends LineEditorTest
55  {
56  
57      /** Constant to prefix relative resource names with. */
58      private static final String ABSOLUTE_RESOURCE_NAME_PREFIX = "/org/jomc/util/test/";
59  
60      /** Creates a new {@code SectionEditorTest} instance. */
61      public SectionEditorTest()
62      {
63          super();
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public SectionEditor getLineEditor()
69      {
70          return (SectionEditor) super.getLineEditor();
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      protected SectionEditor newLineEditor()
76      {
77          return new SectionEditor()
78          {
79  
80              @Override
81              protected void editSection( final Section section ) throws IOException
82              {
83                  super.editSection( section );
84  
85                  if ( section.getName() != null )
86                  {
87                      section.getHeadContent().append( section.getName() ).append( " Head" ).
88                          append( this.getLineSeparator() );
89  
90                      section.getTailContent().append( section.getName() ).append( " Tail" ).
91                          append( this.getLineSeparator() );
92  
93                  }
94              }
95  
96          };
97      }
98  
99      /** {@inheritDoc} */
100     @Override
101     protected SectionEditor newLineEditor( final LineEditor editor )
102     {
103         return new SectionEditor( editor );
104     }
105 
106     @Test
107     public final void testSectionEditor() throws Exception
108     {
109         String test = this.getResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "TestSections.txt" );
110         String expected =
111             convertLineSeparator( this.getResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "TestSectionsEdited.txt" ) );
112 
113         String edited = this.getLineEditor().edit( test );
114 
115         System.out.println( "TEST:" );
116         System.out.println( test );
117         System.out.println( "EXPECTED:" );
118         System.out.println( expected );
119         System.out.println( "EDITED:" );
120         System.out.println( edited );
121 
122         assertEquals( expected, edited );
123         assertTrue( this.getLineEditor().isSectionPresent( "1" ) );
124         assertTrue( this.getLineEditor().isSectionPresent( "1.1" ) );
125         assertTrue( this.getLineEditor().isSectionPresent( "1.1.1" ) );
126         assertTrue( this.getLineEditor().isSectionPresent( "1.1.1.1" ) );
127         assertTrue( this.getLineEditor().isSectionPresent( "1.2" ) );
128         assertTrue( this.getLineEditor().isSectionPresent( "1.3" ) );
129         assertTrue( this.getLineEditor().isSectionPresent( "1.4" ) );
130         assertTrue( this.getLineEditor().isSectionPresent( "2" ) );
131         assertTrue( this.getLineEditor().isSectionPresent( "3" ) );
132         assertTrue( this.getLineEditor().isSectionPresent( "4" ) );
133         assertTrue( this.getLineEditor().isSectionPresent( "5" ) );
134         assertTrue( this.getLineEditor().isSectionPresent( "6" ) );
135         assertTrue( this.getLineEditor().isSectionPresent( "7" ) );
136         assertTrue( this.getLineEditor().isSectionPresent( "8" ) );
137         assertTrue( this.getLineEditor().isSectionPresent( "9" ) );
138         assertTrue( this.getLineEditor().isSectionPresent( "10" ) );
139 
140         test = this.getResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "TestSectionsCont.txt" );
141         expected =
142             convertLineSeparator( this.getResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "TestSectionsContEdited.txt" ) );
143 
144         edited = this.getLineEditor().edit( test );
145 
146         System.out.println( "TEST:" );
147         System.out.println( test );
148         System.out.println( "EXPECTED:" );
149         System.out.println( expected );
150         System.out.println( "EDITED:" );
151         System.out.println( edited );
152 
153         assertEquals( expected, edited );
154         assertTrue( this.getLineEditor().isSectionPresent( "1" ) );
155         assertTrue( this.getLineEditor().isSectionPresent( "1.1" ) );
156         assertTrue( this.getLineEditor().isSectionPresent( "1.1.1" ) );
157         assertTrue( this.getLineEditor().isSectionPresent( "2" ) );
158         assertFalse( this.getLineEditor().isSectionPresent( "10" ) );
159 
160         this.assertUnmatchedSections( ABSOLUTE_RESOURCE_NAME_PREFIX + "UnmatchedSectionTest.txt" );
161         this.assertUnmatchedSections( ABSOLUTE_RESOURCE_NAME_PREFIX + "UnmatchedSectionsTest.txt" );
162         this.assertUnmatchedSections( ABSOLUTE_RESOURCE_NAME_PREFIX + "UnmatchedSubsectionTest.txt" );
163         this.assertUnmatchedSections( ABSOLUTE_RESOURCE_NAME_PREFIX + "UnmatchedSubsectionsTest.txt" );
164         this.assertUnmatchedSections( ABSOLUTE_RESOURCE_NAME_PREFIX + "MissingSectionStartTest.txt" );
165         this.assertUnmatchedSections( ABSOLUTE_RESOURCE_NAME_PREFIX + "MissingSectionsStartTest.txt" );
166 
167         final StringBuilder testNoSections = new StringBuilder();
168         final StringBuilder expectedNoSections = new StringBuilder();
169 
170         for ( int i = 1000; i >= 0; i-- )
171         {
172             testNoSections.append( "Hello editor.\n" );
173             expectedNoSections.append( "Hello editor." ).append( this.getLineEditor().getLineSeparator() );
174         }
175 
176         assertEquals( expectedNoSections.toString(), this.getLineEditor().edit( testNoSections.toString() ) );
177 
178         try
179         {
180             this.getLineEditor().edit( "SECTION-START[Test\nSECTION-END\n" );
181         }
182         catch ( final IOException e )
183         {
184             assertNotNull( e.getMessage() );
185             System.out.println( e.toString() );
186         }
187     }
188 
189     private void assertUnmatchedSections( final String resourceName ) throws Exception
190     {
191         try
192         {
193             this.getLineEditor().edit( this.getResource( resourceName ) );
194             fail( "Expected IOException not thrown for resource '" + resourceName + "'." );
195         }
196         catch ( final IOException e )
197         {
198             assertNotNull( e.getMessage() );
199             System.out.println( e.toString() );
200         }
201     }
202 
203     private static String convertLineSeparator( final String s ) throws IOException
204     {
205         BufferedReader reader = null;
206         boolean suppressExceptionOnClose = true;
207 
208         try
209         {
210             final StringBuilder b = new StringBuilder();
211             reader = new BufferedReader( new StringReader( s ) );
212 
213             String line;
214             while ( ( line = reader.readLine() ) != null )
215             {
216                 b.append( line ).append( System.getProperty( "line.separator", "\n" ) );
217             }
218 
219             suppressExceptionOnClose = false;
220             return b.toString();
221         }
222         finally
223         {
224             try
225             {
226                 if ( reader != null )
227                 {
228                     reader.close();
229                 }
230             }
231             catch ( final IOException e )
232             {
233                 if ( !suppressExceptionOnClose )
234                 {
235                     throw e;
236                 }
237             }
238         }
239     }
240 
241     private String getResource( final String resourceName ) throws IOException
242     {
243 
244         InputStream in = null;
245         boolean suppressExceptionOnClose = true;
246         assertTrue( resourceName.startsWith( "/" ) );
247 
248         try
249         {
250             in = this.getClass().getResourceAsStream( resourceName );
251             assertNotNull( "Resource '" + resourceName + "' not found.", in );
252             final String content = IOUtils.toString( in, this.getResourceEncoding() );
253             suppressExceptionOnClose = false;
254             return content;
255         }
256         finally
257         {
258             try
259             {
260                 if ( in != null )
261                 {
262                     in.close();
263                 }
264             }
265             catch ( final IOException e )
266             {
267                 if ( !suppressExceptionOnClose )
268                 {
269                     throw e;
270                 }
271             }
272         }
273     }
274 
275 }