1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 03:52:06 +01:00

Accept an empty string as the label value

This commit is contained in:
Simon Abykov 2021-10-19 17:34:13 +01:00
parent ab92bc4b40
commit 19270a2eca
3 changed files with 47 additions and 1 deletions

View file

@ -33,6 +33,7 @@ import org.jivesoftware.smack.util.CollectionUtil;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.MultiMap;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
@ -600,7 +601,7 @@ public abstract class FormField implements FullyQualifiedElement {
* @return a reference to this builder.
*/
public B setLabel(String label) {
this.label = StringUtils.requireNotNullNorEmpty(label, "label must not be null or empty");
this.label = Objects.requireNonNull(label, "label must not be null");
return getThis();
}

View file

@ -17,6 +17,8 @@
package org.jivesoftware.smackx.xdata;
import static org.jivesoftware.smack.test.util.XmlAssertUtil.assertXmlSimilar;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import org.jxmpp.jid.JidTestUtil;
@ -35,4 +37,18 @@ class FormFieldTest {
assertXmlSimilar(expectedXml, xml);
}
@Test
public void testEmptyLabel() {
TextSingleFormField.Builder builder = FormField.textSingleBuilder("type");
builder.setLabel("");
TextSingleFormField formField = builder.build();
assertEquals("", formField.getLabel());
}
@Test
public void testThrowExceptionWhenNullLabel() {
TextSingleFormField.Builder builder = FormField.textSingleBuilder("type");
assertThrows(IllegalArgumentException.class, () -> builder.setLabel(null));
}
}

View file

@ -114,4 +114,33 @@ public class DataFormProviderTest {
assertEquals(2, items.size());
}
@Test
public void testRetrieveFieldWithEmptyLabel() throws XmlPullParserException, IOException, SmackParsingException {
String form =
"<x xmlns='jabber:x:data' type='form'>" +
" <title>Advanced User Search</title>" +
" <instructions>The following fields are available for searching. Wildcard (*) characters are allowed as part of the query.</instructions>" +
" <field var='FORM_TYPE' label='' type='hidden'>" +
" <value>jabber:iq:search</value>" +
" </field>" +
" <field label='Search' var='search'>" +
" <required/>" +
" </field>" +
" <field label='Username' var='Username' type='boolean'>" +
" <value>true</value>" +
" </field>" +
" <field label='Name' var='Name' type='boolean'>" +
" <value>true</value>" +
" </field>" +
" <field label='Email' var='Email' type='boolean'>" +
" <value>true</value>" +
" </field>" +
"</x>";
XmlPullParser parser = PacketParserUtils.getParserFor(form);
DataForm dataForm = DataFormProvider.INSTANCE.parse(parser);
FormField usernameFormField = dataForm.getField("FORM_TYPE");
assertEquals(FormField.Type.hidden, usernameFormField.getType());
assertEquals("", usernameFormField.getLabel());
}
}