1 /*
2 * Copyright (C) Christian Schulte, 2005-206
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 4613 2012-09-22 10:07:08Z 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 4613 2012-09-22 10:07:08Z schulte $
45 */
46 public class LineEditorTest
47 {
48
49 /** Constant for the name of the system property holding the name of the encoding of resources backing the test. */
50 private static final String RESOURCE_ENCODING_PROPERTY_NAME = "jomc.test.resourceEncoding";
51
52 /** The {@code LineEditor} instance tests are performed with. */
53 private LineEditor lineEditor;
54
55 /** The name of the encoding to use when reading resources. */
56 private String resourceEncoding;
57
58 /** Creates a new {@code LineEditorTest} instance. */
59 public LineEditorTest()
60 {
61 super();
62 }
63
64 /**
65 * Gets the {@code LineEditor} instance tests are performed with.
66 *
67 * @return The {@code LineEditor} instance tests are performed with.
68 *
69 * @see #newLineEditor()
70 */
71 public LineEditor getLineEditor()
72 {
73 if ( this.lineEditor == null )
74 {
75 this.lineEditor = this.newLineEditor();
76 }
77
78 return this.lineEditor;
79 }
80
81 /**
82 * Gets a new {@code LineEditor} instance to test.
83 *
84 * @return A new {@code LineEditor} instance to test.
85 *
86 * @see #getLineEditor()
87 */
88 protected LineEditor newLineEditor()
89 {
90 return new LineEditor();
91 }
92
93 /**
94 * Gets a new {@code LineEditor} instance to test taking an editor to chain.
95 *
96 * @param editor The editor to chain.
97 *
98 * @return A new {@code LineEditor} instance to test.
99 */
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 }