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