Source code

Download source archive ↓

tests/ExtendedSafetyCheck.scala

1package xstream.galaxy
2import java.io._
3import java.nio.file.{Files,Path}
4import java.nio.charset.StandardCharsets.UTF_8
5import com.fasterxml.jackson.databind.ObjectMapper
6import java.util.concurrent.CancellationException
7import scala.jdk.CollectionConverters._
8object ExtendedSafetyCheck {
9 def main(args:Array[String]):Unit={
10  val mapper=new ObjectMapper;val specs=mapper.readTree(Files.readString(Path.of("extra-specs.json")))
11  var total=0
12  specs.fields.asScala.foreach{entry=>
13   val s=entry.getValue;val op=Class.forName(s.path("entrypoint").asText+"$").getField("MODULE$").get(null).asInstanceOf[ExtendedOperator]
14   val f=s.path("fixture");val data=f.path("data").asText.getBytes(UTF_8);val params=f.path("params");val out=new ByteArrayOutputStream
15   op.transform(new ByteArrayInputStream(data),out,params);assert(out.toString("UTF-8")==f.path("expected").asText,entry.getKey+" actual="+out.toString("UTF-8"))
16   def fails(body: => Unit):Unit={var failed=false;try body catch{case _:IllegalArgumentException=>failed=true};assert(failed,entry.getKey)}
17   fails(op.transform(new ByteArrayInputStream(data),new ByteArrayOutputStream,mapper.readTree("{\"unsupported\":1}")))
18   Thread.currentThread.interrupt()
19   try{op.transform(new ByteArrayInputStream(data),new ByteArrayOutputStream,params);throw new AssertionError("cancellation ignored")}catch{case _:CancellationException=>()}finally Thread.interrupted()
20   var io=false;try op.transform(new InputStream{def read():Int=throw new IOException("input")},new ByteArrayOutputStream,params)catch{case _:IOException=>io=true};assert(io)
21   io=false;try op.transform(new ByteArrayInputStream(data),new OutputStream{def write(b:Int):Unit=throw new IOException("output")},params)catch{case _:IOException=>io=true};assert(io)
22   var utf8=false;try op.transform(new ByteArrayInputStream(Array(0xff.toByte)),new ByteArrayOutputStream,params)catch{case _:java.nio.charset.MalformedInputException=>utf8=true};assert(utf8)
23   fails(op.transform(new ByteArrayInputStream(("A"*1048577).getBytes(UTF_8)),new ByteArrayOutputStream,params))
24   total+=7;println("PASS fixture, validation, cancellation, I/O, encoding and line budget: "+entry.getKey)
25  }
26  val regressions=mapper.readTree(Files.readString(Path.of("extended-regressions.json")))
27  Seq("positive","negative").foreach{kind=>regressions.path(kind).elements.asScala.foreach{c=>
28   val spec=specs.path(c.path("operator").asText);val op=Class.forName(spec.path("entrypoint").asText+"$").getField("MODULE$").get(null).asInstanceOf[ExtendedOperator]
29   val out=new ByteArrayOutputStream;var failed=false
30   try op.transform(new ByteArrayInputStream(c.path("data").asText.getBytes(UTF_8)),out,c.path("params"))catch{case _:IllegalArgumentException=>failed=true}
31   if(kind=="positive")assert(!failed&&out.toString("UTF-8")==c.path("expected").asText,c.path("operator").asText+" actual="+out.toString("UTF-8")+" expected="+c.path("expected").asText)
32   else assert(failed,"malformed input accepted: "+c)
33   total+=1
34  }}
35  println(s"PASS $total extended adapter checks, including upstream golden fixtures")
36 }
37}