Skip to content

Instantly share code, notes, and snippets.

@flavienlaurent
Created September 23, 2013 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flavienlaurent/6673763 to your computer and use it in GitHub Desktop.
Save flavienlaurent/6673763 to your computer and use it in GitHub Desktop.
Testing private field BoundBox
package org.boundbox.sample;
import java.lang.String;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import org.boundbox.BoundBoxException;
import org.boundbox.sample.FieldTestClassA;
import org.boundbox.sample.FieldTestClassB;
import org.boundbox.sample.ListFieldTestClassA;
import org.boundbox.sample.MethodTestClassA;
@SuppressWarnings("all")
public final class BoundBoxOfListFieldTestClassB {
private ListFieldTestClassB boundObject;
private static Class<ListFieldTestClassB> boundClass = ListFieldTestClassB.class;
public BoundBoxOfListFieldTestClassB(ListFieldTestClassB boundObject) {
this.boundObject = boundObject;
}
// ******************************
// Access to constructors
// ******************************
public static ListFieldTestClassB boundBox_new() {
try {
Constructor<? extends ListFieldTestClassB> method = boundClass.getDeclaredConstructor();
method.setAccessible(true);
return (ListFieldTestClassB) method.newInstance();
}
catch( IllegalAccessException e ) {
throw new BoundBoxException(e);
}
catch( IllegalArgumentException e ) {
throw new BoundBoxException(e);
}
catch( InvocationTargetException e ) {
throw new BoundBoxException(e);
}
catch( NoSuchMethodException e ) {
throw new BoundBoxException(e);
}
catch( InstantiationException e ) {
throw new BoundBoxException(e);
}
}
// ******************************
// Direct access to fields
// ******************************
public ArrayList<String> boundBox_getAs() {
try {
Field field = ListFieldTestClassA.class.getDeclaredField("as");
field.setAccessible(true);
return (java.util.ArrayList<java.lang.String>) field.get(boundObject);
}
catch( Exception e ) {
throw new BoundBoxException(e);
}
}
public void boundBox_setAs(ArrayList<String> as) {
try {
Field field = ListFieldTestClassA.class.getDeclaredField("as");
field.setAccessible(true);
field.set(boundObject, as);
}
catch( Exception e ) {
throw new BoundBoxException(e);
}
}
// ******************************
// Access to methods
// ******************************
}
package org.boundbox.sample;
import org.boundbox.BoundBox;
import org.junit.Before;
import org.junit.Test;
public class ListFieldTest {
private ListFieldTestClassB listFieldTestClassB;
@BoundBox(boundClass = ListFieldTestClassB.class)
private BoundBoxOfListFieldTestClassB boundBoxOfB;
@Before
public void setup() {
listFieldTestClassB = new ListFieldTestClassB();
boundBoxOfB = new BoundBoxOfListFieldTestClassB(listFieldTestClassB);
}
@Test
public void test_write_access_to_hidden_field() {
boundBoxOfB.boundBox_getAs();
}
}
package org.boundbox.sample;
import java.util.ArrayList;
@SuppressWarnings("unused")
@edu.umd.cs.findbugs.annotations.SuppressWarnings(
value={"URF_UNREAD_FIELD"},
justification="Only used for tests")
public class ListFieldTestClassA {
private ArrayList<String> as;
}
package org.boundbox.sample;
@edu.umd.cs.findbugs.annotations.SuppressWarnings(
value={"URF_UNREAD_FIELD"},
justification="Only used for tests")
@SuppressWarnings("unused")
public class ListFieldTestClassB extends ListFieldTestClassA {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment