-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
MultiReadableTest.java
206 lines (180 loc) · 6.83 KB
/
MultiReadableTest.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.csv.reader;
import org.junit.Test;
import java.io.IOException;
import java.io.StringReader;
import org.neo4j.collection.RawIterator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.neo4j.csv.reader.Readables.wrap;
public class MultiReadableTest
{
@Test
public void shouldReadFromMultipleReaders() throws Exception
{
// GIVEN
String[][] data = new String[][] {
{"this is", "the first line"},
{"where this", "is the second line"},
{"and here comes", "the third line"}
};
RawIterator<CharReadable,IOException> readers = readerIteratorFromStrings( data, null );
CharSeeker seeker = CharSeekers.charSeeker( new MultiReadable( readers ), CONFIG, true );
// WHEN/THEN
for ( String[] line : data )
{
assertNextLine( line, seeker, mark, extractors );
}
assertFalse( seeker.seek( mark, delimiter ) );
seeker.close();
}
@Test
public void shouldHandleSourcesEndingWithNewLine() throws Exception
{
// GIVEN
String[][] data = new String[][] {
{"this is", "the first line"},
{"where this", "is the second line"},
};
// WHEN
RawIterator<CharReadable,IOException> readers = readerIteratorFromStrings( data, '\n' );
CharSeeker seeker = CharSeekers.charSeeker( new MultiReadable( readers ), CONFIG, true );
// WHEN/THEN
for ( String[] line : data )
{
assertNextLine( line, seeker, mark, extractors );
}
assertFalse( seeker.seek( mark, delimiter ) );
seeker.close();
}
@Test
public void shouldTrackAbsolutePosition() throws Exception
{
// GIVEN
String[][] data = new String[][] {
{"this is", "the first line"}, // 21+delimiter+newline = 23 characters
{"where this", "is the second line"}, // 28+delimiter+newline = 30 characters
};
RawIterator<CharReadable,IOException> readers = readerIteratorFromStrings( data, '\n' );
CharReadable reader = new MultiReadable( readers );
assertEquals( 0L, reader.position() );
SectionedCharBuffer buffer = new SectionedCharBuffer( 15 );
// WHEN
reader.read( buffer, buffer.front() );
assertEquals( 15, reader.position() );
reader.read( buffer, buffer.front() );
assertEquals( "Should not transition to a new reader in the middle of a read", 23, reader.position() );
assertEquals( "Reader1", reader.sourceDescription() );
// we will transition to the new reader in the call below
reader.read( buffer, buffer.front() );
assertEquals( 23 + 15, reader.position() );
reader.read( buffer, buffer.front() );
assertEquals( 23 + 30, reader.position() );
reader.read( buffer, buffer.front() );
assertFalse( buffer.hasAvailable() );
}
@Test
public void shouldNotCrossSourcesInOneRead() throws Exception
{
// given
String source1 = "abcdefghijklm";
String source2 = "nopqrstuvwxyz";
String[][] data = new String[][] { {source1}, {source2} };
CharReadable readable = new MultiReadable( readerIteratorFromStrings( data, '\n' ) );
// when
char[] target = new char[source1.length() + source2.length() / 2];
int read = readable.read( target, 0, target.length );
// then
assertEquals( source1.length() + 1/*added newline-char*/, read );
// and when
target = new char[source2.length()];
read = readable.read( target, 0, target.length );
// then
assertEquals( source2.length(), read );
read = readable.read( target, 0, target.length );
assertEquals( 1/*added newline-char*/, read );
}
private static final Configuration CONFIG = new Configuration.Overridden( Configuration.DEFAULT )
{
@Override
public int bufferSize()
{
return 200;
}
};
private void assertNextLine( String[] line, CharSeeker seeker, Mark mark, Extractors extractors ) throws IOException
{
for ( String value : line )
{
assertTrue( seeker.seek( mark, delimiter ) );
assertEquals( value, seeker.extract( mark, extractors.string() ).value() );
}
assertTrue( mark.isEndOfLine() );
}
private RawIterator<CharReadable,IOException> readerIteratorFromStrings(
final String[][] data, final Character lineEnding )
{
return new RawIterator<CharReadable,IOException>()
{
private int cursor;
@Override
public boolean hasNext()
{
return cursor < data.length;
}
@Override
public CharReadable next()
{
String string = join( data[cursor++] );
return wrap( new StringReader( string )
{
@Override
public String toString()
{
return "Reader" + cursor;
}
}, string.length() * 2 );
}
private String join( String[] strings )
{
StringBuilder builder = new StringBuilder();
for ( String string : strings )
{
builder.append( builder.length() > 0 ? "," : "" ).append( string );
}
if ( lineEnding != null )
{
builder.append( lineEnding );
}
return builder.toString();
}
@Override
public void remove()
{
throw new UnsupportedOperationException();
}
};
}
private final Mark mark = new Mark();
private final Extractors extractors = new Extractors( ';' );
private final int delimiter = ',';
}