@Test public void setTargetInModel() { final A[] as = new A[] {new A()}; IModel<A> target = new IModel<A>() { @Override public A getObject() { return as[0]; } @Override public void setObject(A a) { as[0] = a; } @Override public void detach() {} }; LazyModel<A> model = model(from(target)); assertEquals(A.class, model.getObjectClass()); assertEquals("", model.getPath()); A a = new A(); model.setObject(a); assertEquals(a, as[0]); }
@Test public void detach() { final boolean[] detached = {false}; IModel<A> target = new IModel<A>() { @Override public A getObject() { return null; } @Override public void setObject(A a) {} @Override public void detach() { detached[0] = true; } }; LazyModel<A> model = model(from(target)); model.detach(); assertTrue(detached[0]); }
@Test public void propertyReflectionAwareModelNoProperty() throws Exception { A a = new A(); a.b = new B(); LazyModel<C> model = model(from(a).getB().getC(0)); assertNull(model.getPropertyField()); assertNull(model.getPropertyGetter()); assertNull(model.getPropertySetter()); }
@Test public void getOutOfBoundsFromGenericList() { B b = new B(); LazyModel<C> model = model(from(b).getCs().get(0)); assertEquals(C.class, model.getObjectClass()); assertEquals("cs.get(i)", model.getPath()); assertEquals(null, model.getObject()); }
@Test public void getTargetNotBoundFails() { LazyModel<String> model = model(from(String.class)); try { model.getObject(); fail(); } catch (Exception ex) { } }
@Test public void noBindFails() { LazyModel<B> model = model(from(A.class).getB()); try { model.getObject(); fail(); } catch (Exception ex) { } }
@Test public void getIntFromNull() { A a = new A(); LazyModel<Character> model = model(from(a).getB().getCharacter()); assertEquals(Character.TYPE, model.getObjectClass()); assertEquals("b.character", model.getPath()); assertEquals(null, model.getObject()); }
@Test public void bindToObject() { final A a = new A(); a.b = new B(); LazyModel<B> model = model(from(A.class).getB()).bind(a); assertEquals(B.class, model.getObjectClass()); assertEquals("b", model.getPath()); assertEquals(a.b, model.getObject()); }
@Test public void propertyReflectionAwareModel() throws Exception { A a = new A(); a.b = new B(); LazyModel<Character> model = model(from(a).getB().getCharacter()); assertNull(model.getPropertyField()); assertEquals(B.class.getMethod("getCharacter"), model.getPropertyGetter()); assertEquals(B.class.getMethod("setCharacter", Character.TYPE), model.getPropertySetter()); }
@Test public void getTarget() { A a = new A(); LazyModel<A> model = model(from(a)); assertEquals(A.class, model.getObjectClass()); assertEquals("", model.getPath()); assertEquals(a, model.getObject()); }
@Test public void getFromPrivateConstructor() { final A a = new A(); a.p = P.P1; LazyModel<String> model = model(from(a).getP().getString()); assertEquals(String.class, model.getObjectClass()); assertEquals("p.string", model.getPath()); assertEquals(a.p.getString(), model.getObject()); }
@Test public void getEnum() { final A a = new A(); a.e = E.E1; LazyModel<E> model = model(from(a).getE()); assertEquals(E.class, model.getObjectClass()); assertEquals("e", model.getPath()); assertEquals(a.e, model.getObject()); }
@Test public void getFinal() { final A a = new A(); a.f = new F(); LazyModel<F> model = model(from(a).getF()); assertEquals(F.class, model.getObjectClass()); assertEquals("f", model.getPath()); assertEquals(a.f, model.getObject()); }
@Test public void getRawList() { B b = new B(); b.cs.add(new C()); LazyModel<Object> model = model(from(b).getRs().get(0)); assertEquals(Object.class, model.getObjectClass()); assertEquals("rs.get(i)", model.getPath()); assertEquals(b.cs.get(0), model.getObject()); }
@Test public void getObject() { A a = new A(); a.b = new B(); LazyModel<B> model = model(from(a).getB()); assertEquals(B.class, model.getObjectClass()); assertEquals("b", model.getPath()); assertEquals(a.b, model.getObject()); }
@Test public void setBoolean() { A a = new A(); LazyModel<Boolean> model = model(from(a).isBool()); assertEquals(Boolean.TYPE, model.getObjectClass()); assertEquals("bool", model.getPath()); model.setObject(Boolean.TRUE); assertEquals(true, a.bool); }
@Test public void setInt() { A a = new A(); LazyModel<Integer> model = model(from(a).getInteger()); assertEquals(Integer.TYPE, model.getObjectClass()); assertEquals("integer", model.getPath()); model.setObject(Integer.valueOf(42)); assertEquals(42, a.integer); }
@Test public void getInherited() { final A a = new A(); a.b = new B(); a.b.d = new D(); LazyModel<String> model = model(from(a).getB().getD().getString()); assertEquals(String.class, model.getObjectClass()); assertEquals("b.d.string", model.getPath()); assertEquals(a.b.d.string, model.getObject()); }
@Test public void bindToTypeErasedModelWithNull() { LazyModel<B> model = model(from(A.class).getB()).bind(new Model<A>(null)); assertNull(model.getObjectType()); assertNull(model.getObjectClass()); try { model.getPath(); fail(); } catch (WicketRuntimeException ex) { assertEquals("cannot detect target type", ex.getMessage()); } }
@Test public void setValueToGenericMap() { A a = new A(); a.b = new B(); LazyModel<String> model = model(from(a).getB().getStrings().get("key")); assertEquals(String.class, model.getObjectClass()); assertEquals("b.strings.get(O)", model.getPath()); model.setObject("value"); assertEquals("value", a.b.strings.get("key")); }
@Test public void setTargetFails() { A a = new A(); LazyModel<A> model = model(from(a)); assertEquals(A.class, model.getObjectClass()); assertEquals("", model.getPath()); try { model.setObject(new A()); fail(); } catch (UnsupportedOperationException expected) { } }
@Test public void setIntToNullFails() { A a = new A(); LazyModel<Integer> model = model(from(a).getInteger()); assertEquals(Integer.TYPE, model.getObjectClass()); assertEquals("integer", model.getPath()); try { model.setObject(null); fail(); } catch (WicketRuntimeException expected) { } }
@Test public void setCharacterOnNullFails() { A a = new A(); LazyModel<Character> model = model(from(a).getB().getCharacter()); assertEquals(Character.TYPE, model.getObjectClass()); assertEquals("b.character", model.getPath()); try { model.setObject('c'); fail(); } catch (NullPointerException expected) { } }
@Test public void getObjectFromGenericList() { A a = new A(); a.b = new B(); C c = new C(); a.b.cs.add(c); c.string = "string"; LazyModel<C> model = model(from(a).getB().getCs().get(0)); assertEquals(C.class, model.getObjectClass()); assertEquals("b.cs.get(i)", model.getPath()); assertEquals(c, model.getObject()); }
@Test public void setObjectFromEntryFromGenericList() { A a = new A(); a.b = new B(); C c = new C(); a.b.cs.add(c); LazyModel<String> model = model(from(a).getB().getC(0).getString()); assertEquals(String.class, model.getObjectClass()); assertEquals("b.getC(i).string", model.getPath()); model.setObject("string"); assertEquals("string", c.string); }
@Test public void getWithNestedLazyModel() { A a = new A(); a.integer = 0; B b = new B(); C c = new C(); b.cs.add(c); LazyModel<C> model = model(from(b).getC(from(a).getInteger())); assertEquals(C.class, model.getObjectClass()); assertEquals("getC(i)", model.getPath()); assertEquals(c, model.getObject()); }
@Test public void getObjectFromObjectClassAwareModel() { A a = new A(); a.b = new B(); C c = new C(); a.b.cs.add(c); PropertyModel<B> target = new PropertyModel<B>(a, "b"); LazyModel<C> model = model(from(target).getC(0)); assertEquals(C.class, model.getObjectClass()); assertEquals("getC(i)", model.getPath()); assertEquals(a.b.cs.get(0), model.getObject()); }
@Test public void bindToGenericModel() { final A a = new A(); a.b = new B(); LazyModel<B> model = model(from(A.class).getB()) .bind( new AbstractReadOnlyModel<A>() { @Override public A getObject() { return a; } }); assertEquals(B.class, model.getObjectClass()); assertEquals("b", model.getPath()); assertEquals(a.b, model.getObject()); }
@Test public void getEntryFromGenericList() { final List<C> cs = new ArrayList<C>(); C c = new C(); cs.add(c); IModel<List<C>> target = new AbstractReadOnlyModel<List<C>>() { @Override public List<C> getObject() { return cs; } }; LazyModel<C> model = model(from(target).get(0)); assertEquals(C.class, model.getObjectClass()); assertEquals("get(i)", model.getPath()); assertEquals(c, model.getObject()); }
@Test public void getObjectFromGenericModel() { final A a = new A(); a.b = new B(); C c = new C(); a.b.cs.add(c); IModel<A> target = new AbstractReadOnlyModel<A>() { @Override public A getObject() { return a; } }; LazyModel<C> model = model(from(target).getB().getC(0)); assertEquals(C.class, model.getObjectClass()); assertEquals("b.getC(i)", model.getPath()); assertEquals(a.b.cs.get(0), model.getObject()); }