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: LineEditorTest.java 5091 2016-04-04 15:40:17Z schulte $
29   *
30   */
31  package org.jomc.util.test;
32  
33  import org.jomc.util.LineEditor;
34  import org.jomc.util.test.support.NullEditor;
35  import org.junit.Test;
36  import static org.junit.Assert.assertEquals;
37  import static org.junit.Assert.assertNotNull;
38  import static org.junit.Assert.assertNull;
39  
40  /**
41   * Test cases for class {@code org.jomc.util.LineEditor}.
42   *
43   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
44   * @version $JOMC: LineEditorTest.java 5091 2016-04-04 15:40:17Z schulte $
45   */
46  public class LineEditorTest
47  {
48  
49      /**
50       * Constant for the name of the system property holding the name of the encoding of resources backing the test.
51       */
52      private static final String RESOURCE_ENCODING_PROPERTY_NAME = "jomc.test.resourceEncoding";
53  
54      /**
55       * The {@code LineEditor} instance tests are performed with.
56       */
57      private LineEditor lineEditor;
58  
59      /**
60       * The name of the encoding to use when reading resources.
61       */
62      private String resourceEncoding;
63  
64      /**
65       * Creates a new {@code LineEditorTest} instance.
66       */
67      public LineEditorTest()
68      {
69          super();
70      }
71  
72      /**
73       * Gets the {@code LineEditor} instance tests are performed with.
74       *
75       * @return The {@code LineEditor} instance tests are performed with.
76       *
77       * @see #newLineEditor()
78       */
79      public LineEditor getLineEditor()
80      {
81          if ( this.lineEditor == null )
82          {
83              this.lineEditor = this.newLineEditor();
84          }
85  
86          return this.lineEditor;
87      }
88  
89      /**
90       * Gets a new {@code LineEditor} instance to test.
91       *
92       * @return A new {@code LineEditor} instance to test.
93       *
94       * @see #getLineEditor()
95       */
96      protected LineEditor newLineEditor()
97      {
98          return new LineEditor();
99      }
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 }