1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-23 20:42:06 +01:00

Simplify providers, add JingleSessionTest

This commit is contained in:
vanitasvitae 2017-06-01 02:02:40 +02:00
parent 306a7bc0ff
commit 9e1f777332
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 70 additions and 51 deletions

View file

@ -90,34 +90,15 @@ public class JingleFileTransferPayload extends JingleContentDescriptionPayloadTy
XmlStringBuilder sb = new XmlStringBuilder(this);
sb.rightAngleBracket();
if (date != null) {
sb.element(ELEM_DATE, date);
}
if (desc != null) {
sb.element(ELEM_DESC, desc);
}
if (mediaType != null) {
sb.element(ELEM_MEDIA_TYPE, mediaType);
}
if (name != null) {
sb.element(ELEM_NAME, name);
}
if (range != null) {
sb.element(range);
}
sb.optElement(ELEM_DATE, date);
sb.optElement(ELEM_DESC, desc);
sb.optElement(ELEM_MEDIA_TYPE, mediaType);
sb.optElement(ELEM_NAME, name);
sb.optElement(range);
if (size > 0) {
sb.element(ELEM_SIZE, Integer.toString(size));
}
if (hash != null) {
sb.element(hash);
}
sb.optElement(hash);
sb.closeElement(this);
return sb;
}

View file

@ -20,14 +20,13 @@ import org.jivesoftware.smackx.hash.element.HashElement;
import org.jivesoftware.smackx.hash.provider.HashElementProvider;
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionPayloadType;
import org.jivesoftware.smackx.jingle.provider.JingleContentDescriptionProvider;
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferPayload;
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleContentDescriptionFileTransfer;
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferPayload;
import org.jivesoftware.smackx.jingle_filetransfer.element.Range;
import org.jxmpp.util.XmppDateTime;
import org.xmlpull.v1.XmlPullParser;
import java.util.ArrayList;
import java.util.Date;
import static org.xmlpull.v1.XmlPullParser.END_TAG;
import static org.xmlpull.v1.XmlPullParser.START_TAG;
@ -42,14 +41,8 @@ public class JingleContentDescriptionFileTransferProvider
boolean inRange = false;
Date date = null;
String desc = null;
String mediaType = null;
String name = null;
int size = -1;
Range range = null;
JingleFileTransferPayload.Builder builder = JingleFileTransferPayload.getBuilder();
HashElement inRangeHash = null;
HashElement hash = null;
ArrayList<JingleContentDescriptionPayloadType> payloads = new ArrayList<>();
@ -65,34 +58,27 @@ public class JingleContentDescriptionFileTransferProvider
switch (elem) {
case JingleFileTransferPayload.ELEMENT:
date = null;
desc = null;
mediaType = null;
name = null;
size = -1;
range = null;
inRangeHash = null;
hash = null;
builder = JingleFileTransferPayload.getBuilder();
break;
case JingleFileTransferPayload.ELEM_DATE:
date = XmppDateTime.parseXEP0082Date(parser.nextText());
builder.setDate(XmppDateTime.parseXEP0082Date(parser.nextText()));
break;
case JingleFileTransferPayload.ELEM_DESC:
desc = parser.nextText();
builder.setDescription(parser.nextText());
break;
case JingleFileTransferPayload.ELEM_MEDIA_TYPE:
mediaType = parser.nextText();
builder.setMediaType(parser.nextText());
break;
case JingleFileTransferPayload.ELEM_NAME:
name = parser.nextText();
builder.setName(parser.nextText());
break;
case JingleFileTransferPayload.ELEM_SIZE:
size = Integer.parseInt(parser.nextText());
builder.setSize(Integer.parseInt(parser.nextText()));
break;
case Range.ELEMENT:
@ -103,8 +89,8 @@ public class JingleContentDescriptionFileTransferProvider
length = (lengthString != null ? Integer.parseInt(lengthString) : -1);
if (parser.isEmptyElementTag()) {
range = new Range(offset, length);
inRange = false;
builder.setRange(new Range(offset, length));
}
break;
@ -112,7 +98,7 @@ public class JingleContentDescriptionFileTransferProvider
if (inRange) {
inRangeHash = new HashElementProvider().parse(parser);
} else {
hash = new HashElementProvider().parse(parser);
builder.setHash(new HashElementProvider().parse(parser));
}
break;
}
@ -122,11 +108,12 @@ public class JingleContentDescriptionFileTransferProvider
case Range.ELEMENT:
inRange = false;
range = new Range(offset, length, inRangeHash);
builder.setRange(new Range(offset, length, inRangeHash));
inRangeHash = null;
break;
case JingleFileTransferPayload.ELEMENT:
payloads.add(new JingleFileTransferPayload(date, desc, hash, mediaType, name, size, range));
payloads.add(builder.build());
break;
case JingleContentDescriptionFileTransfer.ELEMENT:

View file

@ -0,0 +1,51 @@
/**
*
* Copyright 2017 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.jingle;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.util.StringUtils;
import org.junit.Test;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotSame;
/**
* Test JingleSession class.
*/
public class JingleSessionTest extends SmackTestSuite {
@Test
public void sessionTest() throws XmppStringprepException {
Jid romeo = JidCreate.from("romeo@montague.lit");
Jid juliet = JidCreate.from("juliet@capulet.lit");
String sid = StringUtils.randomString(24);
JingleSession s1 = new JingleSession(romeo, juliet, sid);
JingleSession s2 = new JingleSession(juliet, romeo, sid);
JingleSession s3 = new JingleSession(romeo, juliet, StringUtils.randomString(23));
JingleSession s4 = new JingleSession(juliet, romeo, sid);
assertNotSame(s1, s2);
assertNotSame(s1, s3);
assertNotSame(s2, s3);
assertEquals(s2, s4);
assertEquals(s2.hashCode(), s4.hashCode());
}
}