/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.strecks.bind.handler;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.strecks.bind.handler.impl.DomainClass;
import org.strecks.exceptions.ApplicationRuntimeException;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* @author Phil Zoio
*/
public class TestBindSelectHandlerMethods
{
@SuppressWarnings("unchecked")
@Test
public void testCreateMap()
{
BindSelectHandler handler = new BindSelectHandler();
handler.setBeanPropertyIdName("id");
Collection collection = new ArrayList();
collection.add(new DomainClass(1));
collection.add(new DomainClass(3));
collection.add(new DomainClass(4));
Map map = handler.createMap(collection);
assert map instanceof HashMap;
Object firstKey = map.keySet().iterator().next();
assert firstKey instanceof Integer;
Object value = map.get(firstKey);
assert value instanceof DomainClass;
System.out.print(map);
}
@SuppressWarnings("unchecked")
@Test
public void testCreateMapNoId()
{
BindSelectHandler handler = new BindSelectHandler();
handler.setBeanPropertyIdName("id");
// create domain class instances with no id
Collection collection = new ArrayList();
collection.add(new DomainClass());
collection.add(new DomainClass());
collection.add(new DomainClass());
Map map = handler.createMap(collection);
assert map.size() == 1;
assert map.keySet().iterator().next().equals(0);
}
@Test
public void testGetPropertyAsMap()
{
BindSelectHandler handler = new BindSelectHandler();
HashMap hashMap = new HashMap();
assert hashMap == handler.getPropertyAsMap(hashMap);
}
@SuppressWarnings("unchecked")
@Test
public void testGetPropertyAsMapWithCollection()
{
BindSelectHandler handler = new BindSelectHandler();
handler.setBeanPropertyIdName("id");
Collection collection = new ArrayList();
collection.add(new DomainClass(1));
collection.add(new DomainClass(3));
collection.add(new DomainClass(4));
Map map = handler.getPropertyAsMap(collection);
assert map.size() == 3;
assert map instanceof HashMap;
Object firstKey = map.keySet().iterator().next();
assert firstKey instanceof Integer;
Object value = map.get(firstKey);
assert value instanceof DomainClass;
}
@Test
public void testGetPropertyAsMapInvalid()
{
try
{
BindSelectHandler handler = new BindSelectHandler();
handler.setBeanLookupExpression("expression");
handler.getPropertyAsMap("hello");
}
catch (ApplicationRuntimeException e)
{
Assert
.assertEquals(e.getMessage(),
"Property expression should evaluate to a java.util.Map or java.util.Collection, not a java.lang.String");
}
}
}