Commit 788eedf4 authored by Ricki Hirner's avatar Ricki Hirner 🐑

move some tests to local unit tests; reduce Android dependencies

parent 95a27610
......@@ -36,6 +36,7 @@ android {
dependencies {
compile 'org.apache.commons:commons-lang3:3.4'
provided 'org.projectlombok:lombok:1.16.10'
// ez-vcard to parse/generate VCards
compile('com.googlecode.ez-vcard:ez-vcard:0.9.11') {
// hCard functionality not needed
......@@ -44,6 +45,8 @@ dependencies {
// jCard functionality not needed
exclude group: 'com.fasterxml.jackson.core'
}
testCompile 'junit:junit:4.12'
}
// grant permissions for unit tests
......
#!/bin/sh
#../gradlew -i check connectedCheck
echo
echo View lint report:
echo -n file://
realpath build/outputs/lint-results-debug.html
echo
echo View local unit test reports:
echo -n file://
realpath build/reports/tests/testDebugUnitTest/debug/index.html
echo -n file://
realpath build/reports/tests/testReleaseUnitTest/release/index.html
echo
echo "View connected unit test reports (debug):"
echo -n file://
realpath build/reports/androidTests/connected/index.html
......@@ -17,6 +17,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.LinkedList;
import java.util.List;
......@@ -212,11 +214,11 @@ public class Contact {
// N
StructuredName n = vCard.getStructuredName();
if (n != null) {
c.prefix = TextUtils.join(" ", n.getPrefixes());
c.prefix = StringUtils.join(n.getPrefixes(), ' ');
c.givenName = n.getGiven();
c.middleName = TextUtils.join(" ", n.getAdditionalNames());
c.middleName = StringUtils.join(n.getAdditionalNames(), ' ');
c.familyName = n.getFamily();
c.suffix = TextUtils.join(" ", n.getSuffixes());
c.suffix = StringUtils.join(n.getSuffixes(), ' ');
vCard.removeProperties(StructuredName.class);
} else
Constants.log.warning("Received VCard without N (structured name)");
......@@ -291,7 +293,7 @@ public class Contact {
for (Note note : vCard.getNotes())
notes.add(note.getValue());
if (!notes.isEmpty())
c.note = TextUtils.join("\n\n\n", notes);
c.note = StringUtils.join(notes, "\n\n\n");
vCard.removeProperties(Note.class);
// CATEGORY
......@@ -315,7 +317,7 @@ public class Contact {
// RELATED
for (Related related : vCard.getRelations()) {
String text = related.getText();
if (!TextUtils.isEmpty(text)) {
if (!StringUtils.isEmpty(text)) {
// process only free-form relations with text
c.relations.add(related);
}
......@@ -405,7 +407,7 @@ public class Contact {
fn = emails.get(0).property.getValue();
Constants.log.warning("No FN (formatted name) available, using " + fn);
}
if (TextUtils.isEmpty(fn)) {
if (StringUtils.isEmpty(fn)) {
fn = "-";
Constants.log.warning("No FN (formatted name) available, using \"-\"");
}
......@@ -415,15 +417,15 @@ public class Contact {
if (prefix != null || familyName != null || middleName != null || givenName != null || suffix != null) {
StructuredName n = new StructuredName();
if (prefix != null)
for (String p : TextUtils.split(prefix, " "))
for (String p : StringUtils.split(prefix, ' '))
n.getPrefixes().add(p);
n.setGiven(givenName);
if (middleName != null)
for (String middle : TextUtils.split(middleName, " "))
for (String middle : StringUtils.split(middleName, ' '))
n.getAdditionalNames().add(middle);
n.setFamily(familyName);
if (suffix != null)
for (String s : TextUtils.split(suffix, " "))
for (String s : StringUtils.split(suffix, ' '))
n.getSuffixes().add(s);
vCard.setStructuredName(n);
......@@ -542,14 +544,20 @@ public class Contact {
}
public static String uriToUID(String uriString) {
Uri uri = Uri.parse(uriString);
if (uri.getScheme() == null)
return uri.getSchemeSpecificPart();
else if ("urn".equalsIgnoreCase(uri.getScheme()) && StringUtils.startsWithIgnoreCase(uri.getSchemeSpecificPart(), "uuid:"))
return uri.getSchemeSpecificPart().substring(5);
else
return null;
URI uri = null;
try {
uri = new URI(uriString);
if (uri.getScheme() == null)
return uri.getSchemeSpecificPart();
else if ("urn".equalsIgnoreCase(uri.getScheme()) && StringUtils.startsWithIgnoreCase(uri.getSchemeSpecificPart(), "uuid:"))
return uri.getSchemeSpecificPart().substring(5);
else
return null;
} catch(URISyntaxException e) {
Constants.log.warning("Invalid URI for UID: " + uri);
return uriString;
}
}
private static void addLabel(LabeledProperty<? extends VCardProperty> labeledUrl, AtomicInteger labelIterator, VCard vCard) {
......
/*
* Copyright © 2013 – 2015 Ricki Hirner (bitfire web engineering).
* Copyright © Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
......@@ -8,9 +8,7 @@
package at.bitfire.vcard4android;
import android.content.res.AssetManager;
import android.test.InstrumentationTestCase;
import android.util.Log;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
......@@ -18,7 +16,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Locale;
import java.util.logging.Level;
......@@ -37,16 +34,25 @@ import ezvcard.property.Url;
import ezvcard.util.IOUtils;
import lombok.Cleanup;
public class ContactTest extends InstrumentationTestCase {
private static final String TAG = "vcard4android.ContactTest";
import static org.junit.Assert.*;
AssetManager assetMgr;
public class ContactTest {
private Contact parseContact(String fname, Charset charset) throws IOException {
@Cleanup InputStream is = getClass().getClassLoader().getResourceAsStream(fname);
assertNotNull(is);
return Contact.fromStream(is, charset, null)[0];
}
public void setUp() throws IOException {
assetMgr = getInstrumentation().getContext().getResources().getAssets();
private Contact regenerate(Contact c, VCardVersion vCardVersion) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
c.write(vCardVersion, GroupMethod.CATEGORIES, true, os);
Constants.log.log(Level.INFO, "Re-generated VCard", os.toString());
return Contact.fromStream(new ByteArrayInputStream(os.toByteArray()), null, null)[0];
}
@Test
public void testVCard3FieldsAsVCard3() throws IOException {
Contact c = regenerate(parseContact("allfields-vcard3.vcf", null), VCardVersion.V3_0);
......@@ -58,7 +64,7 @@ public class ContactTest extends InstrumentationTestCase {
// N
assertEquals("Firstname", c.givenName);
assertEquals("Middlename", c.middleName);
assertEquals("Middlename1 Middlename2", c.middleName);
assertEquals("Lastname", c.familyName);
// phonetic names
......@@ -94,10 +100,10 @@ public class ContactTest extends InstrumentationTestCase {
assertEquals("[email protected]", email.property.getValue());
// ORG, TITLE, ROLE
assertTrue(Arrays.equals(
new String[]{"ABC, Inc.", "North American Division", "Marketing"},
assertArrayEquals(
new String[] { "ABC, Inc.", "North American Division", "Marketing" },
c.organization.getValues().toArray(new String[3])
));
);
assertEquals("Director, Research and Development", c.jobTitle);
assertEquals("Programmer", c.jobDescription);
......@@ -122,7 +128,10 @@ public class ContactTest extends InstrumentationTestCase {
assertEquals("[email protected]", impp.property.getHandle());
// NICKNAME
assertTrue(Arrays.equals(new String[] { "Nick1", "Nick2" }, c.nickName.getValues().toArray()));
assertArrayEquals(
new String[] { "Nick1", "Nick2" },
c.nickName.getValues().toArray()
);
// ADR
assertEquals(2, c.addresses.size());
......@@ -155,10 +164,10 @@ public class ContactTest extends InstrumentationTestCase {
assertEquals("This fax number is operational 0800 to 1715 EST, Mon-Fri.\n\n\nSecond note", c.note);
// CATEGORIES
assertTrue(Arrays.equals(
assertArrayEquals(
new String[] { "A", "B'C" },
c.categories.toArray()
));
);
// URL
assertEquals(2, c.urls.size());
......@@ -188,10 +197,11 @@ public class ContactTest extends InstrumentationTestCase {
assertEquals("muuuum", rel.getText());
// PHOTO
@Cleanup InputStream photo = assetMgr.open("lol.jpg");
assertTrue(Arrays.equals(IOUtils.toByteArray(photo), c.photo));
@Cleanup InputStream photo = getClass().getClassLoader().getResourceAsStream("lol.jpg");
assertArrayEquals(IOUtils.toByteArray(photo), c.photo);
}
@Test
public void testVCard3FieldsAsVCard4() throws IOException {
Contact c = regenerate(parseContact("allfields-vcard3.vcf", null), VCardVersion.V4_0);
// let's check only things that should be different when VCard 4.0 is generated
......@@ -213,17 +223,5 @@ public class ContactTest extends InstrumentationTestCase {
assertNotNull(addr.getPref());
}
private Contact parseContact(String fname, Charset charset) throws IOException {
@Cleanup InputStream is = assetMgr.open(fname, AssetManager.ACCESS_STREAMING);
return Contact.fromStream(is, charset, null)[0];
}
private Contact regenerate(Contact c, VCardVersion vCardVersion) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
c.write(vCardVersion, GroupMethod.CATEGORIES, true, os);
Constants.log.log(Level.INFO, "Re-generated VCard", os.toString());
return Contact.fromStream(new ByteArrayInputStream(os.toByteArray()), null, null)[0];
}
}
......@@ -2,7 +2,7 @@ BEGIN:VCARD
VERSION:3.0
UID:[email protected]
FN;CHARSET=UTF-8:Ämi Display
N:Lastname;Firstname;Middlename;DDDr.;MBA
N:Lastname;Firstname;Middlename1 Middlename2;DDDr.;MBA
X-PHONETIC-FIRST-NAME;CHARSET=UTF-8:Förstnehm
X-PHONETIC-MIDDLE-NAME:Mittelnehm
X-PHONETIC-LAST-NAME;CHARSET=UTF-8:Laastnehm
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment