throw new FormEncodingException( "@className attribute cannot be null"); item.setDataMap(data); } else { throw new FormEncodingException("Type " + obj.getClass().getName() + " cannot be casted to FormItemRepresentation"); throw new FormEncodingException("Couldn't instantiate class " + className, e); } catch (IllegalAccessException e) { throw new FormEncodingException( "Couldn't access constructor of class " + className, e); } catch (ClassNotFoundException e) { throw new FormEncodingException("Couldn't find class " + className, e);
throw new FormEncodingException( "@className attribute cannot be null"); item.setDataMap(data); } else { throw new FormEncodingException("Type " + obj.getClass().getName() + " cannot be casted to FormItemRepresentation"); throw new FormEncodingException("Couldn't instantiate class " + className, e); } catch (IllegalAccessException e) { throw new FormEncodingException( "Couldn't access constructor of class " + className, e); } catch (ClassNotFoundException e) { throw new FormEncodingException("Couldn't find class " + className, e);
@Override public FormItemRepresentation decodeItem(String json) throws FormEncodingException { JsonElement jsonValue = new JsonParser().parse(json); if (jsonValue.isJsonObject()) { JsonObject jsonObj = jsonValue.getAsJsonObject(); Map<String, Object> dataMap = asMap(jsonObj); return (FormItemRepresentation) decode(dataMap); } else { throw new FormEncodingException("Expected json object but found " + jsonValue); } }
@Override public FormItemRepresentation decodeItem(String json) throws FormEncodingException { JsonElement jsonValue = new JsonParser().parse(json); if (jsonValue.isJsonObject()) { JsonObject jsonObj = jsonValue.getAsJsonObject(); Map<String, Object> dataMap = asMap(jsonObj); return (FormItemRepresentation) decode(dataMap); } else { throw new FormEncodingException("Expected json object but found " + jsonValue); } }
@Override public void setDataMap(Map<String, Object> dataMap) throws FormEncodingException { this.documentation = (String) dataMap.get("documentation"); this.id = (String) dataMap.get("id"); this.type = (String) dataMap.get("type"); this.src = (String) dataMap.get("src"); this.content = (String) dataMap.get("content"); this.invokeFunction = (String) dataMap.get("invokeFunction"); @SuppressWarnings("unchecked") List<Object> helpersMap = (List<Object>) dataMap.get("helpers"); if (helpersMap != null) { List<ScriptData> myHelpers = new ArrayList<ScriptData>(); for (Object obj : helpersMap) { try { @SuppressWarnings("unchecked") Map<String, Object> helperMap = (Map<String, Object>) obj; String helperClass = (String) helperMap.get("@className"); // FBScriptHelper helper = (FBScriptHelper) ReflectionHelper // .newInstance(helperClass); Class<?> clazz = Class.forName(helperClass); //TODO porting ScriptData helper = (ScriptData) clazz.newInstance(); helper.setDataMap(helperMap); } catch (Exception e) { throw new FormEncodingException("Problem creating helper " + obj, e); } } setHelpers(myHelpers); } }
@Override public void setDataMap(Map<String, Object> dataMap) throws FormEncodingException { this.documentation = (String) dataMap.get("documentation"); this.id = (String) dataMap.get("id"); this.type = (String) dataMap.get("type"); this.src = (String) dataMap.get("src"); this.content = (String) dataMap.get("content"); this.invokeFunction = (String) dataMap.get("invokeFunction"); @SuppressWarnings("unchecked") List<Object> helpersMap = (List<Object>) dataMap.get("helpers"); if (helpersMap != null) { List<ScriptData> myHelpers = new ArrayList<ScriptData>(); for (Object obj : helpersMap) { try { @SuppressWarnings("unchecked") Map<String, Object> helperMap = (Map<String, Object>) obj; String helperClass = (String) helperMap.get("@className"); // FBScriptHelper helper = (FBScriptHelper) ReflectionHelper // .newInstance(helperClass); Class<?> clazz = Class.forName(helperClass); //TODO porting ScriptData helper = (ScriptData) clazz.newInstance(); helper.setDataMap(helperMap); } catch (Exception e) { throw new FormEncodingException("Problem creating helper " + obj, e); } } setHelpers(myHelpers); } }
item.setDataMap(itemRep.getMap()); } catch (Exception ex) { throw new FormEncodingException("invalid form item", ex); formValidations.add(validation); } catch (Exception ex) { throw new FormEncodingException("invalid validation item", ex);
item.setDataMap(itemRep.getMap()); } catch (Exception ex) { throw new FormEncodingException("invalid form item", ex); formValidations.add(validation); } catch (Exception ex) { throw new FormEncodingException("invalid validation item", ex);
@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 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
@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