1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-25 21:42:07 +01:00

Merge pull request #493 from abyss638/accept_empty_form_label

Accept empty string as form field label value
This commit is contained in:
Florian Schmaus 2021-10-19 20:46:55 +02:00 committed by GitHub
commit bd528d2c32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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 XmlElement {
* @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

@ -115,4 +115,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());
}
}