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 def invoke(input:InputStream,output:OutputStream,parameters:com.fasterxml.jackson.databind.JsonNode):Unit={val extra=f.path("inputs").fields.asScala.map(e=>e.getKey->new ByteArrayInputStream(e.getValue.asText.getBytes(UTF_8))).toMap;op.transformInputs(extra+("source"->input),output,parameters)}
16 invoke(new ByteArrayInputStream(data),out,params);assert(out.toString("UTF-8")==f.path("expected").asText,entry.getKey+" actual="+out.toString("UTF-8"))
17 def fails(body: => Unit):Unit={var failed=false;try body catch{case _:IllegalArgumentException=>failed=true};assert(failed,entry.getKey)}
18 fails(invoke(new ByteArrayInputStream(data),new ByteArrayOutputStream,mapper.readTree("{\"unsupported\":1}")))
19 Thread.currentThread.interrupt()
20 try{invoke(new ByteArrayInputStream(data),new ByteArrayOutputStream,params);throw new AssertionError("cancellation ignored")}catch{case _:CancellationException=>()}finally Thread.interrupted()
21 var io=false;try invoke(new InputStream{def read():Int=throw new IOException("input")},new ByteArrayOutputStream,params)catch{case _:IOException=>io=true};assert(io)
22 io=false;try invoke(new ByteArrayInputStream(data),new OutputStream{def write(b:Int):Unit=throw new IOException("output")},params)catch{case _:IOException=>io=true};assert(io)
23 var utf8=false;try invoke(new ByteArrayInputStream(Array(0xff.toByte)),new ByteArrayOutputStream,params)catch{case _:java.nio.charset.MalformedInputException=>utf8=true};assert(utf8)
24 fails(invoke(new ByteArrayInputStream(("A"*1048577).getBytes(UTF_8)),new ByteArrayOutputStream,params))
25 if(entry.getKey=="emboss_shuffleseq"){
26 val input=">shuffle\nACGTACGTTTGGCCAA\n".getBytes(UTF_8);val first=new ByteArrayOutputStream;val second=new ByteArrayOutputStream
27 invoke(new ByteArrayInputStream(input),first,params);invoke(new ByteArrayInputStream(input),second,params)
28 assert(first.toByteArray.toVector==second.toByteArray.toVector,"shuffle seed differs")
29 assert(first.toString("UTF-8").split("\n")(1).sorted=="ACGTACGTTTGGCCAA".sorted,"shuffle changes composition");total+=2
30 }
31 total+=7;println("PASS fixture, validation, cancellation, I/O, encoding and line budget: "+entry.getKey)
32 }
33 val regressions=mapper.readTree(Files.readString(Path.of("extended-regressions.json")))
34 Seq("positive","negative").foreach{kind=>regressions.path(kind).elements.asScala.foreach{c=>
35 val spec=specs.path(c.path("operator").asText);val op=Class.forName(spec.path("entrypoint").asText+"$").getField("MODULE$").get(null).asInstanceOf[ExtendedOperator]
36 val out=new ByteArrayOutputStream;var failed=false
37 val extra=c.path("inputs").fields.asScala.map(e=>e.getKey->new ByteArrayInputStream(e.getValue.asText.getBytes(UTF_8))).toMap
38 try op.transformInputs(extra+("source"->new ByteArrayInputStream(c.path("data").asText.getBytes(UTF_8))),out,c.path("params"))catch{case _:IllegalArgumentException=>failed=true}
39 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)
40 else assert(failed,"malformed input accepted: "+c)
41 total+=1
42 }}
43 println(s"PASS $total extended adapter checks, including upstream golden fixtures")
44 }
45}