Search in sources :

Example 26 with URLResource

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

the class DataMapTest method testQuoteSqlIdentifiersEncodeAsXML.

@Test
public void testQuoteSqlIdentifiersEncodeAsXML() {
    DataMap map = new DataMap("aaa");
    map.setQuotingSQLIdentifiers(true);
    StringWriter w = new StringWriter();
    XMLEncoder encoder = new XMLEncoder(new PrintWriter(w));
    map.encodeAsXML(encoder, new EmptyConfigurationNodeVisitor());
    assertTrue(map.quotingSQLIdentifiers);
    XMLDataMapLoader loader = new XMLDataMapLoader();
    try {
        URL url = mock(URL.class);
        InputStream is = new ByteArrayInputStream(w.getBuffer().toString().getBytes("UTF-8"));
        when(url.openStream()).thenReturn(is);
        DataMap newMap = loader.load(new URLResource(url));
        assertTrue(newMap.quotingSQLIdentifiers);
    } catch (Exception e) {
        e.printStackTrace();
    }
    map.setQuotingSQLIdentifiers(false);
    StringWriter w2 = new StringWriter();
    XMLEncoder encoder2 = new XMLEncoder(new PrintWriter(w2));
    map.encodeAsXML(encoder2, new EmptyConfigurationNodeVisitor());
    assertFalse(map.quotingSQLIdentifiers);
    try {
        URL url = mock(URL.class);
        InputStream is = new ByteArrayInputStream(w.getBuffer().toString().getBytes("UTF-8"));
        when(url.openStream()).thenReturn(is);
        DataMap newMap = loader.load(new URLResource(url));
        assertFalse(newMap.quotingSQLIdentifiers);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) XMLEncoder(org.apache.cayenne.util.XMLEncoder) EmptyConfigurationNodeVisitor(org.apache.cayenne.configuration.EmptyConfigurationNodeVisitor) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDataMapLoader(org.apache.cayenne.configuration.xml.XMLDataMapLoader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) URL(java.net.URL) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 27 with URLResource

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

the class ObjRelationshipIT method testReadOnly_Flattened1_1__N_1.

@Test
public void testReadOnly_Flattened1_1__N_1() {
    // check common vertical inheritance relationships
    DataMapLoader loader = runtime.getInjector().getInstance(DataMapLoader.class);
    URL url = getClass().getClassLoader().getResource("inheritance-vertical.map.xml");
    DataMap dataMap = loader.load(new URLResource(url));
    EntityResolver resolver = new EntityResolver(Collections.singleton(dataMap));
    ObjEntity iv2Sub1 = resolver.getObjEntity(Iv2Sub1.class);
    ObjRelationship x = iv2Sub1.getRelationship(Iv2Sub1.X.getName());
    assertTrue(x.isFlattened());
    assertFalse(x.isReadOnly());
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) URL(java.net.URL) Test(org.junit.Test)

Example 28 with URLResource

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

the class DefaultDbImportActionTest method testSaveLoadedWithNonEmptyProject.

@Test
public void testSaveLoadedWithNonEmptyProject() throws Exception {
    Logger log = mock(Logger.class);
    Injector i = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(log), new DbImportModule());
    DbImportConfiguration params = mock(DbImportConfiguration.class);
    URL projectURL = new URL(getPackageURL(), "dbimport/cayenne-testProject3.map.xml");
    File projectFile = new File(projectURL.toURI());
    projectFile.delete();
    assertFalse(projectFile.exists());
    Files.write(projectFile.toPath(), ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<domain xmlns=\"http://cayenne.apache.org/schema/10/domain\"\n" + "\t xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "\t xsi:schemaLocation=\"http://cayenne.apache.org/schema/10/domain http://cayenne.apache.org/schema/10/domain.xsd\"\n" + "\t project-version=\"10\">\n" + "</domain>").getBytes(Charset.forName("UTF-8")));
    assertTrue(projectFile.isFile());
    when(params.getCayenneProject()).thenReturn(projectFile);
    DefaultDbImportAction action = (DefaultDbImportAction) i.getInstance(DbImportAction.class);
    URL dataMapURL = new URL(getPackageURL(), "dbimport/testSaveLoaded3.map.xml");
    File dataMapFile = new File(dataMapURL.toURI());
    dataMapFile.delete();
    assertFalse(dataMapFile.exists());
    DataMap map = new DataMap("testSaveLoaded3");
    map.setConfigurationSource(new URLResource(dataMapURL));
    action.saveLoaded(map, params);
    assertTrue(dataMapFile.isFile());
    assertTrue(projectFile.isFile());
    String dataMapContents = Util.stringFromFile(dataMapFile);
    assertTrue("Has no project version saved", dataMapContents.contains("project-version=\""));
    String projectContents = Util.stringFromFile(projectFile);
    assertTrue("Has no project version saved", projectContents.contains("project-version=\""));
    assertTrue("Has no datamap in project", projectContents.contains("<map name=\"testSaveLoaded3\"/>"));
}
Also used : Logger(org.slf4j.Logger) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) URL(java.net.URL) DataMap(org.apache.cayenne.map.DataMap) URLResource(org.apache.cayenne.resource.URLResource) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) File(java.io.File) Test(org.junit.Test)

Example 29 with URLResource

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

the class DefaultDbImportActionTest method testSaveLoadedWithNonEmptyProjectAndNonEmptyDataMap.

@Test
public void testSaveLoadedWithNonEmptyProjectAndNonEmptyDataMap() throws Exception {
    Logger log = mock(Logger.class);
    Injector i = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(log), new DbImportModule());
    DbImportConfiguration params = mock(DbImportConfiguration.class);
    URL projectURL = new URL(getPackageURL(), "dbimport/cayenne-testProject4.map.xml");
    File projectFile = new File(projectURL.toURI());
    projectFile.delete();
    assertFalse(projectFile.exists());
    Files.write(projectFile.toPath(), ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<domain xmlns=\"http://cayenne.apache.org/schema/10/domain\"\n" + "\t xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "\t xsi:schemaLocation=\"http://cayenne.apache.org/schema/10/domain http://cayenne.apache.org/schema/10/domain.xsd\"\n" + "\t project-version=\"10\">\n" + "\t<map name=\"testSaveLoaded4\"/>\n" + "</domain>").getBytes(Charset.forName("UTF-8")));
    assertTrue(projectFile.isFile());
    when(params.getCayenneProject()).thenReturn(projectFile);
    DefaultDbImportAction action = (DefaultDbImportAction) i.getInstance(DbImportAction.class);
    URL dataMapURL = new URL(getPackageURL(), "dbimport/testSaveLoaded4.map.xml");
    File dataMapFile = new File(dataMapURL.toURI());
    dataMapFile.delete();
    assertFalse(dataMapFile.exists());
    Files.write(dataMapFile.toPath(), ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<data-map xmlns=\"http://cayenne.apache.org/schema/10/modelMap\"\n" + "\t xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "\t xsi:schemaLocation=\"http://cayenne.apache.org/schema/10/modelMap http://cayenne.apache.org/schema/10/modelMap.xsd\"\n" + "\t project-version=\"10\">\n" + "\t<db-entity name=\"test\">\n" + "\t\t<db-attribute name=\"test\" type=\"INT\"/>\n" + "\t</db-entity>\n" + "</data-map>").getBytes(Charset.forName("UTF-8")));
    assertTrue(dataMapFile.isFile());
    DataMap map = new DataMap("testSaveLoaded4");
    map.setConfigurationSource(new URLResource(dataMapURL));
    action.saveLoaded(map, params);
    assertTrue(dataMapFile.isFile());
    assertTrue(projectFile.isFile());
    String dataMapContents = Util.stringFromFile(dataMapFile);
    assertTrue("Has no project version saved", dataMapContents.contains("project-version=\""));
    assertFalse(dataMapContents.contains("<db-entity"));
    String projectContents = Util.stringFromFile(projectFile);
    assertTrue("Has no project version saved", projectContents.contains("project-version=\""));
    assertEquals("Has no or too many datamaps in project", 1, Util.countMatches(projectContents, "<map name=\"testSaveLoaded4\"/>"));
}
Also used : Logger(org.slf4j.Logger) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) URL(java.net.URL) DataMap(org.apache.cayenne.map.DataMap) URLResource(org.apache.cayenne.resource.URLResource) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) File(java.io.File) Test(org.junit.Test)

Example 30 with URLResource

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

the class DefaultDbImportActionTest method testSaveLoadedWithEmptyProject.

@Test
public void testSaveLoadedWithEmptyProject() throws Exception {
    Logger log = mock(Logger.class);
    Injector i = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(log), new DbImportModule());
    DbImportConfiguration params = mock(DbImportConfiguration.class);
    URL projectURL = new URL(getPackageURL(), "dbimport/cayenne-testProject2.map.xml");
    File projectFile = new File(projectURL.toURI());
    projectFile.delete();
    assertFalse(projectFile.exists());
    when(params.getCayenneProject()).thenReturn(projectFile);
    DefaultDbImportAction action = (DefaultDbImportAction) i.getInstance(DbImportAction.class);
    URL dataMapURL = new URL(getPackageURL(), "dbimport/testSaveLoaded2.map.xml");
    File dataMapFile = new File(dataMapURL.toURI());
    dataMapFile.delete();
    assertFalse(dataMapFile.exists());
    DataMap map = new DataMap("testSaveLoaded2");
    map.setConfigurationSource(new URLResource(dataMapURL));
    action.saveLoaded(map, params);
    assertTrue(dataMapFile.isFile());
    assertTrue(projectFile.isFile());
    String dataMapContents = Util.stringFromFile(dataMapFile);
    assertTrue("Has no project version saved", dataMapContents.contains("project-version=\""));
    String projectContents = Util.stringFromFile(projectFile);
    assertTrue("Has no project version saved", projectContents.contains("project-version=\""));
    assertTrue("Has no datamap in project", projectContents.contains("<map name=\"testSaveLoaded2\"/>"));
}
Also used : Logger(org.slf4j.Logger) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) URL(java.net.URL) DataMap(org.apache.cayenne.map.DataMap) URLResource(org.apache.cayenne.resource.URLResource) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) File(java.io.File) 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