public void execute() throws MojoExecutionException, MojoFailureException {

    Log logger = new MavenLogger(this);

    DbImportConfiguration config = toParameters();
    config.setLogger(logger);
    Injector injector = DIBootstrap.createInjector(new ToolsModule(logger), new DbImportModule());

    validateDbImportConfiguration(config, injector);

    try {
      injector.getInstance(DbImportAction.class).execute(config);
    } catch (Exception ex) {
      Throwable th = Util.unwindException(ex);

      String message = "Error importing database schema";

      if (th.getLocalizedMessage() != null) {
        message += ": " + th.getLocalizedMessage();
      }

      getLog().error(message);
      throw new MojoExecutionException(message, th);
    }
  }
  @Test
  public void testSaveAs_RecoverFromSaveError() throws Exception {

    FileProjectSaver saver =
        new FileProjectSaver() {

          @Override
          void saveToTempFile(SaveUnit unit, PrintWriter printWriter) {
            throw new CayenneRuntimeException("Test Exception");
          }
        };

    Module testModule =
        new Module() {

          @Override
          public void configure(Binder 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);
          }
        };

    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.toURL()));
      fail("No exception was thrown..");
    } catch (CayenneRuntimeException e) {
      // expected

      assertEquals(0, outFile.list().length);
    }
  }
  @Test
  public void testSetAdapter() throws Exception {
    DataSource ds = mock(DataSource.class);
    Injector injector = DIBootstrap.createInjector(new ToolsModule(mock(Log.class)));

    DbGeneratorTask task = new DbGeneratorTask();

    DbAdapter autoAdapter = task.getAdapter(injector, ds);
    assertTrue(autoAdapter instanceof AutoAdapter);

    task.setAdapter(SQLServerAdapter.class.getName());

    DbAdapter sqlServerAdapter = task.getAdapter(injector, ds);
    assertTrue(sqlServerAdapter instanceof SQLServerAdapter);
  }
  @Test
  public void testSaveAs() throws Exception {

    FileProjectSaver saver = new FileProjectSaver();

    Module testModule =
        new Module() {

          @Override
          public void configure(Binder 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);
          }
        };

    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.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);
  }
Exemple #5
0
  private Injector createInjector() {

    if (linkRestService == null && linkRestServiceType == null) {
      throw new IllegalStateException("Required 'linkRestService' is not set");
    }

    Module module =
        new Module() {

          @Override
          public void configure(Binder binder) {

            binder
                .<EncoderFilter>bindList(EncoderService.ENCODER_FILTER_LIST)
                .addAll(encoderFilters);

            binder.bind(CayenneEntityCompiler.class).to(CayenneEntityCompiler.class);
            binder.bind(PojoEntityCompiler.class).to(PojoEntityCompiler.class);
            binder
                .<LrEntityCompiler>bindList(MetadataService.ENTITY_COMPILER_LIST)
                .add(CayenneEntityCompiler.class)
                .add(PojoEntityCompiler.class);

            binder
                .<LrEntityOverlay<?>>bindMap(CayenneEntityCompiler.ENTITY_OVERLAY_MAP)
                .putAll(entityOverlays);
            binder
                .<Class<?>>bindMap(LinkRestRuntime.BODY_WRITERS_MAP)
                .put(SimpleResponse.class.getName(), SimpleResponseWriter.class)
                .put(DataResponse.class.getName(), DataResponseWriter.class)
                .put(MetadataResponse.class.getName(), MetadataResponseWriter.class);

            binder.<EntityConstraint>bindList(ConstraintsHandler.DEFAULT_READ_CONSTRAINTS_LIST);
            binder.<EntityConstraint>bindList(ConstraintsHandler.DEFAULT_WRITE_CONSTRAINTS_LIST);
            binder
                .<PropertyMetadataEncoder>bindMap(EncoderService.PROPERTY_METADATA_ENCODER_MAP)
                .putAll(metadataEncoders);

            if (linkRestServiceType != null) {
              binder.bind(ILinkRestService.class).to(linkRestServiceType);
            } else {
              binder.bind(ILinkRestService.class).toInstance(linkRestService);
            }

            binder.bind(IProcessorFactory.class).to(CayenneProcessorFactory.class);
            binder.bind(IRequestParser.class).to(RequestParser.class);
            binder
                .bind(IJsonValueConverterFactory.class)
                .to(DefaultJsonValueConverterFactory.class);
            binder.bind(IAttributeEncoderFactory.class).to(AttributeEncoderFactory.class);
            binder.bind(IStringConverterFactory.class).to(StringConverterFactory.class);
            binder.bind(IEncoderService.class).to(EncoderService.class);
            binder.bind(IRelationshipMapper.class).to(RelationshipMapper.class);
            binder.bind(IMetadataService.class).to(MetadataService.class);
            binder.bind(IListenerService.class).to(ListenerService.class);
            binder.bind(IResourceMetadataService.class).to(ResourceMetadataService.class);
            binder.bind(IConstraintsHandler.class).to(ConstraintsHandler.class);
            binder.bind(ICayenneExpProcessor.class).to(CayenneExpProcessor.class);
            binder.bind(IExpressionPostProcessor.class).to(ExpressionPostProcessor.class);
            binder.bind(IKeyValueExpProcessor.class).to(KeyValueExpProcessor.class);

            binder.bind(IJacksonService.class).to(JacksonService.class);
            binder.bind(ICayennePersister.class).toInstance(cayenneService);

            binder.bind(IPathCache.class).to(PathCache.class);
            binder.bind(ISortProcessor.class).to(SortProcessor.class);
            binder.bind(ITreeProcessor.class).to(IncludeExcludeProcessor.class);

            binder.bind(IResourceParser.class).to(ResourceParser.class);
            binder.bind(IUpdateParser.class).to(UpdateParser.class);

            binder
                .bind(ShutdownManager.class)
                .toInstance(new ShutdownManager(Duration.ofSeconds(10)));

            if (executor != null) {
              binder.bind(ExecutorService.class).toInstance(executor);
            } else {
              binder.bind(ExecutorService.class).toProvider(UnboundedExecutorServiceProvider.class);
            }

            // apply adapter-contributed bindings
            for (LinkRestAdapter a : adapters) {
              a.contributeToRuntime(binder);
            }
          }
        };

    return DIBootstrap.createInjector(module);
  }