use of io.codearte.jfairy.Fairy in project providence by morimekta.
the class GeneratorWatcherTest method testRandom_customSerializer.
@Test
public void testRandom_customSerializer() throws IOException {
Random random = new Random();
Fairy fairy = Fairy.create(Locale.ENGLISH);
GeneratorWatcher<SimpleGeneratorBase, SimpleGeneratorContext> watcher = GeneratorWatcher.create().setOutputSerializer(new JsonSerializer()).setMaxCollectionItems(2).setRandom(random).withGenerator(CompactFields.kDescriptor, gen -> gen.setAlwaysPresent(CompactFields._Field.NAME).setAlwaysAbsent(CompactFields._Field.LABEL).setValueGenerator(CompactFields._Field.NAME, ctx -> fairy.textProducer().word(1))).setFairy(fairy).dumpOnFailure();
watcher.starting(Description.EMPTY);
CompactFields compact = watcher.generate(CompactFields.kDescriptor);
assertThat(compact.getLabel(), is(nullValue()));
assertThat(compact.getName(), is(notNullValue()));
assertThat(compact.getName(), not(containsString(" ")));
assertThat(compact.hasId(), is(true));
assertThat(watcher.allGenerated(), hasItem(compact));
watcher.failed(new Throwable(), Description.EMPTY);
assertThat(console.output(), is(""));
assertThat(console.error(), is(json(compact) + "\n"));
}
use of io.codearte.jfairy.Fairy in project providence by morimekta.
the class GeneratorWatcher method setLocale.
/**
* Set the locale to generate values for. Applies to default string
* values. Known good locales are:
* <ul>
* <li>English (US)
* <li>German (DE)
* <li>French (FR)
* <li>Italian (IT)
* <li>Spanish (ES)
* <li>Polish (PL)
* <li>Swedish (SV)
* <li>Chinese (ZH)
* </ul>
*
* @param locale The locale to set.
* @return The message generator.
*/
public GeneratorWatcher<Base, Context> setLocale(Locale locale) {
Fairy fairy = singletonFairyCache.get(locale);
if (fairy == null) {
fairy = Fairy.create(locale);
singletonFairyCache.put(locale, fairy);
}
return setFairy(fairy);
}
use of io.codearte.jfairy.Fairy in project providence by morimekta.
the class GeneratorWatcherTest method testRandom_customWriter.
@Test
public void testRandom_customWriter() throws IOException {
Fairy fairy = Fairy.create(Locale.ENGLISH);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GeneratorWatcher<SimpleGeneratorBase, SimpleGeneratorContext> generator = GeneratorWatcher.create().setMessageWriter(new IOMessageWriter(baos, new FastBinarySerializer())).setMaxCollectionItems(2).withGenerator(CompactFields.kDescriptor, gen -> {
gen.setAlwaysPresent(CompactFields._Field.NAME);
gen.setValueGenerator(CompactFields._Field.NAME, ctx -> fairy.textProducer().word(1));
}).dumpOnFailure();
generator.starting(Description.EMPTY);
CompactFields compact = generator.generate(CompactFields.kDescriptor);
assertThat(compact.getLabel(), is(notNullValue()));
assertThat(compact.getName(), is(notNullValue()));
assertThat(compact.getName(), not(containsString(" ")));
assertThat(compact.hasId(), is(true));
assertThat(generator.allGenerated(), hasItem(compact));
generator.failed(new Throwable(), Description.EMPTY);
assertThat(console.output(), is(""));
assertThat(console.error(), is(""));
IOMessageReader reader = new IOMessageReader(new ByteArrayInputStream(baos.toByteArray()), new FastBinarySerializer());
assertThat(reader.read(CompactFields.kDescriptor), is(equalToMessage(compact)));
}
use of io.codearte.jfairy.Fairy in project providence by morimekta.
the class ITGenerator method generate.
public void generate(final int n) throws IOException, TException {
for (int i = 0; i < n; ++i) {
Fairy fairy = LOCAL_FAIRIES[new Random().nextInt(LOCAL_FAIRIES.length)];
generator.setFairy(fairy);
PM providence = generator.generate(descriptor);
TM thrift = convert(providence);
pvdListBuilder.add(providence);
thrListBuilder.add(thrift);
}
}
Aggregations