/**
* <p>
- * Component instantiation listener that injects references into a component
- * using the injector mini-framework. Together with an implementation of the
+ * Component instantiation listener that injects references into a component
+ * using the injector mini-framework. Together with an implementation of the
* mini-framework, Contexts and Dependency Injection support can be provided or
- * for standard Java EE injection into components. Or, other injection
+ * for standard Java EE injection into components. Or, other injection
* frameworks can be used.
* </p>
*
* <p>
- * To use this injector override {@link WebApplication#init()} and add the
- * listener:
+ * To use this injector override {@link WebApplication#init()} and add the
+ * listener:
* </p>
+ *
* <pre>
- * @Override
- * protected void init() {
- * super.init();
- * addComponentInstantiationListener(new ComponentInstantiationInjector());
- * }
+ * @Override
+ * protected void init() {
+ * super.init();
+ * addComponentInstantiationListener(new ComponentInstantiationInjector());
+ * }
* </pre>
*
* @author Erik Brakkee
*/
public class ComponentInstantiationInjector implements
- IComponentInstantiationListener {
-
- private SimpleInjector injector;
-
+ IComponentInstantiationListener {
+
+ private SimpleInjector injector;
+
/**
- * Constructs the component instantiation listener.
+ * Constructs the component instantiation listener.
*/
- public ComponentInstantiationInjector() {
+ public ComponentInstantiationInjector() {
injector = new SimpleInjector(InjectorBuilder.getInjectorFactory());
}
-
+
@Override
public void onInstantiation(Component aComponent) {
injector.inject(aComponent);
/**
* Injection behavior that performs dependency injection after
- * serialization/deserialisation of the object.
+ * serialization/deserialisation of the object.
*
* @author Erik Brakkee
- *
+ *
*/
public class InjectionBehavior extends AbstractBehavior {
+ private static final long serialVersionUID = 7363393083209418693L;
private transient boolean injectionUptodate;
-
+
/**
* Constructs the behavior.
*/
- public InjectionBehavior() {
- injectionUptodate = true;
+ public InjectionBehavior() {
+ injectionUptodate = true;
}
-
+
@Override
public void beforeRender(Component aComponent) {
- if (!injectionUptodate) {
+ if (!injectionUptodate) {
InjectorBuilder.getInjector().inject(aComponent);
- injectionUptodate = true;
+ injectionUptodate = true;
}
}
}
import org.wamblee.persistence.Detachable;
/**
- * A detachable model that wraps a domain object.
- *
+ * A detachable model that wraps a domain object.
+ *
*/
public class DetachableEntity<T> extends LoadableDetachableModel<T> {
-
- private Detachable<T> object;
-
- /**
- * Constructs the model.
- * @param aObject Object to construct the model with.
- */
- public DetachableEntity(Detachable<T> aObject) {
- super(aObject.get());
+
+ private Detachable<T> object;
+
+ /**
+ * Constructs the model.
+ *
+ * @param aObject
+ * Object to construct the model with.
+ */
+ public DetachableEntity(Detachable<T> aObject) {
+ super(aObject.get());
object = aObject;
- }
-
- @Override
- protected void onDetach() {
- object.detach();
- }
-
- @Override
- protected T load() {
- return object.get();
- }
-
- @Override
- public void setObject(T object) {
- throw new UnsupportedOperationException("setObject is not supported on this class");
- }
+ }
+
+ @Override
+ protected void onDetach() {
+ object.detach();
+ }
+
+ @Override
+ protected T load() {
+ return object.get();
+ }
+
+ @Override
+ public void setObject(T aObject) {
+ throw new UnsupportedOperationException(
+ "setObject is not supported on this class");
+ }
}
* This package provides transaction support for wicket making it really easy
* to work with transactions.
*/
-package org.wamblee.wicket.transactions;
\ No newline at end of file
+package org.wamblee.wicket.transactions;
\ No newline at end of file
import org.junit.Test;
import org.wamblee.inject.Injector;
-
public class ComponentInstantiationInjectorTest {
-
+
@Test
- public void testListener() {
+ public void testListener() {
WicketTester tester = new WicketTester();
- tester.getApplication().addComponentInstantiationListener(new ComponentInstantiationInjector());
-
- Injector injector = mock(Injector.class);
- TestInjectorFactory.setMockInjector(injector);
-
+ tester.getApplication().addComponentInstantiationListener(
+ new ComponentInstantiationInjector());
+
+ Injector injector = mock(Injector.class);
+ TestInjectorFactory.setMockInjector(injector);
+
Component component = new WebMarkupContainer("hello");
-
- // Verify the injector was called with the component as an argument.
- verify(injector).inject(same(component));
+
+ // Verify the injector was called with the component as an argument.
+ verify(injector).inject(same(component));
}
}
public class InjectionBehaviorTest {
- Component component;
- Injector injector;
- InjectorFactory injectorFactory;
+ private Component component;
+ private Injector injector;
+ private InjectorFactory injectorFactory;
@Before
public void setUp() {
@Test
public void testNoInjectionInitially() {
InjectionBehavior behavior = new InjectionBehavior();
- behavior.beforeRender(component);
+ behavior.beforeRender(component);
verifyNoMoreInteractions(injector);
}
-
+
@Test
- public void testInjectOnlyOnceAfterDeserialisation() throws Exception {
+ public void testInjectOnlyOnceAfterDeserialisation() throws Exception {
InjectionBehavior behavior = new InjectionBehavior();
-
- behavior = ObjectSerializationUtils.deserialize(ObjectSerializationUtils.serialize(behavior), InjectionBehavior.class);
-
- behavior.beforeRender(component);
+
+ behavior = ObjectSerializationUtils.deserialize(
+ ObjectSerializationUtils.serialize(behavior),
+ InjectionBehavior.class);
+
+ behavior.beforeRender(component);
verify(injector).inject(same(component));
reset(injector);
- behavior.beforeRender(component);
+ behavior.beforeRender(component);
verifyNoMoreInteractions(injector);
}
import org.wamblee.inject.InjectorFactory;
public class TestInjectorFactory implements InjectorFactory {
-
- private static Injector MOCK_INJECTOR;
-
- public TestInjectorFactory() {
- // Empty.
+
+ private static Injector MOCK_INJECTOR;
+
+ public TestInjectorFactory() {
+ // Empty.
}
-
+
public static void setMockInjector(Injector aMockInjector) {
MOCK_INJECTOR = aMockInjector;
}
@Override
public Injector create(Class aClass) {
- return MOCK_INJECTOR;
+ return MOCK_INJECTOR;
}
}
import org.wamblee.persistence.Detachable;
import static org.mockito.Mockito.*;
-import static junit.framework.TestCase.*;
+import static junit.framework.TestCase.*;
public class DetachableEntityTest {
-
- public static class X {
-
+
+ public static class X {
+
}
-
- private X initialValue;
+
+ private X initialValue;
private Detachable<X> detachable;
- private DetachableEntity<X> entity;
-
+ private DetachableEntity<X> entity;
+
@Before
- public void setUp() {
+ public void setUp() {
initialValue = mock(X.class);
detachable = mock(Detachable.class);
when(detachable.get()).thenReturn(initialValue);
@Test
public void testGetObject() {
- X x = new X();
+ X x = new X();
reset(detachable);
when(detachable.get()).thenReturn(x);
X value = entity.getObject();
@Test
public void testOnDetachReattach() {
- X x = new X();
+ X x = new X();
when(detachable.get()).thenReturn(x);
-
+
entity.detach();
verify(detachable).detach();
-
+
reset(detachable);
when(detachable.get()).thenReturn(x);
-
+
X y = entity.getObject();
- assertSame(x, y);
+ assertSame(x, y);
verify(detachable).get();
}
@Test(expected = UnsupportedOperationException.class)
public void testSetObject() {
- X x = new X();
+ X x = new X();
entity.setObject(x);
}
}
import org.apache.wicket.markup.html.WebPage;
-public class MyPage extends WebPage {
- public MyPage() {
- OpenTransactionInViewRequestCycleTest.BEHAVIOR.run();
+public class MyPage extends WebPage {
+ public MyPage() {
+ OpenTransactionInViewRequestCycleTest.getBEHAVIOR().run();
}
}
\ No newline at end of file
private UserTransaction userTransaction;
private WicketTester wicket;
- public static Runnable BEHAVIOR;
+ private static Runnable BEHAVIOR;
+
+ public static Runnable getBEHAVIOR() {
+ return BEHAVIOR;
+ }
@Before
public void setUp() throws Exception {