public void testSerial() { assertTrue(_nbhm.isEmpty()); final String k1 = "k1"; final String k2 = "k2"; assertThat(_nbhm.put(k1, "v1"), nullValue()); assertThat(_nbhm.put(k2, "v2"), nullValue()); // Serialize it out try { FileOutputStream fos = new FileOutputStream("NBHM_test.txt"); ObjectOutputStream out = new ObjectOutputStream(fos); out.writeObject(_nbhm); out.close(); } catch (IOException ex) { ex.printStackTrace(); } // Read it back try { File f = new File("NBHM_test.txt"); FileInputStream fis = new FileInputStream(f); ObjectInputStream in = new ObjectInputStream(fis); NonBlockingIdentityHashMap nbhm = (NonBlockingIdentityHashMap) in.readObject(); in.close(); assertThat( "serialization works", nbhm.toString(), anyOf(is("{k1=v1, k2=v2}"), is("{k2=v2, k1=v1}"))); if (!f.delete()) throw new IOException("delete failed"); } catch (IOException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } }
/** Method: addToFile(LinkedList<GameNode> g) */ @Test public void testAddToFile() throws Exception { GameNode testGameNode = new GameNode(); testGameNode.setPlayer("rarry"); testGameNode.setOpponent("lobertie"); testGameNode.setGameName("raddle"); testGameNode.setWinLose(true); testGameNode.setScore(29); LinkedList<GameNode> testLLGN = new LinkedList<GameNode>(); testLLGN.add(testGameNode); testScoreboard.addToFile(testLLGN); LinkedList<GameNode> temp = new LinkedList<GameNode>(); ObjectInputStream i = null; try { i = new ObjectInputStream(new FileInputStream("gameStats.ser")); temp = (LinkedList<GameNode>) i.readObject(); i.close(); } catch (FileNotFoundException e) { } catch (IOException e) { } catch (ClassNotFoundException e) { e.printStackTrace(); } assertTrue( "The Object read from file contains the testGameNode added to file", temp.contains(testGameNode)); }
private void doControlTask() throws IOException, ClassNotFoundException { BlockingTaskSummaryResponseHandler handler = new BlockingTaskSummaryResponseHandler(); client.getTasksAssignedAsPotentialOwner("control", "en-UK", handler); List<TaskSummary> sums = handler.getResults(); assertNotNull(sums); assertEquals(1, sums.size()); BlockingTaskOperationResponseHandler startTaskOperationHandler = new BlockingTaskOperationResponseHandler(); client.start(sums.get(0).getId(), "control", startTaskOperationHandler); BlockingGetTaskResponseHandler getTaskHandler = new BlockingGetTaskResponseHandler(); client.getTask(sums.get(0).getId(), getTaskHandler); Task controlTask = getTaskHandler.getTask(); BlockingGetContentResponseHandler getContentHandler = new BlockingGetContentResponseHandler(); client.getContent(controlTask.getTaskData().getDocumentContentId(), getContentHandler); Content content = getContentHandler.getContent(); assertNotNull(content); ByteArrayInputStream bais = new ByteArrayInputStream(content.getContent()); ObjectInputStream ois = new ObjectInputStream(bais); Map<String, Object> deserializedContent = (Map<String, Object>) ois.readObject(); Emergency retrivedEmergency = (Emergency) deserializedContent.get("emergency"); assertNotNull(retrivedEmergency); ActivePatients retrivedActivePatients = (ActivePatients) deserializedContent.get("activePatients"); assertNotNull(retrivedActivePatients); assertEquals(1, retrivedActivePatients.size()); SuggestedProcedures retrivedSuggestedProcedures = (SuggestedProcedures) deserializedContent.get("suggestedProcedures"); assertNotNull(retrivedSuggestedProcedures); assertEquals( "[DefaultHeartAttackProcedure: ]", retrivedSuggestedProcedures.getSuggestedProceduresString()); Map<String, Object> info = new HashMap<String, Object>(); SelectedProcedures selectedProcedures = new SelectedProcedures(retrivedEmergency.getId()); selectedProcedures.addSelectedProcedureName("DefaultHeartAttackProcedure"); info.put("selectedProcedures", selectedProcedures); ContentData result = new ContentData(); result.setAccessType(AccessType.Inline); result.setType("java.util.Map"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(info); out.close(); result.setContent(bos.toByteArray()); BlockingTaskOperationResponseHandler completeTaskOperationHandler = new BlockingTaskOperationResponseHandler(); client.complete(sums.get(0).getId(), "control", result, completeTaskOperationHandler); }
private void doOperatorTask() throws ClassNotFoundException, IOException { BlockingTaskSummaryResponseHandler handler = new BlockingTaskSummaryResponseHandler(); client.getTasksAssignedAsPotentialOwner("operator", "en-UK", handler); List<TaskSummary> sums = handler.getResults(); assertNotNull(sums); assertEquals(1, sums.size()); BlockingTaskOperationResponseHandler startTaskOperationHandler = new BlockingTaskOperationResponseHandler(); client.start(sums.get(0).getId(), "operator", startTaskOperationHandler); BlockingGetTaskResponseHandler getTaskHandler = new BlockingGetTaskResponseHandler(); client.getTask(sums.get(0).getId(), getTaskHandler); Task operatorTask = getTaskHandler.getTask(); BlockingGetContentResponseHandler getContentHandler = new BlockingGetContentResponseHandler(); client.getContent(operatorTask.getTaskData().getDocumentContentId(), getContentHandler); Content content = getContentHandler.getContent(); assertNotNull(content); ByteArrayInputStream bais = new ByteArrayInputStream(content.getContent()); ObjectInputStream ois = new ObjectInputStream(bais); Map<String, Object> deserializedContent = (Map<String, Object>) ois.readObject(); Call restoredCall = (Call) deserializedContent.get("call"); persistenceService.storeCall(restoredCall); Emergency emergency = new Emergency(); emergency.setCall(restoredCall); emergency.setLocation(new Location(1, 2)); emergency.setType(Emergency.EmergencyType.HEART_ATTACK); emergency.setNroOfPeople(1); persistenceService.storeEmergency(emergency); trackingService.attachEmergency(restoredCall.getId(), emergency.getId()); Map<String, Object> info = new HashMap<String, Object>(); info.put("emergency", emergency); ContentData result = new ContentData(); result.setAccessType(AccessType.Inline); result.setType("java.util.Map"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(info); out.close(); result.setContent(bos.toByteArray()); BlockingTaskOperationResponseHandler completeTaskOperationHandler = new BlockingTaskOperationResponseHandler(); client.complete(sums.get(0).getId(), "operator", result, completeTaskOperationHandler); }