1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-12-08 22:21:08 +01:00

SMACK-828: Add support for XEP-0107: User Mood

This commit is contained in:
Paul Schaub 2018-11-11 16:13:41 +01:00
parent b7ea226c56
commit cc1bee4659
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
18 changed files with 1117 additions and 0 deletions

View file

@ -0,0 +1,116 @@
/**
*
* Copyright 2018 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.mood;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smackx.mood.element.MoodConcretisation;
import org.jivesoftware.smackx.mood.element.MoodElement;
import org.jivesoftware.smackx.mood.provider.MoodProvider;
import org.jivesoftware.smackx.mood.provider.SimpleMoodConcretisationProvider;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
/**
* This test checks, if extending XEP-0107: User Mood using custom mood concretisations works.
* For that purpose, the second example of XEP-0107 §2.1 is recreated by creating a custom mood concretisation (ecstatic),
* along with a provider, which is dynamically registered with the {@link ProviderManager}.
*/
public class MoodConcretisationTest extends SmackTestSuite {
@Test
public void concretisationTest() throws Exception {
ProviderManager.addExtensionProvider(
EcstaticMoodConcretisation.ELEMENT,
EcstaticMoodConcretisation.NAMESPACE,
EcstaticMoodConcretisationProvider.INSTANCE);
String xml =
"<mood xmlns='http://jabber.org/protocol/mood'>" +
"<happy>" +
"<ecstatic xmlns='https://example.org/'/>" +
"</happy>" +
"<text>Yay, the mood spec has been approved!</text>" +
"</mood>";
MoodElement element = new MoodElement(
new MoodElement.MoodSubjectElement(
Mood.happy,
new EcstaticMoodConcretisation()),
"Yay, the mood spec has been approved!");
assertXMLEqual(xml, element.toXML(null).toString());
XmlPullParser parser = TestUtils.getParser(xml);
MoodElement parsed = MoodProvider.INSTANCE.parse(parser);
assertXMLEqual(xml, parsed.toXML(null).toString());
assertTrue(parsed.hasConcretisation());
assertTrue(parsed.hasText());
assertEquals(EcstaticMoodConcretisation.ELEMENT, parsed.getMoodConcretisation().getMood());
}
@Test
public void unknownConcretisationTest() throws Exception {
String xml =
"<mood xmlns='http://jabber.org/protocol/mood'>" +
"<sad>" +
"<owl xmlns='https://reddit.com/r/superbowl/'/>" +
"</sad>" +
"<text>Hoot hoot!</text>" +
"</mood>";
XmlPullParser parser = TestUtils.getParser(xml);
MoodElement element = MoodProvider.INSTANCE.parse(parser);
// We should not have a provider for sad owls, so the concretisation should not be there.
assertFalse(element.hasConcretisation());
}
public static class EcstaticMoodConcretisation extends MoodConcretisation {
public static final String NAMESPACE = "https://example.org/";
public static final String ELEMENT = "ecstatic";
@Override
public String getNamespace() {
return NAMESPACE;
}
@Override
public String getElementName() {
return ELEMENT;
}
}
public static class EcstaticMoodConcretisationProvider extends SimpleMoodConcretisationProvider<EcstaticMoodConcretisation> {
@Override
protected EcstaticMoodConcretisation simpleExtension() {
return new EcstaticMoodConcretisation();
}
static EcstaticMoodConcretisationProvider INSTANCE = new EcstaticMoodConcretisationProvider();
}
}

View file

@ -0,0 +1,82 @@
/**
*
* Copyright 2018 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.mood;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNull;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smackx.mood.element.MoodElement;
import org.jivesoftware.smackx.mood.provider.MoodProvider;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
public class MoodElementTest extends SmackTestSuite {
@Test
public void toXmlTest() throws Exception {
String xml =
"<mood xmlns='http://jabber.org/protocol/mood'>" +
"<happy/>" +
"<text>Yay, the mood spec has been approved!</text>" +
"</mood>";
MoodElement moodElement = new MoodElement(new MoodElement.MoodSubjectElement(Mood.happy, null), "Yay, the mood spec has been approved!");
assertXMLEqual(xml, moodElement.toXML(null).toString());
assertFalse(moodElement.hasConcretisation());
assertEquals(Mood.happy, moodElement.getMood());
XmlPullParser parser = TestUtils.getParser(xml);
MoodElement parsed = MoodProvider.INSTANCE.parse(parser);
assertEquals(xml, parsed.toXML(null).toString());
}
@Test(expected = IllegalArgumentException.class)
public void illegalArgumentsTest() {
MoodElement element = new MoodElement(null, "Text alone is not allowed.");
}
@Test
public void emptyMoodTest() throws Exception {
MoodElement empty = new MoodElement(null, null);
assertNull(empty.getText());
assertNull(empty.getMood());
assertNull(empty.getMoodConcretisation());
assertFalse(empty.hasText());
assertFalse(empty.hasConcretisation());
String xml = "<mood xmlns='http://jabber.org/protocol/mood'/>";
XmlPullParser parser = TestUtils.getParser(xml);
MoodElement emptyParsed = MoodProvider.INSTANCE.parse(parser);
assertEquals(empty.toXML(null).toString(), emptyParsed.toXML(null).toString());
}
@Test(expected = XmlPullParserException.class)
public void unknownMoodValueExceptionTest() throws Exception {
String xml =
"<mood xmlns='http://jabber.org/protocol/mood'>" +
"<unknown/>" +
"</mood>";
XmlPullParser parser = TestUtils.getParser(xml);
MoodElement element = MoodProvider.INSTANCE.parse(parser);
}
}

View file

@ -0,0 +1,50 @@
/**
*
* Copyright 2018 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.mood;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.mood.element.MoodElement;
import org.junit.Test;
public class MoodManagerTest extends SmackTestSuite {
@Test
public void addMessageTest() {
Message message = new Message();
MoodManager.addMoodToMessage(message, Mood.sad);
assertTrue(message.hasExtension(MoodElement.ELEMENT, MoodElement.NAMESPACE));
assertTrue(MoodElement.hasMoodElement(message));
MoodElement element = MoodElement.fromMessage(message);
assertNotNull(element);
assertEquals(Mood.sad, element.getMood());
assertFalse(element.hasConcretisation());
assertFalse(element.hasText());
message = new Message();
MoodManager.addMoodToMessage(message, Mood.happy, new MoodConcretisationTest.EcstaticMoodConcretisation());
element = MoodElement.fromMessage(message);
assertTrue(element.hasConcretisation());
}
}