⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ognlvaluestacktest.java

📁 在Struts2中的jar包xwork的源代码.版本为2.0.7
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public void testNullMethod() {        Dog dog = new Dog();        OgnlValueStack stack = new OgnlValueStack();        stack.push(dog);        assertNull(stack.findValue("nullMethod()"));        assertNull(stack.findValue("@com.opensymphony.xwork2.util.OgnlValueStackTest@staticNullMethod()"));    }    public void testPetSoarBug() {        Cat cat = new Cat();        cat.setFoo(new Foo());        Bar bar = new Bar();        bar.setTitle("bar");        bar.setSomethingElse(123);        cat.getFoo().setBar(bar);        OgnlValueStack vs = new OgnlValueStack();        vs.push(cat);        assertEquals("bar:123", vs.findValue("foo.bar", String.class));    }    public void testPrimitiveSettingWithInvalidValueAddsFieldErrorInDevMode() {        SimpleAction action = new SimpleAction();        OgnlValueStack stack = new OgnlValueStack();        stack.getContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);        stack.setDevMode("true");        stack.push(action);                try {            stack.setValue("bar", "3x");            fail("Attempt to set 'bar' int property to '3x' should result in RuntimeException");        }        catch(RuntimeException re) {            assertTrue(true);        }        Map conversionErrors = (Map) stack.getContext().get(ActionContext.CONVERSION_ERRORS);        assertTrue(conversionErrors.containsKey("bar"));    }    public void testPrimitiveSettingWithInvalidValueAddsFieldErrorInNonDevMode() {        SimpleAction action = new SimpleAction();        OgnlValueStack stack = new OgnlValueStack();        stack.getContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);        stack.setDevMode("false");        stack.push(action);        stack.setValue("bar", "3x");        Map conversionErrors = (Map) stack.getContext().get(ActionContext.CONVERSION_ERRORS);        assertTrue(conversionErrors.containsKey("bar"));    }        public void testObjectSettingWithInvalidValueDoesNotCauseSetCalledWithNull() {        SimpleAction action = new SimpleAction();        action.setBean(new TestBean());        OgnlValueStack stack = new OgnlValueStack();        stack.getContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);        stack.push(action);        try {            stack.setValue("bean", "foobar", true);            fail("Should have thrown a type conversion exception");        } catch (XWorkException e) {            // expected        }        Map conversionErrors = (Map) stack.getContext().get(ActionContext.CONVERSION_ERRORS);        assertTrue(conversionErrors.containsKey("bean"));        assertNotNull(action.getBean());    }    public void testSerializable() throws IOException, ClassNotFoundException {        OgnlValueStack vs = new OgnlValueStack();        Dog dog = new Dog();        dog.setAge(12);        dog.setName("Rover");        vs.push(dog);        assertEquals("Rover", vs.findValue("name", String.class));        ByteArrayOutputStream baos = new ByteArrayOutputStream();        ObjectOutputStream oos = new ObjectOutputStream(baos);        oos.writeObject(vs);        oos.flush();        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());        ObjectInputStream ois = new ObjectInputStream(bais);        OgnlValueStack newVs = (OgnlValueStack) ois.readObject();        assertEquals("Rover", newVs.findValue("name", String.class));    }        public void testSetAfterPush() {    	OgnlValueStack vs = new OgnlValueStack();    	Dog d=new Dog();    	d.setName("Rover");    	vs.push(d);    	    	vs.set("name","Bill");    	assertEquals("Bill", vs.findValue("name"));    	    }        public void testSetBarAsString() {        Foo foo = new Foo();        OgnlValueStack vs = new OgnlValueStack();        vs.push(foo);        vs.setValue("bar", "bar:123");        assertEquals("bar", foo.getBar().getTitle());        assertEquals(123, foo.getBar().getSomethingElse());    }    public void testSetBeforePush() {    	OgnlValueStack vs = new OgnlValueStack();    	    	vs.set("name","Bill");    	Dog d=new Dog();    	d.setName("Rover");    	vs.push(d);    	    	assertEquals("Rover", vs.findValue("name"));    	    }        public void testSetDeepBarAsString() {        Foo foo = new Foo();        Foo foo2 = new Foo();        foo.setChild(foo2);        OgnlValueStack vs = new OgnlValueStack();        vs.push(foo);        vs.setValue("child.bar", "bar:123");        assertEquals("bar", foo.getChild().getBar().getTitle());        assertEquals(123, foo.getChild().getBar().getSomethingElse());    }    public void testSetNullList() {        Foo foo = new Foo();        OgnlValueStack vs = new OgnlValueStack();        vs.getContext().put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.TRUE);        vs.push(foo);        vs.setValue("cats[0].name", "Cat One");        vs.setValue("cats[1].name", "Cat Two");        assertNotNull(foo.getCats());        assertEquals(2, foo.getCats().size());        assertEquals("Cat One", ((Cat) foo.getCats().get(0)).getName());        assertEquals("Cat Two", ((Cat) foo.getCats().get(1)).getName());        vs.setValue("cats[0].foo.cats[1].name", "Deep null cat");        assertNotNull(((Cat) foo.getCats().get(0)).getFoo());        assertNotNull(((Cat) foo.getCats().get(0)).getFoo().getCats());        assertNotNull(((Cat) foo.getCats().get(0)).getFoo().getCats().get(1));        assertEquals("Deep null cat", ((Cat) ((Cat) foo.getCats().get(0)).getFoo().getCats().get(1)).name);    }        public void testSetMultiple() {    	OgnlValueStack vs = new OgnlValueStack();    	int origSize=vs.getRoot().size();    	vs.set("something",new Object());    	vs.set("somethingElse",new Object());    	vs.set("yetSomethingElse",new Object());    	assertEquals(origSize+1,vs.getRoot().size());    	    }        public void testSetNullMap() {        Foo foo = new Foo();        OgnlValueStack vs = new OgnlValueStack();        vs.getContext().put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.TRUE);        vs.push(foo);        vs.setValue("catMap['One'].name", "Cat One");        vs.setValue("catMap['Two'].name", "Cat Two");        assertNotNull(foo.getCatMap());        assertEquals(2, foo.getCatMap().size());        assertEquals("Cat One", ((Cat) foo.getCatMap().get("One")).getName());        assertEquals("Cat Two", ((Cat) foo.getCatMap().get("Two")).getName());        vs.setValue("catMap['One'].foo.catMap['Two'].name", "Deep null cat");        assertNotNull(((Cat) foo.getCatMap().get("One")).getFoo());        assertNotNull(((Cat) foo.getCatMap().get("One")).getFoo().getCatMap());        assertNotNull(((Cat) foo.getCatMap().get("One")).getFoo().getCatMap().get("Two"));        assertEquals("Deep null cat", ((Cat) ((Cat) foo.getCatMap().get("One")).getFoo().getCatMap().get("Two")).name);    }    public void testSetReallyDeepBarAsString() {        Foo foo = new Foo();        Foo foo2 = new Foo();        foo.setChild(foo2);        Foo foo3 = new Foo();        foo2.setChild(foo3);        OgnlValueStack vs = new OgnlValueStack();        vs.push(foo);        vs.setValue("child.child.bar", "bar:123");        assertEquals("bar", foo.getChild().getChild().getBar().getTitle());        assertEquals(123, foo.getChild().getChild().getBar().getSomethingElse());    }    public void testSettingDogGender() {        OgnlValueStack vs = new OgnlValueStack();        Dog dog = new Dog();        vs.push(dog);        vs.setValue("male", "false");        assertEquals(false, dog.isMale());    }    public void testStatics() {        OgnlValueStack vs = new OgnlValueStack();        Cat cat = new Cat();        vs.push(cat);        Dog dog = new Dog();        dog.setAge(12);        dog.setName("Rover");        vs.push(dog);        assertEquals("Canine", vs.findValue("@vs@SCIENTIFIC_NAME"));        assertEquals("Canine", vs.findValue("@vs1@SCIENTIFIC_NAME"));        assertEquals("Feline", vs.findValue("@vs2@SCIENTIFIC_NAME"));        assertEquals(new Integer(BigDecimal.ROUND_HALF_DOWN), vs.findValue("@java.math.BigDecimal@ROUND_HALF_DOWN"));        assertNull(vs.findValue("@vs3@BLAH"));        assertNull(vs.findValue("@com.nothing.here.Nothing@BLAH"));    }    public void testTop() {        OgnlValueStack vs = new OgnlValueStack();        Dog dog1 = new Dog();        dog1.setAge(12);        dog1.setName("Rover");        Dog dog2 = new Dog();        dog2.setAge(1);        dog2.setName("Jack");        vs.push(dog1);        vs.push(dog2);        assertEquals(dog2, vs.findValue("top"));        assertEquals("Jack", vs.findValue("top.name"));    }    public void testTopIsDefaultTextProvider() {        OgnlValueStack vs = new OgnlValueStack();        assertEquals(DefaultTextProvider.INSTANCE, vs.findValue("top"));    }    public void testTwoDogs() {        OgnlValueStack vs = new OgnlValueStack();        Dog dog1 = new Dog();        dog1.setAge(12);        dog1.setName("Rover");        Dog dog2 = new Dog();        dog2.setAge(1);        dog2.setName("Jack");        vs.push(dog1);        vs.push(dog2);        assertEquals("Jack", vs.findValue("name"));        assertEquals("Rover", vs.findValue("[1].name"));        assertEquals(dog2, vs.pop());        assertEquals("Rover", vs.findValue("name"));    }    public void testTypeConversionError() {        TestBean bean = new TestBean();        OgnlValueStack stack = new OgnlValueStack();        stack.push(bean);        stack.getContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);        try {            stack.setValue("count", "a", true);            fail("Should have thrown a type conversion exception");        } catch (XWorkException e) {            // expected        }        Map conversionErrors = (Map) stack.getContext().get(ActionContext.CONVERSION_ERRORS);        assertTrue(conversionErrors.containsKey("count"));    }    public void testConstructorWithAStack() {        OgnlValueStack stack = new OgnlValueStack();        stack.push("Hello World");                OgnlValueStack stack2 = new OgnlValueStack(stack);        assertEquals(stack.getRoot(), stack2.getRoot());        assertEquals(stack.peek(), stack2.peek());        assertEquals("Hello World", stack2.pop());                assertNotNull(OgnlValueStack.getAccessor());    }    public void testDefaultType() {        OgnlValueStack stack = new OgnlValueStack();        stack.setDefaultType(String.class);        stack.push("Hello World");                assertEquals("Hello World", stack.findValue("top"));        assertEquals(null, stack.findValue(null));        stack.setDefaultType(Integer.class);        stack.push(new Integer(123));        assertEquals(new Integer(123), stack.findValue("top"));    }        public void testFindString() {        OgnlValueStack stack = new OgnlValueStack();        stack.setDefaultType(Integer.class);        stack.push("Hello World");    	        assertEquals("Hello World", stack.findString("top"));        assertEquals(null, stack.findString(null));    }    public void testExpOverrides() {    	Map overrides = new HashMap();    	overrides.put("claus", "top");    	        OgnlValueStack stack = new OgnlValueStack();        stack.setExprOverrides(overrides);        stack.push("Hello World");                assertEquals("Hello World", stack.findValue("claus"));        assertEquals("Hello World", stack.findString("claus"));        assertEquals("Hello World", stack.findValue("top"));        assertEquals("Hello World", stack.findString("top"));        assertEquals("Hello World", stack.findValue("claus", String.class));        assertEquals("Hello World", stack.findValue("top", String.class));                stack.getContext().put("santa", "Hello Santa");        assertEquals("Hello Santa", stack.findValue("santa", String.class));        assertEquals(null, stack.findValue("unknown", String.class));    }    class BadJavaBean {        private int count;        private int count2;        public void setCount(int count) {            this.count = count;        }        public String getCount() {            return "" + count;        }        public void setCount2(String count2) {            this.count2 = Integer.parseInt(count2);        }        public int getCount2() {            return count2;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -