Java tutorial
/* * Copyright 2014 Carsten Rambow. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package de.elomagic.vaadin.addon.speechrecognition; import java.io.IOException; import java.lang.reflect.Method; import org.apache.commons.io.IOUtils; import org.junit.Test; import com.google.gson.Gson; import junit.framework.Assert; /** * * @author Carsten Rambow */ public class SpeechRecognitionEventDataTest { private String readJsonFile1() throws IOException { return IOUtils.toString(getClass().getClassLoader().getResourceAsStream("events/event1.json"), "utf-8"); } private String readJsonFile2() throws IOException { return IOUtils.toString(getClass().getClassLoader().getResourceAsStream("events/event2.json"), "utf-8"); } private Gson getGson() throws Exception { Method method = SpeechRecognition.class.getDeclaredMethod("createGson"); method.setAccessible(true); return (Gson) method.invoke(null); } @Test public void testDeserializeJSON1() throws Exception { // Prepare test Gson gson = getGson(); // Do test SpeechRecognitionEventData data = gson.fromJson(readJsonFile1(), SpeechRecognitionEventData.class); // Validate results Assert.assertEquals(1398436490336l, data.getTimeStamp().getTime()); Assert.assertEquals("start", data.getType()); } @Test public void testDeserializeJSON2() throws Exception { // Prepare test Gson gson = getGson(); // Do test SpeechRecognitionEventData data = gson.fromJson(readJsonFile2(), SpeechRecognitionEventData.class); // Validate results Assert.assertEquals("result", data.getType()); Assert.assertEquals(1, data.getResults().size()); Assert.assertEquals(1, data.getResults().get(0).size()); Assert.assertEquals("Heinz", data.getResults().get(0).get(0).getTranscript()); Assert.assertEquals(0.009999999776482582, data.getResults().get(0).get(0).getConfidence()); //Assert.assertEquals(1, data.getResults().size()); } }