METAGEN-76 Fixing StringUtil#isPropertyName to ignore hashCode as persistent property

This commit is contained in:
Hardy Ferentschik 2012-01-20 13:11:25 +01:00 committed by Strong Liu
parent 4c968d7f7a
commit 802cbcd546
15 changed files with 264 additions and 60 deletions

View File

@ -23,7 +23,6 @@ import java.util.List;
import java.util.Map;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.Name;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;

View File

@ -50,9 +50,29 @@ public final class StringUtil {
}
public static boolean isPropertyName(String name) {
return name.startsWith( PROPERTY_PREFIX_GET ) || name.startsWith( PROPERTY_PREFIX_IS ) || name.startsWith(
PROPERTY_PREFIX_HAS
);
if ( name == null ) {
return false;
}
return checkPropertyName( name, PROPERTY_PREFIX_GET )
|| checkPropertyName( name, PROPERTY_PREFIX_IS )
|| checkPropertyName( name, PROPERTY_PREFIX_HAS );
}
private static boolean checkPropertyName(String name, String prefix) {
if ( !name.startsWith( prefix ) ) {
return false;
}
// the name has to start with the prefix and have at least one more character
if ( name.length() < prefix.length() + 1 ) {
return false;
}
if ( !Character.isUpperCase( name.charAt( prefix.length() ) ) ) {
return false;
}
return true;
}
public static String getPropertyName(String name) {
@ -70,7 +90,7 @@ public final class StringUtil {
else if ( name.startsWith( PROPERTY_PREFIX_HAS ) ) {
tmp = name.replaceFirst( PROPERTY_PREFIX_HAS, "" );
}
return tmp.substring(0,1).toLowerCase() + tmp.substring(1);
return tmp.substring( 0, 1 ).toLowerCase() + tmp.substring( 1 );
}
}

View File

@ -14,17 +14,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.testng.annotations.Test;
import org.hibernate.jpamodelgen.test.util.CompilationTest;
import org.hibernate.jpamodelgen.test.util.TestForIssue;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor;
/**
* Test for METAGEN-40
*
* @author Hardy Ferentschik
*/
@TestForIssue(jiraKey = "METAGEN-40")
public class DefaultPackageTest extends CompilationTest {
@Test
public void testMetaModelGeneratedForEntitiesInDefaultPackage() {

View File

@ -0,0 +1,50 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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.hibernate.jpamodelgen.test;
import org.testng.annotations.Test;
import org.hibernate.jpamodelgen.test.util.TestForIssue;
import org.hibernate.jpamodelgen.util.StringUtil;
import static org.testng.Assert.assertFalse;
import static org.testng.AssertJUnit.assertTrue;
/**
* @author Hardy Ferentschik
*/
public class StringUtilTest {
@Test
public void testIsPropertyName() {
assertTrue( StringUtil.isPropertyName( "getFoo" ) );
assertTrue( StringUtil.isPropertyName( "isFoo" ) );
assertTrue( StringUtil.isPropertyName( "hasFoo" ) );
assertFalse( StringUtil.isPropertyName( "getfoo" ) );
assertFalse( StringUtil.isPropertyName( "isfoo" ) );
assertFalse( StringUtil.isPropertyName( "hasfoo" ) );
assertFalse( StringUtil.isPropertyName( "" ) );
assertFalse( StringUtil.isPropertyName( null ) );
}
@Test
@TestForIssue(jiraKey = "METAGEN-76")
public void testHashCodeNotAProperty() {
assertFalse( StringUtil.isPropertyName( "hashCode" ) );
}
}

View File

@ -19,6 +19,7 @@ package org.hibernate.jpamodelgen.test.arraytype;
import org.testng.annotations.Test;
import org.hibernate.jpamodelgen.test.util.CompilationTest;
import org.hibernate.jpamodelgen.test.util.TestForIssue;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertAttributeTypeInMetaModelFor;
@ -27,18 +28,14 @@ import static org.hibernate.jpamodelgen.test.util.TestUtil.assertAttributeTypeIn
*/
public class ArrayTest extends CompilationTest {
/**
* METAGEN-2
*/
@Test
@TestForIssue(jiraKey = "METAGEN-2")
public void testPrimitiveArray() {
assertAttributeTypeInMetaModelFor( Image.class, "data", byte[].class, "Wrong type for field." );
}
/**
* METAGEN-2
*/
@Test
@TestForIssue(jiraKey = "METAGEN-2")
public void testIntegerArray() {
assertAttributeTypeInMetaModelFor(
TemperatureSamples.class, "samples", Integer[].class, "Wrong type for field."

View File

@ -19,6 +19,7 @@ package org.hibernate.jpamodelgen.test.blob;
import org.testng.annotations.Test;
import org.hibernate.jpamodelgen.test.util.CompilationTest;
import org.hibernate.jpamodelgen.test.util.TestForIssue;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertPresenceOfFieldInMetamodelFor;
@ -27,10 +28,9 @@ import static org.hibernate.jpamodelgen.test.util.TestUtil.assertPresenceOfField
* @author Hardy Ferentschik
*/
public class BlobTest extends CompilationTest {
/**
* METAGEN-38
*/
@Test
@TestForIssue(jiraKey = "METAGEN-38")
public void testBlobField() {
assertMetamodelClassGeneratedFor( BlobEntity.class );
assertPresenceOfFieldInMetamodelFor( BlobEntity.class, "blob", "the metamodel should have a member 'blob'" );

View File

@ -23,6 +23,7 @@ import java.util.List;
import org.testng.annotations.Test;
import org.hibernate.jpamodelgen.test.util.CompilationTest;
import org.hibernate.jpamodelgen.test.util.TestForIssue;
import org.hibernate.jpamodelgen.test.util.TestUtil;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMapAttributesInMetaModelFor;
@ -33,10 +34,9 @@ import static org.hibernate.jpamodelgen.test.util.TestUtil.assertNoSourceFileGen
* @author Hardy Ferentschik
*/
public class ElementCollectionTest extends CompilationTest {
/**
* METAGEN-8
*/
@Test
@TestForIssue(jiraKey = "METAGEN-8")
public void testElementCollectionOnMap() {
assertMetamodelClassGeneratedFor( House.class );
assertMetamodelClassGeneratedFor( Room.class );
@ -44,10 +44,8 @@ public class ElementCollectionTest extends CompilationTest {
assertNoSourceFileGeneratedFor( String.class );
}
/**
* METAGEN-19
*/
@Test
@TestForIssue(jiraKey = "METAGEN-19")
public void testMapKeyClass() {
assertMetamodelClassGeneratedFor( Hotel.class );
assertMapAttributesInMetaModelFor(
@ -59,10 +57,8 @@ public class ElementCollectionTest extends CompilationTest {
);
}
/**
* METAGEN-22
*/
@Test
@TestForIssue(jiraKey = "METAGEN-22")
public void testMapKeyClassXmlConfigured() {
assertMetamodelClassGeneratedFor( Hostel.class );
assertMapAttributesInMetaModelFor(

View File

@ -21,6 +21,7 @@ package org.hibernate.jpamodelgen.test.embeddablemappedsuperclass;
import org.testng.annotations.Test;
import org.hibernate.jpamodelgen.test.util.CompilationTest;
import org.hibernate.jpamodelgen.test.util.TestForIssue;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertNoCompilationError;
@ -29,13 +30,12 @@ import static org.hibernate.jpamodelgen.test.util.TestUtil.assertNoCompilationEr
* @author Hardy Ferentschik
*/
public class EmbeddableMappedSuperClassTest extends CompilationTest {
/**
* METAGEN-36
*/
@Test
@TestForIssue(jiraKey = "METAGEN-36")
public void testMetaModelsGenerated() {
assertMetamodelClassGeneratedFor( EmbeddableAndMappedSuperClass.class );
assertNoCompilationError(getCompilationDiagnostics());
assertNoCompilationError( getCompilationDiagnostics() );
}
@Override

View File

@ -0,0 +1,46 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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.hibernate.jpamodelgen.test.hashcode;
import org.testng.annotations.Test;
import org.hibernate.jpamodelgen.test.util.CompilationTest;
import org.hibernate.jpamodelgen.test.util.TestForIssue;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertAbsenceOfFieldInMetamodelFor;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertPresenceOfFieldInMetamodelFor;
/**
* @author Hardy Ferentschik
*/
public class HashCodeTest extends CompilationTest {
@Test
@TestForIssue(jiraKey = "METAGEN-76")
public void testHashCodeDoesNotCreateSingularAttribute() {
assertMetamodelClassGeneratedFor( HashEntity.class );
assertPresenceOfFieldInMetamodelFor( HashEntity.class, "id" );
assertAbsenceOfFieldInMetamodelFor( HashEntity.class, "hashCode", "hashCode is not a persistent property" );
}
@Override
protected String getPackageNameOfCurrentTest() {
return HashCodeTest.class.getPackage().getName();
}
}

View File

@ -0,0 +1,64 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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.
*/
// $Id:$
package org.hibernate.jpamodelgen.test.hashcode;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* @author Hardy Ferentschik
*/
@Entity
public class HashEntity {
private long id;
@Id
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
HashEntity that = (HashEntity) o;
if ( id != that.id ) {
return false;
}
return true;
}
@Override
public int hashCode() {
return (int) ( id ^ ( id >>> 32 ) );
}
}

View File

@ -21,6 +21,7 @@ package org.hibernate.jpamodelgen.test.targetannotation;
import org.testng.annotations.Test;
import org.hibernate.jpamodelgen.test.util.CompilationTest;
import org.hibernate.jpamodelgen.test.util.TestForIssue;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertAttributeTypeInMetaModelFor;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor;
@ -30,14 +31,18 @@ import static org.hibernate.jpamodelgen.test.util.TestUtil.assertPresenceOfField
* @author Hardy Ferentschik
*/
public class TargetAnnotationTest extends CompilationTest {
/**
* METAGEN-30
*/
@Test
@TestForIssue(jiraKey = "METAGEN-30")
public void testEmbeddableWithTargetAnnotation() {
assertMetamodelClassGeneratedFor( House.class );
assertPresenceOfFieldInMetamodelFor( House.class, "address", "the metamodel should have a member 'address'" );
assertAttributeTypeInMetaModelFor( House.class, "address", AddressImpl.class, "The target annotation set the type to AddressImpl");
assertAttributeTypeInMetaModelFor(
House.class,
"address",
AddressImpl.class,
"The target annotation set the type to AddressImpl"
);
}
@Override

View File

@ -35,6 +35,7 @@ import org.testng.Assert;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.FileAssert.fail;
/**
@ -144,6 +145,11 @@ public class TestUtil {
return contents.toString();
}
public static void dumpMetaModelSourceFor(Class<?> clazz) {
log.info( "Dumping meta model source for " + clazz.getName() + ":" );
log.info( getMetaModelSourceAsString( clazz ) );
}
public static void assertNoSourceFileGeneratedFor(Class<?> clazz) {
assertNotNull( clazz, "Class parameter cannot be null" );
File sourceFile = getMetaModelSourceFileFor( clazz );
@ -151,15 +157,27 @@ public class TestUtil {
}
public static void assertAbsenceOfFieldInMetamodelFor(Class<?> clazz, String fieldName) {
assertAbsenceOfFieldInMetamodelFor( clazz, fieldName, "field should not be persistent" );
assertAbsenceOfFieldInMetamodelFor(
clazz,
fieldName,
"'" + fieldName + "' should not appear in metamodel class"
);
}
public static void assertAbsenceOfFieldInMetamodelFor(Class<?> clazz, String fieldName, String errorString) {
Assert.assertFalse( hasFieldInMetamodelFor( clazz, fieldName ), errorString );
assertFalse( hasFieldInMetamodelFor( clazz, fieldName ), buildErrorString( errorString, clazz ) );
}
public static void assertPresenceOfFieldInMetamodelFor(Class<?> clazz, String fieldName) {
assertPresenceOfFieldInMetamodelFor(
clazz,
fieldName,
"'" + fieldName + "' should appear in metamodel class"
);
}
public static void assertPresenceOfFieldInMetamodelFor(Class<?> clazz, String fieldName, String errorString) {
Assert.assertTrue( hasFieldInMetamodelFor( clazz, fieldName ), errorString );
assertTrue( hasFieldInMetamodelFor( clazz, fieldName ), buildErrorString( errorString, clazz ) );
}
public static void assertAttributeTypeInMetaModelFor(Class<?> clazz, String fieldName, Class<?> expectedType, String errorString) {
@ -174,7 +192,7 @@ public class TestUtil {
assertEquals(
actualType,
expectedType,
"Types do not match: " + errorString
"Types do not match: " + buildErrorString( errorString, clazz )
);
}
@ -183,10 +201,10 @@ public class TestUtil {
assertNotNull( field );
ParameterizedType type = (ParameterizedType) field.getGenericType();
Type actualMapKeyType = type.getActualTypeArguments()[1];
assertEquals( actualMapKeyType, expectedMapKey, errorString );
assertEquals( actualMapKeyType, expectedMapKey, buildErrorString( errorString, clazz ) );
Type actualMapKeyValue = type.getActualTypeArguments()[2];
assertEquals( actualMapKeyValue, expectedMapValue, errorString );
assertEquals( actualMapKeyValue, expectedMapValue, buildErrorString( errorString, clazz ) );
}
public static void assertSuperClassRelationShipInMetamodel(Class<?> entityClass, Class<?> superEntityClass) {
@ -235,6 +253,18 @@ public class TestUtil {
return getFieldFromMetamodelFor( clazz, fieldName ) != null;
}
private static String buildErrorString(String baseError, Class<?> clazz) {
StringBuilder builder = new StringBuilder();
builder.append( baseError );
builder.append( "\n" );
builder.append( "Source code for " );
builder.append( clazz.getName() );
builder.append( "_.java:" );
builder.append( "\n" );
builder.append( getMetaModelSourceAsString( clazz ) );
return builder.toString();
}
private static Type getComponentType(Type actualType) {
if ( actualType instanceof Class ) {
Class<?> clazz = (Class<?>) actualType;
@ -242,14 +272,15 @@ public class TestUtil {
return clazz.getComponentType();
}
else {
fail("Unexpected component type");
fail( "Unexpected component type" );
}
}
if ( actualType instanceof GenericArrayType ) {
return ( (GenericArrayType) actualType ).getGenericComponentType();
} else {
fail("Unexpected component type");
}
else {
fail( "Unexpected component type" );
return null; // making the compiler happy
}
}

View File

@ -26,11 +26,11 @@ public class Boy {
private String name;
private List nickNames;
private List<String> nickNames;
private Superhero favoriteSuperhero;
private List<Superhero> knowsHeros;
private List<Superhero> knowsHeroes;
private List<Superhero> savedBy;
@ -50,12 +50,12 @@ public class Boy {
this.favoriteSuperhero = favoriteSuperhero;
}
public List<Superhero> getKnowsHeros() {
return knowsHeros;
public List<Superhero> getKnowsHeroes() {
return knowsHeroes;
}
public void setKnowsHeros(List<Superhero> knowsHeros) {
this.knowsHeros = knowsHeros;
public void setKnowsHeroes(List<Superhero> knowsHeroes) {
this.knowsHeroes = knowsHeroes;
}
public long getId() {

View File

@ -23,6 +23,7 @@ import org.testng.annotations.Test;
import org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor;
import org.hibernate.jpamodelgen.test.util.CompilationTest;
import org.hibernate.jpamodelgen.test.util.TestForIssue;
import org.hibernate.jpamodelgen.test.util.TestUtil;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertAttributeTypeInMetaModelFor;
@ -45,10 +46,8 @@ public class XmlMappingTest extends CompilationTest {
assertPresenceOfFieldInMetamodelFor( Building.class, "address", "address field should exist" );
}
/**
* METAGEN-17
*/
@Test
@TestForIssue(jiraKey = "METAGEN-17")
public void testTargetEntityOnOneToOne() {
assertMetamodelClassGeneratedFor( Boy.class );
assertPresenceOfFieldInMetamodelFor( Boy.class, "favoriteSuperhero", "favoriteSuperhero field should exist" );
@ -57,22 +56,18 @@ public class XmlMappingTest extends CompilationTest {
);
}
/**
* METAGEN-17
*/
@Test
@TestForIssue(jiraKey = "METAGEN-17")
public void testTargetEntityOnOneToMany() {
assertMetamodelClassGeneratedFor( Boy.class );
assertPresenceOfFieldInMetamodelFor( Boy.class, "knowsHeros", "knowsHeros field should exist" );
assertPresenceOfFieldInMetamodelFor( Boy.class, "knowsHeroes", "knowsHeroes field should exist" );
assertAttributeTypeInMetaModelFor(
Boy.class, "knowsHeros", FakeHero.class, "target entity overridden in xml"
Boy.class, "knowsHeroes", FakeHero.class, "target entity overridden in xml"
);
}
/**
* METAGEN-17
*/
@Test
@TestForIssue(jiraKey = "METAGEN-17")
public void testTargetEntityOnManyToMany() {
assertMetamodelClassGeneratedFor( Boy.class );
assertPresenceOfFieldInMetamodelFor( Boy.class, "savedBy", "savedBy field should exist" );

View File

@ -10,7 +10,7 @@
<attributes>
<id name="id"/>
<basic name="name"/>
<one-to-many name="knowsHeros" target-entity="org.hibernate.jpamodelgen.test.xmlmapped.FakeHero"/>
<one-to-many name="knowsHeroes" target-entity="org.hibernate.jpamodelgen.test.xmlmapped.FakeHero"/>
<one-to-one name="favoriteSuperhero" target-entity="org.hibernate.jpamodelgen.test.xmlmapped.FakeHero"/>
<many-to-many name="savedBy" target-entity="org.hibernate.jpamodelgen.test.xmlmapped.FakeHero"/>
<element-collection name="nickNames" target-class="java.lang.String"/>