Permalink
Browse files

Added byteman example

  • Loading branch information...
1 parent 031ed78 commit 18604d1443bca7d6530fb4eec4d76a45f6f837c6 Jakub Holy committed Aug 8, 2012
@@ -0,0 +1,3 @@
+.idea/
+target/
+ExampleByteman.iml
@@ -0,0 +1,87 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>net.jakubholy.examples</groupId>
+ <artifactId>ExampleByteman</artifactId>
+ <version>1.0</version>
+ <description>Example of fault injection with JUnit and Byteman, based on https://community.jboss.org/wiki/FaultInjectionTestingWithByteman</description>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8.2</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.byteman</groupId>
+ <artifactId>byteman</artifactId>
+ <scope>test</scope>
+ <version>${byteman.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.byteman</groupId>
+ <artifactId>byteman-submit</artifactId>
+ <scope>test</scope>
+ <version>${byteman.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.byteman</groupId>
+ <artifactId>byteman-install</artifactId>
+ <scope>test</scope>
+ <version>${byteman.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.byteman</groupId>
+ <artifactId>byteman-bmunit</artifactId>
+ <scope>test</scope>
+ <version>${byteman.version}</version>
+ </dependency>
+ <!--dependency>
+ <groupId>com.sun</groupId>
+ <artifactId>tools</artifactId>
+ <version>1.6</version>
+ <scope>system</scope>
+ <systemPath>${tools.jar}</systemPath>
+ </dependency-->
+
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>2.4.3</version>
+ <configuration>
+ <!--includes>
+ <include>org/my/BytemanJUnitTests*.java</include>
+ </includes-->
+ <!-- make sure maven puts the byteman jar in the classpath rather than in a manifest jar -->
+ <useManifestOnlyJar>false</useManifestOnlyJar>
+ <forkMode>once</forkMode>
+ <parallel>false</parallel>
+ <!-- ensure we don't inherit a byteman jar from any env settings -->
+ <environmentVariables>
+ <BYTEMAN_HOME></BYTEMAN_HOME>
+ </environmentVariables>
+ <systemProperties>
+ <property>
+ <name>org.jboss.byteman.home</name>
+ <value></value>
+ </property>
+ </systemProperties>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <properties>
+ <byteman.version>2.0.4</byteman.version>
+ <tools.jar></tools.jar>
+ </properties>
+
+</project>
@@ -0,0 +1,9 @@
+package net.jakubholy.examples.byteman;
+
+public class MainsHelper {
+
+ public String sayHello() {
+ System.out.println(getClass().getSimpleName() + ".sayHello: called");
+ return "Hello from MainsHelper!";
+ }
+}
@@ -0,0 +1,9 @@
+package net.jakubholy.examples.byteman;
+
+public class MyMain {
+
+ public String sayHello() {
+ System.out.println(getClass().getSimpleName() + ".sayHello: called");
+ return "MyMain: helper says: " + new MainsHelper().sayHello();
+ }
+}
@@ -0,0 +1,43 @@
+package net.jakubholy.examples.byteman;
+
+import org.jboss.byteman.contrib.bmunit.BMRule;
+import org.jboss.byteman.contrib.bmunit.BMUnitRunner;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+
+import static junit.framework.Assert.fail;
+
+/**
+ * Example of fault injection into the code under test using Byteman.
+ *
+ * Based on <a href="https://community.jboss.org/wiki/FaultInjectionTestingWithByteman">Fault Injection Testing With Byteman</a> (9/2011).
+ */
+@RunWith(BMUnitRunner.class)
+public class MyMainTest {
+
+ @Rule public final ExpectedException exception = ExpectedException.none();
+
+ /**
+ * Use Byteman to redefine the method <code>MainsHelper.sayHello</code> called from
+ * <code>MyMain.sayHello</code> to throw an IllegalStateException.
+ * <p>
+ * Notice that we do not have access to the instance of MainsHelper that is being
+ * modified. It could well be even class we do not own, e.g. java.net.Socket.
+ * </p>
+ */
+ @Test(expected = IllegalStateException.class)
+ @BMRule(name="throw IllegalStateException from the helper",
+ targetClass = "MainsHelper",
+ targetMethod = "sayHello",
+ action = "throw new java.lang.IllegalStateException(\"Exception injected by Byteman\")")
+ public void testSayHello() {
+ //exception.expect(IllegalStateException.class);
+ //exception.expectMessage("Exception injected by Byteman");
+
+ new MyMain().sayHello();
+
+ fail("sayHello should have failed due to Byteman injecting an exception into its helper");
+ }
+}

0 comments on commit 18604d1

Please sign in to comment.