001: /* ANVLRecordTest
002: *
003: * $Id: ANVLRecordTest.java 4545 2006-08-26 00:33:38Z stack-sf $
004: *
005: * Created on July 26, 2006.
006: *
007: * Copyright (C) 2006 Internet Archive.
008: *
009: * This file is part of the Heritrix web crawler (crawler.archive.org).
010: *
011: * Heritrix is free software; you can redistribute it and/or modify
012: * it under the terms of the GNU Lesser Public License as published by
013: * the Free Software Foundation; either version 2.1 of the License, or
014: * any later version.
015: *
016: * Heritrix is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: * GNU Lesser Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser Public License
022: * along with Heritrix; if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: */
025: package org.archive.util.anvl;
026:
027: import java.io.ByteArrayInputStream;
028: import java.io.IOException;
029: import java.io.UnsupportedEncodingException;
030: import java.util.Map;
031:
032: import junit.framework.TestCase;
033:
034: public class ANVLRecordTest extends TestCase {
035: public void testAdd() throws Exception {
036: ANVLRecord am = new ANVLRecord();
037: am.add(new Element(new Label("entry")));
038: am.add(new Element(new Label("who"), new Value(
039: "Gilbert, W.S. | Sullivan, Arthur")));
040: am.add(new Element(new Label("what"), new Value(
041: "\rThe Yeoman of \rthe guard")));
042: am.add(new Element(new Label("what"), new Value(
043: "The Yeoman of\r\n the guard")));
044: am.add(new Element(new Label("what"), new Value(
045: "The Yeoman of \n\tthe guard")));
046: am.add(new Element(new Label("what"), new Value(
047: "The Yeoman of \r the guard")));
048: am
049: .add(new Element(new Label("when/created"), new Value(
050: "1888")));
051: System.out.println(am.toString());
052: Map m = am.asMap();
053: System.out.println(m.toString());
054: }
055:
056: public void testEmptyRecord() throws Exception {
057: byte[] b = ANVLRecord.EMPTY_ANVL_RECORD.getUTF8Bytes();
058: assertEquals(b.length, 2);
059: assertEquals(b[0], '\r');
060: assertEquals(b[1], '\n');
061: }
062:
063: public void testFolding() throws Exception {
064: ANVLRecord am = new ANVLRecord();
065: Exception e = null;
066: try {
067: am.addLabel("Label with \n in it");
068: } catch (IllegalArgumentException iae) {
069: e = iae;
070: }
071: assertTrue(e != null && e instanceof IllegalArgumentException);
072: am.addLabelValue("label", "value with \n in it");
073: }
074:
075: public void testParse() throws UnsupportedEncodingException,
076: IOException {
077: String record = " a: b\r\n#c#\r\nc:d\r\n \t\t\r\t\n\te"
078: + "\r\nx:\r\n # z\r\n\r\n";
079: ANVLRecord r = ANVLRecord.load(new ByteArrayInputStream(record
080: .getBytes("ISO-8859-1")));
081: System.out.println(r);
082: assertEquals(r.get(0).toString(), "a: b");
083: record = " a: b\r\n\r\nsdfsdsdfds";
084: r = ANVLRecord.load(new ByteArrayInputStream(record
085: .getBytes("ISO-8859-1")));
086: System.out.println(r);
087: record = "x:\r\n # z\r\ny:\r\n\r\n";
088: r = ANVLRecord.load(new ByteArrayInputStream(record
089: .getBytes("ISO-8859-1")));
090: System.out.println(r);
091: assertEquals(r.get(0).toString(), "x:");
092: }
093:
094: public void testExampleParse() throws UnsupportedEncodingException,
095: IOException {
096: final String sample = "entry:\t\t\r\n# first ###draft\r\n"
097: + "who:\tGilbert, W.S. | Sullivan, Arthur\r\n"
098: + "what:\tThe Yeoman of\r\n" + "\t\tthe Guard\r\n"
099: + "when/created:\t 1888\r\n\r\n";
100: ANVLRecord r = ANVLRecord.load(new ByteArrayInputStream(sample
101: .getBytes("ISO-8859-1")));
102: System.out.println(r);
103: }
104:
105: public void testPoundLabel() throws UnsupportedEncodingException,
106: IOException {
107: final String sample = "ent#ry:\t\t\r\n# first ###draft\r\n"
108: + "who:\tGilbert, W.S. | Sullivan, Arthur\r\n"
109: + "what:\tThe Yeoman of\r\n" + "\t\tthe Guard\r\n"
110: + "when/created:\t 1888\r\n\r\n";
111: ANVLRecord r = ANVLRecord.load(sample);
112: System.out.println(r);
113: }
114:
115: public void testNewlineLabel() throws UnsupportedEncodingException,
116: IOException {
117: final String sample = "ent\nry:\t\t\r\n# first ###draft\r\n"
118: + "who:\tGilbert, W.S. | Sullivan, Arthur\r\n"
119: + "what:\tThe Yeoman of\r\n" + "\t\tthe Guard\r\n"
120: + "when/created:\t 1888\r\n\r\n";
121: IllegalArgumentException iae = null;
122: try {
123: ANVLRecord.load(sample);
124: } catch (IllegalArgumentException e) {
125: iae = e;
126: }
127: assertTrue(iae != null);
128: }
129: }
|