protected List<String> getNames() { Map<Object, String> objectToNameMap = provider.getNamesMap(); if (fieldProvider == null) { return new ArrayList<>(objectToNameMap.values()); } final Map<Object, String> clonedMap = new HashMap<>(objectToNameMap); final Map<Object, Field<String>> fieldValuesToMerge = fieldProvider.getObjectFieldMap(); for (Map.Entry<Object, Field<String>> eachEntry : fieldValuesToMerge.entrySet()) { clonedMap.put(eachEntry.getKey(), eachEntry.getValue().getValue()); } return new ArrayList<>(clonedMap.values()); }
@Test public void createFieldDoesNotSelectFirstOptionIfItemExists() throws Exception { // GIVEN initializeSelectFieldFactory(); // WHEN Field field = dialogSelect.createField(); // THEN first option is selected assertThat(field.getValue(), is(nullValue())); }
@Override public void buttonClick(ClickEvent event) { try { form.commit(); event.getButton().setEnabled(false); listener.onGenerateNewKeys(form.getField(ActivationPresenter.KEY_LENGTH).getValue().toString()); } catch (CommitException e) { uiContext.openNotification(MessageStyleTypeEnum.ERROR, true, i18n.translate("key.required")); } finally { event.getButton().setEnabled(true); } } });
@Test public void createFieldDoesntSelectDefaultIfValueAlreadyExists() throws Exception { // GIVEN SelectFieldOptionDefinition option = definition.getOptions().get(1); option.setSelected(true); baseNode.setProperty(propertyName, "3"); baseItem = new JcrNodeAdapter(baseNode); initializeSelectFieldFactory(); // WHEN Field field = dialogSelect.createField(); // THEN assertThat(field.getValue().toString(), is("3")); }
@Test public void radioFieldPreselectsOptionOnPreexistingNodeWithNullValue() throws Exception { // GIVEN // we keep baseItem as a regular JcrNodeAdapter here definition.getOptions().get(2).setSelected(true); dialogSelect = new OptionGroupFieldFactory(definition, baseItem, null, null, componentProvider); // WHEN Field field = dialogSelect.createField(); // THEN assertThat(field.getValue(), is(nullValue())); }
@Test public void checkBoxGroupFieldDoesNotPreselectAnythingByDefault() throws Exception { // GIVEN definition.setMultiselect(true); dialogSelect = new OptionGroupFieldFactory(definition, baseItem, null, null, componentProvider); // WHEN Field field = dialogSelect.createField(); // THEN assertThat(field, is(instanceOf(OptionGroup.class))); assertThat(((OptionGroup) field).isMultiSelect(), is(true)); Collection<?> items = ((OptionGroup) field).getItemIds(); assertThat(items.size(), equalTo(3)); assertThat(field.getValue().toString(), equalTo("[]")); }
@Test public void radioFieldDoesNotPreselectAnythingByDefault() throws Exception { // GIVEN baseItem = new JcrNewNodeAdapter(baseNode, baseNode.getPrimaryNodeType().getName()); dialogSelect = new OptionGroupFieldFactory(definition, baseItem, null, null, componentProvider); // WHEN Field field = dialogSelect.createField(); // THEN assertThat(field.getValue(), is(nullValue())); }
@Test public void testGetField() { // GIVEN StaticFieldFactory staticFieldFactory = new StaticFieldFactory<>(definition, baseItem, uiContext, i18NAuthoringSupport); staticFieldFactory.setComponentProvider(componentProvider); // WHEN Field field = staticFieldFactory.createField(); // THEN assertThat(field, instanceOf(StaticField.class)); assertEquals("value to display", field.getValue()); }
@Test public void createFieldSelectsDefaultOptionIfConfigured() throws Exception { // GIVEN SelectFieldOptionDefinition option = definition.getOptions().get(1); option.setSelected(true); baseItem = new JcrNewNodeAdapter(baseNode, baseNode.getPrimaryNodeType().getName()); initializeSelectFieldFactory(); // WHEN Field field = dialogSelect.createField(); // THEN assertThat(field.getValue().toString(), equalTo(option.getValue())); }
@Test public void radioFieldPreselectsOptionConfiguredAsSelected() throws Exception { // GIVEN baseItem = new JcrNewNodeAdapter(baseNode, baseNode.getPrimaryNodeType().getName()); definition.getOptions().get(2).setSelected(true); dialogSelect = new OptionGroupFieldFactory(definition, baseItem, null, null, componentProvider); // WHEN Field field = dialogSelect.createField(); // THEN assertThat(field.getValue().toString(), equalTo("3")); }
@Test public void createField() throws Exception { // GIVEN baseItem = new JcrNewNodeAdapter(baseNode, baseNode.getPrimaryNodeType().getName()); initializeSelectFieldFactory(); // WHEN Field field = dialogSelect.createField(); // THEN assertTrue(field instanceof ComboBox); Collection<?> items = ((ComboBox) field).getItemIds(); assertThat(items.size(), is(3)); assertThat(field.getValue().toString(), is("1")); }
@Test public void createDecimalTextField() { // GIVEN definition.setType("Decimal"); dialogEdit = new TextFieldFactory(definition, baseItem, uiContext, i18NAuthoringSupport); dialogEdit.setComponentProvider(componentProvider); // WHEN Field<String> field = dialogEdit.createField(); ((AbstractField) field).setLocale(Locale.ENGLISH); field.setValue("1115.37567657"); // THEN assertEquals("1,115.37567657", field.getValue()); }
@Test public void createDoubleTextField() { // GIVEN definition.setType("Double"); dialogEdit = new TextFieldFactory(definition, baseItem, uiContext, i18NAuthoringSupport); dialogEdit.setComponentProvider(componentProvider); // WHEN Field<String> field = dialogEdit.createField(); ((AbstractField) field).setLocale(Locale.ENGLISH); field.setValue("123.456789"); // THEN assertEquals("123.456789", field.getValue()); }
@Test public void testValueForEmptyNewItem() throws Exception { // GIVEN baseItem = new JcrNewNodeAdapter(baseNode, baseNode.getPrimaryNodeType().getName(), "newItem"); fieldFactory = new TestTextFieldFactory(definition, baseItem, null, i18NAuthoringSupport); fieldFactory.setComponentProvider(this.componentProvider); Field<Object> field = fieldFactory.createField(); // WHEN String value = (String) field.getValue(); // THEN assertNull(value); }
@Test public void createCodeFieldChangeValue() { // GIVEN baseItem.addItemProperty(propertyName, new ObjectProperty<>("private String s", String.class)); Field<String> field = factory.createField(); assertEquals("private String s", field.getValue()); // WHEN field.setValue("new Value"); // THEN assertEquals("new Value", baseItem.getItemProperty(propertyName).getValue()); }
@Test public void testValueForEmptyNewItemWithDefaultValue() throws Exception { // GIVEN baseItem = new JcrNewNodeAdapter(baseNode, baseNode.getPrimaryNodeType().getName(), "newItem"); definition.setDefaultValue("defaultValue"); fieldFactory = new TestTextFieldFactory(definition, baseItem, null, i18NAuthoringSupport); fieldFactory.setComponentProvider(this.componentProvider); Field<Object> field = fieldFactory.createField(); // WHEN String value = (String) field.getValue(); // THEN assertNotNull(value); assertEquals("defaultValue", value); }
@Test public void checkBoxField_SetSelectedTest() throws Exception { // GIVEN baseItem = new JcrNewNodeAdapter(baseNode, baseNode.getPrimaryNodeType().getName()); checkBoxField = new CheckBoxFieldFactory(definition, baseItem, uiContext, i18NAuthoringSupport); checkBoxField.setComponentProvider(componentProvider); definition.setDefaultValue("false"); // WHEN Field<Boolean> field = checkBoxField.createField(); // THEN assertEquals(true, field instanceof CheckBoxField); assertEquals(false, field.getValue()); }
@Test public void testValueForNotEmptyNewItem() throws Exception { // GIVEN baseItem = new JcrNewNodeAdapter(baseNode, baseNode.getPrimaryNodeType().getName(), "newItem"); baseItem.addItemProperty(propertyName, new ObjectProperty<>("value")); fieldFactory = new TestTextFieldFactory(definition, baseItem, null, i18NAuthoringSupport); fieldFactory.setComponentProvider(this.componentProvider); Field<Object> field = fieldFactory.createField(); // WHEN String value = (String) field.getValue(); // THEN assertNotNull(value); assertEquals("value", value); }
@Test public void checkBoxValueChangePropagatesToCustomField() throws Exception { // GIVEN baseItem = new JcrNewNodeAdapter(baseNode, baseNode.getPrimaryNodeType().getName()); checkBoxField = new CheckBoxFieldFactory(definition, baseItem, uiContext, i18NAuthoringSupport); checkBoxField.setComponentProvider(componentProvider); Field<Boolean> field = checkBoxField.createField(); CheckBox checkBox = (CheckBox) ((CheckBoxField) field).iterator().next(); // WHEN checkBox.setValue(true); // THEN assertEquals(true, checkBox.getValue()); assertEquals(true, field.getValue()); assertEquals(true, baseItem.getItemProperty(definition.getName()).getValue()); }
@Test public void testValueForNotEmptyNewItemWithDefaultValue() throws Exception { // GIVEN baseItem = new JcrNewNodeAdapter(baseNode, baseNode.getPrimaryNodeType().getName(), "newItem"); baseItem.addItemProperty(propertyName, new ObjectProperty<>("value")); definition.setDefaultValue("defaultValue"); fieldFactory = new TestTextFieldFactory(definition, baseItem, null, i18NAuthoringSupport); fieldFactory.setComponentProvider(this.componentProvider); Field<Object> field = fieldFactory.createField(); // WHEN String value = (String) field.getValue(); // THEN assertNotNull(value); assertEquals("value", value); }