Search in sources :

Example 96 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class ClassGenerationActionTest method testExecuteArtifactPairsMapRelationships.

@Test
public void testExecuteArtifactPairsMapRelationships() throws Exception {
    ObjEntity testEntity1 = new ObjEntity("TE1");
    testEntity1.setClassName("org.example.TestClass1");
    final ObjEntity testEntity2 = new ObjEntity("TE1");
    testEntity2.setClassName("org.example.TestClass2");
    ObjRelationship relationship = new ObjRelationship("xMap") {

        private static final long serialVersionUID = 8042147877503405974L;

        @Override
        public boolean isToMany() {
            return true;
        }

        @Override
        public ObjEntity getTargetEntity() {
            return testEntity2;
        }
    };
    relationship.setCollectionType("java.util.Map");
    testEntity1.addRelationship(relationship);
    cgenConfiguration.setMakePairs(true);
    List<String> generated = execute(new EntityArtifact(testEntity1));
    assertNotNull(generated);
    assertEquals(2, generated.size());
    String superclass = generated.get(0);
    assertTrue(superclass, superclass.contains("import java.util.Map;"));
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) Test(org.junit.Test)

Example 97 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class ClassGenerationActionTest method testFileForClass.

@Test
public void testFileForClass() throws Exception {
    TemplateType templateType = TemplateType.DATAMAP_SINGLE_CLASS;
    cgenConfiguration.setRootPath(tempFolder.getRoot().toPath());
    cgenConfiguration.setRelPath(".");
    action = new ClassGenerationAction(cgenConfiguration);
    ObjEntity testEntity1 = new ObjEntity("TEST");
    testEntity1.setClassName("TestClass1");
    action.context.put(Artifact.SUB_PACKAGE_KEY, "");
    action.context.put(Artifact.SUB_CLASS_KEY, "TestClass1");
    File outFile = new File(tempFolder.getRoot() + "/TestClass1.java");
    assertFalse(outFile.exists());
    action.openWriter(templateType);
    assertTrue(outFile.exists());
    assertNull(action.openWriter(templateType));
    cgenConfiguration.setMakePairs(false);
    assertNull(action.openWriter(templateType));
    cgenConfiguration.setOverwrite(true);
    assertNull(action.openWriter(templateType));
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) TestClassGenerationAction(org.apache.cayenne.gen.mock.TestClassGenerationAction) Test(org.junit.Test)

Example 98 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class ClassGenerationActionTest method testExecuteArtifactPairsAttribute.

@Test
public void testExecuteArtifactPairsAttribute() throws Exception {
    ObjEntity testEntity1 = new ObjEntity("TE1");
    testEntity1.setClassName("org.example.TestClass1");
    ObjAttribute attr = new ObjAttribute();
    attr.setName("ID");
    attr.setType("int");
    ObjAttribute attr1 = new ObjAttribute();
    attr1.setName("name");
    attr1.setType("char");
    testEntity1.addAttribute(attr);
    testEntity1.addAttribute(attr1);
    cgenConfiguration.setMakePairs(true);
    List<String> generated = execute(new EntityArtifact(testEntity1));
    assertNotNull(generated);
    assertEquals(2, generated.size());
    String superclass = generated.get(0);
    assertTrue(superclass, superclass.contains("public void setID(int ID)"));
    assertTrue(superclass, superclass.contains("this.ID = ID;"));
    assertTrue(superclass, superclass.contains("public int getID()"));
    assertTrue(superclass, superclass.contains("return this.ID;"));
    assertTrue(superclass, superclass.contains("public void setName(char name)"));
    assertTrue(superclass, superclass.contains("this.name = name;"));
    assertTrue(superclass, superclass.contains("public char getName()"));
    assertTrue(superclass, superclass.contains("return this.name;"));
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) Test(org.junit.Test)

Example 99 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class ClassGenerationActionTest method assertCallbacks.

private void assertCallbacks(boolean isClient) throws Exception {
    ObjEntity testEntity1 = new ObjEntity("TE1");
    testEntity1.setClassName("org.example.TestClass1");
    int i = 0;
    for (CallbackDescriptor cb : testEntity1.getCallbackMap().getCallbacks()) {
        cb.addCallbackMethod("cb" + i++);
    }
    if (isClient) {
        cgenConfiguration.setClient(true);
        action = new TestClassGenerationAction(getUnitTestInjector().getInstance(ClassGenerationActionFactory.class).createAction(cgenConfiguration), writers);
    }
    cgenConfiguration.setMakePairs(true);
    List<String> generated = execute(new EntityArtifact(testEntity1));
    assertNotNull(generated);
    assertEquals(2, generated.size());
    String superclass = generated.get(0);
    assertTrue(superclass, superclass.contains("public abstract class _TestClass1"));
    for (int j = 0; j < i; j++) {
        assertTrue(superclass, superclass.contains("protected abstract void cb" + j + "();"));
    }
    String subclass = generated.get(1);
    for (int j = 0; j < i; j++) {
        assertTrue(subclass, subclass.contains("protected void cb" + j + "() {"));
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) TestClassGenerationAction(org.apache.cayenne.gen.mock.TestClassGenerationAction) CallbackDescriptor(org.apache.cayenne.map.CallbackDescriptor)

Example 100 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class ClassGenerationActionTest method testExecuteArtifactPairsImports.

@Test
public void testExecuteArtifactPairsImports() throws Exception {
    ObjEntity testEntity1 = new ObjEntity("TE1");
    testEntity1.setClassName("org.example.TestClass1");
    cgenConfiguration.setMakePairs(true);
    cgenConfiguration.setSuperPkg("org.example.auto");
    List<String> generated = execute(new EntityArtifact(testEntity1));
    assertNotNull(generated);
    assertEquals(2, generated.size());
    String superclass = generated.get(0);
    assertTrue(superclass, superclass.contains("package org.example.auto;"));
    assertTrue(superclass, superclass.contains("import org.apache.cayenne.BaseDataObject;"));
    String subclass = generated.get(1);
    assertTrue(subclass, subclass.contains("package org.example;"));
    assertTrue(subclass, subclass.contains("import org.example.auto._TestClass1;"));
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) Test(org.junit.Test)

Aggregations

ObjEntity (org.apache.cayenne.map.ObjEntity)294 Test (org.junit.Test)110 DbEntity (org.apache.cayenne.map.DbEntity)72 ObjAttribute (org.apache.cayenne.map.ObjAttribute)68 ObjRelationship (org.apache.cayenne.map.ObjRelationship)62 DataMap (org.apache.cayenne.map.DataMap)57 DbAttribute (org.apache.cayenne.map.DbAttribute)37 DbRelationship (org.apache.cayenne.map.DbRelationship)29 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)27 ObjectId (org.apache.cayenne.ObjectId)26 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)22 ArrayList (java.util.ArrayList)19 Embeddable (org.apache.cayenne.map.Embeddable)18 EntityResolver (org.apache.cayenne.map.EntityResolver)17 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)16 Expression (org.apache.cayenne.exp.Expression)15 Persistent (org.apache.cayenne.Persistent)12 EntityEvent (org.apache.cayenne.map.event.EntityEvent)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 Entity (org.apache.cayenne.map.Entity)11