Пример #1
1
  public void testCopySameType() {
    Foo foo1 = new Foo();
    Foo foo2 = new Foo();

    Map context = Ognl.createDefaultContext(foo1);

    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.FEBRUARY);
    cal.set(Calendar.DAY_OF_MONTH, 12);
    cal.set(Calendar.YEAR, 1982);

    foo1.setTitle("blah");
    foo1.setNumber(1);
    foo1.setPoints(new long[] {1, 2, 3});
    foo1.setBirthday(cal.getTime());
    foo1.setUseful(false);

    ognlUtil.copy(foo1, foo2, context);

    assertEquals(foo1.getTitle(), foo2.getTitle());
    assertEquals(foo1.getNumber(), foo2.getNumber());
    assertEquals(foo1.getPoints(), foo2.getPoints());
    assertEquals(foo1.getBirthday(), foo2.getBirthday());
    assertEquals(foo1.isUseful(), foo2.isUseful());
  }
Пример #2
0
 public void testSetProperty() {
   Foo foo = new Foo();
   Map context = Ognl.createDefaultContext(foo);
   assertFalse(123456 == foo.getNumber());
   ognlUtil.setProperty("number", "123456", foo, context);
   assertEquals(123456, foo.getNumber());
 }
Пример #3
0
  public void testSetCoercion2() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("sampleBean", SampleBean.class);

    Serializable s = compileSetExpression("sampleBean.map2['bleh']", ctx);

    Foo foo = new Foo();
    executeSetExpression(s, foo, "12");

    assertEquals(12, foo.getSampleBean().getMap2().get("bleh").intValue());

    foo = new Foo();
    executeSetExpression(s, foo, "13");

    assertEquals(13, foo.getSampleBean().getMap2().get("bleh").intValue());

    OptimizerFactory.setDefaultOptimizer("ASM");

    ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("sampleBean", SampleBean.class);

    s = compileSetExpression("sampleBean.map2['bleh']", ctx);

    foo = new Foo();
    executeSetExpression(s, foo, "12");

    assertEquals(12, foo.getSampleBean().getMap2().get("bleh").intValue());

    executeSetExpression(s, foo, new Integer(12));

    assertEquals(12, foo.getSampleBean().getMap2().get("bleh").intValue());
  }
Пример #4
0
  public void testOgnlHandlesCrapAtTheEndOfANumber() {
    Foo foo = new Foo();
    Map<String, Object> context = Ognl.createDefaultContext(foo);

    Map<String, Object> props = new HashMap<String, Object>();
    props.put("aLong", "123a");

    ognlUtil.setProperties(props, foo, context);
    assertEquals(0, foo.getALong());
  }
Пример #5
0
  public void testSetPropertiesString() {
    Foo foo = new Foo();

    Map context = Ognl.createDefaultContext(foo);

    Map props = new HashMap();
    props.put("title", "this is a title");
    ognlUtil.setProperties(props, foo, context);

    assertEquals(foo.getTitle(), "this is a title");
  }
Пример #6
0
  public void testSetPropertiesInt() {
    Foo foo = new Foo();

    Map context = Ognl.createDefaultContext(foo);

    Map props = new HashMap();
    props.put("number", "2");
    ognlUtil.setProperties(props, foo, context);

    assertEquals(2, foo.getNumber());
  }
Пример #7
0
  public void testSetCoercion() {
    Serializable s = compileSetExpression("name");

    Foo foo = new Foo();
    executeSetExpression(s, foo, 12);
    assertEquals("12", foo.getName());

    foo = new Foo();
    setProperty(foo, "name", 12);
    assertEquals("12", foo.getName());
  }
Пример #8
0
  public void testDeepSetting() {
    Foo foo = new Foo();
    foo.setBar(new Bar());

    Map<String, Object> context = Ognl.createDefaultContext(foo);

    Map<String, Object> props = new HashMap();
    props.put("bar.title", "i am barbaz");
    ognlUtil.setProperties(props, foo, context);

    assertEquals(foo.getBar().getTitle(), "i am barbaz");
  }
Пример #9
0
  public static void main(String[] args) throws Exception {
    ByteArrayOutputStream bout;
    ByteArrayInputStream bin;
    ObjectOutputStream oout;
    ObjectInputStream oin;
    FileInputStream fin;
    File foof;
    CustomOutputStream cout;
    CustomInputStream cin;

    // test for backwards compatibility
    bout = new ByteArrayOutputStream();
    foof = new File(System.getProperty("test.src", "."), "Foo.ser");
    fin = new FileInputStream(foof);
    while (fin.available() > 0) bout.write(fin.read());
    byte[] buf1 = bout.toByteArray();

    bout = new ByteArrayOutputStream();
    oout = new ObjectOutputStream(bout);
    Foo foo = new Foo();
    oout.writeObject(foo);
    oout.flush();
    byte[] buf2 = bout.toByteArray();

    if (!Arrays.equals(buf1, buf2)) throw new Error("Incompatible stream format (write)");

    fin = new FileInputStream(foof);
    oin = new ObjectInputStream(fin);
    Foo foocopy = (Foo) oin.readObject();
    if (!foo.equals(foocopy)) throw new Error("Incompatible stream format (read)");

    // make sure write hook not called when old protocol in use
    bout = new ByteArrayOutputStream();
    cout = new CustomOutputStream(bout);
    cout.useProtocolVersion(PROTOCOL_VERSION_1);
    cout.writeObject(foo);
    if (cout.hookCalled) throw new Error("write descriptor hook should not be called");

    // write custom class descriptor representations
    bout = new ByteArrayOutputStream();
    cout = new CustomOutputStream(bout);
    cout.writeObject(foo);
    cout.flush();
    bin = new ByteArrayInputStream(bout.toByteArray());
    cin = new CustomInputStream(bin);
    foocopy = (Foo) cin.readObject();
    if (!cout.hookCalled) throw new Error("write descriptor hook never called");
    if (!cin.hookCalled) throw new Error("read descriptor hook never called");
    if (!foo.equals(foocopy)) throw new Error("serialization failed when hooks active");
  }
Пример #10
0
  public void testNullProperties() {
    Foo foo = new Foo();
    foo.setALong(88);

    Map context = Ognl.createDefaultContext(foo);

    ognlUtil.setProperties(null, foo, context);
    assertEquals(88, foo.getALong());

    Map props = new HashMap();
    props.put("aLong", "99");
    ognlUtil.setProperties(props, foo, context);
    assertEquals(99, foo.getALong());
  }
Пример #11
0
  public void testSetPropertiesLongArray() {
    Foo foo = new Foo();

    Map context = Ognl.createDefaultContext(foo);

    Map props = new HashMap();
    props.put("points", new String[] {"1", "2"});
    ognlUtil.setProperties(props, foo, context);

    assertNotNull(foo.getPoints());
    assertEquals(2, foo.getPoints().length);
    assertEquals(1, foo.getPoints()[0]);
    assertEquals(2, foo.getPoints()[1]);
  }
Пример #12
0
  public void testStringToLong() {
    Foo foo = new Foo();

    Map context = Ognl.createDefaultContext(foo);

    Map props = new HashMap();
    props.put("aLong", "123");

    ognlUtil.setProperties(props, foo, context);
    assertEquals(123, foo.getALong());

    props.put("aLong", new String[] {"123"});

    foo.setALong(0);
    ognlUtil.setProperties(props, foo, context);
    assertEquals(123, foo.getALong());
  }
Пример #13
0
  public void testSetPropertiesBoolean() {
    Foo foo = new Foo();

    Map context = Ognl.createDefaultContext(foo);

    Map props = new HashMap();
    props.put("useful", "true");
    ognlUtil.setProperties(props, foo, context);

    assertEquals(true, foo.isUseful());

    props = new HashMap();
    props.put("useful", "false");
    ognlUtil.setProperties(props, foo, context);

    assertEquals(false, foo.isUseful());
  }
Пример #14
0
  public void testFieldCoercion1() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("bar", Bar.class);

    Serializable s = compileSetExpression("bar.assignTest", ctx);

    Foo foo = new Foo();

    executeSetExpression(s, foo, 12);

    assertEquals("12", foo.getBar().getAssignTest());

    foo = new Foo();

    executeSetExpression(s, foo, 13);

    assertEquals("13", foo.getBar().getAssignTest());

    OptimizerFactory.setDefaultOptimizer("ASM");

    ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("bar", Bar.class);

    s = compileSetExpression("bar.assignTest", ctx);

    foo = new Foo();

    executeSetExpression(s, foo, 12);

    assertEquals("12", foo.getBar().getAssignTest());

    executeSetExpression(s, foo, 13);

    assertEquals("13", foo.getBar().getAssignTest());
  }
Пример #15
0
  public void testListCoercion() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("bar", Bar.class);

    Serializable s = compileSetExpression("bar.testList[0]", ctx);

    Foo foo = new Foo();
    foo.getBar().getTestList().add(new Integer(-1));

    executeSetExpression(s, foo, "12");

    assertEquals(12, foo.getBar().getTestList().get(0).intValue());

    foo = new Foo();
    foo.getBar().getTestList().add(new Integer(-1));

    executeSetExpression(s, foo, "13");

    assertEquals(13, foo.getBar().getTestList().get(0).intValue());

    OptimizerFactory.setDefaultOptimizer("ASM");

    ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("bar", Bar.class);

    s = compileSetExpression("bar.testList[0]", ctx);

    foo = new Foo();
    foo.getBar().getTestList().add(new Integer(-1));

    executeSetExpression(s, foo, "12");

    assertEquals(12, foo.getBar().getTestList().get(0).intValue());

    executeSetExpression(s, foo, "13");

    assertEquals(13, foo.getBar().getTestList().get(0).intValue());
  }
Пример #16
0
  public void testCopyUnevenObjects() {
    Foo foo = new Foo();
    Bar bar = new Bar();

    Map<String, Object> context = Ognl.createDefaultContext(foo);

    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.FEBRUARY);
    cal.set(Calendar.DAY_OF_MONTH, 12);
    cal.set(Calendar.YEAR, 1982);

    foo.setTitle("blah");
    foo.setNumber(1);
    foo.setPoints(new long[] {1, 2, 3});
    foo.setBirthday(cal.getTime());
    foo.setUseful(false);

    ognlUtil.copy(foo, bar, context);

    assertEquals(foo.getTitle(), bar.getTitle());
    assertEquals(0, bar.getSomethingElse());
  }
Пример #17
0
  public void testGetBeanMap() throws Exception {
    Bar bar = new Bar();
    bar.setTitle("I have beer");

    Foo foo = new Foo();
    foo.setALong(123);
    foo.setNumber(44);
    foo.setBar(bar);
    foo.setTitle("Hello Santa");
    foo.setUseful(true);

    // just do some of the 15 tests
    Map beans = ognlUtil.getBeanMap(foo);
    assertNotNull(beans);
    assertEquals(19, beans.size());
    assertEquals("Hello Santa", beans.get("title"));
    assertEquals(new Long("123"), beans.get("ALong"));
    assertEquals(new Integer("44"), beans.get("number"));
    assertEquals(bar, beans.get("bar"));
    assertEquals(Boolean.TRUE, beans.get("useful"));
  }
Пример #18
0
  public void testIncudeExcludes() {

    Foo foo1 = new Foo();
    Foo foo2 = new Foo();

    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.FEBRUARY);
    cal.set(Calendar.DAY_OF_MONTH, 12);
    cal.set(Calendar.YEAR, 1982);

    foo1.setPoints(new long[] {1, 2, 3});
    foo1.setBirthday(cal.getTime());
    foo1.setUseful(false);

    foo1.setTitle("foo1 title");
    foo1.setNumber(1);

    foo2.setTitle("foo2 title");
    foo2.setNumber(2);

    Map<String, Object> context = Ognl.createDefaultContext(foo1);

    List<String> excludes = new ArrayList<String>();
    excludes.add("title");
    excludes.add("number");

    ognlUtil.copy(foo1, foo2, context, excludes, null);
    // these values should remain unchanged in foo2
    assertEquals(foo2.getTitle(), "foo2 title");
    assertEquals(foo2.getNumber(), 2);

    // these values should be changed/copied
    assertEquals(foo1.getPoints(), foo2.getPoints());
    assertEquals(foo1.getBirthday(), foo2.getBirthday());
    assertEquals(foo1.isUseful(), foo2.isUseful());

    Bar b1 = new Bar();
    Bar b2 = new Bar();

    b1.setTitle("bar1 title");
    b1.setSomethingElse(10);

    b1.setId(new Long(1));

    b2.setTitle("");
    b2.setId(new Long(2));

    context = Ognl.createDefaultContext(b1);
    List<String> includes = new ArrayList<String>();
    includes.add("title");
    includes.add("somethingElse");

    ognlUtil.copy(b1, b2, context, null, includes);
    // includes properties got copied
    assertEquals(b1.getTitle(), b2.getTitle());
    assertEquals(b1.getSomethingElse(), b2.getSomethingElse());

    // id properties did not
    assertEquals(b2.getId(), new Long(2));
  }
Пример #19
0
  public void testSetPropertiesDate() {
    Locale orig = Locale.getDefault();
    Locale.setDefault(Locale.US);

    Foo foo = new Foo();

    Map context = Ognl.createDefaultContext(foo);

    Map props = new HashMap();
    props.put("birthday", "02/12/1982");
    // US style test
    ognlUtil.setProperties(props, foo, context);

    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.FEBRUARY);
    cal.set(Calendar.DAY_OF_MONTH, 12);
    cal.set(Calendar.YEAR, 1982);

    assertEquals(cal.getTime(), foo.getBirthday());

    Locale.setDefault(Locale.UK);
    // UK style test
    props.put("event", "18/10/2006 14:23:45");
    props.put("meeting", "09/09/2006 14:30");
    ognlUtil.setProperties(props, foo, context);

    cal = Calendar.getInstance();
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.OCTOBER);
    cal.set(Calendar.DAY_OF_MONTH, 18);
    cal.set(Calendar.YEAR, 2006);
    cal.set(Calendar.HOUR_OF_DAY, 14);
    cal.set(Calendar.MINUTE, 23);
    cal.set(Calendar.SECOND, 45);

    assertEquals(cal.getTime(), foo.getEvent());

    cal = Calendar.getInstance();
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.SEPTEMBER);
    cal.set(Calendar.DAY_OF_MONTH, 9);
    cal.set(Calendar.YEAR, 2006);
    cal.set(Calendar.HOUR_OF_DAY, 14);
    cal.set(Calendar.MINUTE, 30);

    assertEquals(cal.getTime(), foo.getMeeting());

    Locale.setDefault(orig);

    Locale.setDefault(orig);

    // test RFC 3339 date format for JSON
    props.put("event", "1996-12-19T16:39:57Z");
    ognlUtil.setProperties(props, foo, context);

    cal = Calendar.getInstance();
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.DECEMBER);
    cal.set(Calendar.DAY_OF_MONTH, 19);
    cal.set(Calendar.YEAR, 1996);
    cal.set(Calendar.HOUR_OF_DAY, 16);
    cal.set(Calendar.MINUTE, 39);
    cal.set(Calendar.SECOND, 57);

    assertEquals(cal.getTime(), foo.getEvent());

    // test setting a calendar property
    props.put("calendar", "1996-12-19T16:39:57Z");
    ognlUtil.setProperties(props, foo, context);
    assertEquals(cal, foo.getCalendar());
  }