package org.gap.jseed;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.gap.jseed.annotation.Bean;
import org.gap.jseed.annotation.BeanInvocationHandler;
import org.gap.jseed.annotation.BeanValidator;
import org.junit.Before;
import org.junit.Test;
public class BeanInjectorTest extends AbstractInjectorHelper {
@Before
public void setUp() {
init();
injector.registerClass(Bean.class, BeanInvocationHandler.class, new BeanValidator());
}
@Test
public void injectBean() throws InstantiationException, IllegalAccessException {
Interface instance = getInstance(Interface.class);
instance.setString("hello");
assertEquals("hello", instance.getString());
assertEquals(0, instance.getInt());
instance.setInt(15);
assertEquals(15, instance.getInt());
instance.setInt(-3);
assertEquals(-3, instance.getInt());
assertFalse(instance.isTruth());
instance.setTruth(true);
assertTrue(instance.isTruth());
}
@Test
public void equality() throws InstantiationException, IllegalAccessException {
Class<? extends Interface> inject = injector.inject(Interface.class);
Interface first = inject.newInstance();
Interface second = inject.newInstance();
first.setString("hi");
second.setString("hey");
assertFalse(first.equals(second));
assertFalse(first.hashCode() == second.hashCode());
second.setString("hi");
assertTrue(first.equals(second));
assertEquals("hashcodes should also be equal", first.hashCode(), second.hashCode());
second.setInt(15);
assertFalse("the two instances should not be equal", first.equals(second));
assertNotSame(first.hashCode(), second.hashCode());
}
@Test
public void collections() throws InstantiationException, IllegalAccessException {
CollectionBean bean = getInstance(CollectionBean.class);
assertTrue(bean.getItems().isEmpty());
bean.addItem("hello");
bean.addItem("bye");
assertEquals(2, bean.getItems().size());
bean.removeItem("hello");
assertEquals(1, bean.getItems().size());
}
@Test
public void primitiveCheck() throws InstantiationException, IllegalAccessException {
Primitives primitive = getInstance(Primitives.class);
assertFalse(primitive.getBoolean());
assertEquals(0, primitive.getByte());
assertEquals("Expected '0' but was [" + primitive.getChar() + "]", '\u0000', primitive.getChar());
}
@Test
public void theToString() throws InstantiationException, IllegalAccessException {
Interface instance = getInstance(Interface.class);
instance.toString();
instance.setInt(15);
instance.toString();
instance.setString("hello to you");
instance.toString();
}
@Test
public void listeners() throws InstantiationException, IllegalAccessException {
final boolean[] isCalled = new boolean[1];
Broadcast instance = getInstance(Broadcast.class);
PropertyChangeListener listener = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
assertEquals("String", evt.getPropertyName());
assertEquals("hey", evt.getNewValue());
isCalled[0] = true;
}
};
instance.addPropertyChangeListener(listener);
instance.setString("hey");
assertTrue("Listener was not called", isCalled[0]);
isCalled[0] = false;
instance.setString("hey");
assertFalse("Listener is not called on setting the same value", isCalled[0]);
instance.removePropertyChangeListener(listener);
instance.setString("wassup");
assertFalse("removing the listener ensures not property change", isCalled[0]);
instance.addPropertyChangeListener(listener);
instance.setString("hey");
assertTrue("Listener was not called", isCalled[0]);
}
@Test(expected=InjectCodeException.class)
public void injectBadBean() throws InstantiationException, IllegalAccessException {
getInstance(BadBean.class);
}
@Test(expected=InjectCodeException.class)
public void injectAnotherBadBean() throws Exception {
try {
getInstance(OtherBean.class);
} catch (Exception e) {
System.out.println(e.getMessage());
throw e;
}
}
@Test(expected=InjectCodeException.class)
public void injectTooManyBean() throws Exception {
try {
getInstance(TooManyBad.class);
} catch (Exception e) {
System.out.println(e.getMessage());
throw e;
}
}
@Bean
public interface Interface {
void setString(String value);
String getString();
void setInt(int x);
int getInt();
void setTruth(boolean value);
boolean isTruth();
}
@Bean
public interface Broadcast {
void setString(String value);
String getString();
void addPropertyChangeListener(PropertyChangeListener listener);
void removePropertyChangeListener(PropertyChangeListener listener);
}
@Bean
public interface OtherBean {
String getValue();
}
@Bean
public interface TooManyBad {
String getValue();
String getValue(int x);
void setValue(String value);
void setValue(int x, String value);
}
@Bean
public interface BadBean {
void setString(String value);
void setInt(int x);
int getInt();
}
@Bean
public interface Primitives {
void setChar(char x);
char getChar();
void setByte(byte x);
byte getByte();
void setBoolean(boolean x);
boolean getBoolean();
}
@Bean
public interface CollectionBean {
void addItem(Object item);
void removeItem(Object item);
java.util.Collection<Object> getItems();
}
}