@Test
 public void testApply_notUnique() {
   Generator<String> source = new SequenceTestGenerator<String>("A", "B");
   source.init(new DefaultBeneratorContext());
   Generator<String> generator = fcn.applyTo(source, false);
   generator.init(context);
   int n = 1000;
   Map<String, AtomicInteger> counts = super.countProducts(generator, n);
   assertEquals(2, counts.size());
   assertTrue(counts.get("A").doubleValue() > counts.get("B").doubleValue());
 }
 @Test
 public void testSyntheticGeneration() {
   // given an array descriptor
   ArrayTypeDescriptor arrayDescriptor = createArrayType("");
   ArrayElementDescriptor e1 = createArrayElement(0, "string");
   ((SimpleTypeDescriptor) e1.getLocalType(false)).setValues("'Alice', 'Bob'");
   arrayDescriptor.addElement(e1);
   ArrayElementDescriptor e2 = createArrayElement(1, "int");
   ((SimpleTypeDescriptor) e2.getLocalType(false)).setValues("23,34");
   arrayDescriptor.addElement(e2);
   // when creating a generator for the descriptor
   Generator<Object[]> generator =
       (Generator<Object[]>)
           arrayTypeGeneratorFactory.createGenerator(
               arrayDescriptor, "testSyntheticGeneration", false, Uniqueness.NONE, context);
   // it is expected to generate as specified
   generator.init(context);
   for (int i = 0; i < 10; i++) {
     Object[] product = GeneratorUtil.generateNonNull(generator);
     assertNotNull(product);
     assertTrue(
         "Expected 'Alice' or 'Bob', but was: " + product[0],
         "Alice".equals(product[0]) || "Bob".equals(product[0]));
     assertTrue((Integer) product[1] == 23 || (Integer) product[1] == 34);
   }
 }
  /**
   * TODO v0.8 implement array mutation @Test public void testMutatingGeneration() { Object[]
   * MUTATED_ALICE = new Object[] { "Alice", 24 };
   *
   * <p>// define descriptor ArrayTypeDescriptor descriptor = createPersonDescriptor();
   * descriptor.setGenerator(PersonAttrArrayGenerator.class.getName());
   * descriptor.getElement(1).getLocalType(false).setScript("p[1] + 1");
   *
   * <p>// create generator Generator<Object[]> generator =
   * ArrayGeneratorFactory.createArrayGenerator( "p", descriptor, Uniqueness.NONE, context);
   * generator.init(context);
   *
   * <p>// validate for (int i = 0; i < 10; i++) assertEqualArrays(MUTATED_ALICE,
   * GeneratorUtil.generateNonNull(generator)); }
   */
  @Test
  public void testUniqueArrayGeneration() {
    ArrayTypeDescriptor arrayTypeDescriptor = createArrayType("MyArray");

    // create descriptor
    context.set("gen0", new SequenceTestGenerator<Integer>(1, 2));
    ArrayElementDescriptor e0 = createArrayElement(0, "int");
    ((SimpleTypeDescriptor) e0.getLocalType(false)).setGenerator("gen0");
    arrayTypeDescriptor.addElement(e0);

    context.set("gen1", new SequenceTestGenerator<Integer>(3, 4));
    ArrayElementDescriptor e1 = createArrayElement(1, "int");
    ((SimpleTypeDescriptor) e1.getLocalType(false)).setGenerator("gen1");
    arrayTypeDescriptor.addElement(e1);

    InstanceDescriptor arrayInstDescriptor = createInstance("array", arrayTypeDescriptor);
    arrayInstDescriptor.setUnique(true);

    // create generator
    Generator<Object[]> generator =
        (Generator<Object[]>)
            InstanceGeneratorFactory.createSingleInstanceGenerator(
                arrayInstDescriptor, Uniqueness.NONE, context);
    generator.init(context);

    // test generator
    assertArray(INT13, GeneratorUtil.generateNonNull(generator));
    assertArray(INT14, GeneratorUtil.generateNonNull(generator));
    assertArray(INT23, GeneratorUtil.generateNonNull(generator));
    assertArray(INT24, GeneratorUtil.generateNonNull(generator));
    assertUnavailable(generator);
  }
  @Test
  public void testUniqueValuesArrayGeneration() {
    ArrayElementDescriptor e0 = createArrayElement(0, "int");
    ((SimpleTypeDescriptor) e0.getLocalType(false)).setValues("1,2");

    ArrayElementDescriptor e1 = createArrayElement(1, "int");
    ((SimpleTypeDescriptor) e1.getLocalType(false)).setValues("3,4");

    ArrayTypeDescriptor arrayTypeDescriptor = createArrayType("MyArray");
    arrayTypeDescriptor.addElement(e0);
    arrayTypeDescriptor.addElement(e1);

    InstanceDescriptor arrayInstDescriptor = createInstance("array", arrayTypeDescriptor);
    arrayInstDescriptor.setUnique(true);

    Generator<Object[]> generator =
        (Generator<Object[]>)
            InstanceGeneratorFactory.createSingleInstanceGenerator(
                arrayInstDescriptor, Uniqueness.NONE, context);
    generator.init(context);
    for (int i = 0; i < 4; i++) {
      Object[] product = GeneratorUtil.generateNonNull(generator);
      assertTrue(
          Arrays.equals(INT13, product)
              || Arrays.equals(INT14, product)
              || Arrays.equals(INT23, product)
              || Arrays.equals(INT24, product));
    }
    assertUnavailable(generator);
  }
 @Test
 public void testCreateGenerator_int() {
   LiteralSequence sequence = new LiteralSequence("2, 3, 5, 7, 11");
   Generator<Integer> generator = sequence.createNumberGenerator(Integer.class, 1, 20, 1, false);
   generator.init(context);
   expectGeneratedSequence(generator, 2, 3, 5, 7, 11);
 }
 @SuppressWarnings({"unchecked", "rawtypes"})
 protected <T> void expectSet(PartDescriptor name, int n, T... products) {
   ComponentBuilder builder = createComponentBuilder(name);
   Generator<T> helper = new ComponentBuilderGenerator(builder, name.getName());
   helper.init(context);
   expectGeneratedSet(helper, n, products);
 }
 @SuppressWarnings({"unchecked", "rawtypes"})
 protected <T> void expectUniqueSet(PartDescriptor name, T... products) {
   ComponentBuilder builder = createComponentBuilder(name);
   Generator<T> helper = new ComponentBuilderGenerator(builder, name.getName());
   helper.init(context);
   expectUniquelyGeneratedSet(helper, products).withCeasedAvailability();
 }
 @Test
 public void testApply() {
   Generator<String> source = new SequenceTestGenerator<String>("A", "B", "C", "D");
   LiteralSequence sequence = new LiteralSequence("1, 3");
   Generator<String> generator = sequence.applyTo(source, false);
   generator.init(context);
   expectGeneratedSequence(generator, "B", "D");
 }
Пример #9
0
 @Test
 public void testApplyTo() {
   SequenceGenerator<Integer> source = new SequenceGenerator<Integer>(Integer.class, 1, 2, 3);
   source.init(context);
   Generator<Integer> generator = createWeight().applyTo(source, false);
   generator.init(context);
   expectRelativeWeights(generator, 5000, 1, 1, 2, 2, 3, 3);
 }
 @Test(expected = ConfigurationError.class)
 public void testIllegalSourceType() {
   ArrayTypeDescriptor descriptor = createArrayType("");
   descriptor.setSource("illegalSource");
   context.set("illegalSource", new File("txt.txt"));
   Generator<Object[]> generator =
       (Generator<Object[]>)
           arrayTypeGeneratorFactory.createGenerator(
               descriptor, "testIllegalSourceType", false, Uniqueness.NONE, context);
   generator.init(context);
 }
 @Test
 public void testGenerator() {
   ArrayTypeDescriptor descriptor = createArrayType("testGenerator");
   descriptor.setGenerator(PersonAttrArrayGenerator.class.getName());
   Generator<Object[]> generator =
       (Generator<Object[]>)
           arrayTypeGeneratorFactory.createGenerator(
               descriptor, "testGenerator", false, Uniqueness.NONE, context);
   generator.init(context);
   for (int i = 0; i < 10; i++)
     assertEqualArrays(PersonAttrArrayGenerator.ALICE, GeneratorUtil.generateNonNull(generator));
 }
 @Test
 public void testXlsSource() {
   ArrayTypeDescriptor parent = createPersonDescriptor();
   ArrayTypeDescriptor descriptor = createArrayType("testXlsSource", parent);
   descriptor.setSource("org/databene/benerator/factory/person.ent.xls");
   Generator<Object[]> generator =
       (Generator<Object[]>)
           arrayTypeGeneratorFactory.createGenerator(
               descriptor, "testXlsSource", false, Uniqueness.NONE, context);
   context.set("otto_age", 89);
   generator.init(context);
   expectGeneratedSequence(generator, ALICE, OTTO).withCeasedAvailability();
 }
 @Test
 public void testGeneratorSource() {
   context.set("myGen", new PersonAttrArrayGenerator());
   ArrayTypeDescriptor parent = createPersonDescriptor();
   ArrayTypeDescriptor descriptor = createArrayType("testGeneratorSource", parent);
   descriptor.setSource("myGen");
   Generator<Object[]> generator =
       (Generator<Object[]>)
           arrayTypeGeneratorFactory.createGenerator(
               descriptor, "testGeneratorSource", false, Uniqueness.NONE, context);
   generator.init(context);
   for (int i = 0; i < 10; i++) assertEqualArrays(ALICE, GeneratorUtil.generateNonNull(generator));
 }
 @Test
 public void testCreateDoubleGenerator_notUnique() {
   Generator<Double> generator = fcn.createNumberGenerator(Double.class, 1., 2., 0.5, false);
   generator.init(context);
   int n = 2000;
   Map<Double, AtomicInteger> counts = super.countProducts(generator, n);
   assertEquals(3, counts.size());
   int lastCount = n + 1;
   for (double d = 1; d <= 2; d += 0.5) {
     int count = counts.get(d).intValue();
     assertTrue(count < lastCount);
     lastCount = count;
   }
 }
 @Test
 public void testCsvSource() {
   ArrayTypeDescriptor parent = createPersonDescriptor();
   ArrayTypeDescriptor descriptor = createArrayType("testCsvSource", parent);
   descriptor.setSource("org/databene/benerator/factory/person.ent.csv");
   Generator<Object[]> generator =
       (Generator<Object[]>)
           arrayTypeGeneratorFactory.createGenerator(
               descriptor, "testCsvSource", false, Uniqueness.NONE, context);
   context.set("ottos_age", 89);
   generator.init(context);
   assertEqualArrays(ALICE, GeneratorUtil.generateNonNull(generator));
   assertEqualArrays(OTTO, GeneratorUtil.generateNonNull(generator));
   assertUnavailable(generator);
 }
 @Test
 public void testEntitySource() {
   ArrayTypeDescriptor descriptor = createArrayType("testEntitySourceType");
   descriptor.setSource(PersonSource.class.getName());
   Generator<Object[]> generator =
       (Generator<Object[]>)
           arrayTypeGeneratorFactory.createGenerator(
               descriptor, "testEntitySource", false, Uniqueness.NONE, context);
   generator.init(context);
   for (int i = 0; i < 2; i++) {
     Object[] product = GeneratorUtil.generateNonNull(generator);
     assertTrue(
         "Found: " + ArrayFormat.format(product),
         Arrays.equals(ALICE, product) || Arrays.equals(BOB, product));
   }
   assertUnavailable(generator);
 }
 @Test
 public void testCsvDataset() {
   ArrayTypeDescriptor parent = createPersonDescriptor();
   ArrayTypeDescriptor descriptor = createArrayType("testCsvDataset", parent);
   descriptor.setSource("org/databene/benerator/factory/dataset_{0}.csv");
   descriptor.setNesting("org/databene/benerator/factory/testnesting");
   descriptor.setDataset("DACH");
   Generator<Object[]> generator =
       (Generator<Object[]>)
           arrayTypeGeneratorFactory.createGenerator(
               descriptor, "testCsvDataset", false, Uniqueness.SIMPLE, context);
   Generator<String> g =
       WrapperFactory.applyConverter(
           generator, new ArrayElementExtractor<String>(String.class, 0));
   generator.init(context);
   expectUniquelyGeneratedSet(g, "de", "at", "ch");
   assertUnavailable(generator);
 }
  @Test
  public void testDatabaseSource() throws Exception {
    // prepare DB
    DBSystem db =
        new DBSystem(
            "db",
            HSQLUtil.getInMemoryURL("benerator"),
            HSQLUtil.DRIVER,
            "sa",
            null,
            context.getDataModel());
    context.set("db", db);
    try {
      db.execute(
          "create table agft_person ("
              + "  id   int         NOT NULL,"
              + "  name varchar(30) NOT NULL,"
              + "  age  int         NOT NULL"
              + ")");
      db.execute("insert into agft_person (id, name, age) values (1, 'Alice', 23)");
      db.execute("insert into agft_person (id, name, age) values (2, 'Otto', 89)");

      // prepare descriptor
      ArrayTypeDescriptor parent = createPersonDescriptor();
      ArrayTypeDescriptor descriptor = createArrayType("testDatabaseSource", parent);
      descriptor.setSource("db");
      descriptor.setSelector("select name, age from agft_person");
      Generator<Object[]> generator =
          (Generator<Object[]>)
              arrayTypeGeneratorFactory.createGenerator(
                  descriptor, "testDatabaseSource", false, Uniqueness.NONE, context);
      generator.init(context);

      // verify results
      assertEqualArrays(ALICE, GeneratorUtil.generateNonNull(generator));
      Object[] p2 = GeneratorUtil.generateNonNull(generator);
      assertEqualArrays(OTTO, p2);
      assertUnavailable(generator);

    } finally {
      db.execute("drop table agft_person if exists");
      db.close();
    }
  }
 @Test(expected = IllegalArgumentException.class)
 public void testApply_unique() {
   Generator<String> source = new SequenceTestGenerator<String>("A", "B");
   source.init(new DefaultBeneratorContext());
   fcn.applyTo(source, true);
 }