Skip to content

Commit

Permalink
Support output of vcard list
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Sep 25, 2024
1 parent df6d709 commit 6cdcdd5
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 18 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ dependencies {
// spock
testImplementation platform("org.spockframework:spock-bom:$spockVersion"),
"org.spockframework:spock-core",
"org.apache.logging.log4j:log4j-core:$log4jVersion"
"org.apache.logging.log4j:log4j-core:$log4jVersion",
"org.apache.logging.log4j:log4j-slf4j2-impl:$log4jVersion"

testRuntimeOnly("org.mnode.ical4j:ical4j:$ical4jVersion") {
capabilities {
Expand Down
11 changes: 5 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
ical4jVersion=4.0.3

log4jVersion=2.22.1
commonsIoVersion=2.15.1
groovyVersion=3.0.20
ical4jVersion=4.0.4
log4jVersion=2.23.1
commonsIoVersion=2.16.1
groovyVersion=3.0.22
bndVersion=6.3.1
junitVintageVersion=5.10.2
junitVintageVersion=5.10.3
spockVersion=2.4-M1-groovy-3.0

jacoco_htmlReport=true
Expand Down
33 changes: 31 additions & 2 deletions src/main/java/net/fortuna/ical4j/vcard/VCardOutputter.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public VCardOutputter(boolean validating, int foldLength) {
}

/**
* Outputs an iCalender string to the specified output stream.
* Outputs a vCard string to the specified output stream.
* @param card a vCard object to output as a string
* @param out an output stream the output stream to write the vCard string to
* @throws IOException thrown when unable to write to output stream
Expand All @@ -86,7 +86,7 @@ public final void output(final VCard card, final OutputStream out) throws IOExce
}

/**
* Outputs an iCalender string to the specified writer.
* Outputs an vCard string to the specified writer.
* @param card a vCard object to output as a string
* @param out a writer to write the output string to
* @throws IOException thrown when unable to write to writer
Expand All @@ -102,4 +102,33 @@ public final void output(final VCard card, final Writer out) throws IOException,
}
}

/**
* Outputs a vCard string to the specified output stream.
*
* @param cards a vCard object to output as a string
* @param out an output stream the output stream to write the vCard string to
* @throws IOException thrown when unable to write to output stream
* @throws ValidationException where the specified vCard is not valid
*/
public final void output(final VCardList cards, final OutputStream out) throws IOException, ValidationException {
output(cards, new OutputStreamWriter(out, DEFAULT_CHARSET));
}

/**
* Outputs an vCard string to the specified writer.
*
* @param cards a vCard object to output as a string
* @param out a writer to write the output string to
* @throws IOException thrown when unable to write to writer
* @throws ValidationException where the specified vCard is not valid
*/
public final void output(final VCardList cards, final Writer out) throws IOException, ValidationException {
if (isValidating()) {
cards.getAll().forEach(VCard::validate);
}

try (FoldingWriter writer = new FoldingWriter(out, foldLength)) {
writer.write(cards.toString());
}
}
}
41 changes: 41 additions & 0 deletions src/test/groovy/net/fortuna/ical4j/vcard/VCardOutputterSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package net.fortuna.ical4j.vcard

import spock.lang.Specification

class VCardOutputterSpec extends Specification {

def 'test output of vcard list'() {
given: 'a list of cards'
def list = new VCardBuilder(getClass().getResourceAsStream('/samples/vcard-rfc2426.vcf'))
.buildAll()

expect: 'string output matches expected'
StringWriter w = []
new VCardOutputter(true).output(list, w)
w as String == '''BEGIN:VCARD\r
VERSION:3.0\r
FN:Frank Dawson\r
N:Dawson;Frank;;;\r
ORG:Lotus Development Corporation\r
ADR;TYPE=WORK,POSTAL,PARCEL:;;6544 Battleford Drive;Raleigh;NC;27613-3502\r
;U.S.A.;\r
TEL;TYPE=VOICE,MSG,WORK:+1-919-676-9515\r
TEL;TYPE=FAX,WORK:+1-919-676-9564\r
EMAIL;TYPE=INTERNET,PREF:[email protected]\r
EMAIL;TYPE=INTERNET:[email protected]\r
URL:http://home.earthlink.net/~fdawson\r
END:VCARD\r
\r
BEGIN:VCARD\r
VERSION:3.0\r
FN:Tim Howes\r
N:Howes;Tim;;;\r
ORG:Netscape Communications Corp.\r
ADR;TYPE=WORK:;;501 E. Middlefield Rd.;Mountain View;CA; 94043;U.S.A.;\r
TEL;TYPE=VOICE,MSG,WORK:+1-415-937-3419\r
TEL;TYPE=FAX,WORK:+1-415-528-4164\r
EMAIL;TYPE=INTERNET:[email protected]\r
END:VCARD\r
'''
}
}
17 changes: 8 additions & 9 deletions src/test/java/net/fortuna/ical4j/vcard/VCardOutputterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static junit.framework.Assert.assertEquals;

Expand Down Expand Up @@ -74,24 +73,24 @@ public VCardOutputterTest(VCardOutputter outputter, VCard card, String expectedO

@Test
public void testOutput() throws IOException, ValidationException {
StringWriter out = new StringWriter();
var out = new StringWriter();
outputter.output(card, out);
assertEquals(expectedOutput, out.toString().replaceAll("\\r\\n ", ""));
}

@Parameters
public static Collection<Object[]> parameters() throws IOException, ParserException {
VCardOutputter outputter = new VCardOutputter(false, 1000);
var outputter = new VCardOutputter(false, 1000);
VCardBuilder builder = null;
List<Object[]> params = new ArrayList<Object[]>();
File[] testFiles = new File("src/test/resources/samples/valid").listFiles(
var params = new ArrayList<Object[]>();
var testFiles = new File("src/test/resources/samples/valid").listFiles(
(FileFilter) VCardFileFilter.INSTANCE);
// enable relaxed parsing for non-standard GEO support..
CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING, true);
for (int i = 0; i < testFiles.length; i++) {
builder = new VCardBuilder(new FileReader(testFiles[i]));
VCard card = builder.build();
params.add(new Object[] {outputter, card, card.toString()});
for (var testFile : testFiles) {
builder = new VCardBuilder(new FileReader(testFile));
var card = builder.build();
params.add(new Object[]{outputter, card, card.toString()});
}
CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING, false);
return params;
Expand Down

0 comments on commit 6cdcdd5

Please sign in to comment.