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: LineEditorTest.java 3846 2011-10-10 14:04:55Z schulte2005 $
029     *
030     */
031    package org.jomc.util.test;
032    
033    import org.jomc.util.LineEditor;
034    import org.jomc.util.test.support.NullEditor;
035    import org.junit.Test;
036    import static org.junit.Assert.assertEquals;
037    import static org.junit.Assert.assertNotNull;
038    import static org.junit.Assert.assertNull;
039    
040    /**
041     * Test cases for class {@code org.jomc.util.LineEditor}.
042     *
043     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
044     * @version $JOMC: LineEditorTest.java 3846 2011-10-10 14:04:55Z schulte2005 $
045     */
046    public class LineEditorTest
047    {
048    
049        /** Constant for the name of the system property holding the name of the encoding of resources backing the test. */
050        private static final String RESOURCE_ENCODING_PROPERTY_NAME = "jomc.test.resourceEncoding";
051    
052        /** The {@code LineEditor} instance tests are performed with. */
053        private LineEditor lineEditor;
054    
055        /** The name of the encoding to use when reading resources. */
056        private String resourceEncoding;
057    
058        /** Creates a new {@code LineEditorTest} instance. */
059        public LineEditorTest()
060        {
061            super();
062        }
063    
064        /**
065         * Gets the {@code LineEditor} instance tests are performed with.
066         *
067         * @return The {@code LineEditor} instance tests are performed with.
068         *
069         * @see #newLineEditor()
070         */
071        public LineEditor getLineEditor()
072        {
073            if ( this.lineEditor == null )
074            {
075                this.lineEditor = this.newLineEditor();
076            }
077    
078            return this.lineEditor;
079        }
080    
081        /**
082         * Gets a new {@code LineEditor} instance to test.
083         *
084         * @return A new {@code LineEditor} instance to test.
085         *
086         * @see #getLineEditor()
087         */
088        protected LineEditor newLineEditor()
089        {
090            return new LineEditor();
091        }
092    
093        /**
094         * Gets a new {@code LineEditor} instance to test taking an editor to chain.
095         *
096         * @param editor The editor to chain.
097         *
098         * @return A new {@code LineEditor} instance to test.
099         */
100        protected LineEditor newLineEditor( final LineEditor editor )
101        {
102            return new LineEditor( editor );
103        }
104    
105        /**
106         * Gets the name of the encoding used when reading resources.
107         *
108         * @return The name of the encoding used when reading resources.
109         *
110         * @see #setResourceEncoding(java.lang.String)
111         */
112        public final String getResourceEncoding()
113        {
114            if ( this.resourceEncoding == null )
115            {
116                this.resourceEncoding = System.getProperty( RESOURCE_ENCODING_PROPERTY_NAME );
117                assertNotNull( "Expected '" + RESOURCE_ENCODING_PROPERTY_NAME + "' system property not found.",
118                               this.resourceEncoding );
119    
120            }
121    
122            return this.resourceEncoding;
123        }
124    
125        /**
126         * Sets the name of the encoding to use when reading resources.
127         *
128         * @param value The new name of the encoding to use when reading resources or {@code null}.
129         *
130         * @see #getResourceEncoding()
131         */
132        public final void setResourceEncoding( final String value )
133        {
134            this.resourceEncoding = value;
135        }
136    
137        @Test
138        public final void testLineEditor() throws Exception
139        {
140            assertEquals( this.getLineEditor().getLineSeparator(), this.getLineEditor().edit( "" ) );
141            assertEquals( 1, this.getLineEditor().getLineNumber() );
142            assertEquals( "NO LINE SEPARATOR" + this.getLineEditor().getLineSeparator(),
143                          this.getLineEditor().edit( "NO LINE SEPARATOR" ) );
144    
145            assertEquals( 1, this.getLineEditor().getLineNumber() );
146            assertEquals( this.getLineEditor().getLineSeparator(), this.getLineEditor().edit( "\n" ) );
147            assertEquals( 1, this.getLineEditor().getLineNumber() );
148            assertNull( this.getLineEditor().edit( null ) );
149            assertEquals( 0, this.getLineEditor().getLineNumber() );
150        }
151    
152        @Test
153        public final void testLineEditorChain() throws Exception
154        {
155            final LineEditor chained = this.newLineEditor( new NullEditor() );
156            assertNull( chained.edit( "" ) );
157            assertEquals( 1, chained.getLineNumber() );
158            assertNull( chained.edit( "NO LINE SEPARATOR" ) );
159            assertEquals( 1, chained.getLineNumber() );
160            assertNull( chained.edit( "\n" ) );
161            assertEquals( 1, chained.getLineNumber() );
162            assertNull( chained.edit( null ) );
163            assertEquals( 0, chained.getLineNumber() );
164        }
165    
166    }