use of org.apache.cayenne.resource.Resource in project cayenne by apache.
the class DataChannelProjectLoaderTest method testLoad.
@Test
public void testLoad() {
DataChannelProjectLoader loader = new DataChannelProjectLoader();
Module testModule = binder -> {
binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
binder.bind(DataMapLoader.class).to(XMLDataMapLoader.class);
binder.bind(DataChannelDescriptorLoader.class).to(XMLDataChannelDescriptorLoader.class);
binder.bind(ConfigurationNameMapper.class).to(DefaultConfigurationNameMapper.class);
binder.bind(HandlerFactory.class).to(DefaultHandlerFactory.class);
binder.bind(DataChannelMetaData.class).to(NoopDataChannelMetaData.class);
binder.bind(XMLReader.class).toProviderInstance(new XMLReaderProvider(false)).withoutScope();
};
Injector injector = DIBootstrap.createInjector(testModule);
injector.injectMembers(loader);
String testConfigName = "PROJECT1";
String baseUrl = getClass().getPackage().getName().replace('.', '/');
URL url = getClass().getClassLoader().getResource(baseUrl + "/cayenne-" + testConfigName + ".xml");
Resource rootSource = new URLResource(url);
Project project = loader.loadProject(rootSource);
assertNotNull(project);
DataChannelDescriptor rootNode = (DataChannelDescriptor) project.getRootNode();
assertNotNull(rootNode);
assertSame(rootSource, rootNode.getConfigurationSource());
assertNotNull(project.getConfigurationResource());
assertEquals(project.getConfigurationResource(), rootSource);
}
use of org.apache.cayenne.resource.Resource in project cayenne by apache.
the class DataChannelProjectSaverTest method testSaveAs.
@Test
public void testSaveAs() throws Exception {
FileProjectSaver saver = new FileProjectSaver(Collections.<ProjectExtension>emptyList());
Module testModule = binder -> {
binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
binder.bind(DataMapLoader.class).to(XMLDataMapLoader.class);
binder.bind(DataChannelDescriptorLoader.class).to(XMLDataChannelDescriptorLoader.class);
binder.bind(ProjectLoader.class).to(DataChannelProjectLoader.class);
binder.bind(ConfigurationNameMapper.class).to(DefaultConfigurationNameMapper.class);
binder.bind(HandlerFactory.class).to(DefaultHandlerFactory.class);
binder.bind(DataChannelMetaData.class).to(NoopDataChannelMetaData.class);
binder.bind(XMLReader.class).toProviderInstance(new XMLReaderProvider(false)).withoutScope();
};
Injector injector = DIBootstrap.createInjector(testModule);
injector.injectMembers(saver);
String testConfigName = "PROJECT2";
String baseUrl = getClass().getPackage().getName().replace('.', '/');
URL url = getClass().getClassLoader().getResource(baseUrl + "/cayenne-" + testConfigName + ".xml");
Resource source = new URLResource(url);
Project project = injector.getInstance(ProjectLoader.class).loadProject(source);
File outFile = setupTestDirectory("testSave");
saver.saveAs(project, new URLResource(outFile.toURI().toURL()));
File rootFile = new File(outFile, "cayenne-PROJECT2.xml");
assertTrue(rootFile.exists());
assertTrue(rootFile.length() > 0);
File map1File = new File(outFile, "testProjectMap2_1.map.xml");
assertTrue(map1File.exists());
assertTrue(map1File.length() > 0);
File map2File = new File(outFile, "testProjectMap2_2.map.xml");
assertTrue(map2File.exists());
assertTrue(map2File.length() > 0);
}
use of org.apache.cayenne.resource.Resource in project cayenne by apache.
the class DefaultUpgradeServiceTest method getAdditionalDatamapResources.
@Test
public void getAdditionalDatamapResources() throws Exception {
URL url = getClass().getResource("../cayenne-PROJECT1.xml");
Resource resource = new URLResource(url);
Document document = readDocument(url);
UpgradeUnit unit = new UpgradeUnit(resource, document);
List<Resource> resources = upgradeService.getAdditionalDatamapResources(unit);
assertEquals(2, resources.size());
assertTrue(resources.get(0).getURL().sameFile(getClass().getResource("../testProjectMap1_1.map.xml")));
}
use of org.apache.cayenne.resource.Resource 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]);
}
use of org.apache.cayenne.resource.Resource in project cayenne by apache.
the class OpenProjectAction method openProject.
/**
* Opens specified project file. File must already exist.
*/
public void openProject(File file) {
try {
if (!file.exists()) {
JOptionPane.showMessageDialog(Application.getFrame(), "Can't open project - file \"" + file.getPath() + "\" does not exist", "Can't Open Project", JOptionPane.ERROR_MESSAGE);
return;
}
CayenneModelerController controller = Application.getInstance().getFrameController();
controller.addToLastProjListAction(file);
URL url = file.toURI().toURL();
Resource rootSource = new URLResource(url);
UpgradeService upgradeService = getApplication().getInjector().getInstance(UpgradeService.class);
UpgradeMetaData metaData = upgradeService.getUpgradeType(rootSource);
switch(metaData.getUpgradeType()) {
case INTERMEDIATE_UPGRADE_NEEDED:
String modelerVersion = PROJECT_TO_MODELER_VERSION.get(metaData.getProjectVersion());
if (modelerVersion == null) {
modelerVersion = "";
}
JOptionPane.showMessageDialog(Application.getFrame(), "Open the project in the older Modeler " + modelerVersion + " to do an intermediate upgrade\nbefore you can upgrade to latest version.", "Can't Upgrade Project", JOptionPane.ERROR_MESSAGE);
closeProject(false);
return;
case DOWNGRADE_NEEDED:
JOptionPane.showMessageDialog(Application.getFrame(), "Can't open project - it was created using a newer version of the Modeler", "Can't Open Project", JOptionPane.ERROR_MESSAGE);
closeProject(false);
return;
case UPGRADE_NEEDED:
if (processUpgrades()) {
rootSource = upgradeService.upgradeProject(rootSource);
} else {
closeProject(false);
return;
}
break;
}
openProjectResourse(rootSource, controller);
} catch (Exception ex) {
logObj.warn("Error loading project file.", ex);
ErrorDebugDialog.guiWarning(ex, "Error loading project");
}
}
Aggregations