Commit 2d0a79bd authored by Andreas Jonsson's avatar Andreas Jonsson
Browse files

XCOMMONS-278: Replaced method used for unit testing execution context...

XCOMMONS-278: Replaced method used for unit testing execution context properties with reflection access.
parent 758f198f
...@@ -188,18 +188,6 @@ public void inheritFrom(ExecutionContext executionContext) ...@@ -188,18 +188,6 @@ public void inheritFrom(ExecutionContext executionContext)
} }
} }
/**
* This method is intentionally non-public and should only be used for unit testing the property objects.
*
* @param key The property key.
* @return The declared property.
* @since 4.3M2
*/
ExecutionContextProperty fetchProperty(String key)
{
return properties.get(key);
}
/** /**
* @param property Property to check. * @param property Property to check.
* @throws IllegalStateException if the property may not be ignored. * @throws IllegalStateException if the property may not be ignored.
...@@ -332,5 +320,4 @@ public DeclarationBuilder nonNull() ...@@ -332,5 +320,4 @@ public DeclarationBuilder nonNull()
return this; return this;
} }
} }
} }
...@@ -22,6 +22,11 @@ ...@@ -22,6 +22,11 @@
import org.junit.Test; import org.junit.Test;
import org.junit.Assert; import org.junit.Assert;
import java.util.Map;
import java.lang.reflect.Field;
import org.xwiki.context.internal.ExecutionContextProperty;
/** /**
* @version $Id$ * @version $Id$
* @since 4.3M1 * @since 4.3M1
...@@ -29,6 +34,26 @@ ...@@ -29,6 +34,26 @@
public class ExecutionContextPropertyTest public class ExecutionContextPropertyTest
{ {
/**
* Access the properties via reflection. This method requires ReflectPermission suppressAccessChecks.
*
* @param context The execution context
* @param key The property key
* @return the execution context property corresponding to the given key.
*/
@SuppressWarnings("unchecked")
private ExecutionContextProperty fetch(ExecutionContext context, String key) throws Exception
{
Field propertiesField = ExecutionContext.class.getDeclaredField("properties");
propertiesField.setAccessible(true);
Map<String, ExecutionContextProperty> properties
= (Map<String, ExecutionContextProperty>) propertiesField.get(context);
return properties.get(key);
}
@Test @Test
public void defaultValues() throws Exception public void defaultValues() throws Exception
{ {
...@@ -37,16 +62,16 @@ public void defaultValues() throws Exception ...@@ -37,16 +62,16 @@ public void defaultValues() throws Exception
ExecutionContext context = new ExecutionContext(); ExecutionContext context = new ExecutionContext();
context.newProperty(key).declare(); context.newProperty(key).declare();
Assert.assertFalse(context.fetchProperty(key).isFinal()); Assert.assertFalse(fetch(context, key).isFinal());
Assert.assertTrue(key.equals(context.fetchProperty(key).getKey())); Assert.assertTrue(key.equals(fetch(context, key).getKey()));
Assert.assertTrue(null == context.fetchProperty(key).getValue()); Assert.assertTrue(null == fetch(context, key).getValue());
Assert.assertFalse(context.fetchProperty(key).isInherited()); Assert.assertFalse(fetch(context, key).isInherited());
Object o = new Object(); Object o = new Object();
context.setProperty(key, o); context.setProperty(key, o);
Assert.assertTrue(context.fetchProperty(key).getValue() == o); Assert.assertTrue(fetch(context, key).getValue() == o);
context.setProperty(key, null); context.setProperty(key, null);
Assert.assertTrue(context.fetchProperty(key).getValue() == null); Assert.assertTrue(fetch(context, key).getValue() == null);
} }
@Test @Test
...@@ -62,21 +87,21 @@ public void cloning() throws Exception ...@@ -62,21 +87,21 @@ public void cloning() throws Exception
context.newProperty(k1).initial(value).declare(); context.newProperty(k1).initial(value).declare();
Assert.assertTrue(value == context.fetchProperty(k1).clone().getValue()); Assert.assertTrue(value == fetch(context, k1).clone().getValue());
context.newProperty(k2).initial(value).cloneValue().declare(); context.newProperty(k2).initial(value).cloneValue().declare();
TestCloneable clonedValue = (TestCloneable) context.fetchProperty(k2).clone().getValue(); TestCloneable clonedValue = (TestCloneable) fetch(context, k2).clone().getValue();
Assert.assertTrue(value != clonedValue && clonedValue.value.equals("clone")); Assert.assertTrue(value != clonedValue && clonedValue.value.equals("clone"));
context.newProperty(k3).initial(value).cloneValue().makeFinal().inherited().declare(); context.newProperty(k3).initial(value).cloneValue().makeFinal().inherited().declare();
Assert.assertTrue(context.fetchProperty(k3).clone().isClonedFrom(context.fetchProperty(k3))); Assert.assertTrue(fetch(context, k3).clone().isClonedFrom(fetch(context, k3)));
} }
@Test(expected=IllegalStateException.class) @Test(expected=IllegalStateException.class)
public void cloningNonPublicCloneMethod() public void cloningNonPublicCloneMethod() throws Exception
{ {
ExecutionContext context = new ExecutionContext(); ExecutionContext context = new ExecutionContext();
...@@ -86,7 +111,7 @@ public void cloningNonPublicCloneMethod() ...@@ -86,7 +111,7 @@ public void cloningNonPublicCloneMethod()
context.newProperty(key).cloneValue().initial(value).declare(); context.newProperty(key).cloneValue().initial(value).declare();
context.fetchProperty(key).clone(); fetch(context, key).clone();
} }
@Test(expected=IllegalArgumentException.class) @Test(expected=IllegalArgumentException.class)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment