Skip to content

Commit

Permalink
HHH-12238 - aliasToBean throws confusing ClassCastException if class …
Browse files Browse the repository at this point in the history
…lacks setters
  • Loading branch information
David Tombs authored and vladmihalcea committed Jan 24, 2018
1 parent 48eb680 commit fda63bf
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
Expand Up @@ -6,6 +6,7 @@
*/
package org.hibernate.property.access.internal;

import org.hibernate.mapping.Map;
import org.hibernate.property.access.spi.PropertyAccess;
import org.hibernate.property.access.spi.PropertyAccessStrategy;

Expand All @@ -21,6 +22,19 @@ public class PropertyAccessStrategyMapImpl implements PropertyAccessStrategy {

@Override
public PropertyAccess buildPropertyAccess(Class containerJavaType, String propertyName) {

// Sometimes containerJavaType is null, but if it isn't, make sure it's a Map.
if (containerJavaType != null && !Map.class.isAssignableFrom(containerJavaType)) {
throw new IllegalArgumentException(
String.format(
"Expecting class: [%1$s], but containerJavaType is of type: [%2$s] for propertyName: [%3$s]",
Map.class.getName(),
containerJavaType.getName(),
propertyName
)
);
}

return new PropertyAccessMapImpl( this, propertyName );
}
}
@@ -0,0 +1,65 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.property;

import java.util.Date;
import java.util.HashMap;

import org.hibernate.mapping.Map;
import org.hibernate.property.access.internal.PropertyAccessStrategyMapImpl;
import org.hibernate.property.access.spi.PropertyAccess;

import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class PropertyAccessStrategyMapTest extends BaseUnitTestCase {

@Test
public void testBasicMapClass() {
testBasic( Map.class );
}

@Test
public void testBasicNullClass() {
testBasic( null );
}

@Test
public void testNonMap() {
final PropertyAccessStrategyMapImpl accessStrategy = PropertyAccessStrategyMapImpl.INSTANCE;

try {
accessStrategy.buildPropertyAccess( Date.class, "time" );

fail("Should throw IllegalArgumentException");
}
catch (IllegalArgumentException e) {
assertEquals(
"Expecting class: [org.hibernate.mapping.Map], but containerJavaType is of type: [java.util.Date] for propertyName: [time]",
e.getMessage()
);
}
}

private void testBasic(final Class<?> clazz) {

final String key = "testKey";
final String value = "testValue";

final PropertyAccessStrategyMapImpl accessStrategy = PropertyAccessStrategyMapImpl.INSTANCE;
final PropertyAccess access = accessStrategy.buildPropertyAccess( clazz, key );

final HashMap<String, String> map = new HashMap<>();

access.getSetter().set( map, value, null );
assertEquals( value, map.get( key ) );
assertEquals( value, access.getGetter().get( map ) );
}
}

0 comments on commit fda63bf

Please sign in to comment.