Search in sources :

Example 6 with FieldMetadata

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");
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) AnotherSample(org.motechproject.mds.annotations.internal.samples.AnotherSample) Sample(org.motechproject.mds.builder.Sample) LookupDto(org.motechproject.mds.dto.LookupDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with FieldMetadata

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);
    }
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) AnotherSample(org.motechproject.mds.annotations.internal.samples.AnotherSample) Sample(org.motechproject.mds.builder.Sample) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with FieldMetadata

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));
        }
    }
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) MemberMetadata(javax.jdo.metadata.MemberMetadata) IndexMetadata(javax.jdo.metadata.IndexMetadata) HashSet(java.util.HashSet)

Example 9 with FieldMetadata

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);
}
Also used : ClassMetadata(javax.jdo.metadata.ClassMetadata) DiscriminatorMetadata(javax.jdo.metadata.DiscriminatorMetadata) FieldMetadata(javax.jdo.metadata.FieldMetadata) VersionMetadata(javax.jdo.metadata.VersionMetadata) PackageMetadata(javax.jdo.metadata.PackageMetadata) InheritanceMetadata(javax.jdo.metadata.InheritanceMetadata)

Example 10 with FieldMetadata

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");
}
Also used : CtClass(javassist.CtClass) FieldMetadata(javax.jdo.metadata.FieldMetadata) ForeignKeyMetadata(javax.jdo.metadata.ForeignKeyMetadata) CtField(javassist.CtField) Matchers.anyString(org.mockito.Matchers.anyString) MetadataDto(org.motechproject.mds.dto.MetadataDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

FieldMetadata (javax.jdo.metadata.FieldMetadata)16 Test (org.junit.Test)6 FieldDto (org.motechproject.mds.dto.FieldDto)6 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 ForeignKeyMetadata (javax.jdo.metadata.ForeignKeyMetadata)3 MetadataDto (org.motechproject.mds.dto.MetadataDto)3 CtClass (javassist.CtClass)2 IndexMetadata (javax.jdo.metadata.IndexMetadata)2 JoinMetadata (javax.jdo.metadata.JoinMetadata)2 Matchers.anyString (org.mockito.Matchers.anyString)2 AnotherSample (org.motechproject.mds.annotations.internal.samples.AnotherSample)2 Sample (org.motechproject.mds.builder.Sample)2 LookupDto (org.motechproject.mds.dto.LookupDto)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CtField (javassist.CtField)1 NotFoundException (javassist.NotFoundException)1 ClassMetadata (javax.jdo.metadata.ClassMetadata)1 CollectionMetadata (javax.jdo.metadata.CollectionMetadata)1