@Override @SuppressWarnings("unchecked") public void setDataMap(Map<String, Object> dataMap) throws FormEncodingException { FormRepresentationDecoder decoder = FormEncodingFactory.getDecoder(); this.mimeType = (String) dataMap.get("mimeType"); this.name = (String) dataMap.get("name"); this.value = (String) dataMap.get("value"); this.formatter = (Formatter) decoder .decode((Map<String, Object>) dataMap.get("formatter")); }
@Test public void testListItemsEncodingProblem() throws Exception { FSMenuService service = createMockedService(null); FormRepresentationDecoder decoder = EasyMock.createMock(FormRepresentationDecoder.class); FormEncodingFactory.register(FormEncodingFactory.getEncoder(), decoder); FormEncodingException exception = new FormEncodingException("Something going wrong"); EasyMock.expect(decoder.decodeMenuItemsMap(EasyMock.anyObject(String.class))).andThrow(exception).once(); EasyMock.replay(decoder); try { service.listMenuItems(); fail("listOptions shouldn't succeed"); } catch (MenuServiceException e) { assertNotNull("e shouldn't be null", e); Throwable cause = e.getCause(); assertNotNull("cause shouldn't be null", cause); assertTrue("cause should be a FormEncodingException", cause instanceof FormEncodingException); } EasyMock.verify(decoder); } @Test
@Test public void testSaveMenuItemEncodingProblem() throws Exception { FSMenuService service = createMockedService(null); MenuItemDescription sampleDescription = new MenuItemDescription(); sampleDescription.setAllowedEvents(new ArrayList<String>()); sampleDescription.setEffects(new ArrayList<FormEffectDescription>()); FormItemRepresentation item = MockFormHelper.createMockForm("form", "param1").getFormItems().iterator().next(); sampleDescription.setItemRepresentationMap(item.getDataMap()); sampleDescription.setName("name"); FormRepresentationEncoder encoder = EasyMock.createMock(FormRepresentationEncoder.class); FormEncodingException exception = new FormEncodingException(); @SuppressWarnings("unchecked") Map<String, List<MenuItemDescription>> anyObject = EasyMock.anyObject(Map.class); EasyMock.expect(encoder.encodeMenuItemsMap(anyObject)).andThrow(exception).once(); FormEncodingFactory.register(encoder, FormEncodingFactory.getDecoder()); EasyMock.replay(encoder); try { service.saveMenuItem("group", sampleDescription); fail("saveMenuItem shouldn't succeed"); } catch (MenuServiceException e) { assertNotNull("e shouldn't be null", e); Throwable cause = e.getCause(); assertNotNull("cause shouldn't be null", cause); assertTrue("cause should be a FormEncodingException", cause instanceof FormEncodingException); } EasyMock.verify(encoder); } // THIS TEST REQUIRES THE REAL GWT Components so it should be executed in the showcase
private void writeMenuItems(Map<String, List<MenuItemDescription>> items) throws MenuServiceException { try { FormRepresentationEncoder encoder = FormEncodingFactory.getEncoder(); String json = encoder.encodeMenuItemsMap(items); URL url = asURL("/menuItems.json"); writeToURL(url, json); } catch (FormEncodingException e) { throw new MenuServiceException("Problem transforming menu items to json", e); } catch (URISyntaxException e) { throw new MenuServiceException("Problem finding menu items json file", e); } catch (FileNotFoundException e) { throw new MenuServiceException("No menu items json file found", e); } catch (IOException e) { throw new MenuServiceException("Problem writing menu items json file", e); } catch (Exception e) { throw new MenuServiceException("Unexpected error", e); } }
@Before public void setUp() throws Exception { FormEncodingFactory.register(FormEncodingServerFactory.getEncoder(), FormEncodingServerFactory.getDecoder()); }
@Test public void testListItemsEncodingProblem() throws Exception { FSMenuService service = createMockedService(null); FormRepresentationDecoder decoder = EasyMock.createMock(FormRepresentationDecoder.class); FormEncodingFactory.register(FormEncodingFactory.getEncoder(), decoder); FormEncodingException exception = new FormEncodingException("Something going wrong"); EasyMock.expect(decoder.decodeMenuItemsMap(EasyMock.anyObject(String.class))).andThrow(exception).once(); EasyMock.replay(decoder); try { service.listMenuItems(); fail("listOptions shouldn't succeed"); } catch (MenuServiceException e) { assertNotNull("e shouldn't be null", e); Throwable cause = e.getCause(); assertNotNull("cause shouldn't be null", cause); assertTrue("cause should be a FormEncodingException", cause instanceof FormEncodingException); } EasyMock.verify(decoder); } @Test
@Test public void testSaveMenuItemEncodingProblem() throws Exception { FSMenuService service = createMockedService(null); MenuItemDescription sampleDescription = new MenuItemDescription(); sampleDescription.setAllowedEvents(new ArrayList<String>()); sampleDescription.setEffects(new ArrayList<FormEffectDescription>()); FormItemRepresentation item = MockFormHelper.createMockForm("form", "param1").getFormItems().iterator().next(); sampleDescription.setItemRepresentationMap(item.getDataMap()); sampleDescription.setName("name"); FormRepresentationEncoder encoder = EasyMock.createMock(FormRepresentationEncoder.class); FormEncodingException exception = new FormEncodingException(); @SuppressWarnings("unchecked") Map<String, List<MenuItemDescription>> anyObject = EasyMock.anyObject(Map.class); EasyMock.expect(encoder.encodeMenuItemsMap(anyObject)).andThrow(exception).once(); FormEncodingFactory.register(encoder, FormEncodingFactory.getDecoder()); EasyMock.replay(encoder); try { service.saveMenuItem("group", sampleDescription); fail("saveMenuItem shouldn't succeed"); } catch (MenuServiceException e) { assertNotNull("e shouldn't be null", e); Throwable cause = e.getCause(); assertNotNull("cause shouldn't be null", cause); assertTrue("cause should be a FormEncodingException", cause instanceof FormEncodingException); } EasyMock.verify(encoder); } // THIS TEST REQUIRES THE REAL GWT Components so it should be executed in the showcase
private void writeMenuItems(Map<String, List<MenuItemDescription>> items) throws MenuServiceException { try { FormRepresentationEncoder encoder = FormEncodingFactory.getEncoder(); String json = encoder.encodeMenuItemsMap(items); URL url = asURL("/menuItems.json"); writeToURL(url, json); } catch (FormEncodingException e) { throw new MenuServiceException("Problem transforming menu items to json", e); } catch (URISyntaxException e) { throw new MenuServiceException("Problem finding menu items json file", e); } catch (FileNotFoundException e) { throw new MenuServiceException("No menu items json file found", e); } catch (IOException e) { throw new MenuServiceException("Problem writing menu items json file", e); } catch (Exception e) { throw new MenuServiceException("Unexpected error", e); } }
@Before public void setUp() throws Exception { FormEncodingFactory.register(FormEncodingServerFactory.getEncoder(), FormEncodingServerFactory.getDecoder()); }
@Override @SuppressWarnings("unchecked") public void setDataMap(Map<String, Object> dataMap) throws FormEncodingException { FormRepresentationDecoder decoder = FormEncodingFactory.getDecoder(); this.mimeType = (String) dataMap.get("mimeType"); this.name = (String) dataMap.get("name"); this.value = (String) dataMap.get("value"); this.formatter = (Formatter) decoder .decode((Map<String, Object>) dataMap.get("formatter")); }
@Override @SuppressWarnings("unchecked") public void setDataMap(Map<String, Object> data) throws FormEncodingException { super.setDataMap(data); this.inputName = (String) data.get("inputName"); this.variableName = (String) data.get("variableName"); this.loopBlock = (FormItemRepresentation) FormEncodingFactory.getDecoder().decode((Map<String, Object>) data.get("loopBlock")); }
@Override @SuppressWarnings("unchecked") public void setDataMap(Map<String, Object> data) throws FormEncodingException { super.setDataMap(data); this.inputName = (String) data.get("inputName"); this.variableName = (String) data.get("variableName"); this.loopBlock = (FormItemRepresentation) FormEncodingFactory.getDecoder().decode((Map<String, Object>) data.get("loopBlock")); }
@Override @SuppressWarnings("unchecked") public void setDataMap(Map<String, Object> data) throws FormEncodingException { super.setDataMap(data); this.borderWidth = data.get("borderWidth") == null ? null : ((Number) data.get("borderWidth")).intValue(); this.spacing = data.get("spacing") == null ? null : ((Number) data.get("spacing")).intValue(); this.cssClassName = (String) data.get("cssClassName"); this.horizontalAlignment = (String) data.get("horizontalAlignment"); this.verticalAlignment = (String) data.get("verticalAlignment"); this.title = (String) data.get("title"); this.id = (String) data.get("id"); this.items.clear(); FormRepresentationDecoder decoder = FormEncodingFactory.getDecoder(); List<Map<String, Object>> mapItems = (List<Map<String, Object>>) data.get("items"); if (mapItems != null) { for (Map<String, Object> mapItem : mapItems) { this.items.add((FormItemRepresentation) decoder.decode(mapItem)); } } }
@Override @SuppressWarnings("unchecked") public void setDataMap(Map<String, Object> data) throws FormEncodingException { super.setDataMap(data); this.borderWidth = data.get("borderWidth") == null ? null : ((Number) data.get("borderWidth")).intValue(); this.spacing = data.get("spacing") == null ? null : ((Number) data.get("spacing")).intValue(); this.cssClassName = (String) data.get("cssClassName"); this.horizontalAlignment = (String) data.get("horizontalAlignment"); this.verticalAlignment = (String) data.get("verticalAlignment"); this.title = (String) data.get("title"); this.id = (String) data.get("id"); this.items.clear(); FormRepresentationDecoder decoder = FormEncodingFactory.getDecoder(); List<Map<String, Object>> mapItems = (List<Map<String, Object>>) data.get("items"); if (mapItems != null) { for (Map<String, Object> mapItem : mapItems) { this.items.add((FormItemRepresentation) decoder.decode(mapItem)); } } }
@Override @SuppressWarnings("unchecked") public void setDataMap(Map<String, Object> data) throws FormEncodingException { super.setDataMap(data); this.text = (String) data.get("text"); this.name = (String) data.get("name"); this.id = (String) data.get("id"); Map<String, String> i18nMap = (Map<String, String>) data.get("i18n"); if (i18nMap != null) { this.i18n = new HashMap<String, String>(); this.i18n.putAll(i18nMap); } this.format = (String) data.get("format"); FormRepresentationDecoder decoder = FormEncodingFactory.getDecoder(); this.onClickScript = (ScriptRepresentation) decoder.decode((Map<String, Object>) data.get("onClickScript")); }
@Override @SuppressWarnings("unchecked") public void setDataMap(Map<String, Object> data) throws FormEncodingException { super.setDataMap(data); this.cssClassName = (String) data.get("cssClassName"); this.id = (String) data.get("id"); this.cssStylesheetUrl = (String) data.get("cssStylesheetUrl"); this.items.clear(); List<Object> mapItems = (List<Object>) data.get("items"); FormRepresentationDecoder decoder = FormEncodingFactory.getDecoder(); if (mapItems != null) { for (Object obj : mapItems) { Map<String, Object> itemMap = (Map<String, Object>) obj; FormItemRepresentation item = (FormItemRepresentation) decoder.decode(itemMap); this.items.add(item); } } }
@Override @SuppressWarnings("unchecked") public void setDataMap(Map<String, Object> data) throws FormEncodingException { super.setDataMap(data); this.text = (String) data.get("text"); this.name = (String) data.get("name"); this.id = (String) data.get("id"); Map<String, String> i18nMap = (Map<String, String>) data.get("i18n"); if (i18nMap != null) { this.i18n = new HashMap<String, String>(); this.i18n.putAll(i18nMap); } this.format = (String) data.get("format"); FormRepresentationDecoder decoder = FormEncodingFactory.getDecoder(); this.onClickScript = (ScriptRepresentation) decoder.decode((Map<String, Object>) data.get("onClickScript")); }
@Override @SuppressWarnings("unchecked") public void setDataMap(Map<String, Object> data) throws FormEncodingException { super.setDataMap(data); this.condition = (String) data.get("condition"); FormRepresentationDecoder decoder = FormEncodingFactory.getDecoder(); this.ifBlock = (FormItemRepresentation) decoder.decode((Map<String, Object>) data.get("ifBlock")); this.elseBlock = (FormItemRepresentation) decoder.decode((Map<String, Object>) data.get("elseBlock")); }
@Override @SuppressWarnings("unchecked") public void setDataMap(Map<String, Object> data) throws FormEncodingException { super.setDataMap(data); this.id = (String) data.get("id"); this.items.clear(); List<Map<String, Object>> mapItems = (List<Map<String, Object>>) data.get("items"); FormRepresentationDecoder decoder = FormEncodingFactory.getDecoder(); if (mapItems != null) { for (Map<String, Object> entry : mapItems) { int x = entry.get("x") == null ? 0 : ((Number) entry.get("x")).intValue(); int y = entry.get("y") == null ? 0 : ((Number) entry.get("y")).intValue(); Position pos = new Position(x, y); FormItemRepresentation item = (FormItemRepresentation) decoder.decode(entry); this.items.put(pos, item); } } }
@Override @SuppressWarnings("unchecked") public void setDataMap(Map<String, Object> data) throws FormEncodingException { super.setDataMap(data); this.id = (String) data.get("id"); this.items.clear(); List<Map<String, Object>> mapItems = (List<Map<String, Object>>) data.get("items"); FormRepresentationDecoder decoder = FormEncodingFactory.getDecoder(); if (mapItems != null) { for (Map<String, Object> entry : mapItems) { int x = entry.get("x") == null ? 0 : ((Number) entry.get("x")).intValue(); int y = entry.get("y") == null ? 0 : ((Number) entry.get("y")).intValue(); Position pos = new Position(x, y); FormItemRepresentation item = (FormItemRepresentation) decoder.decode(entry); this.items.put(pos, item); } } }