use of ezvcard.io.LuckyNumProperty in project ez-vcard by mangstadt.
the class XCardDocumentTest method add_extendedTypes.
@Test
public void add_extendedTypes() throws Throwable {
VCard vcard = new VCard();
// contains marshal methods and QName
LuckyNumProperty num = new LuckyNumProperty(24);
vcard.addProperty(num);
// contains marshal methods, but does not have a QName
SalaryProperty salary = new SalaryProperty(1000000);
vcard.addProperty(salary);
// does not contain marshal methods nor QName
AgeProperty age = new AgeProperty(22);
vcard.addProperty(age);
XCardDocument xcard = new XCardDocument();
XCardDocumentStreamWriter writer = xcard.writer();
writer.setAddProdId(false);
writer.registerScribe(new LuckyNumScribe());
writer.registerScribe(new SalaryScribe());
writer.registerScribe(new AgeScribe());
writer.write(vcard);
Document actual = xcard.getDocument();
// @formatter:off
String xml = "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + "<vcard>" + "<a:lucky-num xmlns:a=\"http://luckynum.com\">24</a:lucky-num>" + "<x-salary>1000000</x-salary>" + "<x-age><unknown>22</unknown></x-age>" + "</vcard>" + "</vcards>";
Document expected = XmlUtils.toDocument(xml);
// @formatter:on
assertXMLEqual(XmlUtils.toString(actual), expected, actual);
}
use of ezvcard.io.LuckyNumProperty in project ez-vcard by mangstadt.
the class HCardParserTest method registerExtendedProperty.
@Test
public void registerExtendedProperty() throws Exception {
// @formatter:off
String html = "<html>" + "<body>" + "<div class=\"vcard\">" + "<span class=\"x-lucky-num\">24</span>" + "<span class=\"x-lucky-num\">22</span>" + "<span class=\"x-gender\">male</span>" + "</div>" + "</body>" + "</html>";
// @formatter:on
HCardParser parser = new HCardParser(html);
parser.registerScribe(new LuckyNumScribe());
VCard vcard = parser.readNext();
assertVersion(V3_0, vcard);
assertPropertyCount(3, vcard);
// read a type that has a type class
List<LuckyNumProperty> luckyNumTypes = vcard.getProperties(LuckyNumProperty.class);
assertEquals(2, luckyNumTypes.size());
assertEquals(24, luckyNumTypes.get(0).luckyNum);
assertEquals(22, luckyNumTypes.get(1).luckyNum);
// read a type without a type class
assertEquals("male", vcard.getExtendedProperty("X-GENDER").getValue());
assertParseWarnings(parser);
assertNoMoreVCards(parser);
}
use of ezvcard.io.LuckyNumProperty in project ez-vcard by mangstadt.
the class VCardReaderTest method extended_properties.
@Test
public void extended_properties() throws Exception {
for (VCardVersion version : VCardVersion.values()) {
// @formatter:off
String str = "BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "X-LUCKY-NUM:24\r\n" + "X-GENDER:ma\\,le\r\n" + "X-LUCKY-NUM:22\r\n" + "END:VCARD\r\n";
// @formatter:on
VCardReader reader = new VCardReader(str);
reader.registerScribe(new LuckyNumScribe());
VCard vcard = reader.readNext();
assertVersion(version, vcard);
assertPropertyCount(3, vcard);
// read a type that has a type class
List<LuckyNumProperty> luckyNumTypes = vcard.getProperties(LuckyNumProperty.class);
assertEquals(2, luckyNumTypes.size());
assertEquals(24, luckyNumTypes.get(0).luckyNum);
assertEquals(22, luckyNumTypes.get(1).luckyNum);
assertTrue(vcard.getExtendedProperties("X-LUCKY-NUM").isEmpty());
// read a type without a type class
List<RawProperty> genderTypes = vcard.getExtendedProperties("X-GENDER");
assertEquals(1, genderTypes.size());
// raw type values are not unescaped
assertEquals("ma\\,le", genderTypes.get(0).getValue());
assertParseWarnings(reader);
assertNoMoreVCards(reader);
}
}
use of ezvcard.io.LuckyNumProperty in project ez-vcard by mangstadt.
the class XCardReaderTest method read_non_standard_properties.
@Test
public void read_non_standard_properties() throws Exception {
// @formatter:off
String xml = "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + "<vcard>" + // expected: XML property
"<foo xmlns=\"http://example.com\">bar</foo>" + // expected: LuckyNumProperty
"<a:lucky-num xmlns:a=\"http://luckynum.com\"><a:num>21</a:num></a:lucky-num>" + // expected: SalaryProperty
"<x-salary><integer>1000000</integer></x-salary>" + // expected: AgeProperty (should be unmarshalled using the default parseXml implementation)
"<x-age><integer>24</integer></x-age>" + // expected: RawProperty
"<x-gender><text>m</text></x-gender>" + // expected: MyFormattedNameProperty
"<fn><name>John Doe</name></fn>" + "</vcard>" + "</vcards>";
// @formatter:on
XCardReader reader = new XCardReader(xml);
reader.registerScribe(new LuckyNumScribe());
reader.registerScribe(new SalaryScribe());
reader.registerScribe(new AgeScribe());
reader.registerScribe(new MyFormattedNameScribe());
{
VCard vcard = reader.readNext();
assertVersion(V4_0, vcard);
assertPropertyCount(6, vcard);
{
Iterator<Xml> xmlIt = vcard.getXmls().iterator();
Xml xmlType = xmlIt.next();
assertXMLEqual(XmlUtils.toDocument("<foo xmlns=\"http://example.com\">bar</foo>"), xmlType.getValue());
assertFalse(xmlIt.hasNext());
}
LuckyNumProperty luckyNum = vcard.getProperty(LuckyNumProperty.class);
assertEquals(21, luckyNum.luckyNum);
SalaryProperty salary = vcard.getProperty(SalaryProperty.class);
assertEquals(1000000, salary.salary);
AgeProperty age = vcard.getProperty(AgeProperty.class);
assertEquals(24, age.age);
RawProperty gender = vcard.getExtendedProperty("X-GENDER");
assertEquals(VCardDataType.TEXT, gender.getDataType());
assertEquals("m", gender.getValue());
MyFormattedNameProperty fn = vcard.getProperty(MyFormattedNameProperty.class);
assertEquals("JOHN DOE", fn.value);
assertParseWarnings(reader);
}
assertNoMoreVCards(reader);
}
use of ezvcard.io.LuckyNumProperty in project ez-vcard by mangstadt.
the class XCardWriterTest method write_extended_properties.
@Test
public void write_extended_properties() throws Exception {
writer.registerScribe(new LuckyNumScribe());
writer.registerScribe(new SalaryScribe());
writer.registerScribe(new AgeScribe());
VCard vcard = new VCard();
// contains marshal methods and QName
LuckyNumProperty num = new LuckyNumProperty(24);
vcard.addProperty(num);
// contains marshal methods, but does not have a QName
SalaryProperty salary = new SalaryProperty(1000000);
vcard.addProperty(salary);
// does not contain marshal methods nor QName
AgeProperty age = new AgeProperty(22);
vcard.addProperty(age);
writer.write(vcard);
writer.close();
// @formatter:off
String expected = "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + "<vcard>" + "<a:lucky-num xmlns:a=\"http://luckynum.com\">24</a:lucky-num>" + "<x-salary>1000000</x-salary>" + "<x-age><unknown>22</unknown></x-age>" + "</vcard>" + "</vcards>";
// @formatter:on
assertOutput(expected);
}
Aggregations