/** * 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 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); } }
@SuppressWarnings("rawtypes") private Generator<?>[] createSyntheticElementGenerators( ArrayTypeDescriptor arrayType, Uniqueness uniqueness, BeneratorContext context) { Generator[] result = new Generator[arrayType.getElementCount()]; for (int i = 0; i < arrayType.getElementCount(); i++) { ArrayElementDescriptor element = getElementOfTypeOrParents(arrayType, i); if (element.getMode() != Mode.ignored) { Generator<?> generator = InstanceGeneratorFactory.createSingleInstanceGenerator(element, uniqueness, context); result[i] = generator; } } return result; }