Search in sources :

Example 41 with Injector

use of org.apache.cayenne.di.Injector 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)

Example 42 with Injector

use of org.apache.cayenne.di.Injector 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 43 with Injector

use of org.apache.cayenne.di.Injector in project cayenne by apache.

the class ModuleLoaderTest method testLoad.

@Test
public void testLoad() {
    List<Module> modules = new ModuleLoader().load(ModuleProvider.class);
    assertEquals(4, modules.size());
    assertTrue(String.valueOf(modules.get(0)), modules.get(0) instanceof Module3);
    assertTrue(String.valueOf(modules.get(1)), modules.get(1) instanceof Module4);
    assertTrue(String.valueOf(modules.get(2)), modules.get(2) instanceof Module2);
    assertTrue(String.valueOf(modules.get(3)), modules.get(3) instanceof Module1);
    Injector i = DIBootstrap.createInjector(modules);
    assertEquals("a", i.getInstance(String.class));
    assertEquals(Integer.valueOf(56), i.getInstance(Integer.class));
}
Also used : Injector(org.apache.cayenne.di.Injector) Module(org.apache.cayenne.di.Module) Test(org.junit.Test)

Example 44 with Injector

use of org.apache.cayenne.di.Injector in project cayenne by apache.

the class CgenTask method generate.

@TaskAction
public void generate() {
    File dataMapFile = getDataMapFile();
    Injector injector = DIBootstrap.createInjector(new ToolsModule(LoggerFactory.getLogger(CgenTask.class)));
    CayenneGeneratorMapLoaderAction loaderAction = new CayenneGeneratorMapLoaderAction(injector);
    loaderAction.setMainDataMapFile(dataMapFile);
    CayenneGeneratorEntityFilterAction filterAction = new CayenneGeneratorEntityFilterAction();
    filterAction.setClient(client);
    filterAction.setNameFilter(NamePatternMatcher.build(getLogger(), includeEntities, excludeEntities));
    try {
        loaderAction.setAdditionalDataMapFiles(convertAdditionalDataMaps());
        ClassGenerationAction generator = this.createGenerator();
        DataMap dataMap = loaderAction.getMainDataMap();
        generator.setLogger(getLogger());
        if (this.force || getProject().hasProperty("force")) {
            generator.setForce(true);
        }
        generator.setTimestamp(dataMapFile.lastModified());
        generator.setDataMap(dataMap);
        generator.addEntities(filterAction.getFilteredEntities(dataMap));
        generator.addEmbeddables(dataMap.getEmbeddables());
        generator.addQueries(dataMap.getQueryDescriptors());
        generator.execute();
    } catch (Exception exception) {
        throw new GradleException("Error generating classes: ", exception);
    }
}
Also used : Injector(org.apache.cayenne.di.Injector) GradleException(org.gradle.api.GradleException) File(java.io.File) InputFile(org.gradle.api.tasks.InputFile) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) GradleException(org.gradle.api.GradleException) InvalidUserDataException(org.gradle.api.InvalidUserDataException) ClientClassGenerationAction(org.apache.cayenne.gen.ClientClassGenerationAction) ClassGenerationAction(org.apache.cayenne.gen.ClassGenerationAction) DataMap(org.apache.cayenne.map.DataMap) TaskAction(org.gradle.api.tasks.TaskAction)

Example 45 with Injector

use of org.apache.cayenne.di.Injector in project cayenne by apache.

the class DbGenerateTask method createDbAdapter.

DbAdapter createDbAdapter() {
    Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(getLogger()));
    AdhocObjectFactory objectFactory = injector.getInstance(AdhocObjectFactory.class);
    return (adapter == null) ? objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName()) : objectFactory.newInstance(DbAdapter.class, adapter);
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule)

Aggregations

Injector (org.apache.cayenne.di.Injector)64 Test (org.junit.Test)52 Module (org.apache.cayenne.di.Module)27 DIBootstrap (org.apache.cayenne.di.DIBootstrap)23 URL (java.net.URL)15 ToolsModule (org.apache.cayenne.dbsync.reverse.configuration.ToolsModule)15 DataMap (org.apache.cayenne.map.DataMap)15 RuntimeProperties (org.apache.cayenne.configuration.RuntimeProperties)14 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)14 JdbcEventLogger (org.apache.cayenne.log.JdbcEventLogger)14 Slf4jJdbcEventLogger (org.apache.cayenne.log.Slf4jJdbcEventLogger)14 URLResource (org.apache.cayenne.resource.URLResource)14 Assert (org.junit.Assert)14 DefaultAdhocObjectFactory (org.apache.cayenne.di.spi.DefaultAdhocObjectFactory)13 DefaultRuntimeProperties (org.apache.cayenne.configuration.DefaultRuntimeProperties)12 DbSyncModule (org.apache.cayenne.dbsync.DbSyncModule)12 ClassLoaderManager (org.apache.cayenne.di.ClassLoaderManager)12 DefaultClassLoaderManager (org.apache.cayenne.di.spi.DefaultClassLoaderManager)12 Logger (org.slf4j.Logger)11 Constants (org.apache.cayenne.configuration.Constants)10