コード例 #1
0
  /** Test the generate schema feature from the Persistence API. */
  public void testPersistenceGenerateSchemaUseConnection() {
    String GENERATE_SCHEMA_NO_CONNECTION_DROP_TARGET =
        "jpa21-generate-schema-use-connection-drop.jdbc";
    String GENERATE_SCHEMA_NO_CONNECTION_CREATE_TARGET =
        "jpa21-generate-schema-use-connection-create.jdbc";
    String GENERATE_SCHEMA_NO_CONNECTION_SESSION_NAME = "generate-schema-use-conn-session";

    Map properties = new HashMap();
    // Get database properties will pick up test.properties database connection details.
    properties.putAll(JUnitTestCaseHelper.getDatabaseProperties(getPersistenceUnitName()));
    properties.put(
        PersistenceUnitProperties.SESSION_NAME, GENERATE_SCHEMA_NO_CONNECTION_SESSION_NAME);
    properties.put(PersistenceUnitProperties.ORM_SCHEMA_VALIDATION, "true");
    properties.put(
        PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_ACTION,
        PersistenceUnitProperties.SCHEMA_GENERATION_DROP_AND_CREATE_ACTION);
    properties.put(
        PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_DROP_TARGET,
        GENERATE_SCHEMA_NO_CONNECTION_DROP_TARGET);
    properties.put(
        PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_CREATE_TARGET,
        GENERATE_SCHEMA_NO_CONNECTION_CREATE_TARGET);

    Persistence.generateSchema(getPersistenceUnitName(), properties);

    // Now create an entity manager and build some objects for this PU using
    // the same session name. Create the schema on the database with the
    // target scripts built previously.
    testPersistenceGenerateSchemaOnDatabase(
        GENERATE_SCHEMA_NO_CONNECTION_CREATE_TARGET,
        GENERATE_SCHEMA_NO_CONNECTION_DROP_TARGET,
        GENERATE_SCHEMA_NO_CONNECTION_SESSION_NAME);
  }
コード例 #2
0
  public void testDatabaseSchemaGenerationDropAndCreate() {
    // currently server test framework supports multiple persistence units tests up to 5
    // PUs(MulitPU-1...5), the tests of this 6th PU will be excluded from running on server
    if (!isOnServer()) {
      Map properties = new HashMap();
      // Get database properties will pick up test.properties database connection details.
      properties.putAll(JUnitTestCaseHelper.getDatabaseProperties("ddl-schema-template"));
      properties.put(PersistenceUnitProperties.SESSION_NAME, "ddl-schema-drop-and-create-session");
      properties.put(
          PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_ACTION,
          PersistenceUnitProperties.SCHEMA_GENERATION_DROP_AND_CREATE_ACTION);
      properties.put(PersistenceUnitProperties.SCHEMA_GENERATION_CREATE_DATABASE_SCHEMAS, "true");
      properties.put(
          PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_DROP_TARGET,
          "jpa21-ddl-schema-drop-and-create-drop.jdbc");
      properties.put(
          PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_CREATE_TARGET,
          "jpa21-ddl-schema-drop-and-create-create.jdbc");

      try {
        Persistence.generateSchema("ddl-schema-template", properties);
      } catch (Exception exception) {
        fail("Exception caught when generating schema: " + exception.getMessage());
      }
    }
  }
コード例 #3
0
  public static void main(String[] args) {
    Persistence.generateSchema("myunit", null); // New in JPA 2.1

    /*
     Requires :
     	<property name="javax.persistence.schema-generation.scripts.action"        value="drop-and-create"/>
    <property name="javax.persistence.schema-generation.scripts.create-target" value="sampleCreate.ddl"/>
    <property name="javax.persistence.schema-generation.scripts.drop-target"   value="sampleDrop.ddl"/>
     in persitence.xml
     */

    // Persistence.createEntityManagerFactory("myunit") provoque aussi la génération des scripts
    // Persistence.createEntityManagerFactory("myunit");
  }
コード例 #4
0
  /**
   * Test the generate schema feature from the Persistence API.
   *
   * <p>This test will then further use the PU and make sure the connection occurs and we can
   * persist objects.
   *
   * <p>All properties are set in code.
   */
  public void testPersistenceGenerateSchemaNoConnection(
      String platform, String majorVersion, String minorVersion) {
    // Get platform call will deploy our app and be stored in our
    // testing framework. Need to clear it for this test.
    closeEntityManagerFactory();

    String GENERATE_SCHEMA_NO_CONNECTION_DROP_TARGET =
        "jpa21-generate-schema-no-connection-drop-" + platform + ".jdbc";
    String GENERATE_SCHEMA_NO_CONNECTION_CREATE_TARGET =
        "jpa21-generate-schema-no-connection-create-" + platform + ".jdbc";
    String GENERATE_SCHEMA_NO_CONNECTION_SESSION_NAME =
        "generate-schema-no-conn-session-" + platform;

    Map properties = new HashMap();
    properties.put(
        PersistenceUnitProperties.SESSION_NAME, GENERATE_SCHEMA_NO_CONNECTION_SESSION_NAME);
    properties.put(PersistenceUnitProperties.ORM_SCHEMA_VALIDATION, true);
    properties.put(PersistenceUnitProperties.SCHEMA_DATABASE_PRODUCT_NAME, platform);

    if (majorVersion != null) {
      properties.put(PersistenceUnitProperties.SCHEMA_DATABASE_MAJOR_VERSION, majorVersion);
    }

    if (minorVersion != null) {
      properties.put(PersistenceUnitProperties.SCHEMA_DATABASE_MINOR_VERSION, minorVersion);
    }

    properties.put(
        PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_ACTION,
        PersistenceUnitProperties.SCHEMA_GENERATION_DROP_AND_CREATE_ACTION);
    properties.put(
        PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_DROP_TARGET,
        GENERATE_SCHEMA_NO_CONNECTION_DROP_TARGET);
    properties.put(
        PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_CREATE_TARGET,
        GENERATE_SCHEMA_NO_CONNECTION_CREATE_TARGET);

    Persistence.generateSchema(getPersistenceUnitName(), properties);

    // Now create an entity manager and build some objects for this PU using
    // the same session name. Create the schema on the database with the
    // target scripts built previously.
    testPersistenceGenerateSchemaOnDatabase(
        GENERATE_SCHEMA_NO_CONNECTION_CREATE_TARGET,
        GENERATE_SCHEMA_NO_CONNECTION_DROP_TARGET,
        GENERATE_SCHEMA_NO_CONNECTION_SESSION_NAME);
  }
コード例 #5
0
  /** Test the generate schema feature from the Persistence API. */
  public void testPersistenceGenerateSchemaDropOnlyScript() {
    Map properties = new HashMap();
    properties.put(
        PersistenceUnitProperties.SESSION_NAME, "generate-schema-no-conn-session-drop-only");
    properties.put(PersistenceUnitProperties.ORM_SCHEMA_VALIDATION, true);
    properties.put(PersistenceUnitProperties.SCHEMA_DATABASE_PRODUCT_NAME, "MySQL");
    properties.put(PersistenceUnitProperties.SCHEMA_DATABASE_MAJOR_VERSION, "5");
    properties.put(PersistenceUnitProperties.SCHEMA_DATABASE_MINOR_VERSION, "5");
    properties.put(
        PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_ACTION,
        PersistenceUnitProperties.SCHEMA_GENERATION_DROP_ACTION);
    properties.put(
        PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_DROP_TARGET,
        "jpa21-generate-schema-no-connection-drop-only.jdbc");

    Persistence.generateSchema(getPersistenceUnitName(), properties);
  }
コード例 #6
0
  public void testRootTargetScriptFileName() {
    // This test is not called. It can be tested manually by uncommenting
    // it out in the suite setup.
    Map properties = new HashMap();
    // Get database properties will pick up test.properties database connection details.
    properties.putAll(JUnitTestCaseHelper.getDatabaseProperties(getPersistenceUnitName()));
    properties.put(PersistenceUnitProperties.SESSION_NAME, "testRootTargetScriptFileName");
    properties.put(PersistenceUnitProperties.ORM_SCHEMA_VALIDATION, "true");
    properties.put(
        PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_ACTION,
        PersistenceUnitProperties.SCHEMA_GENERATION_DROP_AND_CREATE_ACTION);
    properties.put(
        PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_DROP_TARGET,
        "/temp-generate-schema-drop.jdbc");
    properties.put(
        PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_CREATE_TARGET,
        "/temp-generate-schema-create.jdbc");

    Persistence.generateSchema(getPersistenceUnitName(), properties);
  }
コード例 #7
0
  public void testDatabaseSchemaGenerationURLTargets() {
    // This test is not called. It can be tested manually by uncommenting
    // it out in the suite setup.
    Map properties = new HashMap();
    // Get database properties will pick up test.properties database connection details.
    properties.putAll(JUnitTestCaseHelper.getDatabaseProperties("ddl-schema-template"));
    properties.put(PersistenceUnitProperties.SESSION_NAME, "ddl-schema-url-target-session");
    properties.put(
        PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_ACTION,
        PersistenceUnitProperties.SCHEMA_GENERATION_DROP_AND_CREATE_ACTION);
    properties.put(
        PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_CREATE_TARGET,
        "file:///jpa21-ddl-schema-url-target-create.jdbc");
    properties.put(
        PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_DROP_TARGET,
        "file:///jpa21-ddl-schema-url-target-drop.jdbc");

    try {
      Persistence.generateSchema("ddl-schema-template", properties);
    } catch (Exception exception) {
      fail("Exception caught when generating schema: " + exception.getMessage());
    }
  }
コード例 #8
0
  public void testIllegalArgumentExceptionWithNoScriptTargetProvided() {
    Map properties = new HashMap();
    // Get database properties will pick up test.properties database connection details.
    properties.putAll(JUnitTestCaseHelper.getDatabaseProperties(getPersistenceUnitName()));
    properties.put(
        PersistenceUnitProperties.SESSION_NAME,
        "testIllegalArgumentExceptionWithNoScriptTargetsProvided");
    properties.put(PersistenceUnitProperties.ORM_SCHEMA_VALIDATION, "true");
    properties.put(
        PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_ACTION,
        PersistenceUnitProperties.SCHEMA_GENERATION_CREATE_ACTION);

    try {
      Persistence.generateSchema(getPersistenceUnitName(), properties);
    } catch (PersistenceException exception) {
      assertTrue(
          "IllegalArgumentException not thrown",
          exception.getCause() instanceof IllegalArgumentException);
      return;
    }

    fail("Illegal Argument Exception was not thrown when a target script name not provided.");
  }
コード例 #9
0
  /** Test the generate schema feature from the Persistence API. */
  public void testPersistenceGenerateSchemaUsingProvidedWriters() {
    Map properties = new HashMap();
    // Get database properties will pick up test.properties database connection details.
    properties.putAll(JUnitTestCaseHelper.getDatabaseProperties(getPersistenceUnitName()));
    properties.put(
        PersistenceUnitProperties.SESSION_NAME, "generate-schema-using-provided-writers");
    properties.put(PersistenceUnitProperties.ORM_SCHEMA_VALIDATION, "true");
    properties.put(
        PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_ACTION,
        PersistenceUnitProperties.SCHEMA_GENERATION_DROP_AND_CREATE_ACTION);

    try {
      properties.put(
          PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_DROP_TARGET,
          new FileWriter(new File("jpa21-generate-schema-using-drop-writer.jdbc")));
      properties.put(
          PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_CREATE_TARGET,
          new FileWriter(new File("jpa21-generate-schema-using-create-writer.jdbc")));

      Persistence.generateSchema(getPersistenceUnitName(), properties);
    } catch (IOException e) {
      fail("Error occurred: " + e);
    }
  }
コード例 #10
0
 public static void main(String[] args) {
   Persistence.generateSchema(deploy.DeploymentConfiguration.PU_NAME, null);
 }
コード例 #11
0
  public void testPersistenceGenerateSchemaOnDatabase(
      String createSource, String dropSource, String sessionName) {
    // Now create an entity manager and build some objects for this PU using
    // the same session name. Create the schema on the database with the
    // target scripts built previously.
    EntityManager em = null;

    try {
      Map properties = new HashMap();
      // Get database properties will pick up test.properties database connection details.
      properties.putAll(JUnitTestCaseHelper.getDatabaseProperties(getPersistenceUnitName()));
      properties.put(PersistenceUnitProperties.SESSION_NAME, sessionName);
      properties.put(PersistenceUnitProperties.ORM_SCHEMA_VALIDATION, true);
      properties.put(
          PersistenceUnitProperties.SCHEMA_GENERATION_DATABASE_ACTION,
          PersistenceUnitProperties.SCHEMA_GENERATION_DROP_AND_CREATE_ACTION);
      properties.put(
          PersistenceUnitProperties.SCHEMA_GENERATION_CREATE_SOURCE,
          PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPT_SOURCE);
      properties.put(
          PersistenceUnitProperties.SCHEMA_GENERATION_CREATE_SCRIPT_SOURCE,
          new FileReader(new File(createSource)));
      properties.put(
          PersistenceUnitProperties.SCHEMA_GENERATION_DROP_SOURCE,
          PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPT_SOURCE);
      properties.put(
          PersistenceUnitProperties.SCHEMA_GENERATION_DROP_SCRIPT_SOURCE,
          new FileReader(new File(dropSource)));

      em = createEntityManager(properties);

      // If on the server, need to trigger the DDL through Persistence
      // API, createEntityManager call above will not do it.
      if (isOnServer()) {
        Persistence.generateSchema(this.getPersistenceUnitName(), properties);
      }

      beginTransaction(em);

      Runner runner = new Runner();
      runner.setAge(53);
      runner.setIsFemale();
      runner.setFirstName("Doris");
      runner.setLastName("Day");
      runner.addPersonalBest("10 KM", "47:34");
      runner.addPersonalBest("5", "26:41");
      runner.addAccomplishment("Ran 100KM without stopping", new Date(System.currentTimeMillis()));
      RunnerInfo runnerInfo = new RunnerInfo();
      runnerInfo.setHealth(Health.H);
      runnerInfo.setLevel(Level.A);
      RunnerStatus runnerStatus = new RunnerStatus();
      runnerStatus.setRunningStatus(RunningStatus.D);
      runnerInfo.setStatus(runnerStatus);
      runner.setInfo(runnerInfo);

      Race race = new Race();
      race.setName("The Ultimate Marathon");
      race.addRunner(runner);

      Organizer organizer = new Organizer();
      organizer.setName("Joe Organ");
      organizer.setRace(race);

      Responsibility responsibility = new Responsibility();
      responsibility.setUniqueIdentifier(new Long(System.currentTimeMillis()));
      responsibility.setDescription("Raise funds");

      race.addOrganizer(organizer, responsibility);

      em.persist(race);
      em.persist(organizer);
      em.persist(runner);
      commitTransaction(em);

      // Clear the cache
      em.clear();
      clearCache();

      Runner runnerRefreshed = em.find(Runner.class, runner.getId());
      assertTrue("The age conversion did not work.", runnerRefreshed.getAge() == 52);
      assertTrue(
          "The embeddable health conversion did not work.",
          runnerRefreshed.getInfo().getHealth().equals(Health.HEALTHY));
      assertTrue(
          "The embeddable level conversion did not work.",
          runnerRefreshed.getInfo().getLevel().equals(Level.AMATEUR));
      assertTrue(
          "The nested embeddable running status conversion did not work.",
          runnerRefreshed.getInfo().getStatus().getRunningStatus().equals(RunningStatus.DOWN_TIME));
      assertTrue(
          "The number of personal bests for this runner is incorrect.",
          runnerRefreshed.getPersonalBests().size() == 2);
      assertTrue(
          "Distance (map key) conversion did not work.",
          runnerRefreshed.getPersonalBests().keySet().contains("10K"));
      assertTrue(
          "Distance (map key) conversion did not work.",
          runnerRefreshed.getPersonalBests().keySet().contains("5K"));
      assertTrue(
          "Time (map value) conversion did not work.",
          runnerRefreshed.getPersonalBests().values().contains("47:34.0"));
      assertTrue(
          "Time (map value) conversion did not work.",
          runnerRefreshed.getPersonalBests().values().contains("26:41.0"));

      Race raceRefreshed = em.find(Race.class, race.getId());
      Map<Responsibility, Organizer> organizers = raceRefreshed.getOrganizers();
      assertFalse("No race organizers returned.", organizers.isEmpty());
      assertTrue("More than one race organizer returned.", organizers.size() == 1);

      Responsibility resp = organizers.keySet().iterator().next();
      assertTrue(
          "Responsibility was not uppercased by the converter",
          resp.getDescription().equals("RAISE FUNDS"));

      for (String accomplishment : runnerRefreshed.getAccomplishments().keySet()) {
        assertTrue(
            "Accomplishment (map key) conversion did not work.", accomplishment.endsWith("!!!"));
      }
    } catch (RuntimeException e) {
      if (isTransactionActive(em)) {
        rollbackTransaction(em);
      }

      throw e;
    } catch (FileNotFoundException e) {
      fail("Error loading source script file: " + e);
    } finally {
      closeEntityManager(em);
    }
  }