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

📄 ognlvaluestacktest.java

📁 在Struts2中的jar包xwork的源代码.版本为2.0.7
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 2002-2006 by OpenSymphony * All rights reserved. */package com.opensymphony.xwork2.util;import com.opensymphony.xwork2.*;import com.opensymphony.xwork2.test.TestBean2;import java.io.*;import java.math.BigDecimal;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.Map;/** * Unit test for OgnlValueStack. */public class OgnlValueStackTest extends XWorkTestCase {    public static Integer staticNullMethod() {        return null;    }            public void testExpOverridesCanStackExpUp() throws Exception {    	Map expr1 = new LinkedHashMap();    	expr1.put("expr1", "'expr1value'");    	    	OgnlValueStack vs = new OgnlValueStack();    	vs.setExprOverrides(expr1);    	    	assertEquals(vs.findValue("expr1"), "expr1value");    	    	Map expr2 = new LinkedHashMap();    	expr2.put("expr2", "'expr2value'");    	expr2.put("expr3", "'expr3value'");    	vs.setExprOverrides(expr2);    	    	assertEquals(vs.findValue("expr2"), "expr2value");    	assertEquals(vs.findValue("expr3"), "expr3value");    }        public void testArrayAsString() {        OgnlValueStack vs = new OgnlValueStack();        Dog dog = new Dog();        dog.setAge(12);        dog.setName("Rover");        dog.setChildAges(new int[]{1, 2});        vs.push(dog);        assertEquals("1, 2", vs.findValue("childAges", String.class));    }    public void testBasic() {        OgnlValueStack vs = new OgnlValueStack();        Dog dog = new Dog();        dog.setAge(12);        dog.setName("Rover");        vs.push(dog);        assertEquals("Rover", vs.findValue("name", String.class));    }        public void testStatic() {        OgnlValueStack vs = new OgnlValueStack();        Dog dog = new Dog();        dog.setDeity("fido");        vs.push(dog);        assertEquals("fido", vs.findValue("@com.opensymphony.xwork2.util.Dog@getDeity()", String.class));    }        public void testStaticMethodDisallow() {        OgnlValueStack.setAllowStaticMethodAccess(false);        try {            OgnlValueStack vs = new OgnlValueStack();                Dog dog = new Dog();            dog.setDeity("fido");            vs.push(dog);            assertNull(vs.findValue("@com.opensymphony.xwork2.util.Dog@getDeity()", String.class));        } finally {            OgnlValueStack.setAllowStaticMethodAccess(true);        }    }        public void testBasicSet() {    	OgnlValueStack vs = new OgnlValueStack();            	Dog dog = new Dog();        dog.setAge(12);        dog.setName("Rover");        vs.set("dog",dog);        assertEquals("Rover", vs.findValue("dog.name", String.class));    }    public void testCallMethodOnNullObject() {        OgnlValueStack stack = new OgnlValueStack();        assertNull(stack.findValue("foo.size()"));    }    public void testCallMethodThatThrowsExceptionTwice() {        SimpleAction action = new SimpleAction();        OgnlValueStack stack = new OgnlValueStack();        stack.push(action);        action.setThrowException(true);        assertNull(stack.findValue("exceptionMethod1()"));        action.setThrowException(false);        assertEquals("OK", stack.findValue("exceptionMethod()"));    }            public void testCallMethodWithNullArg() {        SimpleAction action = new SimpleAction();        OgnlValueStack stack = new OgnlValueStack();        stack.push(action);        stack.findValue("setName(blah)");        assertNull(action.getName());        action.setBlah("blah");        stack.findValue("setName(blah)");        assertEquals("blah", action.getName());    }    public void testConvertStringArrayToList() {        Foo foo = new Foo();        OgnlValueStack vs = new OgnlValueStack();        vs.push(foo);        vs.setValue("strings", new String[]{"one", "two"});        assertNotNull(foo.getStrings());        assertEquals("one", foo.getStrings().get(0));        assertEquals("two", foo.getStrings().get(1));    }    public void testFindValueWithConversion() {        // register converter        TestBean2 tb2 = new TestBean2();        OgnlValueStack stack = new OgnlValueStack();        stack.push(tb2);        Map myContext = stack.getContext();        Map props = new HashMap();        props.put("cat", "Kitty");        OgnlUtil.setProperties(props, tb2, myContext);        // expect String to be converted into a Cat        assertEquals("Kitty", tb2.getCat().getName());        // findValue should be able to access the name        Object value = stack.findValue("cat.name == 'Kitty'", Boolean.class);        assertNotNull(value);        assertEquals(Boolean.class, value.getClass());        assertEquals(Boolean.TRUE, value);        value = stack.findValue("cat == null", Boolean.class);        assertNotNull(value);        assertEquals(Boolean.class, value.getClass());        assertEquals(Boolean.FALSE, value);    }    public void testDeepProperties() {        OgnlValueStack vs = new OgnlValueStack();        Cat cat = new Cat();        cat.setName("Smokey");        Dog dog = new Dog();        dog.setAge(12);        dog.setName("Rover");        dog.setChildAges(new int[]{1, 2});        dog.setHates(cat);        vs.push(dog);        assertEquals("Smokey", vs.findValue("hates.name", String.class));    }    public void testFooBarAsString() {        OgnlValueStack vs = new OgnlValueStack();        Foo foo = new Foo();        Bar bar = new Bar();        bar.setTitle("blah");        bar.setSomethingElse(123);        foo.setBar(bar);        vs.push(foo);        assertEquals("blah:123", vs.findValue("bar", String.class));    }    public void testGetBarAsString() {        Foo foo = new Foo();        Bar bar = new Bar();        bar.setTitle("bar");        bar.setSomethingElse(123);        foo.setBar(bar);        OgnlValueStack vs = new OgnlValueStack();        vs.push(foo);        String output = (String) vs.findValue("bar", String.class);        assertEquals("bar:123", output);    }    public void testGetComplexBarAsString() {        // children foo->foo->foo        Foo foo = new Foo();        Foo foo2 = new Foo();        foo.setChild(foo2);        Foo foo3 = new Foo();        foo2.setChild(foo3);        // relatives        Foo fooA = new Foo();        foo.setRelatives(new Foo[]{fooA});        Foo fooB = new Foo();        foo2.setRelatives(new Foo[]{fooB});        Foo fooC = new Foo();        foo3.setRelatives(new Foo[]{fooC});        // the bar        Bar bar = new Bar();        bar.setTitle("bar");        bar.setSomethingElse(123);        // now place the bar all over        foo.setBar(bar);        foo2.setBar(bar);        foo3.setBar(bar);        fooA.setBar(bar);        fooB.setBar(bar);        fooC.setBar(bar);        OgnlValueStack vs = new OgnlValueStack();        vs.push(foo);        vs.getContext().put("foo", foo);        assertEquals("bar:123", vs.findValue("#foo.bar", String.class));        assertEquals("bar:123", vs.findValue("bar", String.class));        assertEquals("bar:123", vs.findValue("child.bar", String.class));        assertEquals("bar:123", vs.findValue("child.child.bar", String.class));        assertEquals("bar:123", vs.findValue("relatives[0].bar", String.class));        assertEquals("bar:123", vs.findValue("child.relatives[0].bar", String.class));        assertEquals("bar:123", vs.findValue("child.child.relatives[0].bar", String.class));        vs.push(vs.findValue("child"));        assertEquals("bar:123", vs.findValue("bar", String.class));        assertEquals("bar:123", vs.findValue("child.bar", String.class));        assertEquals("bar:123", vs.findValue("relatives[0].bar", String.class));        assertEquals("bar:123", vs.findValue("child.relatives[0].bar", String.class));    }    public void testGetNullValue() {        Dog dog = new Dog();        OgnlValueStack stack = new OgnlValueStack();        stack.push(dog);        assertNull(stack.findValue("name"));    }    public void testMapEntriesAvailableByKey() {        Foo foo = new Foo();        String title = "a title";        foo.setTitle(title);        OgnlValueStack vs = new OgnlValueStack();        vs.push(foo);        Map map = new HashMap();        String a_key = "a";        String a_value = "A";        map.put(a_key, a_value);        String b_key = "b";        String b_value = "B";        map.put(b_key, b_value);        vs.push(map);        assertEquals(title, vs.findValue("title"));        assertEquals(a_value, vs.findValue(a_key));        assertEquals(b_value, vs.findValue(b_key));    }    public void testMethodCalls() {        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(new Boolean(false), vs.findValue("'Rover'.endsWith('Jack')"));        //assertEquals(new Boolean(false), vs.findValue("'Rover'.endsWith(name)"));        //assertEquals("RoverJack", vs.findValue("[1].name + name"));        assertEquals(new Boolean(false), vs.findValue("[1].name.endsWith(name)"));        assertEquals(new Integer(1 * 7), vs.findValue("computeDogYears()"));        assertEquals(new Integer(1 * 2), vs.findValue("multiplyAge(2)"));        assertEquals(new Integer(12 * 7), vs.findValue("[1].computeDogYears()"));        assertEquals(new Integer(12 * 5), vs.findValue("[1].multiplyAge(5)"));        assertNull(vs.findValue("thisMethodIsBunk()"));        assertEquals(new Integer(12 * 1), vs.findValue("[1].multiplyAge(age)"));        assertEquals("Jack", vs.findValue("name"));        assertEquals("Rover", vs.findValue("[1].name"));        //hates will be null        assertEquals(Boolean.TRUE, vs.findValue("nullSafeMethod(hates)"));    }    public void testMismatchedGettersAndSettersCauseExceptionInSet() {        OgnlValueStack vs = new OgnlValueStack();        BadJavaBean bean = new BadJavaBean();        vs.push(bean);        try {            vs.setValue("count", "1", true);            fail("Expected an exception for mismatched getter and setter");        } catch (XWorkException e) {            //expected        }        try {            vs.setValue("count2", "a", true);            fail("Expected an exception for mismatched getter and setter");        } catch (XWorkException e) {            //expected        }    }    public void testNoExceptionInSetForDefault() {        OgnlValueStack vs = new OgnlValueStack();        BadJavaBean bean = new BadJavaBean();        vs.push(bean);        try {            vs.setValue("count", "1", true);            fail("Unexpected an exception for mismatched getter and setter");        } catch (XWorkException e) {            //expected        }        try {            vs.setValue("count2", "a", true);            fail("Unexpected an exception for mismatched getter and setter");        } catch (XWorkException e) {            //expected        }    }    public void testNullEntry() {        OgnlValueStack vs = new OgnlValueStack();        Dog dog = new Dog();        dog.setName("Rover");        vs.push(dog);        assertEquals("Rover", vs.findValue("name", String.class));        vs.push(null);        assertEquals("Rover", vs.findValue("name", String.class));    }

⌨️ 快捷键说明

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