HHH-6488 Renaming JandexHelper#getValkueAsEnum to JandexHelper#getEnumValue

Also adding a test to JandexHelperTest which tests retrieving a unknown
annotation parameter
This commit is contained in:
Hardy Ferentschik 2011-07-27 13:39:26 +02:00
parent 9d63b05426
commit 9bf55b6e07
3 changed files with 35 additions and 8 deletions

View File

@ -43,7 +43,6 @@ import org.jboss.jandex.Indexer;
import org.jboss.jandex.MethodInfo;
import org.jboss.jandex.Type;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.service.classloading.spi.ClassLoaderService;
@ -61,7 +60,7 @@ public class JandexHelper {
* annotation class is retrieved instead.
* <p>
* There are two special cases. {@code Class} parameters should be retrieved as strings (and then can later be
* loaded) and enumerated values should be retrieved via {@link #getValueAsEnum(AnnotationInstance, String, Class)}.
* loaded) and enumerated values should be retrieved via {@link #getEnumValue(AnnotationInstance, String, Class)}.
* </p>
*
* @param annotation the annotation containing the element with the supplied name
@ -131,7 +130,7 @@ public class JandexHelper {
* @see #getValue(AnnotationInstance, String, Class)
*/
@SuppressWarnings("unchecked")
public static <T extends Enum<T>> T getValueAsEnum(AnnotationInstance annotation, String element, Class<T> type) {
public static <T extends Enum<T>> T getEnumValue(AnnotationInstance annotation, String element, Class<T> type) {
AnnotationValue val = annotation.value( element );
if ( val == null ) {
return (T) getDefaultValue( annotation, element );
@ -323,7 +322,10 @@ public class JandexHelper {
throw error;
}
catch ( Exception error ) {
throw new AnnotationException( error );
throw new AssertionFailure(
String.format( "The annotation %s does not define a parameter '%s'", name, element ),
error
);
}
}

View File

@ -85,7 +85,7 @@ public class FetchProfileBinder {
AnnotationInstance[].class
);
for ( AnnotationInstance override : overrideAnnotations ) {
FetchMode fetchMode = JandexHelper.getValueAsEnum( override, "mode", FetchMode.class );
FetchMode fetchMode = JandexHelper.getEnumValue( override, "mode", FetchMode.class );
if ( !fetchMode.equals( org.hibernate.annotations.FetchMode.JOIN ) ) {
throw new MappingException( "Only FetchMode.JOIN is currently supported" );
}

View File

@ -28,6 +28,7 @@ import java.util.Map;
import javax.persistence.AttributeOverride;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.LockModeType;
import javax.persistence.NamedQuery;
@ -48,12 +49,12 @@ import org.hibernate.metamodel.source.annotations.JandexHelper;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.internal.BasicServiceRegistryImpl;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
/**
* Tests for the helper class {@link JandexHelper}.
@ -156,7 +157,7 @@ public class JandexHelperTest extends BaseUnitTestCase {
assertTrue( annotationInstances.size() == 1 );
AnnotationInstance annotationInstance = annotationInstances.get( 0 );
LockModeType lockMode = JandexHelper.getValueAsEnum( annotationInstance, "lockMode", LockModeType.class );
LockModeType lockMode = JandexHelper.getEnumValue( annotationInstance, "lockMode", LockModeType.class );
assertEquals( "Wrong lock mode", LockModeType.NONE, lockMode );
}
@ -171,7 +172,7 @@ public class JandexHelperTest extends BaseUnitTestCase {
assertTrue( annotationInstances.size() == 1 );
AnnotationInstance annotationInstance = annotationInstances.get( 0 );
LockModeType lockMode = JandexHelper.getValueAsEnum( annotationInstance, "lockMode", LockModeType.class );
LockModeType lockMode = JandexHelper.getEnumValue( annotationInstance, "lockMode", LockModeType.class );
assertEquals( "Wrong lock mode", LockModeType.OPTIMISTIC, lockMode );
}
@ -219,6 +220,30 @@ public class JandexHelperTest extends BaseUnitTestCase {
String fqcn = JandexHelper.getValue( annotationInstance, "resultClass", String.class );
assertEquals( "Wrong class names", Foo.class.getName(), fqcn );
}
@Test
public void testRetrieveUnknownParameter() {
@Entity
class Foo {
}
Index index = JandexHelper.indexForClass( classLoaderService, Foo.class );
List<AnnotationInstance> annotationInstances = index.getAnnotations( JPADotNames.ENTITY );
assertTrue( annotationInstances.size() == 1 );
AnnotationInstance annotationInstance = annotationInstances.get( 0 );
try {
JandexHelper.getValue( annotationInstance, "foo", String.class );
fail();
}
catch ( AssertionFailure e ) {
assertTrue(
e.getMessage()
.startsWith( "The annotation javax.persistence.Entity does not define a parameter 'foo'" )
);
}
}
}