java 7 - Byteman JUnit Runner - impossible to trigger IOException on auto-closed InputStream#close -



java 7 - Byteman JUnit Runner - impossible to trigger IOException on auto-closed InputStream#close -

i have got next code:

collection<string> errors = ...; seek (inputstream stream = my.class.getresourceasstream(resource)) { // stuff } catch(ioexception ex) { errors.add("fail"); }

i'm trying byteman junit runner trigger ioexception when (valid) input stream give supposedly closed:

@runwith(bmunitrunner.class) public class mytest { private my = new my(); @bmrule( name = "force_read_error", targetclass = "java.io.inputstream", targetmethod = "close()", action = "throw new ioexception(\"bazinga\")" ) @test public void catches_read_error() throws ioexception { collection<string> errors = my.foo("/valid-resource-in-classpath"); assertthat(errors).containsexactly("fail"); } }

my test fails: errors empty, means byteman rule isn't executed (it's loaded agent, don't understand what's going on).

how can trigger ioexception on close method called via try-with-resources?

your rule ist not firing, because class of stream object received when calling

inputstream stream = my.class.getresourceasstream(resource)

is not "java.io.inputstream" class. class extending "java.io.inputstream", "bufferedinputstream".

to tell byteman "trigger rule class extending java.io.inputstream", need set '^' before class name:

targetclass = "^java.io.inputstream"

this alter might have unwanted side effect, rule gets triggered when other objects extending "java.io.inputstream" closed. prevent happening, status should added rule triggered, when caller matches "foo" method of "my" class. byteman has helper method called "callermatches" (please see advanced tutorial)

a working status case be:

condition = "callermatches(\".+my.foo\",true,true)"

the finish byteman rule definition bmrule annotation should like:

@bmrule( name = "force_read_error", targetclass = "^java.io.inputstream", targetmethod = "close()", status = "callermatches(\".+my.foo\",true,true)", action = "throw new java.io.ioexception(\"bazinga\")" )

junit java-7 try-with-resources

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -