Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
FileEncodingTest
package scoverage
import java.nio.charset.{MalformedInputException, CodingErrorAction}
import org.scalatest.FunSuite
import scala.io.{Source, Codec}
class FileEncodingTest extends FunSuite {
test("read file with encoding error must throw error") {
val class1 = getClass.getResource("badEncoding.csv").getFile()
intercept[MalformedInputException] {
val s = source2(class1)
assert(!s.isEmpty)
}
}
test("read file with correct encoding must not throw error") {
val class1 = getClass.getResource("badEncoding.csv").getFile()
val s = source(class1)
assert(!s.isEmpty)
}
def source(filePath: String): String = {
implicit val codec = Codec("UTF-8")
codec.onMalformedInput(CodingErrorAction.REPLACE)
codec.onUnmappableCharacter(CodingErrorAction.REPLACE)
Source.fromFile(filePath).mkString
}
def source2(filePath: String): String = {
Source.fromFile(filePath).mkString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.