示例#1
0
  private void createSecDB(SecondaryConfig secConfig) {
    TupleBinding<DataRow> tBinder = new GenericTupleBinder(tMap);
    SecondaryKeyCreator secKeyCreator = new GenericSecKeyCreator(tBinder, secIndexPos, tMap);

    // Get a secondary object and set the key creator on it.
    secConfig.setKeyCreator(secKeyCreator);
    String secDbName = tableName + "-secDB";
    secDB = myDbEnvironment.openSecondaryDatabase(null, secDbName, myDB, secConfig);
  }
  @Test
  public void testConfigMatching() throws Throwable {

    try {
      /* DatabaseConfig matching. */

      DatabaseConfig confA = new DatabaseConfig();
      DatabaseConfig confB = new DatabaseConfig();

      try {
        confA.validate(confB);
      } catch (Exception E) {
        fail("expected valid match");
      }

      try {
        confB.validate(confA);
      } catch (Exception E) {
        fail("expected valid match");
      }

      try {
        confA.validate(null); // uses the DEFAULT config
      } catch (Exception E) {
        fail("expected valid match");
      }

      confA.setReadOnly(true);
      try {
        confA.validate(confB);
        fail("expected exception");
      } catch (IllegalArgumentException E) {
        // ok
      }

      confA.setReadOnly(false);
      confA.setSortedDuplicates(true);
      try {
        confA.validate(confB);
        fail("expected exception");
      } catch (IllegalArgumentException E) {
        // ok
      }
      confA.setSortedDuplicates(false);

      confA.setOverrideBtreeComparator(true);
      confA.setBtreeComparator(TestComparator.class);
      confB.setOverrideBtreeComparator(true);
      confB.setBtreeComparator(TestComparator2.class);
      try {
        confA.validate(confB);
        fail("expected exception");
      } catch (IllegalArgumentException E) {
        // ok
      }
      confA.setBtreeComparator((Class) null);
      confA.setOverrideBtreeComparator(false);
      confB.setBtreeComparator((Class) null);
      confB.setOverrideBtreeComparator(false);

      confA.setOverrideDuplicateComparator(true);
      confA.setDuplicateComparator(TestComparator.class);
      confB.setOverrideDuplicateComparator(true);
      confB.setDuplicateComparator(TestComparator2.class);
      try {
        confA.validate(confB);
        fail("expected exception");
      } catch (IllegalArgumentException E) {
        // ok
      }

      /* Same tests as above but for serialized comparators. */

      confA.setOverrideBtreeComparator(true);
      confA.setBtreeComparator(new TestSerialComparator());
      confB.setOverrideBtreeComparator(true);
      confB.setBtreeComparator(new TestSerialComparator2());
      try {
        confA.validate(confB);
        fail("expected exception");
      } catch (IllegalArgumentException E) {
        // ok
      }
      confA.setBtreeComparator((Comparator) null);
      confA.setOverrideBtreeComparator(false);
      confB.setBtreeComparator((Comparator) null);
      confB.setOverrideBtreeComparator(false);

      confA.setOverrideDuplicateComparator(true);
      confA.setDuplicateComparator(new TestSerialComparator());
      confB.setOverrideDuplicateComparator(true);
      confB.setDuplicateComparator(new TestSerialComparator2());
      try {
        confA.validate(confB);
        fail("expected exception");
      } catch (IllegalArgumentException E) {
        // ok
      }

      /* SecondaryConfig matching. */

      SecondaryConfig confC = new SecondaryConfig();
      SecondaryConfig confD = new SecondaryConfig();
      confC.setKeyCreator(new SecKeyCreator1());
      confD.setKeyCreator(new SecKeyCreator1());

      try {
        confC.validate(confD);
      } catch (Exception E) {
        E.printStackTrace();
        fail("expected valid match");
      }

      try {
        confD.validate(confC);
      } catch (Exception E) {
        fail("expected valid match");
      }

      try {
        confC.validate(null);
        fail("expected exception");
      } catch (IllegalArgumentException E) {
        // ok
      }

      confD.setKeyCreator(new SecKeyCreator2());
      try {
        confC.validate(confD);
        fail("expected exception");
      } catch (IllegalArgumentException E) {
        // ok
      }
      confD.setKeyCreator(new SecKeyCreator1());

      confD.setMultiKeyCreator(new SecMultiKeyCreator1());
      try {
        confC.validate(confD);
        fail("expected exception");
      } catch (IllegalArgumentException E) {
        // ok
      }
      confD.setMultiKeyCreator(null);

      confC.setForeignKeyDeleteAction(ForeignKeyDeleteAction.NULLIFY);
      try {
        confC.validate(confD);
        fail("expected exception");
      } catch (IllegalArgumentException E) {
        // ok
      }
      confC.setForeignKeyDeleteAction(ForeignKeyDeleteAction.ABORT);

      confC.setForeignKeyNullifier(new ForeignKeyNullifier1());
      try {
        confC.validate(confD);
        fail("expected exception");
      } catch (IllegalArgumentException E) {
        // ok
      }
      confC.setForeignKeyNullifier(null);

      confC.setForeignMultiKeyNullifier(new ForeignMultiKeyNullifier1());
      try {
        confC.validate(confD);
        fail("expected exception");
      } catch (IllegalArgumentException E) {
        // ok
      }
      confC.setForeignMultiKeyNullifier(null);

      confC.setImmutableSecondaryKey(true);
      try {
        confC.validate(confD);
        fail("expected exception");
      } catch (IllegalArgumentException E) {
        // ok
      }
      confC.setImmutableSecondaryKey(false);
    } catch (Throwable t) {
      t.printStackTrace();
      throw t;
    }
  }