BooleanGrammarCreator extends AbstractGrammarCreator implements GrammarCreator { @Override public byte[] createGrammar(final URI uri) throws BadFetchError, IOException { final Map<String, String> parameters = getParameters(uri); final ModeType mode = getMode(uri); if (mode == ModeType.VOICE) { parameters.put(YES_PARAMETER_NAME, "yes"); } else { if (!parameters.containsKey(YES_PARAMETER_NAME)) { parameters.put(YES_PARAMETER_NAME, "1"); } } if (mode == ModeType.VOICE) { parameters.put(NO_PARAMETER_NAME, "no"); } else { if (!parameters.containsKey(NO_PARAMETER_NAME)) { parameters.put(NO_PARAMETER_NAME, "2"); } } final SrgsXmlDocument document = createGrammar(parameters, mode); return getBytes(document); }  @Override byte[] createGrammar(final URI uri); @Override String getTypeName(); static final String TYPE_NAME; }
@Test public void testCreateGrammar() throws Exception, BadFetchError { final GrammarCreator creator = new BooleanGrammarCreator(); final URI dtmfUri = new URI("builtin:dtmf/boolean"); final byte[] bytes1 = creator.createGrammar(dtmfUri); final ByteArrayInputStream in1 = new ByteArrayInputStream(bytes1); final InputSource source1 = new InputSource(in1); final SrgsXmlDocument dtmfDocument = new SrgsXmlDocument(source1); final Grammar dtmfGrammar = dtmfDocument.getGrammar(); Assert.assertEquals(ModeType.DTMF, dtmfGrammar.getMode()); final URI voiceUri = new URI("builtin:voice/boolean"); final byte[] bytes2 = creator.createGrammar(voiceUri); final ByteArrayInputStream in2 = new ByteArrayInputStream(bytes2); final InputSource source2 = new InputSource(in2); final SrgsXmlDocument voiceDocument = new SrgsXmlDocument(source2); final Grammar voiceGrammar = voiceDocument.getGrammar(); Assert.assertEquals(ModeType.VOICE, voiceGrammar.getMode()); }
@Test public void testCreateGrammarParameters() throws Exception, BadFetchError { final GrammarCreator creator = new BooleanGrammarCreator(); final URI uri1 = new URI("builtin:dtmf/boolean?y=7;n=9"); final byte[] bytes1 = creator.createGrammar(uri1); final ByteArrayInputStream in1 = new ByteArrayInputStream(bytes1); final InputSource source1 = new InputSource(in1); final SrgsXmlDocument document1 = new SrgsXmlDocument(source1); final Grammar grammar1 = document1.getGrammar(); Assert.assertEquals(ModeType.DTMF, grammar1.getMode()); final URI uri2 = new URI("builtin:dtmf/boolean?y=7"); final byte[] bytes2 = creator.createGrammar(uri2); final ByteArrayInputStream in2 = new ByteArrayInputStream(bytes2); final InputSource source2 = new InputSource(in2); final SrgsXmlDocument document2 = new SrgsXmlDocument(source2); final Grammar grammar2 = document2.getGrammar(); Assert.assertEquals(ModeType.DTMF, grammar2.getMode()); }
@Test(expected = BadFetchError.class) public void testCreateGrammarInvalidParameters() throws Exception, BadFetchError { final GrammarCreator creator = new BooleanGrammarCreator(); final URI uri = new URI("builtin:dtmf/boolean?y="); creator.createGrammar(uri); }