WebComponent implements Serializable { @SuppressWarnings("unchecked") public <P extends Serializable> void setProperty(PropertyConfiguration<C, P> propertyConfiguration, P value) { Objects.requireNonNull(propertyConfiguration, "Parameter " + "'propertyConfiguration' must not be null!"); if (!(propertyConfiguration instanceof PropertyConfigurationImpl)) { throw new IllegalArgumentException(String.format("Parameter " + "'propertyConfiguration' is not an implementation of %s", PropertyConfigurationImpl.class)); } PropertyConfigurationImpl<C, P> propertyConfigurationImpl = (PropertyConfigurationImpl<C, P>) propertyConfiguration; String propertyName = propertyConfigurationImpl.getPropertyData().getName(); if (!binding.hasProperty(propertyName)) { throw new IllegalArgumentException(String.format("%s does not " + "have a property identified by '%s'!", WebComponent.class.getSimpleName(), propertyName)); } if (value != null && !binding.getPropertyType(propertyName).isAssignableFrom(value.getClass())) { throw new IllegalArgumentException(String.format("Property '%s' " + "of type '%s' cannot be assigned value of type '%s'!", propertyName, binding.getPropertyType(propertyName).getName(), value.getClass().getCanonicalName())); } setProperty(propertyName, value); } private  WebComponent();  WebComponent(WebComponentBinding binding,
                        Element componentHost); void fireEvent(String eventName); void fireEvent(String eventName, JsonValue objectData); void fireEvent(String eventName, JsonValue objectData, EventOptions options); @SuppressWarnings("unchecked") void setProperty(PropertyConfiguration<C, P> propertyConfiguration, P value);  }
@Test public void setProperty_throwsOnNullPropertyConfiguration() { exception.expect(NullPointerException.class); exception.expectMessage("propertyConfiguration"); webComponent.setProperty(null, "value"); }
@Test public void setProperty_throwsOnUnknownProperty() { exception.expect(IllegalArgumentException.class); exception.expectMessage("WebComponent does not have a property identified"); WebComponentBinding<Component> binding = new WebComponentBinding<>(mock(Component.class)); WebComponent<Component> webComponent = new WebComponent<>(binding, new Element("tag")); PropertyConfigurationImpl<Component, String> configuration = new PropertyConfigurationImpl<>( Component.class, "property", String.class, "value"); webComponent.setProperty(configuration, "newValue"); }
@Test public void setProperty_throwsWhenGivenWrongPropertyTypeAsParameter() { exception.expect(IllegalArgumentException.class); exception.expectMessage("Property 'property' of type " + "'java.lang.Integer' cannot be assigned value of type " + "'java.lang.String'!"); PropertyConfigurationImpl<Component, Integer> intConfiguration = new PropertyConfigurationImpl<>( Component.class, "property", Integer.class, 0); WebComponentBinding<Component> binding = new WebComponentBinding<>(mock(Component.class)); binding.bindProperty(intConfiguration, false, null); WebComponent<Component> webComponent = new WebComponent<>(binding, new Element("tag")); PropertyConfigurationImpl<Component, String> stringConfiguration = new PropertyConfigurationImpl<>( Component.class, "property", String.class, "value"); webComponent.setProperty(stringConfiguration, "newValue"); }
@Test public void setProperty_attemptsToWriteSupportedTypes() { Element element = spy(new Element("tag")); PropertyConfigurationImpl<Component, Integer> intConfiguration = new PropertyConfigurationImpl<>( Component.class, "int", Integer.class, 0); PropertyConfigurationImpl<Component, Double> doubleConfiguration = new PropertyConfigurationImpl<>( Component.class, "double", Double.class, 0.0); PropertyConfigurationImpl<Component, String> stringConfiguration = new PropertyConfigurationImpl<>( Component.class, "string", String.class, ""); PropertyConfigurationImpl<Component, Boolean> booleanConfiguration = new PropertyConfigurationImpl<>( Component.class, "boolean", Boolean.class, false); PropertyConfigurationImpl<Component, JsonValue> jsonConfiguration = new PropertyConfigurationImpl<>( Component.class, "json", JsonValue.class, Json.createNull()); WebComponentBinding<Component> binding = new WebComponentBinding<>(mock(Component.class)); binding.bindProperty(intConfiguration, false, null); binding.bindProperty(doubleConfiguration, false, null); binding.bindProperty(stringConfiguration, false, null); binding.bindProperty(booleanConfiguration, false, null); binding.bindProperty(jsonConfiguration, false, null); WebComponent<Component> webComponent = new WebComponent<>(binding, element); webComponent.setProperty(intConfiguration, 1); verify(element, Mockito.times(1)).executeJs(Matchers.anyString(), Matchers.any(), Matchers.any()); webComponent.setProperty(doubleConfiguration, 1.0); verify(element, Mockito.times(2)).executeJs(Matchers.anyString(), Matchers.any(), Matchers.any()); webComponent.setProperty(stringConfiguration, "asd"); verify(element, Mockito.times(3)).executeJs(Matchers.anyString(), Matchers.any(), Matchers.any()); webComponent.setProperty(booleanConfiguration, true); verify(element, Mockito.times(4)).executeJs(Matchers.anyString(), Matchers.any(), Matchers.any()); webComponent.setProperty(jsonConfiguration, Json.create(true)); verify(element, Mockito.times(1)).executeJs(Matchers.anyString(), Matchers.any()); }