Search in sources :

Example 31 with URLResource

use of org.apache.cayenne.resource.URLResource in project cayenne by apache.

the class ManyToManyCandidateEntityTest method setUp.

@Before
public void setUp() throws Exception {
    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(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);
    // create and initialize loader instance to test
    XMLDataChannelDescriptorLoader loader = new XMLDataChannelDescriptorLoader();
    injector.injectMembers(loader);
    String testConfigName = "relationship-optimisation";
    URL url = getClass().getResource("cayenne-" + testConfigName + ".xml");
    ConfigurationTree<DataChannelDescriptor> tree = loader.load(new URLResource(url));
    map = tree.getRootNode().getDataMap(testConfigName);
}
Also used : Injector(org.apache.cayenne.di.Injector) ObjEntity(org.apache.cayenne.map.ObjEntity) HandlerFactory(org.apache.cayenne.configuration.xml.HandlerFactory) XMLReaderProvider(org.apache.cayenne.configuration.xml.XMLReaderProvider) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) Relationship(org.apache.cayenne.map.Relationship) URL(java.net.URL) ConfigurationNameMapper(org.apache.cayenne.configuration.ConfigurationNameMapper) Module(org.apache.cayenne.di.Module) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) ArrayList(java.util.ArrayList) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) XMLDataChannelDescriptorLoader(org.apache.cayenne.configuration.xml.XMLDataChannelDescriptorLoader) DefaultObjectNameGenerator(org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator) XMLReader(org.xml.sax.XMLReader) DefaultHandlerFactory(org.apache.cayenne.configuration.xml.DefaultHandlerFactory) ConfigurationTree(org.apache.cayenne.configuration.ConfigurationTree) NoopDataChannelMetaData(org.apache.cayenne.configuration.xml.NoopDataChannelMetaData) NoStemStemmer(org.apache.cayenne.dbsync.naming.NoStemStemmer) XMLDataMapLoader(org.apache.cayenne.configuration.xml.XMLDataMapLoader) DIBootstrap(org.apache.cayenne.di.DIBootstrap) Before(org.junit.Before) URLResource(org.apache.cayenne.resource.URLResource) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Test(org.junit.Test) DataChannelMetaData(org.apache.cayenne.configuration.xml.DataChannelMetaData) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) Assert(org.junit.Assert) DataMap(org.apache.cayenne.map.DataMap) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) XMLDataMapLoader(org.apache.cayenne.configuration.xml.XMLDataMapLoader) URL(java.net.URL) NoopDataChannelMetaData(org.apache.cayenne.configuration.xml.NoopDataChannelMetaData) XMLDataChannelDescriptorLoader(org.apache.cayenne.configuration.xml.XMLDataChannelDescriptorLoader) XMLReaderProvider(org.apache.cayenne.configuration.xml.XMLReaderProvider) URLResource(org.apache.cayenne.resource.URLResource) DefaultHandlerFactory(org.apache.cayenne.configuration.xml.DefaultHandlerFactory) Injector(org.apache.cayenne.di.Injector) Module(org.apache.cayenne.di.Module) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) XMLReader(org.xml.sax.XMLReader) Before(org.junit.Before)

Example 32 with URLResource

use of org.apache.cayenne.resource.URLResource in project cayenne by apache.

the class DataChannelProjectSaverTest method testSaveAs_RecoverFromSaveError.

@Test
public void testSaveAs_RecoverFromSaveError() throws Exception {
    FileProjectSaver saver = new FileProjectSaver(Collections.<ProjectExtension>emptyList()) {

        @Override
        void saveToTempFile(SaveUnit unit, PrintWriter printWriter) {
            throw new CayenneRuntimeException("Test Exception");
        }
    };
    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("testSaveAs_RecoverFromSaveError");
    assertEquals(0, outFile.list().length);
    try {
        saver.saveAs(project, new URLResource(outFile.toURI().toURL()));
        fail("No exception was thrown..");
    } catch (CayenneRuntimeException e) {
        // expected
        assertEquals(0, outFile.list().length);
    }
}
Also used : Injector(org.apache.cayenne.di.Injector) HandlerFactory(org.apache.cayenne.configuration.xml.HandlerFactory) XMLReaderProvider(org.apache.cayenne.configuration.xml.XMLReaderProvider) Project2Case(org.apache.cayenne.project.unit.Project2Case) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) URL(java.net.URL) ConfigurationNameMapper(org.apache.cayenne.configuration.ConfigurationNameMapper) Module(org.apache.cayenne.di.Module) DataChannelDescriptorLoader(org.apache.cayenne.configuration.DataChannelDescriptorLoader) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) XMLDataChannelDescriptorLoader(org.apache.cayenne.configuration.xml.XMLDataChannelDescriptorLoader) XMLReader(org.xml.sax.XMLReader) DefaultHandlerFactory(org.apache.cayenne.configuration.xml.DefaultHandlerFactory) NoopDataChannelMetaData(org.apache.cayenne.configuration.xml.NoopDataChannelMetaData) XMLDataMapLoader(org.apache.cayenne.configuration.xml.XMLDataMapLoader) DIBootstrap(org.apache.cayenne.di.DIBootstrap) ProjectExtension(org.apache.cayenne.project.extension.ProjectExtension) PrintWriter(java.io.PrintWriter) URLResource(org.apache.cayenne.resource.URLResource) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Test(org.junit.Test) DataChannelMetaData(org.apache.cayenne.configuration.xml.DataChannelMetaData) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) File(java.io.File) Resource(org.apache.cayenne.resource.Resource) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Assert(org.junit.Assert) Collections(java.util.Collections) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) XMLDataMapLoader(org.apache.cayenne.configuration.xml.XMLDataMapLoader) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) URL(java.net.URL) XMLDataChannelDescriptorLoader(org.apache.cayenne.configuration.xml.XMLDataChannelDescriptorLoader) NoopDataChannelMetaData(org.apache.cayenne.configuration.xml.NoopDataChannelMetaData) XMLReaderProvider(org.apache.cayenne.configuration.xml.XMLReaderProvider) URLResource(org.apache.cayenne.resource.URLResource) DefaultHandlerFactory(org.apache.cayenne.configuration.xml.DefaultHandlerFactory) Injector(org.apache.cayenne.di.Injector) Module(org.apache.cayenne.di.Module) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) File(java.io.File) XMLReader(org.xml.sax.XMLReader) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 33 with URLResource

use of org.apache.cayenne.resource.URLResource in project cayenne by apache.

the class FileProjectSaverTest method testSaveAs_Sorted.

@Test
public void testSaveAs_Sorted() throws Exception {
    File testFolder = setupTestDirectory("testSaveAs_Sorted");
    DataChannelDescriptor rootNode = new DataChannelDescriptor();
    rootNode.setName("test");
    // add maps and nodes in reverse alpha order. Check that they are saved in alpha
    // order
    rootNode.getDataMaps().add(new DataMap("C"));
    rootNode.getDataMaps().add(new DataMap("B"));
    rootNode.getDataMaps().add(new DataMap("A"));
    DataNodeDescriptor[] nodes = new DataNodeDescriptor[3];
    nodes[0] = new DataNodeDescriptor("Z");
    nodes[1] = new DataNodeDescriptor("Y");
    nodes[2] = new DataNodeDescriptor("X");
    nodes[0].getDataMapNames().add("C");
    nodes[0].getDataMapNames().add("B");
    nodes[0].getDataMapNames().add("A");
    rootNode.getNodeDescriptors().addAll(Arrays.asList(nodes));
    Project project = new Project(new ConfigurationTree<DataChannelDescriptor>(rootNode));
    saver.saveAs(project, new URLResource(testFolder.toURI().toURL()));
    File target = new File(testFolder, "cayenne-test.xml");
    assertTrue(target.isFile());
    assertSaveAs_Sorted(target);
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) File(java.io.File) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 34 with URLResource

use of org.apache.cayenne.resource.URLResource in project cayenne by apache.

the class DefaultUpgradeServiceTest method upgradeDOM.

@Test
public void upgradeDOM() throws Exception {
    Resource resource = new URLResource(getClass().getResource("../cayenne-PROJECT1.xml"));
    // Mock service so it will use actual reading but skip actual saving part
    upgradeService = mock(DefaultUpgradeService.class);
    when(upgradeService.upgradeDOM(any(Resource.class), ArgumentMatchers.<UpgradeHandler>anyList())).thenCallRealMethod();
    when(upgradeService.getAdditionalDatamapResources(any(UpgradeUnit.class))).thenCallRealMethod();
    upgradeService.upgradeDOM(resource, handlers);
    // verify(upgradeService, times(3)).saveDocument(any(UpgradeUnit.class));
    for (UpgradeHandler handler : handlers) {
        verify(handler).getVersion();
        verify(handler).processProjectDom(any(UpgradeUnit.class));
        // two data maps
        verify(handler, times(2)).processDataMapDom(any(UpgradeUnit.class));
        verifyNoMoreInteractions(handler);
    }
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) UpgradeHandler(org.apache.cayenne.project.upgrade.handlers.UpgradeHandler) Test(org.junit.Test)

Example 35 with URLResource

use of org.apache.cayenne.resource.URLResource in project cayenne by apache.

the class CompatibilityDataMapLoaderIT method testLoad.

@Test
public void testLoad() throws Exception {
    Injector injector = getInjector();
    DataMapLoader loader = injector.getInstance(DataMapLoader.class);
    assertTrue(loader instanceof CompatibilityDataMapLoader);
    URL resourceUrl = getClass().getResource("../../project/compatibility/test-map-v6.map.xml");
    Resource resource = new URLResource(resourceUrl);
    DataMap dataMap = loader.load(resource);
    assertNotNull(dataMap);
    assertEquals(1, dataMap.getDbEntities().size());
    assertEquals(1, dataMap.getObjEntities().size());
    assertNotNull(dataMap.getObjEntity("Artist"));
    assertNotNull(dataMap.getDbEntity("Artist"));
    assertEquals(2, dataMap.getDbEntity("Artist").getAttributes().size());
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) Injector(org.apache.cayenne.di.Injector) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) URL(java.net.URL) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Aggregations

URLResource (org.apache.cayenne.resource.URLResource)40 URL (java.net.URL)31 Test (org.junit.Test)29 DataMap (org.apache.cayenne.map.DataMap)19 File (java.io.File)12 Injector (org.apache.cayenne.di.Injector)12 Resource (org.apache.cayenne.resource.Resource)11 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)10 DataMapLoader (org.apache.cayenne.configuration.DataMapLoader)7 XMLDataChannelDescriptorLoader (org.apache.cayenne.configuration.xml.XMLDataChannelDescriptorLoader)5 XMLDataMapLoader (org.apache.cayenne.configuration.xml.XMLDataMapLoader)5 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)4 ConfigurationNameMapper (org.apache.cayenne.configuration.ConfigurationNameMapper)4 DefaultConfigurationNameMapper (org.apache.cayenne.configuration.DefaultConfigurationNameMapper)4 DataChannelMetaData (org.apache.cayenne.configuration.xml.DataChannelMetaData)4 DefaultHandlerFactory (org.apache.cayenne.configuration.xml.DefaultHandlerFactory)4 HandlerFactory (org.apache.cayenne.configuration.xml.HandlerFactory)4 NoopDataChannelMetaData (org.apache.cayenne.configuration.xml.NoopDataChannelMetaData)4 XMLReaderProvider (org.apache.cayenne.configuration.xml.XMLReaderProvider)4 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)4