Search in sources :

Example 1 with FileProjectSaver

use of org.apache.cayenne.project.FileProjectSaver in project cayenne by apache.

the class DefaultDbImportActionTest method testImportWithoutChanges.

@Test
public void testImportWithoutChanges() throws Exception {
    DbImportConfiguration config = mock(DbImportConfiguration.class);
    when(config.getTargetDataMap()).thenReturn(FILE_STUB);
    when(config.createMergeDelegate()).thenReturn(new DefaultModelMergeDelegate());
    when(config.getDbLoaderConfig()).thenReturn(new DbLoaderConfiguration());
    DbLoader dbLoader = new DbLoader(mockAdapter, mockConnection, config.getDbLoaderConfig(), mockDelegate, mockNameGenerator) {

        @Override
        public DataMap load() throws SQLException {
            DataMap dataMap = new DataMap();
            new DataMapBuilder(dataMap).with(dbEntity("ARTGROUP").attributes(dbAttr("NAME").typeVarchar(100).mandatory()));
            return dataMap;
        }
    };
    FileProjectSaver projectSaver = mock(FileProjectSaver.class);
    doNothing().when(projectSaver).save(any(Project.class));
    DataMapLoader mapLoader = mock(DataMapLoader.class);
    when(mapLoader.load(any(Resource.class))).thenReturn(new DataMapBuilder().with(dbEntity("ARTGROUP").attributes(dbAttr("NAME").typeVarchar(100).mandatory())).build());
    DefaultDbImportAction action = buildDbImportAction(projectSaver, mapLoader, dbLoader);
    action.execute(config);
    // no changes - we still
    verify(projectSaver, never()).save(any(Project.class));
    verify(mapLoader, times(1)).load(any(Resource.class));
}
Also used : Project(org.apache.cayenne.project.Project) DataMapBuilder(org.apache.cayenne.dbsync.merge.builders.DataMapBuilder) FileProjectSaver(org.apache.cayenne.project.FileProjectSaver) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) Resource(org.apache.cayenne.resource.Resource) URLResource(org.apache.cayenne.resource.URLResource) DbLoader(org.apache.cayenne.dbsync.reverse.dbload.DbLoader) DefaultModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.DefaultModelMergeDelegate) DbLoaderConfiguration(org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 2 with FileProjectSaver

use of org.apache.cayenne.project.FileProjectSaver in project cayenne by apache.

the class DefaultDbImportActionTest method testImportWithFieldChanged.

@Test
public void testImportWithFieldChanged() throws Exception {
    DbImportConfiguration config = mock(DbImportConfiguration.class);
    when(config.getTargetDataMap()).thenReturn(FILE_STUB);
    when(config.createMergeDelegate()).thenReturn(new DefaultModelMergeDelegate());
    when(config.getDbLoaderConfig()).thenReturn(new DbLoaderConfiguration());
    when(config.createNameGenerator()).thenReturn(new DefaultObjectNameGenerator(NoStemStemmer.getInstance()));
    when(config.createMeaningfulPKFilter()).thenReturn(NamePatternMatcher.EXCLUDE_ALL);
    DbLoader dbLoader = new DbLoader(mockAdapter, mockConnection, config.getDbLoaderConfig(), mockDelegate, mockNameGenerator) {

        @Override
        public DataMap load() throws SQLException {
            DataMap dataMap = new DataMap();
            new DataMapBuilder(dataMap).with(dbEntity("ARTGROUP").attributes(dbAttr("GROUP_ID").typeInt().primaryKey(), dbAttr("NAME").typeVarchar(100).mandatory(), dbAttr("NAME_01").typeVarchar(100).mandatory(), dbAttr("PARENT_GROUP_ID").typeInt())).with(objEntity("org.apache.cayenne.testdo.testmap", "ArtGroup", "ARTGROUP").attributes(objAttr("name").type(String.class).dbPath("NAME")));
            return dataMap;
        }
    };
    final boolean[] haveWeTriedToSave = { false };
    DefaultDbImportAction action = buildDbImportAction(new FileProjectSaver(Collections.<ProjectExtension>emptyList()) {

        @Override
        public void save(Project project) {
            haveWeTriedToSave[0] = true;
            // Validation phase
            DataMap rootNode = (DataMap) project.getRootNode();
            assertEquals(1, rootNode.getObjEntities().size());
            assertEquals(1, rootNode.getDbEntityMap().size());
            DbEntity entity = rootNode.getDbEntity("ARTGROUP");
            assertNotNull(entity);
            assertEquals(4, entity.getAttributes().size());
            assertNotNull(entity.getAttribute("NAME_01"));
        }
    }, new DataMapLoader() {

        @Override
        public DataMap load(Resource configurationResource) throws CayenneRuntimeException {
            return new DataMapBuilder().with(dbEntity("ARTGROUP").attributes(dbAttr("GROUP_ID").typeInt().primaryKey(), dbAttr("NAME").typeVarchar(100).mandatory(), dbAttr("PARENT_GROUP_ID").typeInt())).with(objEntity("org.apache.cayenne.testdo.testmap", "ArtGroup", "ARTGROUP").attributes(objAttr("name").type(String.class).dbPath("NAME"))).build();
        }
    }, dbLoader);
    action.execute(config);
    assertTrue("We should try to save.", haveWeTriedToSave[0]);
}
Also used : FileProjectSaver(org.apache.cayenne.project.FileProjectSaver) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) Resource(org.apache.cayenne.resource.Resource) URLResource(org.apache.cayenne.resource.URLResource) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DefaultModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.DefaultModelMergeDelegate) DbLoaderConfiguration(org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration) DataMap(org.apache.cayenne.map.DataMap) DefaultObjectNameGenerator(org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator) Project(org.apache.cayenne.project.Project) DataMapBuilder(org.apache.cayenne.dbsync.merge.builders.DataMapBuilder) DbEntity(org.apache.cayenne.map.DbEntity) ProjectExtension(org.apache.cayenne.project.extension.ProjectExtension) DbLoader(org.apache.cayenne.dbsync.reverse.dbload.DbLoader) Test(org.junit.Test)

Example 3 with FileProjectSaver

use of org.apache.cayenne.project.FileProjectSaver in project cayenne by apache.

the class DefaultDbImportActionTest method testNewDataMapImport.

@Test
public void testNewDataMapImport() throws Exception {
    DbImportConfiguration config = mock(DbImportConfiguration.class);
    when(config.createMergeDelegate()).thenReturn(new DefaultModelMergeDelegate());
    when(config.getDbLoaderConfig()).thenReturn(new DbLoaderConfiguration());
    when(config.getTargetDataMap()).thenReturn(new File("xyz.map.xml"));
    when(config.createNameGenerator()).thenReturn(new DefaultObjectNameGenerator(NoStemStemmer.getInstance()));
    when(config.createMeaningfulPKFilter()).thenReturn(NamePatternMatcher.EXCLUDE_ALL);
    DbLoader dbLoader = new DbLoader(mockAdapter, mockConnection, config.getDbLoaderConfig(), mockDelegate, mockNameGenerator) {

        @Override
        public DataMap load() throws SQLException {
            DataMap map = new DataMap();
            new DataMapBuilder(map).withDbEntities(2).build();
            return map;
        }
    };
    final boolean[] haveWeTriedToSave = { false };
    DefaultDbImportAction action = buildDbImportAction(new FileProjectSaver(Collections.emptyList()) {

        @Override
        public void save(Project project) {
            haveWeTriedToSave[0] = true;
            // Validation phase
            assertTrue(project.getRootNode() instanceof DataMap);
        }
    }, null, dbLoader);
    action.execute(config);
    assertTrue("We should try to save.", haveWeTriedToSave[0]);
}
Also used : FileProjectSaver(org.apache.cayenne.project.FileProjectSaver) DefaultModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.DefaultModelMergeDelegate) DbLoaderConfiguration(org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration) DataMap(org.apache.cayenne.map.DataMap) DefaultObjectNameGenerator(org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator) Project(org.apache.cayenne.project.Project) DataMapBuilder(org.apache.cayenne.dbsync.merge.builders.DataMapBuilder) File(java.io.File) DbLoader(org.apache.cayenne.dbsync.reverse.dbload.DbLoader) Test(org.junit.Test)

Example 4 with FileProjectSaver

use of org.apache.cayenne.project.FileProjectSaver in project cayenne by apache.

the class DefaultDbImportActionTest method testImportWithDbError.

@Test
public void testImportWithDbError() throws Exception {
    DbLoader dbLoader = mock(DbLoader.class);
    doThrow(new SQLException()).when(dbLoader).load();
    DbImportConfiguration params = mock(DbImportConfiguration.class);
    FileProjectSaver projectSaver = mock(FileProjectSaver.class);
    doNothing().when(projectSaver).save(any(Project.class));
    DataMapLoader mapLoader = mock(DataMapLoader.class);
    when(mapLoader.load(any(Resource.class))).thenReturn(null);
    DefaultDbImportAction action = buildDbImportAction(projectSaver, mapLoader, dbLoader);
    try {
        action.execute(params);
        fail();
    } catch (SQLException e) {
    // expected
    }
    verify(projectSaver, never()).save(any(Project.class));
    verify(mapLoader, never()).load(any(Resource.class));
}
Also used : Project(org.apache.cayenne.project.Project) SQLException(java.sql.SQLException) FileProjectSaver(org.apache.cayenne.project.FileProjectSaver) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) Resource(org.apache.cayenne.resource.Resource) URLResource(org.apache.cayenne.resource.URLResource) DbLoader(org.apache.cayenne.dbsync.reverse.dbload.DbLoader) Test(org.junit.Test)

Aggregations

DbLoader (org.apache.cayenne.dbsync.reverse.dbload.DbLoader)4 FileProjectSaver (org.apache.cayenne.project.FileProjectSaver)4 Project (org.apache.cayenne.project.Project)4 Test (org.junit.Test)4 DataMapLoader (org.apache.cayenne.configuration.DataMapLoader)3 DataMapBuilder (org.apache.cayenne.dbsync.merge.builders.DataMapBuilder)3 DbLoaderConfiguration (org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration)3 DefaultModelMergeDelegate (org.apache.cayenne.dbsync.reverse.dbload.DefaultModelMergeDelegate)3 DataMap (org.apache.cayenne.map.DataMap)3 Resource (org.apache.cayenne.resource.Resource)3 URLResource (org.apache.cayenne.resource.URLResource)3 DefaultObjectNameGenerator (org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator)2 File (java.io.File)1 SQLException (java.sql.SQLException)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 DbEntity (org.apache.cayenne.map.DbEntity)1 ProjectExtension (org.apache.cayenne.project.extension.ProjectExtension)1