@Test(expected = IllegalArgumentException.class) public void testCharacterEditorSetAsTextWithStringLongerThanOneCharacter() throws Exception { PropertyEditor charEditor = new CharacterEditor(false); charEditor.setAsText("ColdWaterCanyon"); }
@Override public String getAsText() { Object value = getValue(); return (value != null ? value.toString() : ""); }
@Override public void setAsText(@Nullable String text) throws IllegalArgumentException { if (this.allowEmpty && !StringUtils.hasLength(text)) { // Treat empty String as null value. setValue(null); } else if (text == null) { throw new IllegalArgumentException("null String cannot be converted to char type"); } else if (isUnicodeCharacterSequence(text)) { setAsUnicode(text); } else if (text.length() == 1) { setValue(Character.valueOf(text.charAt(0))); } else { throw new IllegalArgumentException("String [" + text + "] with length " + text.length() + " cannot be converted to char type: neither Unicode nor single character"); } }
@Test public void testCharacterEditor() { CharBean cb = new CharBean(); BeanWrapper bw = new BeanWrapperImpl(cb); bw.setPropertyValue("myChar", new Character('c')); assertEquals('c', cb.getMyChar()); bw.setPropertyValue("myChar", "c"); assertEquals('c', cb.getMyChar()); bw.setPropertyValue("myChar", "\u0041"); assertEquals('A', cb.getMyChar()); bw.setPropertyValue("myChar", "\\u0022"); assertEquals('"', cb.getMyChar()); CharacterEditor editor = new CharacterEditor(false); editor.setAsText("M"); assertEquals("M", editor.getAsText()); }
private void setAsUnicode(String text) { int code = Integer.parseInt(text.substring(UNICODE_PREFIX.length()), 16); setValue(Character.valueOf((char) code)); }
private void setAsUnicode(String text) { int code = Integer.parseInt(text.substring(UNICODE_PREFIX.length()), 16); setValue(Character.valueOf((char) code)); }
@Override public void setAsText(@Nullable String text) throws IllegalArgumentException { if (this.allowEmpty && !StringUtils.hasLength(text)) { // Treat empty String as null value. setValue(null); } else if (text == null) { throw new IllegalArgumentException("null String cannot be converted to char type"); } else if (isUnicodeCharacterSequence(text)) { setAsUnicode(text); } else if (text.length() == 1) { setValue(Character.valueOf(text.charAt(0))); } else { throw new IllegalArgumentException("String [" + text + "] with length " + text.length() + " cannot be converted to char type: neither Unicode nor single character"); } }
@Test(expected = IllegalArgumentException.class) public void testCharacterEditorSetAsTextWithNullNotAllowingEmptyAsNull() throws Exception { PropertyEditor charEditor = new CharacterEditor(false); charEditor.setAsText(null); }
private void setAsUnicode(String text) { int code = Integer.parseInt(text.substring(UNICODE_PREFIX.length()), 16); setValue(new Character((char) code)); }
@Override public String getAsText() { Object value = getValue(); return (value != null ? value.toString() : ""); }
@Override public void setAsText(String text) throws IllegalArgumentException { if (this.allowEmpty && !StringUtils.hasLength(text)) { // Treat empty String as null value. setValue(null); } else if (text == null) { throw new IllegalArgumentException("null String cannot be converted to char type"); } else if (isUnicodeCharacterSequence(text)) { setAsUnicode(text); } else if (text.length() != 1) { throw new IllegalArgumentException("String [" + text + "] with length " + text.length() + " cannot be converted to char type"); } else { setValue(new Character(text.charAt(0))); } }
this.defaultEditors.put(char.class, new CharacterEditor(false)); this.defaultEditors.put(Character.class, new CharacterEditor(true));
public void setAsText(String text) throws IllegalArgumentException { if (this.allowEmpty && !StringUtils.hasText(text)) { // Treat empty String as null value. setValue(null); } else if (text.length() != 1) { throw new IllegalArgumentException( "String [" + text + "] with length " + text.length() + " cannot be converted to char type"); } else { setValue(new Character(text.charAt(0))); } }
@Override public String getAsText() { Object value = getValue(); return (value != null ? value.toString() : ""); }
@Override public void setAsText(String text) throws IllegalArgumentException { if (this.allowEmpty && !StringUtils.hasLength(text)) { // Treat empty String as null value. setValue(null); } else if (text == null) { throw new IllegalArgumentException("null String cannot be converted to char type"); } else if (isUnicodeCharacterSequence(text)) { setAsUnicode(text); } else if (text.length() == 1) { setValue(Character.valueOf(text.charAt(0))); } else { throw new IllegalArgumentException("String [" + text + "] with length " + text.length() + " cannot be converted to char type: neither Unicode nor single character"); } }
@Test public void testCharacterEditorGetAsTextReturnsEmptyStringIfValueIsNull() throws Exception { PropertyEditor charEditor = new CharacterEditor(false); assertEquals("", charEditor.getAsText()); charEditor = new CharacterEditor(true); charEditor.setAsText(null); assertEquals("", charEditor.getAsText()); charEditor.setAsText(""); assertEquals("", charEditor.getAsText()); charEditor.setAsText(" "); assertEquals(" ", charEditor.getAsText()); }
private void setAsUnicode(String text) { int code = Integer.parseInt(text.substring(UNICODE_PREFIX.length()), 16); setValue(Character.valueOf((char) code)); }
@Override public String getAsText() { Object value = getValue(); return (value != null ? value.toString() : ""); }
this.defaultEditors.put(char.class, new CharacterEditor(false)); this.defaultEditors.put(Character.class, new CharacterEditor(true));
public String getAsText() { Object value = getValue(); return (value != null ? value.toString() : ""); }