use of javax.jdo.metadata.FieldMetadata in project motech by motech.
the class EntityMetadataBuilderTest method shouldSetIndexOnMetadataLookupField.
@Test
public void shouldSetIndexOnMetadataLookupField() throws Exception {
FieldDto lookupField = fieldDto("lookupField", String.class);
LookupDto lookup = new LookupDto();
lookup.setLookupName("A lookup");
lookup.setLookupFields(singletonList(new LookupFieldDto("lookupField", LookupFieldType.VALUE)));
lookup.setIndexRequired(true);
lookupField.setLookups(singletonList(lookup));
FieldMetadata fmd = mock(FieldMetadata.class);
when(entity.getName()).thenReturn(ENTITY_NAME);
when(entity.getId()).thenReturn(14L);
when(schemaHolder.getFields(entity)).thenReturn(singletonList(lookupField));
when(entity.getTableName()).thenReturn(TABLE_NAME);
when(jdoMetadata.newPackageMetadata(PACKAGE)).thenReturn(packageMetadata);
when(packageMetadata.newClassMetadata(ENTITY_NAME)).thenReturn(classMetadata);
when(classMetadata.newFieldMetadata("lookupField")).thenReturn(fmd);
when(fmd.newIndexMetadata()).thenReturn(indexMetadata);
PowerMockito.mockStatic(FieldUtils.class);
when(FieldUtils.getDeclaredField(eq(Sample.class), anyString(), eq(true))).thenReturn(Sample.class.getDeclaredField("notInDefFg"));
entityMetadataBuilder.addEntityMetadata(jdoMetadata, entity, Sample.class, schemaHolder);
verifyCommonClassMetadata();
verify(fmd).newIndexMetadata();
verify(indexMetadata).setName("lkp_idx_" + ENTITY_NAME + "_lookupField_14");
}
use of javax.jdo.metadata.FieldMetadata in project motech by motech.
the class EntityMetadataBuilderTest method shouldAddObjectValueGeneratorToAppropriateFields.
@Test
public void shouldAddObjectValueGeneratorToAppropriateFields() throws Exception {
when(entity.getName()).thenReturn(ENTITY_NAME);
when(entity.getTableName()).thenReturn(TABLE_NAME);
when(jdoMetadata.newPackageMetadata(anyString())).thenReturn(packageMetadata);
when(packageMetadata.newClassMetadata(anyString())).thenReturn(classMetadata);
List<FieldDto> fields = new ArrayList<>();
// for these fields the appropriate generator should be added
fields.add(fieldDto(1L, CREATOR_FIELD_NAME, String.class.getName(), CREATOR_DISPLAY_FIELD_NAME, null));
fields.add(fieldDto(2L, OWNER_FIELD_NAME, String.class.getName(), OWNER_DISPLAY_FIELD_NAME, null));
fields.add(fieldDto(3L, CREATION_DATE_FIELD_NAME, DateTime.class.getName(), CREATION_DATE_DISPLAY_FIELD_NAME, null));
fields.add(fieldDto(4L, MODIFIED_BY_FIELD_NAME, String.class.getName(), MODIFIED_BY_DISPLAY_FIELD_NAME, null));
fields.add(fieldDto(5L, MODIFICATION_DATE_FIELD_NAME, DateTime.class.getName(), MODIFICATION_DATE_DISPLAY_FIELD_NAME, null));
doReturn(fields).when(schemaHolder).getFields(CLASS_NAME);
final List<FieldMetadata> list = new ArrayList<>();
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
// we create a mock ...
FieldMetadata metadata = mock(FieldMetadata.class);
// ... and it should return correct name
doReturn(invocation.getArguments()[0]).when(metadata).getName();
// Because we want to check that appropriate methods was executed
// we added metadata to list and later we will verify conditions
list.add(metadata);
// in the end we have to return the mock
return metadata;
}
}).when(classMetadata).newFieldMetadata(anyString());
PowerMockito.mockStatic(FieldUtils.class);
when(FieldUtils.getDeclaredField(eq(Sample.class), anyString(), eq(true))).thenReturn(Sample.class.getDeclaredField("notInDefFg"));
entityMetadataBuilder.addEntityMetadata(jdoMetadata, entity, Sample.class, schemaHolder);
for (FieldMetadata metadata : list) {
String name = metadata.getName();
// the id field should not have set object value generator metadata
int invocations = "id".equalsIgnoreCase(name) ? 0 : 1;
verify(classMetadata).newFieldMetadata(name);
verify(metadata, times(invocations)).setPersistenceModifier(PersistenceModifier.PERSISTENT);
verify(metadata, times(invocations)).setDefaultFetchGroup(true);
verify(metadata, times(invocations)).newExtensionMetadata(DATANUCLEUS, VALUE_GENERATOR, "ovg." + name);
}
}
use of javax.jdo.metadata.FieldMetadata in project tests by datanucleus.
the class JDOMetadataAPITest method assertFields.
private void assertFields(final Class<?> clazz, final List<IndexMetadata> indexes, final String... fieldNames) {
if (fieldNames == null) {
assertTrue("More indices than expected returned! " + clazz.getSimpleName(), indexes.isEmpty());
} else {
assertFalse("More indices than expected returned! " + clazz.getSimpleName(), indexes.size() > fieldNames.length);
Set<String> indexesNames = new HashSet<String>();
for (final IndexMetadata indexMetada : indexes) {
MemberMetadata[] members = indexMetada.getMembers();
if (members == null) {
FieldMetadata fieldMetadata = (FieldMetadata) indexMetada.getParent();
indexesNames.add(fieldMetadata.getName());
} else {
for (MemberMetadata memberMetadata : members) {
memberMetadata.getIndexMetadata();
indexesNames.add(memberMetadata.getName());
}
}
}
for (final String name : fieldNames) {
assertTrue("Index not returned: " + name, indexesNames.contains(name));
}
}
}
use of javax.jdo.metadata.FieldMetadata in project tests by datanucleus.
the class DynamicEnhanceSchemaToolTest method createMetadata.
private static void createMetadata(JDOMetadata filemd) {
PackageMetadata pmd = filemd.newPackageMetadata("test");
ClassMetadata cmd = pmd.newClassMetadata("Client");
cmd.setTable("CLIENT").setDetachable(true).setIdentityType(javax.jdo.annotations.IdentityType.DATASTORE);
cmd.setPersistenceModifier(javax.jdo.metadata.ClassPersistenceModifier.PERSISTENCE_CAPABLE);
FieldMetadata fmd = cmd.newFieldMetadata("name");
fmd.setNullValue(javax.jdo.annotations.NullValue.DEFAULT).setColumn("name").setIndexed(true).setUnique(true);
InheritanceMetadata inhmd = cmd.newInheritanceMetadata();
inhmd.setStrategy(javax.jdo.annotations.InheritanceStrategy.NEW_TABLE);
DiscriminatorMetadata dmd = inhmd.newDiscriminatorMetadata();
dmd.setColumn("disc").setValue("Client").setStrategy(javax.jdo.annotations.DiscriminatorStrategy.VALUE_MAP).setIndexed(Indexed.TRUE);
VersionMetadata vermd = cmd.newVersionMetadata();
vermd.setStrategy(javax.jdo.annotations.VersionStrategy.VERSION_NUMBER).setColumn("version").setIndexed(Indexed.TRUE);
}
use of javax.jdo.metadata.FieldMetadata in project motech by motech.
the class EntityMetadataBuilderTest method shouldAddOneToOneRelationshipMetadata.
@Test
public void shouldAddOneToOneRelationshipMetadata() throws NotFoundException, CannotCompileException {
final String relClassName = "org.motechproject.test.MyClass";
final String relFieldName = "myField";
FieldDto oneToOneField = fieldDto("oneToOneName", OneToOneRelationship.class);
oneToOneField.addMetadata(new MetadataDto(RELATED_CLASS, relClassName));
oneToOneField.addMetadata(new MetadataDto(RELATED_FIELD, relFieldName));
FieldMetadata fmd = mock(FieldMetadata.class);
when(fmd.getName()).thenReturn("oneToOneName");
ForeignKeyMetadata fkmd = mock(ForeignKeyMetadata.class);
when(entity.getName()).thenReturn(ENTITY_NAME);
when(entity.getId()).thenReturn(3L);
when(schemaHolder.getFields(entity)).thenReturn(singletonList(oneToOneField));
when(entity.getTableName()).thenReturn(TABLE_NAME);
when(jdoMetadata.newPackageMetadata(PACKAGE)).thenReturn(packageMetadata);
when(packageMetadata.newClassMetadata(ENTITY_NAME)).thenReturn(classMetadata);
when(classMetadata.newFieldMetadata("oneToOneName")).thenReturn(fmd);
when(fmd.newForeignKeyMetadata()).thenReturn(fkmd);
/* We simulate configuration for the bi-directional relationship (the related class has got
a field that links back to the main class) */
CtClass myClass = mock(CtClass.class);
CtClass relatedClass = mock(CtClass.class);
CtField myField = mock(CtField.class);
CtField relatedField = mock(CtField.class);
when(myClass.getName()).thenReturn(relClassName);
when(myClass.getDeclaredFields()).thenReturn(new CtField[] { myField });
when(myField.getType()).thenReturn(relatedClass);
when(myField.getName()).thenReturn(relFieldName);
when(relatedClass.getDeclaredFields()).thenReturn(new CtField[] { relatedField });
when(relatedClass.getName()).thenReturn(CLASS_NAME);
entityMetadataBuilder.addEntityMetadata(jdoMetadata, entity, Sample.class, schemaHolder);
verifyCommonClassMetadata();
verify(fmd).setDefaultFetchGroup(true);
verify(fmd).setPersistenceModifier(PersistenceModifier.PERSISTENT);
verify(fkmd).setName("fk_Sample_oneToOneName_3");
}
Aggregations