View Javadoc
1   /*
2    *   Copyright (C) 2005 Christian Schulte <cs@schulte.it>
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 5291 2016-08-29 17:31:15Z schulte $
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 java.util.concurrent.ExecutorService;
38  import org.apache.commons.io.IOUtils;
39  import org.jomc.util.LineEditor;
40  import org.jomc.util.Section;
41  import org.jomc.util.SectionEditor;
42  import org.junit.After;
43  import org.junit.Test;
44  import static org.junit.Assert.assertEquals;
45  import static org.junit.Assert.assertFalse;
46  import static org.junit.Assert.assertNotNull;
47  import static org.junit.Assert.assertTrue;
48  import static org.junit.Assert.fail;
49  
50  /**
51   * Test cases for class {@code org.jomc.util.SectionEditor}.
52   *
53   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
54   * @version $JOMC: SectionEditorTest.java 5291 2016-08-29 17:31:15Z schulte $
55   */
56  public class SectionEditorTest extends LineEditorTest
57  {
58  
59      /**
60       * Constant to prefix relative resource names with.
61       */
62      private static final String ABSOLUTE_RESOURCE_NAME_PREFIX = "/org/jomc/util/test/";
63  
64      /**
65       * The {@code ExecutorService} backing the tests.
66       */
67      private ExecutorService executorService;
68  
69      /**
70       * Creates a new {@code SectionEditorTest} instance.
71       */
72      public SectionEditorTest()
73      {
74          super();
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public SectionEditor getLineEditor()
82      {
83          return (SectionEditor) super.getLineEditor();
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      protected SectionEditor newLineEditor()
91      {
92          final SectionEditor sectionEditor = new SectionEditor()
93          {
94  
95              @Override
96              protected void editSection( final Section section ) throws IOException
97              {
98                  super.editSection( section );
99  
100                 if ( section.getName() != null )
101                 {
102                     section.getHeadContent().append( section.getName() ).append( " Head" ).
103                         append( this.getLineSeparator() );
104 
105                     section.getTailContent().append( section.getName() ).append( " Tail" ).
106                         append( this.getLineSeparator() );
107 
108                 }
109             }
110 
111         };
112 
113         sectionEditor.setExecutorService( this.getExecutorService() );
114         return sectionEditor;
115     }
116 
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     protected SectionEditor newLineEditor( final LineEditor editor )
122     {
123         final SectionEditor sectionEditor = new SectionEditor( editor );
124         sectionEditor.setExecutorService( this.getExecutorService() );
125         return sectionEditor;
126     }
127 
128     /**
129      * Gets the {@code ExecutorService} backing the tests.
130      *
131      * @return The {@code ExecutorService} backing the tests.
132      *
133      * @see #newExecutorService()
134      * @since 1.10
135      */
136     public final ExecutorService getExecutorService()
137     {
138         if ( this.executorService == null )
139         {
140             this.executorService = this.newExecutorService();
141         }
142 
143         return this.executorService;
144     }
145 
146     /**
147      * Creates a new {@code ExecutorService} backing the tests.
148      *
149      * @return A new {@code ExecutorService} backing the tests, or {@code null}.
150      *
151      * @see #getExecutorService()
152      * @since 1.10
153      */
154     protected ExecutorService newExecutorService()
155     {
156         return null;
157     }
158 
159     /**
160      * Shuts down the {@code ExecutorService} backing the tests, if not {@code null}.
161      */
162     @After
163     public final void shutdown()
164     {
165         if ( this.executorService != null )
166         {
167             this.executorService.shutdown();
168             this.executorService = null;
169         }
170     }
171 
172     @Test
173     public final void testSectionEditor() throws Exception
174     {
175         String test = this.getResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "TestSections.txt" );
176         String expected =
177             convertLineSeparator( this.getResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "TestSectionsEdited.txt" ) );
178 
179         String edited = this.getLineEditor().edit( test );
180 
181         System.out.println( "TEST:" );
182         System.out.println( test );
183         System.out.println( "EXPECTED:" );
184         System.out.println( expected );
185         System.out.println( "EDITED:" );
186         System.out.println( edited );
187 
188         assertEquals( expected, edited );
189         assertTrue( this.getLineEditor().isSectionPresent( "1" ) );
190         assertTrue( this.getLineEditor().isSectionPresent( "1.1" ) );
191         assertTrue( this.getLineEditor().isSectionPresent( "1.1.1" ) );
192         assertTrue( this.getLineEditor().isSectionPresent( "1.1.1.1" ) );
193         assertTrue( this.getLineEditor().isSectionPresent( "1.2" ) );
194         assertTrue( this.getLineEditor().isSectionPresent( "1.3" ) );
195         assertTrue( this.getLineEditor().isSectionPresent( "1.4" ) );
196         assertTrue( this.getLineEditor().isSectionPresent( "2" ) );
197         assertTrue( this.getLineEditor().isSectionPresent( "3" ) );
198         assertTrue( this.getLineEditor().isSectionPresent( "4" ) );
199         assertTrue( this.getLineEditor().isSectionPresent( "5" ) );
200         assertTrue( this.getLineEditor().isSectionPresent( "6" ) );
201         assertTrue( this.getLineEditor().isSectionPresent( "7" ) );
202         assertTrue( this.getLineEditor().isSectionPresent( "8" ) );
203         assertTrue( this.getLineEditor().isSectionPresent( "9" ) );
204         assertTrue( this.getLineEditor().isSectionPresent( "10" ) );
205 
206         test = this.getResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "TestSectionsCont.txt" );
207         expected =
208             convertLineSeparator( this.getResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "TestSectionsContEdited.txt" ) );
209 
210         edited = this.getLineEditor().edit( test );
211 
212         System.out.println( "TEST:" );
213         System.out.println( test );
214         System.out.println( "EXPECTED:" );
215         System.out.println( expected );
216         System.out.println( "EDITED:" );
217         System.out.println( edited );
218 
219         assertEquals( expected, edited );
220         assertTrue( this.getLineEditor().isSectionPresent( "1" ) );
221         assertTrue( this.getLineEditor().isSectionPresent( "1.1" ) );
222         assertTrue( this.getLineEditor().isSectionPresent( "1.1.1" ) );
223         assertTrue( this.getLineEditor().isSectionPresent( "2" ) );
224         assertFalse( this.getLineEditor().isSectionPresent( "10" ) );
225 
226         this.assertUnmatchedSections( ABSOLUTE_RESOURCE_NAME_PREFIX + "UnmatchedSectionTest.txt" );
227         this.assertUnmatchedSections( ABSOLUTE_RESOURCE_NAME_PREFIX + "UnmatchedSectionsTest.txt" );
228         this.assertUnmatchedSections( ABSOLUTE_RESOURCE_NAME_PREFIX + "UnmatchedSubsectionTest.txt" );
229         this.assertUnmatchedSections( ABSOLUTE_RESOURCE_NAME_PREFIX + "UnmatchedSubsectionsTest.txt" );
230         this.assertUnmatchedSections( ABSOLUTE_RESOURCE_NAME_PREFIX + "MissingSectionStartTest.txt" );
231         this.assertUnmatchedSections( ABSOLUTE_RESOURCE_NAME_PREFIX + "MissingSectionsStartTest.txt" );
232 
233         final StringBuilder testNoSections = new StringBuilder();
234         final StringBuilder expectedNoSections = new StringBuilder();
235 
236         for ( int i = 1000; i >= 0; i-- )
237         {
238             testNoSections.append( "Hello editor.\n" );
239             expectedNoSections.append( "Hello editor." ).append( this.getLineEditor().getLineSeparator() );
240         }
241 
242         assertEquals( expectedNoSections.toString(), this.getLineEditor().edit( testNoSections.toString() ) );
243 
244         try
245         {
246             this.getLineEditor().edit( "SECTION-START[Test\nSECTION-END\n" );
247         }
248         catch ( final IOException e )
249         {
250             assertNotNull( e.getMessage() );
251             System.out.println( e.toString() );
252         }
253     }
254 
255     private void assertUnmatchedSections( final String resourceName ) throws Exception
256     {
257         try
258         {
259             this.getLineEditor().edit( this.getResource( resourceName ) );
260             fail( "Expected IOException not thrown for resource '" + resourceName + "'." );
261         }
262         catch ( final IOException e )
263         {
264             assertNotNull( e.getMessage() );
265             System.out.println( e.toString() );
266         }
267     }
268 
269     private static String convertLineSeparator( final String s ) throws IOException
270     {
271         BufferedReader reader = null;
272         boolean suppressExceptionOnClose = true;
273 
274         try
275         {
276             final StringBuilder b = new StringBuilder();
277             reader = new BufferedReader( new StringReader( s ) );
278 
279             String line;
280             while ( ( line = reader.readLine() ) != null )
281             {
282                 b.append( line ).append( System.getProperty( "line.separator", "\n" ) );
283             }
284 
285             suppressExceptionOnClose = false;
286             return b.toString();
287         }
288         finally
289         {
290             try
291             {
292                 if ( reader != null )
293                 {
294                     reader.close();
295                 }
296             }
297             catch ( final IOException e )
298             {
299                 if ( !suppressExceptionOnClose )
300                 {
301                     throw e;
302                 }
303             }
304         }
305     }
306 
307     private String getResource( final String resourceName ) throws IOException
308     {
309 
310         InputStream in = null;
311         boolean suppressExceptionOnClose = true;
312         assertTrue( resourceName.startsWith( "/" ) );
313 
314         try
315         {
316             in = this.getClass().getResourceAsStream( resourceName );
317             assertNotNull( "Resource '" + resourceName + "' not found.", in );
318             final String content = IOUtils.toString( in, this.getResourceEncoding() );
319             suppressExceptionOnClose = false;
320             return content;
321         }
322         finally
323         {
324             try
325             {
326                 if ( in != null )
327                 {
328                     in.close();
329                 }
330             }
331             catch ( final IOException e )
332             {
333                 if ( !suppressExceptionOnClose )
334                 {
335                     throw e;
336                 }
337             }
338         }
339     }
340 
341 }