Skip to content

Commit 3d7debc

Browse files
committed
Implement a custom record extractor debugger.
1 parent 2f1f615 commit 3d7debc

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2018 ABSA Group Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package za.co.absa.cobrix.cobol.mock
18+
19+
import za.co.absa.cobrix.cobol.reader.extractors.raw.{RawRecordContext, RawRecordExtractor}
20+
21+
/**
22+
* This is a record extractor for a binary file where
23+
* - even records have the size of 2 bytes.
24+
* - odd records have the size of 3 bytes.
25+
*/
26+
class CustomRecordExtractorMock(ctx: RawRecordContext) extends Serializable with RawRecordExtractor {
27+
CustomRecordExtractorMock.additionalInfo = ctx.additionalInfo
28+
CustomRecordExtractorMock.catchContext = ctx
29+
30+
private var recordNumber = ctx.startingRecordNumber
31+
32+
override def offset: Long = ctx.inputStream.offset
33+
34+
override def hasNext: Boolean = !ctx.inputStream.isEndOfStream
35+
36+
@throws[NoSuchElementException]
37+
override def next(): Array[Byte] = {
38+
if (!hasNext) {
39+
throw new NoSuchElementException
40+
}
41+
42+
val rawRecord = if (recordNumber % 2 == 0) {
43+
ctx.inputStream.next(2)
44+
} else {
45+
ctx.inputStream.next(3)
46+
}
47+
48+
recordNumber += 1
49+
50+
rawRecord
51+
}
52+
}
53+
54+
object CustomRecordExtractorMock {
55+
var additionalInfo: String = ""
56+
var catchContext: RawRecordContext = _
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2018 ABSA Group Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package za.co.absa.cobrix.cobol.reader
18+
19+
import org.scalatest.wordspec.AnyWordSpec
20+
import org.slf4j.LoggerFactory
21+
import za.co.absa.cobrix.cobol.mock.CustomRecordExtractorMock
22+
import za.co.absa.cobrix.cobol.reader.extractors.raw.RawRecordContext
23+
import za.co.absa.cobrix.cobol.reader.memorystream.TestStringStream
24+
25+
class RecordExtractorDebugSpec extends AnyWordSpec {
26+
private val log = LoggerFactory.getLogger("debug")
27+
28+
private val data = "AABBBCCDDDEEFFF"
29+
30+
// This is a code snippet that can be used to test custom record extractors
31+
"record extractor data should be extracted as expected" in {
32+
val dataStream = new TestStringStream(data)
33+
val headerStream = new TestStringStream(data)
34+
35+
val ctx = RawRecordContext(0, dataStream, headerStream, null, null, null, "")
36+
val extractor = new CustomRecordExtractorMock(ctx)
37+
38+
var i = 0
39+
log.info(s"${headerStream.offset} : headers")
40+
41+
while (extractor.hasNext) {
42+
val offset = dataStream.offset
43+
val record = extractor.next()
44+
val recordHex = record.map(b => f"$b%02X").mkString.take(10)
45+
log.info(s"$offset : ${record.length} : $recordHex")
46+
i += 1
47+
}
48+
49+
assert(i == 6)
50+
}
51+
}

0 commit comments

Comments
 (0)