CoroutinesAgent { public static void premain(String agentArgs, Instrumentation inst) { MarkerType markerType = MarkerType.NONE; boolean debugMode = false; boolean autoSerializable = true; if (agentArgs != null && !agentArgs.isEmpty()) { String[] splitArgs = agentArgs.split(","); for (String splitArg : splitArgs) { String[] keyVal = splitArg.split("=", 2); if (keyVal.length != 2) { throw new IllegalArgumentException("Unrecognized arg passed to Coroutines Java agent: " + splitArg); } String key = keyVal[0]; String val = keyVal[1]; switch (key) { case "markerType": try { markerType = MarkerType.valueOf(val); } catch (IllegalArgumentException iae) { throw new IllegalArgumentException("Unable to parse marker type -- must be one of the following: " + Arrays.toString(MarkerType.values()), iae); } break; case "debugMode": if (val.equalsIgnoreCase("true")) { debugMode = true; } else if (val.equalsIgnoreCase("false")) { debugMode = false; } else { throw new IllegalArgumentException("Unable to parse debug mode -- must be true or false"); } break; case "autoSerializable": if (val.equalsIgnoreCase("true")) { autoSerializable = true; } else if (val.equalsIgnoreCase("false")) { autoSerializable = false; } else { throw new IllegalArgumentException("Unable to parse debug mode -- must be true or false"); } break; default: throw new IllegalArgumentException("Unrecognized arg passed to Coroutines Java agent: " + keyVal); } } } inst.addTransformer(new CoroutinesClassFileTransformer(markerType, debugMode, autoSerializable)); } private  CoroutinesAgent(); static void premain(String agentArgs, Instrumentation inst);  }
@Test public void mustInstrumentClasses() throws Exception { Instrumentation inst = mock(Instrumentation.class); String agentArgs = null; CoroutinesAgent.premain(agentArgs, inst); ArgumentCaptor<ClassFileTransformer> captor = ArgumentCaptor.forClass(ClassFileTransformer.class); verify(inst).addTransformer(captor.capture()); byte[] inputContent = readZipFromResource("NormalInvokeTest.zip").get("NormalInvokeTest.class"); ClassFileTransformer tranformer = captor.getValue(); byte[] outputContent = tranformer.transform( getClass().getClassLoader(), "NormalInvokeTest", null, null, inputContent); assertTrue(outputContent.length > inputContent.length); }
@Test public void mustInstrumentClassesWithParams() throws Exception { Instrumentation inst = mock(Instrumentation.class); String agentArgs = "markerType=STDOUT,debugMode=true,autoSerializable=false"; CoroutinesAgent.premain(agentArgs, inst); ArgumentCaptor<ClassFileTransformer> captor = ArgumentCaptor.forClass(ClassFileTransformer.class); verify(inst).addTransformer(captor.capture()); byte[] inputContent = readZipFromResource("NormalInvokeTest.zip").get("NormalInvokeTest.class"); ClassFileTransformer tranformer = captor.getValue(); byte[] outputContent = tranformer.transform( getClass().getClassLoader(), "NormalInvokeTest", null, null, inputContent); assertTrue(outputContent.length > inputContent.length); }
@Test public void mustFailIfDebugTypeIncorrect() throws Exception { Instrumentation inst = mock(Instrumentation.class); String agentArgs = "markerType=NONE,debugType=ffffffffffff"; assertThrows(IllegalArgumentException.class, () -> { CoroutinesAgent.premain(agentArgs, inst); }); }
@Test public void mustFailIfMarkerTypeIncorrect() throws Exception { Instrumentation inst = mock(Instrumentation.class); String agentArgs = "markerType=fffffffffff,debugMode=false"; assertThrows(IllegalArgumentException.class, () -> { CoroutinesAgent.premain(agentArgs, inst); }); }
@Test public void mustFailIfNoEqualsInArg() throws Exception { Instrumentation inst = mock(Instrumentation.class); String agentArgs = "NONE,debugMode=false"; assertThrows(IllegalArgumentException.class, () -> { CoroutinesAgent.premain(agentArgs, inst); }); }
@Test public void mustFailIfUnknownArg() throws Exception { Instrumentation inst = mock(Instrumentation.class); String agentArgs = "pewpewpew=false"; assertThrows(IllegalArgumentException.class, () -> { CoroutinesAgent.premain(agentArgs, inst); }); }