Search in sources :

Example 6 with ClassLoaderResolverImpl

use of org.datanucleus.ClassLoaderResolverImpl in project tests by datanucleus.

the class AnnotationTest method testBasic.

/**
 * Test of basic JPA annotations reading capability
 */
public void testBasic() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    ClassLoaderResolver clr = new ClassLoaderResolverImpl();
    // Checks for Department
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Department.class.getName(), clr);
    String prefix = cmd1.getFullClassName() + " : ";
    assertEquals(prefix + "detachable is wrong", cmd1.isDetachable(), true);
    assertEquals(prefix + "identity-type is wrong", cmd1.getIdentityType(), IdentityType.APPLICATION);
    assertEquals(prefix + "embedded-only is wrong", cmd1.isEmbeddedOnly(), false);
    assertEquals(prefix + "requires-extent is wrong", cmd1.isRequiresExtent(), true);
    assertEquals(prefix + "catalog is wrong", cmd1.getCatalog(), null);
    assertEquals(prefix + "schema is wrong", cmd1.getSchema(), null);
    assertEquals(prefix + "table is wrong", cmd1.getTable(), "JPA_AN_DEPARTMENT");
    assertEquals(prefix + "has incorrect number of persistent fields", cmd1.getNoOfManagedMembers(), 4);
    InheritanceMetaData inhmd1 = cmd1.getInheritanceMetaData();
    assertEquals("Inheritance strategy is incorrect", InheritanceStrategy.NEW_TABLE, inhmd1.getStrategy());
    // "projects"
    AbstractMemberMetaData fmd = cmd1.getMetaDataForMember("projects");
    assertNotNull(prefix + "doesnt have required field", fmd);
    assertEquals(prefix + "should be persistent", fmd.getPersistenceModifier(), FieldPersistenceModifier.PERSISTENT);
    assertFalse(prefix + "pk is wrong", fmd.isPrimaryKey());
    assertFalse(prefix + "dfg is wrong", fmd.isDefaultFetchGroup());
    assertTrue(prefix + "has no container specified!", fmd.getCollection() != null);
    assertEquals(prefix + "should have collection of Project elements but hasnt", fmd.getCollection().getElementType(), Project.class.getName());
    assertEquals(prefix + "shouldnt have collection of serialised elements but has", fmd.getCollection().isSerializedElement(), false);
    assertEquals(prefix + "shouldnt have collection of dependent elements but has", fmd.getCollection().isDependentElement(), false);
    // Checks for Project
    ClassMetaData cmd2 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Project.class.getName(), clr);
    prefix = cmd2.getFullClassName() + " : ";
    assertEquals(prefix + "detachable is wrong", true, cmd2.isDetachable());
    assertEquals(prefix + "identity-type is wrong", cmd2.getIdentityType(), IdentityType.APPLICATION);
    assertEquals(prefix + "objectid-class is wrong", "org.datanucleus.identity.StringId", cmd2.getObjectidClass());
    assertEquals(prefix + "embedded-only is wrong", cmd2.isEmbeddedOnly(), false);
    assertEquals(prefix + "requires-extent is wrong", cmd2.isRequiresExtent(), true);
    assertEquals(prefix + "catalog is wrong", cmd2.getCatalog(), null);
    assertEquals(prefix + "schema is wrong", cmd2.getSchema(), null);
    assertEquals(prefix + "table is wrong", "JPA_AN_PROJECT", cmd2.getTable());
    assertEquals(prefix + "has incorrect number of persistent fields", cmd2.getNoOfManagedMembers(), 2);
    InheritanceMetaData inhmd2 = cmd2.getInheritanceMetaData();
    assertEquals("Inheritance strategy is incorrect", InheritanceStrategy.NEW_TABLE, inhmd2.getStrategy());
    // "name"
    fmd = cmd2.getMetaDataForMember("name");
    assertNotNull(prefix + "doesnt have required field", fmd);
    assertTrue(prefix + "pk is wrong", fmd.isPrimaryKey());
    assertTrue(prefix + "dfg is wrong", fmd.isDefaultFetchGroup());
    assertEquals(prefix + "should be persistent", fmd.getPersistenceModifier(), FieldPersistenceModifier.PERSISTENT);
    // "budget"
    fmd = cmd2.getMetaDataForMember("budget");
    assertNotNull(prefix + "doesnt have required field", fmd);
    assertEquals(prefix + "has incorrect persistent field", fmd.getName(), "budget");
    assertFalse(prefix + "pk is wrong", fmd.isPrimaryKey());
    assertTrue(prefix + "dfg is wrong", fmd.isDefaultFetchGroup());
    assertEquals(prefix + "should be persistent", fmd.getPersistenceModifier(), FieldPersistenceModifier.PERSISTENT);
}
Also used : Project(org.datanucleus.samples.annotations.models.company.Project) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) InheritanceMetaData(org.datanucleus.metadata.InheritanceMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 7 with ClassLoaderResolverImpl

use of org.datanucleus.ClassLoaderResolverImpl in project tests by datanucleus.

the class AnnotationTest method testMapKeyColumn.

/**
 * Test of persistent properties using annotations.
 */
/*public void testPersistentProperties()
    {
        NucleusContext nucleusCtx = new NucleusContext("JPA", null);
        MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);

        // Retrieve the metadata from the MetaDataManager (populates and initialises everything)
        ClassLoaderResolver clr = new ClassLoaderResolverImpl();
        ClassMetaData cmd1 = (ClassMetaData)metaDataMgr.getMetaDataForClass(JPAGetter.class.getName(), clr);
        assertEquals(1, cmd1.getNoOfPrimaryKeyMembers());
    }*/
/**
 * Test of column name for property instead of field
 */
/*public void testPropertyColumName()
    {
        NucleusContext nucleusCtx = new NucleusContext("JPA", null);
        MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);

        // Retrieve the metadata from the MetaDataManager (populates and initialises everything)
        ClassLoaderResolver clr = new ClassLoaderResolverImpl();
        ClassMetaData cmd1 = (ClassMetaData)metaDataMgr.getMetaDataForClass(Employee.class.getName(), clr);

        // it is valid according JPA to have property accessor instead of field accessors. property accessors are persistent while field not.
        assertNotNull("Employee.lastName has no field information", cmd1.getMetaDataForMember("lastName"));
        assertNotNull("Employee.lastName has no column information", cmd1.getMetaDataForMember("lastName").getColumnMetaData());
        assertEquals("Employee.lastName has incorrect number of columns", 
            1, cmd1.getMetaDataForMember("lastName").getColumnMetaData().length);
        assertEquals("Employee.last has incorrect column spec", 
            "LASTNAME", cmd1.getMetaDataForMember("lastName").getColumnMetaData()[0].getName());

        ClassMetaData cmd2 = (ClassMetaData)metaDataMgr.getMetaDataForClass(Person.class.getName(), clr);

        // it is valid according JPA to have property accessor instead of field accessors. property accessors are persistent while field not.
        assertNotNull(cmd2.getMetaDataForMember("age"));
        assertNotNull("AGE_COL",cmd2.getMetaDataForMember("age").getColumnMetaData()[0].getName());
        assertNotNull(cmd2.getMetaDataForMember("maidenName"));
        assertEquals(FieldPersistenceModifier.NONE,cmd2.getMetaDataForMember("_maidenName").getPersistenceModifier());
        assertEquals(FieldPersistenceModifier.PERSISTENT,cmd2.getMetaDataForMember("maidenName").getPersistenceModifier());
    }*/
/**
 * Test of JPA @MapKeyColumn.
 */
public void testMapKeyColumn() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    // Retrieve the metadata from the MetaDataManager (populates and initialises everything)
    ClassLoaderResolver clr = new ClassLoaderResolverImpl();
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Person.class.getName(), clr);
    assertEquals("phoneNumbers_key1", cmd1.getMetaDataForMember("phoneNumbers").getKeyMetaData().getColumnMetaData()[0].getName());
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) ClassMetaData(org.datanucleus.metadata.ClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 8 with ClassLoaderResolverImpl

use of org.datanucleus.ClassLoaderResolverImpl in project tests by datanucleus.

the class AnnotationTest method testByteArrayEmbeddedByDefault.

/**
 * Test of JPA Byte[] is embedded by default
 */
public void testByteArrayEmbeddedByDefault() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    // Retrieve the metadata from the MetaDataManager (populates and initialises everything)
    ClassLoaderResolver clr = new ClassLoaderResolverImpl();
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(ByteArray.class.getName(), clr);
    assertTrue(cmd1.getMetaDataForMember("array1").isEmbedded());
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) ClassMetaData(org.datanucleus.metadata.ClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 9 with ClassLoaderResolverImpl

use of org.datanucleus.ClassLoaderResolverImpl in project tests by datanucleus.

the class IdentifierFactoryTest method testDataNucleus2.

/**
 * Tests for DNIdentifierFactory
 */
public void testDataNucleus2() {
    RDBMSStoreManager srm = (RDBMSStoreManager) storeMgr;
    IdentifierFactory idFactory = srm.getIdentifierFactory();
    ClassLoaderResolver clr = new ClassLoaderResolverImpl();
    // Table identifiers
    // a). generated name shorter than max length
    DatastoreIdentifier id = idFactory.newIdentifier(IdentifierType.TABLE, "MyClass");
    assertTrue("newIdentifier(TABLE, String) has generated an incorrect name : " + id.getName(), "MYCLASS".equalsIgnoreCase(id.getName()));
    // b). specified name shorter than max length
    id = idFactory.newTableIdentifier("MY_TABLE_NAME");
    assertTrue("newDatastoreContainerIdentifier(String) has returned an incorrect name when should have used the supplied name " + id.getName(), "MY_TABLE_NAME".equalsIgnoreCase(id.getName()));
    // c). name specified via ClassMetaData
    AbstractClassMetaData managerCMD = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass("org.jpox.samples.models.company.Manager", clr);
    id = idFactory.newTableIdentifier(managerCMD);
    assertTrue("newDatastoreContainerIdentifier(clr, ClassMetaData) has returned an incorrect generated name " + id.getName(), "MANAGER".equalsIgnoreCase(id.getName()));
    // d). name specified via ClassMetaData
    AbstractMemberMetaData fmd = managerCMD.getMetaDataForMember("subordinates");
    id = idFactory.newTableIdentifier(fmd);
    assertTrue("newDatastoreContainerIdentifier(clr, AbstractMemberMetaData) has returned an incorrect generated name " + id.getName(), "MANAGER_SUBORDINATES".equalsIgnoreCase(id.getName()));
    // Column identifiers
    // a). generated name shorter than max length
    id = idFactory.newIdentifier(IdentifierType.COLUMN, "myField");
    assertTrue("newIdentifier(COLUMN, String) has generated an incorrect name : " + id.getName(), "MYFIELD".equalsIgnoreCase(id.getName()));
    // b). specified name shorter than max length
    id = idFactory.newColumnIdentifier("MYCOLUMNNAME");
    assertTrue("newColumnIdentifier(String) has returned an incorrect name when should have used the supplied name " + id.getName(), "MYCOLUMNNAME".equalsIgnoreCase(id.getName()));
    // c). Discriminator column identifier
    id = idFactory.newDiscriminatorFieldIdentifier();
    assertTrue("newDiscriminatorFieldIdentifier() has returned an incorrect name : " + id.getName(), "DISCRIMINATOR".equalsIgnoreCase(id.getName()));
    // d). Version column identifier
    id = idFactory.newVersionFieldIdentifier();
    assertTrue("newVersionFieldIdentifier() has returned an incorrect name : " + id.getName(), "VERSION".equalsIgnoreCase(id.getName()));
    // e). Index (ordering) column identifier
    id = idFactory.newIndexFieldIdentifier(fmd);
    assertTrue("newIndexFieldIdentifier() has returned an incorrect name : " + id.getName(), "IDX".equalsIgnoreCase(id.getName()));
    // f). Adapter Index column identifier
    id = idFactory.newAdapterIndexFieldIdentifier();
    assertTrue("newAdapterIndexFieldIdentifier() has returned an incorrect name : " + id.getName(), "IDX".equalsIgnoreCase(id.getName()));
    AbstractMemberMetaData[] relatedMmds = fmd.getRelatedMemberMetaData(clr);
    // g). join table owner column identifier (1-N bi JoinTable)
    DatastoreIdentifier destId = idFactory.newColumnIdentifier("MANAGER_ID");
    id = idFactory.newJoinTableFieldIdentifier(fmd, relatedMmds != null ? relatedMmds[0] : null, destId, false, FieldRole.ROLE_OWNER);
    assertTrue("newJoinTableFieldIdentifier(OWNER) has returned an incorrect generated name " + id.getName(), "MANAGER_ID_OID".equalsIgnoreCase(id.getName()));
    // h). join table element column identifier (1-N bi JoinTable)
    destId = idFactory.newColumnIdentifier("EMPLOYEE_ID");
    id = idFactory.newJoinTableFieldIdentifier(fmd, relatedMmds != null ? relatedMmds[0] : null, destId, false, FieldRole.ROLE_COLLECTION_ELEMENT);
    assertTrue("newJoinTableFieldIdentifier(ELEMENT) has returned an incorrect generated name " + id.getName(), "EMPLOYEE_ID_EID".equalsIgnoreCase(id.getName()));
    // i). FK owner column identifier (1-N bi FK)
    AbstractMemberMetaData deptsFMD = managerCMD.getMetaDataForMember("departments");
    AbstractMemberMetaData[] deptsRelatedMmds = deptsFMD.getRelatedMemberMetaData(clr);
    destId = idFactory.newColumnIdentifier("MANAGER_ID");
    id = idFactory.newForeignKeyFieldIdentifier(deptsFMD, deptsRelatedMmds != null ? deptsRelatedMmds[0] : null, destId, false, FieldRole.ROLE_OWNER);
    assertTrue("newForeignKeyFieldIdentifier(OWNER) has returned an incorrect generated name " + id.getName(), "MANAGER_MANAGER_ID_OID".equalsIgnoreCase(id.getName()));
// Primary key identifiers
// Index identifiers
// Foreign key identifiers
// Candidate key identifiers
// Sequence identifiers
}
Also used : DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPOXIdentifierFactory(org.datanucleus.store.rdbms.identifier.JPOXIdentifierFactory) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 10 with ClassLoaderResolverImpl

use of org.datanucleus.ClassLoaderResolverImpl in project tests by datanucleus.

the class InheritanceTest method testSubclassInformation.

/**
 * Some test for subclass information.
 */
public void testSubclassInformation() {
    JDOMetaDataManager mgr = new JDOMetaDataManager(new PersistenceNucleusContextImpl("JDO", null));
    // initialize metadata
    mgr.getMetaDataForClass(JBase.class, new ClassLoaderResolverImpl());
    mgr.getMetaDataForClass(JBowl.class, new ClassLoaderResolverImpl());
    mgr.getMetaDataForClass(JSpottedMarble.class, new ClassLoaderResolverImpl());
    mgr.getMetaDataForClass(JTransparentMarble.class, new ClassLoaderResolverImpl());
    mgr.getSubclassesForClass(JBase.class.getName(), false);
    Set test = getSet(mgr.getSubclassesForClass(JBase.class.getName(), false));
    assertTrue(test.contains(JBowl.class.getName()));
    assertTrue(test.contains(JMarble.class.getName()));
    test = getSet(mgr.getSubclassesForClass(JMarble.class.getName(), false));
    assertTrue(test.contains(JSpottedMarble.class.getName()));
    assertTrue(test.contains(JTransparentMarble.class.getName()));
    test = getSet(mgr.getSubclassesForClass(JBase.class.getName(), true));
    assertTrue(test.contains(JBowl.class.getName()));
    assertTrue(test.contains(JMarble.class.getName()));
    assertTrue(test.contains(JSpottedMarble.class.getName()));
    assertTrue(test.contains(JTransparentMarble.class.getName()));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) JBase(org.jpox.samples.inheritance.JBase) JMarble(org.jpox.samples.inheritance.JMarble) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Aggregations

ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)53 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)35 MetaDataManager (org.datanucleus.metadata.MetaDataManager)31 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)27 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)23 JDOMetaDataManager (org.datanucleus.api.jdo.metadata.JDOMetaDataManager)22 NucleusContext (org.datanucleus.NucleusContext)14 ClassMetaData (org.datanucleus.metadata.ClassMetaData)14 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)12 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)10 FetchPlan (org.datanucleus.FetchPlan)7 FetchPlanForClass (org.datanucleus.FetchPlanForClass)7 JDOFetchPlan (org.datanucleus.api.jdo.JDOFetchPlan)7 BitSet (java.util.BitSet)6 NonManagedPluginRegistry (org.datanucleus.plugin.NonManagedPluginRegistry)6 HashSet (java.util.HashSet)3 Properties (java.util.Properties)3 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)3 Bundle (org.datanucleus.plugin.Bundle)3 ArrayList (java.util.ArrayList)2