/** * {@inheritDoc} */ @Override protected void showDefaultValue() { text.setText(property.getDefaultValue()); }
/** * {@inheritDoc} */ @Override public Control createControl(Composite parent) { int style = SWT.BORDER; if (property.isPassword()) { style |= SWT.PASSWORD; } text = new Text(parent, style); text.setText(property.getValue()); text.addModifyListener(new ModifyListener() { @Override public void modifyText(ModifyEvent e) { sendPropertyUpdateEvent(text.getText()); } }); text.addFocusListener(new FocusAdapter() { @Override public void focusLost(FocusEvent e) { String value = text.getText(); if (value.isEmpty() && (null == StringPropertyControl.super.propertyUpdate)) { text.setText(getLastCorrectValue()); } } }); return text; }
@Test public void stringPropertyUpdateXmlSaveLoad() throws PropertyValidationException, JAXBException { String value = RandomStringUtils.randomAlphabetic(20); StringProperty stringProperty = new StringProperty("name", "description", "logical-name", "balbla", false, false); AbstractPropertyUpdate<String> stringPropertyUpdate = stringProperty.createAndValidatePropertyUpdate(value); JAXBContext context = JAXBContext.newInstance(StringPropertyUpdate.class); Marshaller marshaller = context.createMarshaller(); Unmarshaller unmarshaller = context.createUnmarshaller(); marshaller.marshal(stringPropertyUpdate, f); Object object = unmarshaller.unmarshal(f); assertThat(object, is(instanceOf(StringPropertyUpdate.class))); StringPropertyUpdate unmarsalled = (StringPropertyUpdate) object; assertThat(stringProperty.canUpdate(unmarsalled), is(true)); stringProperty.setValue(unmarsalled.getUpdateValue()); assertThat(stringProperty.getValue(), is(value)); }
@Test public void stringPropertyXmlSaveLoad() throws JAXBException { String value = RandomStringUtils.randomAlphabetic(20); StringProperty stringProperty = new StringProperty("name", "description", "logical-name", value, false, false); JAXBContext context = JAXBContext.newInstance(ByteProperty.class); Marshaller marshaller = context.createMarshaller(); Unmarshaller unmarshaller = context.createUnmarshaller(); marshaller.marshal(stringProperty, f); Object object = unmarshaller.unmarshal(f); assertThat(object, is(instanceOf(StringProperty.class))); StringProperty unmarsalled = (StringProperty) object; assertThat(unmarsalled.getDefaultValue(), is(value)); }
@Test public void marshalConfigurationUpdate() throws JAXBException, PropertyValidationException { ConfigurationUpdate configurationUpdate = new ConfigurationUpdate(); SingleProperty<String> property1 = new StringProperty("title", "Define title", "properties.title", "Lorem ipsum", true, false); configurationUpdate.addPropertyUpdate(property1.createAndValidatePropertyUpdate("New value")); SingleProperty<Long> property2 = new LongProperty("speed", "Define speed", "properties.speed", 10L, true, true); configurationUpdate.addPropertyUpdate(property2.createAndValidatePropertyUpdate(1000L)); SingleProperty<Boolean> property3 = new BooleanProperty("boolean", "Define boolean", "properties.boolean", true, true, true); configurationUpdate.addPropertyUpdate(property3.createAndValidatePropertyUpdate(false)); JAXBContext context = JAXBContext.newInstance(ConfigurationUpdate.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(configurationUpdate, System.out); marshaller.marshal(configurationUpdate, f); Unmarshaller unmarshaller = context.createUnmarshaller(); Object object = unmarshaller.unmarshal(f); assertThat(object, is(instanceOf(ConfigurationUpdate.class))); assertThat((ConfigurationUpdate) object, is(equalTo(configurationUpdate))); }
/** * Test that {@link Properties} will hold an updated value of the property if it's included * in the {@link ConfigurationUpdate}. */ @Test @SuppressWarnings("unchecked") public void savedPropertyUpdateFromTheDefaultConfiguration() throws JAXBException, IOException, SAXException { Configuration configuration = new Configuration(); PropertySection section = new PropertySection(); SingleProperty<String> property = new StringProperty("", "", "property1", "value1", false, false); section.addProperty(property); configuration.addSection(section); doReturn(configuration).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(Configuration.class)); doReturn(configurationUpdate).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(ConfigurationUpdate.class)); AbstractPropertyUpdate<String> propertyUpdate = mock(AbstractPropertyUpdate.class); when(propertyUpdate.getPropertyLogicalName()).thenReturn("property1"); when(propertyUpdate.getUpdateValue()).thenReturn("updatedValue"); when(configurationUpdate.getPropertyUpdates()).thenReturn(Collections.<IPropertyUpdate<?>> singleton(propertyUpdate)); Properties properties = propertyManager.getProperties(); assertThat(properties.getProperty("property1"), is(equalTo("updatedValue"))); assertThat(properties.size(), is(1)); }
/** * Test that {@link Properties} will reseted to default if it's included in the * {@link ConfigurationUpdate}. */ @Test @SuppressWarnings("unchecked") public void savedPropertyRestoreToDefaultUpdate() throws JAXBException, IOException, SAXException { String defaultValue = "value1"; Configuration configuration = new Configuration(); PropertySection section = new PropertySection(); SingleProperty<String> property = new StringProperty("", "", "property1", defaultValue, false, false); property.setValue("someotherthandefault"); section.addProperty(property); configuration.addSection(section); doReturn(configuration).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(Configuration.class)); doReturn(configurationUpdate).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(ConfigurationUpdate.class)); AbstractPropertyUpdate<String> propertyUpdate = mock(AbstractPropertyUpdate.class); when(propertyUpdate.getPropertyLogicalName()).thenReturn("property1"); when(propertyUpdate.isRestoreDefault()).thenReturn(true); when(configurationUpdate.getPropertyUpdates()).thenReturn(Collections.<IPropertyUpdate<?>> singleton(propertyUpdate)); Properties properties = propertyManager.getProperties(); assertThat(properties.getProperty("property1"), is(equalTo(defaultValue))); assertThat(properties.size(), is(1)); }
configuration.addSection(section); SingleProperty<String> property1 = new StringProperty("title", "Define title", "properties.title", "Lorem ipsum", true, false); property1.addValidator(new NotEmptyValidator<String>()); section.addProperty(property1);