Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-12238 - aliasToBean throws confusing ClassCastException if class …
…lacks setters
- Loading branch information
1 parent
48eb680
commit fda63bf
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
hibernate-core/src/test/java/org/hibernate/property/PropertyAccessStrategyMapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ) ); | ||
} | ||
} |