1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
42
43
44
45
46 public class LineEditorTest
47 {
48
49
50 private static final String RESOURCE_ENCODING_PROPERTY_NAME = "jomc.test.resourceEncoding";
51
52
53 private LineEditor lineEditor;
54
55
56 private String resourceEncoding;
57
58
59 public LineEditorTest()
60 {
61 super();
62 }
63
64
65
66
67
68
69
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
83
84
85
86
87
88 protected LineEditor newLineEditor()
89 {
90 return new LineEditor();
91 }
92
93
94
95
96
97
98
99
100 protected LineEditor newLineEditor( final LineEditor editor )
101 {
102 return new LineEditor( editor );
103 }
104
105
106
107
108
109
110
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
127
128
129
130
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 }