001    /*
002     *   Copyright (C) Christian Schulte, 2005-206
003     *   All rights reserved.
004     *
005     *   Redistribution and use in source and binary forms, with or without
006     *   modification, are permitted provided that the following conditions
007     *   are met:
008     *
009     *     o Redistributions of source code must retain the above copyright
010     *       notice, this list of conditions and the following disclaimer.
011     *
012     *     o Redistributions in binary form must reproduce the above copyright
013     *       notice, this list of conditions and the following disclaimer in
014     *       the documentation and/or other materials provided with the
015     *       distribution.
016     *
017     *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018     *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019     *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020     *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021     *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022     *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023     *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024     *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025     *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026     *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027     *
028     *   $JOMC: SectionEditorTest.java 3934 2011-11-12 00:42:08Z schulte2005 $
029     *
030     */
031    package org.jomc.util.test;
032    
033    import java.io.BufferedReader;
034    import java.io.IOException;
035    import java.io.InputStream;
036    import java.io.StringReader;
037    import org.apache.commons.io.IOUtils;
038    import org.jomc.util.LineEditor;
039    import org.jomc.util.Section;
040    import org.jomc.util.SectionEditor;
041    import org.junit.Test;
042    import static org.junit.Assert.assertEquals;
043    import static org.junit.Assert.assertFalse;
044    import static org.junit.Assert.assertNotNull;
045    import static org.junit.Assert.assertTrue;
046    import static org.junit.Assert.fail;
047    
048    /**
049     * Test cases for class {@code org.jomc.util.SectionEditor}.
050     *
051     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
052     * @version $JOMC: SectionEditorTest.java 3934 2011-11-12 00:42:08Z schulte2005 $
053     */
054    public class SectionEditorTest extends LineEditorTest
055    {
056    
057        /** Constant to prefix relative resource names with. */
058        private static final String ABSOLUTE_RESOURCE_NAME_PREFIX = "/org/jomc/util/test/";
059    
060        /** Creates a new {@code SectionEditorTest} instance. */
061        public SectionEditorTest()
062        {
063            super();
064        }
065    
066        /** {@inheritDoc} */
067        @Override
068        public SectionEditor getLineEditor()
069        {
070            return (SectionEditor) super.getLineEditor();
071        }
072    
073        /** {@inheritDoc} */
074        @Override
075        protected SectionEditor newLineEditor()
076        {
077            return new SectionEditor()
078            {
079    
080                @Override
081                protected void editSection( final Section section ) throws IOException
082                {
083                    super.editSection( section );
084    
085                    if ( section.getName() != null )
086                    {
087                        section.getHeadContent().append( section.getName() ).append( " Head" ).
088                            append( this.getLineSeparator() );
089    
090                        section.getTailContent().append( section.getName() ).append( " Tail" ).
091                            append( this.getLineSeparator() );
092    
093                    }
094                }
095    
096            };
097        }
098    
099        /** {@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    }