use of jakarta.persistence.metamodel.Type in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testMapAttribute_getKeyJavaType_UC1a_Method.
public void testMapAttribute_getKeyJavaType_UC1a_Method() {
EntityManager em = null;
boolean exceptionThrown = false;
try {
em = privateTestSetup();
assertNotNull(em);
Metamodel metamodel = em.getMetamodel();
assertNotNull("The metamodel should never be null after an em.getMetamodel() call here.", metamodel);
EntityTypeImpl<Manufacturer> entityManufacturer_ = (EntityTypeImpl) metamodel.entity(Manufacturer.class);
assertNotNull(entityManufacturer_);
// Actual Test Case
/**
* Return the Java type of the map key.
* @return Java key type
*/
// Class<K> getKeyJavaType();
MapAttribute<? super Manufacturer, ?, ?> anAttribute = entityManufacturer_.getMap("hardwareDesignersMapUC1a");
// verify the key type is the Map key - not the managedType PK
Class<?> keyJavaType = anAttribute.getKeyJavaType();
// UC 1a: Generics KV set, no @MapKey present, PK is singular field
// @OneToMany(cascade=ALL, mappedBy="mappedEmployerUC1a")
// private Map<String, HardwareDesigner> hardwareDesignersMapUC1a;
Type keyType = anAttribute.getKeyType();
// When @MapKey(name="name") is present - or generics are set
assertEquals(String.class, keyJavaType);
assertNotNull(keyType);
assertTrue(keyType instanceof Type);
assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
exceptionThrown = true;
} finally {
cleanup(em);
assertFalse("An IAE exception should not occur here.", exceptionThrown);
}
}
use of jakarta.persistence.metamodel.Type in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testOutOfSpecificationInternalAPI.
public void testOutOfSpecificationInternalAPI() {
EntityManager em = null;
boolean exceptionThrown = false;
try {
em = privateTestSetup();
assertNotNull(em);
Metamodel metamodel = em.getMetamodel();
assertNotNull(metamodel);
// Actual Test Case
EntityTypeImpl<ArrayProcessor> entityArrayProcessor_ = (EntityTypeImpl) metamodel.entity(ArrayProcessor.class);
assertNotNull(entityArrayProcessor_);
EntityTypeImpl<Processor> entityProcessor_ = (EntityTypeImpl) metamodel.entity(Processor.class);
assertNotNull(entityProcessor_);
// verify all Types have their javaClass set
Collection<TypeImpl<?>> types = ((MetamodelImpl) metamodel).getTypes().values();
assertNotNull(types);
for (TypeImpl type : types) {
assertNotNull(type);
assertNotNull(type.getJavaType());
}
// verify all embeddables are only embeddables
Set<EmbeddableType<?>> embeddables = metamodel.getEmbeddables();
assertNotNull(embeddables);
for (EmbeddableType embeddable : embeddables) {
// This method works only on EntityType
assertNotNull(embeddable);
assertTrue(embeddable instanceof EmbeddableTypeImpl);
}
// verify all entities are only entities
Set<EntityType<?>> entities = metamodel.getEntities();
assertNotNull(entities);
for (EntityType entity : entities) {
// This method works only on EntityType
assertNotNull(entity.getName());
assertTrue(entity instanceof EntityTypeImpl);
}
// Verify all Attributes and their element and declaring/managed types
List<Attribute> allAttributes = ((MetamodelImpl) metamodel).getAllManagedTypeAttributes();
assertNotNull(allAttributes);
assertEquals(METAMODEL_ALL_ATTRIBUTES_SIZE, allAttributes.size());
// Why do we have this function? So we can verify attribute integrity
for (Attribute anAttribute : allAttributes) {
ManagedType declaringType = anAttribute.getDeclaringType();
assertNotNull(declaringType);
Type elementType = null;
if (((AttributeImpl) anAttribute).isPlural()) {
elementType = ((PluralAttributeImpl) anAttribute).getElementType();
} else {
elementType = ((SingularAttributeImpl) anAttribute).getType();
}
assertNotNull("elementType should not be null", elementType);
// Since the javaType may be computed off the elementType - it must not be null or we will get a NPE below
Class<?> javaType = anAttribute.getJavaType();
}
boolean expectedIAExceptionThrown = false;
// Check entity-->entity hierarchy
// Processor:entity (Board boards)
// +--VectorProcessor
Set<Attribute<ArrayProcessor, ?>> entityArrayProcessorDeclaredAttributes = entityArrayProcessor_.getDeclaredAttributes();
assertEquals(1, entityArrayProcessorDeclaredAttributes.size());
// verify getting the attribute directly
Attribute<ArrayProcessor, ?> entityArrayProcessorDeclaredAttribute = entityArrayProcessor_.getDeclaredAttribute("speed");
// verify we do get an IAE on declared type above
try {
Attribute<ArrayProcessor, ?> entityArrayProcessorDeclaredAttributeThatIsNonExistent = entityArrayProcessor_.getDeclaredAttribute("non-existent");
} catch (IllegalArgumentException iae) {
// expecting no exception
expectedIAExceptionThrown = true;
}
assertTrue("Expected thrown IllegalArgumentException", expectedIAExceptionThrown);
// Verify we get an IAE on a type declared above
try {
Attribute<ArrayProcessor, ?> entityArrayProcessorDeclaredAttributeThatIsDeclaredAbove = entityArrayProcessor_.getDeclaredAttribute("id");
} catch (IllegalArgumentException iae) {
// expecting no exception
expectedIAExceptionThrown = true;
}
assertTrue("Expected thrown IllegalArgumentException", expectedIAExceptionThrown);
Set<Attribute<Processor, ?>> entityProcessorDeclaredAttributes = entityProcessor_.getDeclaredAttributes();
assertEquals(3, entityProcessorDeclaredAttributes.size());
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
exceptionThrown = true;
} finally {
cleanup(em);
assertFalse("An IAE exception should not occur here.", exceptionThrown);
}
}
use of jakarta.persistence.metamodel.Type in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testManagedType_getDeclaredMap_Type_param_Method.
public void testManagedType_getDeclaredMap_Type_param_Method() {
EntityManager em = null;
boolean expectedIAExceptionThrown = false;
try {
em = privateTestSetup();
assertNotNull(em);
Metamodel metamodel = em.getMetamodel();
assertNotNull("The metamodel should never be null after an em.getMetamodel() call here.", metamodel);
EntityTypeImpl<Manufacturer> entityManufacturer_ = (EntityTypeImpl) metamodel.entity(Manufacturer.class);
assertNotNull(entityManufacturer_);
MappedSuperclassTypeImpl<Person> msPerson_ = (MappedSuperclassTypeImpl) metamodel.managedType(Person.class);
assertNotNull(msPerson_);
MappedSuperclassTypeImpl<Corporation> msCorporation_ = (MappedSuperclassTypeImpl) metamodel.managedType(Corporation.class);
assertNotNull(msCorporation_);
EntityTypeImpl<GalacticPosition> entityLocation_ = (EntityTypeImpl) metamodel.entity(GalacticPosition.class);
assertNotNull(entityLocation_);
EntityTypeImpl<Computer> entityComputer_ = (EntityTypeImpl) metamodel.entity(Computer.class);
assertNotNull(entityComputer_);
EntityTypeImpl<HardwareDesigner> entityHardwareDesigner_ = (EntityTypeImpl) metamodel.entity(HardwareDesigner.class);
assertNotNull(entityHardwareDesigner_);
/**
* Return the Map-valued attribute declared by the managed
* type that corresponds to the specified name and Java key
* and value types.
* @param name the name of the represented attribute
* @param keyType the key type of the represented attribute
* @param valueType the value type of the represented attribute
* @return declared MapAttribute of the given name and key
* and value types
* @throws IllegalArgumentException if attribute of the given
* name and type is not declared in the managed type
*/
// <K, V> MapAttribute<X, K, V> getDeclaredMap(String name, Class<K> keyType, Class<V> valueType);
expectedIAExceptionThrown = false;
MapAttribute<? super Manufacturer, ?, ?> anAttribute = entityManufacturer_.getDeclaredMap("hardwareDesignersMap", Integer.class, HardwareDesigner.class);
// verify the default key type is the not the Map key - rather that is is the managedType PK
Class<?> keyJavaType = anAttribute.getKeyJavaType();
// @OneToMany(cascade=ALL, mappedBy="mappedEmployer")
// private Map<String, HardwareDesigner> hardwareDesignersMap;// = new HashMap<String, HardwareDesigner>();
// http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_63:_20090824:_Add_Map_support_for_.40MapKey_to_MapAttribute
// Key is the primary key (PK) of the target entity - in this case HardwareDesigner which inherits its @Id from the Person @MappedSuperclass as '''Integer'''.
Type keyType = anAttribute.getKeyType();
// When @MapKey(name="name") is present - or generics are set
assertEquals(String.class, keyJavaType);
// assertEquals(Integer.class, keyJavaType); // When @MapKey or generics are not present - we default to the PK
assertNotNull(keyType);
assertTrue(keyType instanceof Type);
assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
expectedIAExceptionThrown = true;
} finally {
cleanup(em);
assertFalse("An IAE exception should not occur here.", expectedIAExceptionThrown);
}
}
use of jakarta.persistence.metamodel.Type in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testManagedType_getMap_Type_param_Method.
public void testManagedType_getMap_Type_param_Method() {
EntityManager em = null;
boolean expectedIAExceptionThrown = false;
try {
em = privateTestSetup();
assertNotNull(em);
Metamodel metamodel = em.getMetamodel();
assertNotNull("The metamodel should never be null after an em.getMetamodel() call here.", metamodel);
EntityTypeImpl<Manufacturer> entityManufacturer_ = (EntityTypeImpl) metamodel.entity(Manufacturer.class);
assertNotNull(entityManufacturer_);
MappedSuperclassTypeImpl<Person> msPerson_ = (MappedSuperclassTypeImpl) metamodel.managedType(Person.class);
assertNotNull(msPerson_);
MappedSuperclassTypeImpl<Corporation> msCorporation_ = (MappedSuperclassTypeImpl) metamodel.managedType(Corporation.class);
assertNotNull(msCorporation_);
EntityTypeImpl<GalacticPosition> entityLocation_ = (EntityTypeImpl) metamodel.entity(GalacticPosition.class);
assertNotNull(entityLocation_);
EntityTypeImpl<Computer> entityComputer_ = (EntityTypeImpl) metamodel.entity(Computer.class);
assertNotNull(entityComputer_);
EntityTypeImpl<HardwareDesigner> entityHardwareDesigner_ = (EntityTypeImpl) metamodel.entity(HardwareDesigner.class);
assertNotNull(entityHardwareDesigner_);
/**
* Return the Map-valued attribute of the managed type that
* corresponds to the specified name and Java key and value
* types.
* @param name the name of the represented attribute
* @param keyType the key type of the represented attribute
* @param valueType the value type of the represented attribute
* @return MapAttribute of the given name and key and value
* types
* @throws IllegalArgumentException if attribute of the given
* name and type is not present in the managed type
*/
// <K, V> MapAttribute<? super X, K, V> getMap(String name, Class<K> keyType, Class<V> valueType);
expectedIAExceptionThrown = false;
MapAttribute<? super Manufacturer, ?, ?> anAttribute = entityManufacturer_.getMap("hardwareDesignersMap", Integer.class, HardwareDesigner.class);
// verify the default key type is the not the Map key - rather that is is the managedType PK
Class<?> keyJavaType = anAttribute.getKeyJavaType();
// @OneToMany(cascade=ALL, mappedBy="mappedEmployer")
// private Map<String, HardwareDesigner> hardwareDesignersMap;// = new HashMap<String, HardwareDesigner>();
// http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_63:_20090824:_Add_Map_support_for_.40MapKey_to_MapAttribute
// Key is the primary key (PK) of the target entity - in this case HardwareDesigner which inherits its @Id from the Person @MappedSuperclass as '''Integer'''.
Type keyType = anAttribute.getKeyType();
// When @MapKey(name="name") is present - or generics are set
assertEquals(String.class, keyJavaType);
// assertEquals(Integer.class, keyJavaType); // When @MapKey or generics are not present - we default to the PK
assertNotNull(keyType);
assertTrue(keyType instanceof Type);
assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
expectedIAExceptionThrown = true;
} finally {
cleanup(em);
assertFalse("An IAE exception should not occur here.", expectedIAExceptionThrown);
}
}
use of jakarta.persistence.metamodel.Type in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testMapAttribute_getKeyType_UC1a_Method.
public void testMapAttribute_getKeyType_UC1a_Method() {
EntityManager em = null;
boolean exceptionThrown = false;
try {
em = privateTestSetup();
assertNotNull(em);
Metamodel metamodel = em.getMetamodel();
assertNotNull("The metamodel should never be null after an em.getMetamodel() call here.", metamodel);
EntityTypeImpl<Manufacturer> entityManufacturer_ = (EntityTypeImpl) metamodel.entity(Manufacturer.class);
assertNotNull(entityManufacturer_);
// Actual Test Case
/**
* Return the type representing the key type of the map.
* @return type representing key type
*/
// Type<K> getKeyType();
MapAttribute<? super Manufacturer, ?, ?> anAttribute = entityManufacturer_.getMap("hardwareDesignersMapUC1a");
// verify the key type is the Map key - not the managedType PK
Class<?> keyJavaType = anAttribute.getKeyJavaType();
// UC 1a: Generics KV set, no @MapKey present, PK is singular field
// @OneToMany(cascade=ALL, mappedBy="mappedEmployerUC1a")
// private Map<String, HardwareDesigner> hardwareDesignersMapUC1a;
Type keyType = anAttribute.getKeyType();
// When @MapKey(name="name") is present - or generics are set
assertEquals(String.class, keyJavaType);
assertNotNull(keyType);
assertTrue(keyType instanceof Type);
assertEquals(Type.PersistenceType.BASIC, keyType.getPersistenceType());
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
exceptionThrown = true;
} finally {
cleanup(em);
assertFalse("An IAE exception should not occur here.", exceptionThrown);
}
}
Aggregations