package beanface.el.functor.testing.tests; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; import javax.el.ELContext; import junit.framework.JUnit4TestAdapter; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.JUnitCore; import beanface.el.functor.FunctorELResolver; import beanface.el.functor.testing.ELContextFactory; public class MapTests { static private ELContextFactory elcFactory; private ELContext elContext; private Map theMap; @BeforeClass public static void setupOnce() { elcFactory = new ELContextFactory(); elcFactory.addELResolver(new FunctorELResolver()); } @Before public void setup() { elContext = elcFactory.getELContext(); theMap = new LinkedHashMap(); theMap.put("a", "Apple"); theMap.put("b", "Banana"); theMap.put("o", "Orange"); theMap.put("p", "Pear"); } private Object testResolve(Object base, Object... properties) { Object result = base; for (Object prop : properties) { result = elContext.getELResolver().getValue(elContext, result, prop); Assert.assertNotNull("Expect result of applying property '" + prop + "' to base is not null", result); } return result; } private void testSetProp(Object base, Object prop, Object value) { elContext.getELResolver().setValue(elContext, base, prop, value); } @Test public void testGetMapItem() { String key = "a"; Object r = testResolve(theMap, key); Assert.assertNotNull("Resolve result", r); Assert.assertEquals("Expect a String to be returned", r.getClass(), String.class); Assert.assertEquals("Expected returned value", theMap.get(key), r); } @Test public void testPutMapItem1() { Map roMap = Collections.unmodifiableMap(theMap); String key = "c"; String value = "Coconut"; testSetProp(roMap, key, value); Object r = testResolve(roMap, key); Assert.assertNotNull("Resolve result", r); Assert.assertEquals("Expect a String to be returned", r.getClass(), String.class); Assert.assertEquals("Expected returned value", value, r); } @Test public void testPutMapItem2() { Map roMap = Collections .synchronizedMap(Collections.unmodifiableMap(theMap)); String key = "c"; String value = "Coconut"; testSetProp(roMap, key, value); Object r = testResolve(roMap, key); Assert.assertNotNull("Resolve result", r); Assert.assertEquals("Expect a String to be returned", r.getClass(), String.class); Assert.assertEquals("Expected returned value", value, r); } @Test public void testMapClass() { Object r = testResolve(theMap, "class"); Assert.assertNotNull("Resolve result", r); Assert.assertEquals("Expect a class to be returned", r.getClass(), Class.class); Class rClass = (Class) r; Assert.assertEquals("Expected returned class", theMap.getClass(), rClass); } @Test public void testMapEmpty() { Object r = testResolve(theMap, "empty"); Assert.assertNotNull("Resolve result", r); Assert.assertEquals("Expect a bool to be returned", r.getClass(), Boolean.class); Boolean rBool = (Boolean) r; Assert.assertEquals("Expected returned value", theMap.isEmpty(), rBool.booleanValue()); } public static junit.framework.Test suite() { return new JUnit4TestAdapter(MapTests.class); } public static void main(String[] args) { JUnitCore.runClasses(MapTests.class); } }