BuiltinSchemeStrategy implements SchemeStrategy { @Override public InputStream getInputStream(final String sessionId, final URI uri, final RequestMethod method, final long timeout, final Collection<KeyValuePair> parameters) throws BadFetchError, UnsupportedElementError, IOException { final String type = extractBuiltinType(uri); final GrammarCreator creator = creators.get(type); if (creator == null) { throw new UnsupportedBuiltinError("builtin type '" + type + "' is not supported!"); } final byte[] bytes = creator.createGrammar(uri); return new ByteArrayInputStream(bytes); } BuiltinSchemeStrategy(); void setGrammarCreators(final Collection<GrammarCreator> col); void addGrammarCreator(final GrammarCreator creator); @Override InputStream getInputStream(final String sessionId, final URI uri,
            final RequestMethod method, final long timeout,
            final Collection<KeyValuePair> parameters); @Override String getScheme(); @Override void sessionClosed(final String sessionId); static final String SCHEME_NAME; }
@Test public void testGetInputStream() throws Exception, JVoiceXMLEvent { final URI dtmfUri = new URI("builtin:dtmf/boolean"); final InputStream input = strategy.getInputStream(null, dtmfUri, null, 0, null); final InputSource source = new InputSource(input); final SrgsXmlDocument dtmfDocument = new SrgsXmlDocument(source); input.close(); final Grammar dtmfGrammar = dtmfDocument.getGrammar(); Assert.assertEquals(ModeType.DTMF, dtmfGrammar.getMode()); final URI voiceUri = new URI("builtin:voice/boolean"); final InputStream voiceInput = strategy.getInputStream(null, voiceUri, null, 0, null); final InputSource voiceSource = new InputSource(voiceInput); final SrgsXmlDocument voiceDocument = new SrgsXmlDocument(voiceSource); input.close(); final Grammar voiceGrammar = voiceDocument.getGrammar(); Assert.assertEquals(ModeType.VOICE, voiceGrammar.getMode()); final URI grammarUri = new URI("builtin:grammar/boolean"); final InputStream grammarInput = strategy.getInputStream(null, grammarUri, null, 0, null); final InputSource grammarSource = new InputSource(grammarInput); final SrgsXmlDocument gramamrDocument = new SrgsXmlDocument(grammarSource); input.close(); final Grammar grammarGrammar = gramamrDocument.getGrammar(); Assert.assertEquals(ModeType.VOICE, grammarGrammar.getMode()); }
@Test public void testGetInputStreamParameters() throws Exception, JVoiceXMLEvent { final URI dtmfUri = new URI("builtin:dtmf/boolean?y=7;n=9"); final InputStream input = strategy.getInputStream(null, dtmfUri, null, 0, null); final InputSource source = new InputSource(input); final SrgsXmlDocument dtmfDocument = new SrgsXmlDocument(source); input.close(); final Grammar dtmfGrammar = dtmfDocument.getGrammar(); Assert.assertEquals(ModeType.DTMF, dtmfGrammar.getMode()); }
@Test(expected = UnsupportedBuiltinError.class) public void testGetInputStreamUnknowBuiltin() throws Exception, JVoiceXMLEvent { final URI uri = new URI("builtin:builtin/cheese?y=7;n=9"); strategy.getInputStream(null, uri, null, 0, null); }