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:
parent
306a7bc0ff
commit
9e1f777332
3 changed files with 70 additions and 51 deletions
|
@ -90,34 +90,15 @@ public class JingleFileTransferPayload extends JingleContentDescriptionPayloadTy
|
||||||
XmlStringBuilder sb = new XmlStringBuilder(this);
|
XmlStringBuilder sb = new XmlStringBuilder(this);
|
||||||
sb.rightAngleBracket();
|
sb.rightAngleBracket();
|
||||||
|
|
||||||
if (date != null) {
|
sb.optElement(ELEM_DATE, date);
|
||||||
sb.element(ELEM_DATE, date);
|
sb.optElement(ELEM_DESC, desc);
|
||||||
}
|
sb.optElement(ELEM_MEDIA_TYPE, mediaType);
|
||||||
|
sb.optElement(ELEM_NAME, name);
|
||||||
if (desc != null) {
|
sb.optElement(range);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (size > 0) {
|
if (size > 0) {
|
||||||
sb.element(ELEM_SIZE, Integer.toString(size));
|
sb.element(ELEM_SIZE, Integer.toString(size));
|
||||||
}
|
}
|
||||||
|
sb.optElement(hash);
|
||||||
if (hash != null) {
|
|
||||||
sb.element(hash);
|
|
||||||
}
|
|
||||||
|
|
||||||
sb.closeElement(this);
|
sb.closeElement(this);
|
||||||
return sb;
|
return sb;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,14 +20,13 @@ import org.jivesoftware.smackx.hash.element.HashElement;
|
||||||
import org.jivesoftware.smackx.hash.provider.HashElementProvider;
|
import org.jivesoftware.smackx.hash.provider.HashElementProvider;
|
||||||
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionPayloadType;
|
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionPayloadType;
|
||||||
import org.jivesoftware.smackx.jingle.provider.JingleContentDescriptionProvider;
|
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.JingleContentDescriptionFileTransfer;
|
||||||
|
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferPayload;
|
||||||
import org.jivesoftware.smackx.jingle_filetransfer.element.Range;
|
import org.jivesoftware.smackx.jingle_filetransfer.element.Range;
|
||||||
import org.jxmpp.util.XmppDateTime;
|
import org.jxmpp.util.XmppDateTime;
|
||||||
import org.xmlpull.v1.XmlPullParser;
|
import org.xmlpull.v1.XmlPullParser;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import static org.xmlpull.v1.XmlPullParser.END_TAG;
|
import static org.xmlpull.v1.XmlPullParser.END_TAG;
|
||||||
import static org.xmlpull.v1.XmlPullParser.START_TAG;
|
import static org.xmlpull.v1.XmlPullParser.START_TAG;
|
||||||
|
@ -42,14 +41,8 @@ public class JingleContentDescriptionFileTransferProvider
|
||||||
|
|
||||||
boolean inRange = false;
|
boolean inRange = false;
|
||||||
|
|
||||||
Date date = null;
|
JingleFileTransferPayload.Builder builder = JingleFileTransferPayload.getBuilder();
|
||||||
String desc = null;
|
|
||||||
String mediaType = null;
|
|
||||||
String name = null;
|
|
||||||
int size = -1;
|
|
||||||
Range range = null;
|
|
||||||
HashElement inRangeHash = null;
|
HashElement inRangeHash = null;
|
||||||
HashElement hash = null;
|
|
||||||
|
|
||||||
ArrayList<JingleContentDescriptionPayloadType> payloads = new ArrayList<>();
|
ArrayList<JingleContentDescriptionPayloadType> payloads = new ArrayList<>();
|
||||||
|
|
||||||
|
@ -65,34 +58,27 @@ public class JingleContentDescriptionFileTransferProvider
|
||||||
switch (elem) {
|
switch (elem) {
|
||||||
|
|
||||||
case JingleFileTransferPayload.ELEMENT:
|
case JingleFileTransferPayload.ELEMENT:
|
||||||
date = null;
|
builder = JingleFileTransferPayload.getBuilder();
|
||||||
desc = null;
|
|
||||||
mediaType = null;
|
|
||||||
name = null;
|
|
||||||
size = -1;
|
|
||||||
range = null;
|
|
||||||
inRangeHash = null;
|
|
||||||
hash = null;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JingleFileTransferPayload.ELEM_DATE:
|
case JingleFileTransferPayload.ELEM_DATE:
|
||||||
date = XmppDateTime.parseXEP0082Date(parser.nextText());
|
builder.setDate(XmppDateTime.parseXEP0082Date(parser.nextText()));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JingleFileTransferPayload.ELEM_DESC:
|
case JingleFileTransferPayload.ELEM_DESC:
|
||||||
desc = parser.nextText();
|
builder.setDescription(parser.nextText());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JingleFileTransferPayload.ELEM_MEDIA_TYPE:
|
case JingleFileTransferPayload.ELEM_MEDIA_TYPE:
|
||||||
mediaType = parser.nextText();
|
builder.setMediaType(parser.nextText());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JingleFileTransferPayload.ELEM_NAME:
|
case JingleFileTransferPayload.ELEM_NAME:
|
||||||
name = parser.nextText();
|
builder.setName(parser.nextText());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JingleFileTransferPayload.ELEM_SIZE:
|
case JingleFileTransferPayload.ELEM_SIZE:
|
||||||
size = Integer.parseInt(parser.nextText());
|
builder.setSize(Integer.parseInt(parser.nextText()));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Range.ELEMENT:
|
case Range.ELEMENT:
|
||||||
|
@ -103,8 +89,8 @@ public class JingleContentDescriptionFileTransferProvider
|
||||||
length = (lengthString != null ? Integer.parseInt(lengthString) : -1);
|
length = (lengthString != null ? Integer.parseInt(lengthString) : -1);
|
||||||
|
|
||||||
if (parser.isEmptyElementTag()) {
|
if (parser.isEmptyElementTag()) {
|
||||||
range = new Range(offset, length);
|
|
||||||
inRange = false;
|
inRange = false;
|
||||||
|
builder.setRange(new Range(offset, length));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -112,7 +98,7 @@ public class JingleContentDescriptionFileTransferProvider
|
||||||
if (inRange) {
|
if (inRange) {
|
||||||
inRangeHash = new HashElementProvider().parse(parser);
|
inRangeHash = new HashElementProvider().parse(parser);
|
||||||
} else {
|
} else {
|
||||||
hash = new HashElementProvider().parse(parser);
|
builder.setHash(new HashElementProvider().parse(parser));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -122,11 +108,12 @@ public class JingleContentDescriptionFileTransferProvider
|
||||||
|
|
||||||
case Range.ELEMENT:
|
case Range.ELEMENT:
|
||||||
inRange = false;
|
inRange = false;
|
||||||
range = new Range(offset, length, inRangeHash);
|
builder.setRange(new Range(offset, length, inRangeHash));
|
||||||
|
inRangeHash = null;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JingleFileTransferPayload.ELEMENT:
|
case JingleFileTransferPayload.ELEMENT:
|
||||||
payloads.add(new JingleFileTransferPayload(date, desc, hash, mediaType, name, size, range));
|
payloads.add(builder.build());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JingleContentDescriptionFileTransfer.ELEMENT:
|
case JingleContentDescriptionFileTransfer.ELEMENT:
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue