Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a custom record extractor debugger. #753

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2018 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package za.co.absa.cobrix.cobol.mock

import za.co.absa.cobrix.cobol.reader.extractors.raw.{RawRecordContext, RawRecordExtractor}

/**
* This is a record extractor for a binary file where
* - even records have the size of 2 bytes.
* - odd records have the size of 3 bytes.
*/
class CustomRecordExtractorMock(ctx: RawRecordContext) extends Serializable with RawRecordExtractor {
CustomRecordExtractorMock.additionalInfo = ctx.additionalInfo
CustomRecordExtractorMock.catchContext = ctx

private var recordNumber = ctx.startingRecordNumber

override def offset: Long = ctx.inputStream.offset

override def hasNext: Boolean = !ctx.inputStream.isEndOfStream

@throws[NoSuchElementException]
override def next(): Array[Byte] = {
if (!hasNext) {
throw new NoSuchElementException
}

val rawRecord = if (recordNumber % 2 == 0) {
ctx.inputStream.next(2)
} else {
ctx.inputStream.next(3)
}

recordNumber += 1

rawRecord
}
}

object CustomRecordExtractorMock {
var additionalInfo: String = ""
var catchContext: RawRecordContext = _
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2018 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package za.co.absa.cobrix.cobol.reader

import org.scalatest.wordspec.AnyWordSpec
import org.slf4j.LoggerFactory
import za.co.absa.cobrix.cobol.mock.CustomRecordExtractorMock
import za.co.absa.cobrix.cobol.reader.extractors.raw.RawRecordContext
import za.co.absa.cobrix.cobol.reader.memorystream.TestStringStream

class RecordExtractorDebugSpec extends AnyWordSpec {
private val log = LoggerFactory.getLogger("debug")

private val data = "AABBBCCDDDEEFFF"

// This is a code snippet that can be used to test custom record extractors
"record extractor data should be extracted as expected" in {
val dataStream = new TestStringStream(data)
val headerStream = new TestStringStream(data)

val ctx = RawRecordContext(0, dataStream, headerStream, null, null, null, "")
val extractor = new CustomRecordExtractorMock(ctx)

var i = 0
log.info(s"${headerStream.offset} : headers")

while (extractor.hasNext) {
val offset = dataStream.offset
val record = extractor.next()
val recordHex = record.map(b => f"$b%02X").mkString.take(10)
log.info(s"$offset : ${record.length} : $recordHex")
i += 1
}

assert(i == 6)
}
}
Loading