thedevstack-smack-extensions/test/de/thedevstack/smackx/filetransferhttp/provider/FileListProviderTest.java

117 lines
4.1 KiB
Java

package de.thedevstack.smackx.filetransferhttp.provider;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import de.thedevstack.smackx.filetransferhttp.element.FileList;
import de.thedevstack.smackx.filetransferhttp.element.RemoteFile;
import de.thedevstack.smackx.filetransferhttp.element.RemoteFileInfo;
public class FileListProviderTest {
@Test
public void testParseRemoteFileInfo() throws Exception {
String xml = "<file-info>"
+ "<filename>my_juliet.png</filename>"
+ "<size>23456</size>"
+ "<content-type>image/png</content-type>"
+ "</file-info>";
XmlPullParser parser = PacketParserUtils.getParserFor(xml);
FileListProvider provider = new FileListProvider();
RemoteFileInfo info = provider.parseRemoteFileInfo(parser, 0);
assertNotNull(info);
assertEquals("my_juliet.png", info.getFilename());
assertEquals(23456, info.getSize());
assertEquals("image/png", info.getContentType());
}
@Test
public void testParseRemoteFileInfoWithMissingChildElements() throws Exception {
String xml = "<file-info>"
+ "<size>23456</size>"
+ "<content-type>image/png</content-type>"
+ "</file-info>";
XmlPullParser parser = PacketParserUtils.getParserFor(xml);
FileListProvider provider = new FileListProvider();
RemoteFileInfo info = provider.parseRemoteFileInfo(parser, 0);
assertNull(info);
xml = "<file-info>"
+ "<filename>my_juliet.png</filename>"
+ "<content-type>image/png</content-type>"
+ "</file-info>";
parser = PacketParserUtils.getParserFor(xml);
provider = new FileListProvider();
info = provider.parseRemoteFileInfo(parser, 0);
assertNull(info);
xml = "<file-info>"
+ "<filename>my_juliet.png</filename>"
+ "<size>23456</size>"
+ "</file-info>";
parser = PacketParserUtils.getParserFor(xml);
provider = new FileListProvider();
info = provider.parseRemoteFileInfo(parser, 0);
assertNotNull(info);
assertEquals("my_juliet.png", info.getFilename());
assertEquals(23456, info.getSize());
assertNull(info.getContentType());
}
@Test
public void testParseRemoteFile() throws XmlPullParserException, IOException {
String xml = "<file timestamp='1234567890' to='julia@capulet.tld' from='romeo@montague.tld'>"
+ "<file-info>"
+ "<filename>my_juliet.png</filename>"
+ "<size>23456</size>"
+ "<content-type>image/png</content-type>"
+ "</file-info>"
+ "</file>";
XmlPullParser parser = PacketParserUtils.getParserFor(xml);
FileListProvider provider = new FileListProvider();
RemoteFile file = provider.parseRemoteFile(parser, 0);
assertNotNull(file);
assertEquals(1234567890, file.getTimestamp());
assertEquals("my_juliet.png", file.getFilename());
assertEquals(23456, file.getSize());
assertEquals("image/png", file.getContentType());
assertEquals("julia@capulet.tld", file.getTo());
assertEquals("romeo@montague.tld", file.getFrom());
}
@Test
public void testParse() throws Exception {
String xml = "<list>"
+ "<file timestamp='1234567890' to='julia@capulet.tld' from='romeo@montague.tld'>"
+ "<file-info>"
+ "<filename>my_juliet.png</filename>"
+ "<size>23456</size>"
+ "<content-type>image/png</content-type>"
+ "</file-info>"
+ "</file>"
+ "</list>";
XmlPullParser parser = PacketParserUtils.getParserFor(xml);
FileListProvider provider = new FileListProvider();
FileList fileList = provider.parse(parser, 0);
assertNotNull(fileList);
assertNotNull(fileList.getFiles());
assertEquals(1, fileList.getFiles().size());
RemoteFile file = fileList.getFiles().get(0);
assertEquals(1234567890, file.getTimestamp());
assertEquals("my_juliet.png", file.getFilename());
assertEquals(23456, file.getSize());
assertEquals("image/png", file.getContentType());
assertEquals("julia@capulet.tld", file.getTo());
assertEquals("romeo@montague.tld", file.getFrom());
}
}