001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.bind.handler;
016:
017: import java.util.ArrayList;
018: import java.util.Collection;
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.strecks.bind.handler.impl.DomainClass;
023: import org.strecks.exceptions.ApplicationRuntimeException;
024: import org.testng.Assert;
025: import org.testng.annotations.Test;
026:
027: /**
028: * @author Phil Zoio
029: */
030: public class TestBindSelectHandlerMethods {
031:
032: @SuppressWarnings("unchecked")
033: @Test
034: public void testCreateMap() {
035:
036: BindSelectHandler handler = new BindSelectHandler();
037: handler.setBeanPropertyIdName("id");
038:
039: Collection collection = new ArrayList();
040: collection.add(new DomainClass(1));
041: collection.add(new DomainClass(3));
042: collection.add(new DomainClass(4));
043:
044: Map map = handler.createMap(collection);
045: assert map instanceof HashMap;
046:
047: Object firstKey = map.keySet().iterator().next();
048: assert firstKey instanceof Integer;
049:
050: Object value = map.get(firstKey);
051: assert value instanceof DomainClass;
052:
053: System.out.print(map);
054: }
055:
056: @SuppressWarnings("unchecked")
057: @Test
058: public void testCreateMapNoId() {
059:
060: BindSelectHandler handler = new BindSelectHandler();
061: handler.setBeanPropertyIdName("id");
062:
063: // create domain class instances with no id
064: Collection collection = new ArrayList();
065: collection.add(new DomainClass());
066: collection.add(new DomainClass());
067: collection.add(new DomainClass());
068:
069: Map map = handler.createMap(collection);
070: assert map.size() == 1;
071: assert map.keySet().iterator().next().equals(0);
072: }
073:
074: @Test
075: public void testGetPropertyAsMap() {
076:
077: BindSelectHandler handler = new BindSelectHandler();
078: HashMap hashMap = new HashMap();
079: assert hashMap == handler.getPropertyAsMap(hashMap);
080: }
081:
082: @SuppressWarnings("unchecked")
083: @Test
084: public void testGetPropertyAsMapWithCollection() {
085:
086: BindSelectHandler handler = new BindSelectHandler();
087: handler.setBeanPropertyIdName("id");
088:
089: Collection collection = new ArrayList();
090: collection.add(new DomainClass(1));
091: collection.add(new DomainClass(3));
092: collection.add(new DomainClass(4));
093: Map map = handler.getPropertyAsMap(collection);
094:
095: assert map.size() == 3;
096: assert map instanceof HashMap;
097:
098: Object firstKey = map.keySet().iterator().next();
099: assert firstKey instanceof Integer;
100:
101: Object value = map.get(firstKey);
102: assert value instanceof DomainClass;
103: }
104:
105: @Test
106: public void testGetPropertyAsMapInvalid() {
107:
108: try {
109: BindSelectHandler handler = new BindSelectHandler();
110: handler.setBeanLookupExpression("expression");
111: handler.getPropertyAsMap("hello");
112: } catch (ApplicationRuntimeException e) {
113: Assert
114: .assertEquals(
115: e.getMessage(),
116: "Property expression should evaluate to a java.util.Map or java.util.Collection, not a java.lang.String");
117: }
118: }
119:
120: }
|