KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > util > anvl > ANVLRecordTest


1 /* ANVLRecordTest
2 *
3 * $Id: ANVLRecordTest.java,v 1.6 2006/08/26 00:33:38 stack-sf Exp $
4 *
5 * Created on July 26, 2006.
6 *
7 * Copyright (C) 2006 Internet Archive.
8 *
9 * This file is part of the Heritrix web crawler (crawler.archive.org).
10 *
11 * Heritrix is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * any later version.
15 *
16 * Heritrix is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser Public License
22 * along with Heritrix; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */

25 package org.archive.util.anvl;
26
27 import java.io.ByteArrayInputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.UnsupportedEncodingException JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import junit.framework.TestCase;
33
34 public class ANVLRecordTest extends TestCase {
35     public void testAdd() throws Exception JavaDoc {
36         ANVLRecord am = new ANVLRecord();
37         am.add(new Element(new Label("entry")));
38         am.add(new Element(new Label("who"),
39             new Value("Gilbert, W.S. | Sullivan, Arthur")));
40         am.add(new Element(new Label("what"),
41                 new Value("\rThe Yeoman of \rthe guard")));
42         am.add(new Element(new Label("what"),
43             new Value("The Yeoman of\r\n the guard")));
44         am.add(new Element(new Label("what"),
45                 new Value("The Yeoman of \n\tthe guard")));
46         am.add(new Element(new Label("what"),
47                 new Value("The Yeoman of \r the guard")));
48         am.add(new Element(new Label("when/created"),
49             new Value("1888")));
50         System.out.println(am.toString());
51         Map JavaDoc m = am.asMap();
52         System.out.println(m.toString());
53     }
54     
55     public void testEmptyRecord() throws Exception JavaDoc {
56         byte [] b = ANVLRecord.EMPTY_ANVL_RECORD.getUTF8Bytes();
57         assertEquals(b.length, 2);
58         assertEquals(b[0], '\r');
59         assertEquals(b[1], '\n');
60     }
61     
62     public void testFolding() throws Exception JavaDoc {
63         ANVLRecord am = new ANVLRecord();
64         Exception JavaDoc e = null;
65         try {
66             am.addLabel("Label with \n in it");
67         } catch (IllegalArgumentException JavaDoc iae) {
68             e = iae;
69         }
70         assertTrue(e != null && e instanceof IllegalArgumentException JavaDoc);
71         am.addLabelValue("label", "value with \n in it");
72     }
73     
74     public void testParse() throws UnsupportedEncodingException JavaDoc, IOException JavaDoc {
75         String JavaDoc record = " a: b\r\n#c#\r\nc:d\r\n \t\t\r\t\n\te" +
76                 "\r\nx:\r\n # z\r\n\r\n";
77         ANVLRecord r = ANVLRecord.load(new ByteArrayInputStream JavaDoc(
78                 record.getBytes("ISO-8859-1")));
79         System.out.println(r);
80         assertEquals(r.get(0).toString(), "a: b");
81         record = " a: b\r\n\r\nsdfsdsdfds";
82         r = ANVLRecord.load(new ByteArrayInputStream JavaDoc(
83             record.getBytes("ISO-8859-1")));
84         System.out.println(r);
85         record = "x:\r\n # z\r\ny:\r\n\r\n";
86         r = ANVLRecord.load(new ByteArrayInputStream JavaDoc(
87             record.getBytes("ISO-8859-1")));
88         System.out.println(r);
89         assertEquals(r.get(0).toString(), "x:");
90     }
91     
92     public void testExampleParse()
93     throws UnsupportedEncodingException JavaDoc, IOException JavaDoc {
94         final String JavaDoc sample = "entry:\t\t\r\n# first ###draft\r\n" +
95             "who:\tGilbert, W.S. | Sullivan, Arthur\r\n" +
96             "what:\tThe Yeoman of\r\n" +
97             "\t\tthe Guard\r\n" +
98             "when/created:\t 1888\r\n\r\n";
99         ANVLRecord r = ANVLRecord.load(new ByteArrayInputStream JavaDoc(
100                 sample.getBytes("ISO-8859-1")));
101         System.out.println(r);
102     }
103     
104     public void testPoundLabel()
105     throws UnsupportedEncodingException JavaDoc, IOException JavaDoc {
106         final String JavaDoc sample = "ent#ry:\t\t\r\n# first ###draft\r\n" +
107             "who:\tGilbert, W.S. | Sullivan, Arthur\r\n" +
108             "what:\tThe Yeoman of\r\n" +
109             "\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()
116     throws UnsupportedEncodingException JavaDoc, IOException JavaDoc {
117         final String JavaDoc 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" +
120             "\t\tthe Guard\r\n" +
121             "when/created:\t 1888\r\n\r\n";
122         IllegalArgumentException JavaDoc iae = null;
123         try {
124             ANVLRecord.load(sample);
125         } catch(IllegalArgumentException JavaDoc e) {
126             iae = e;
127         }
128         assertTrue(iae != null);
129     }
130 }
131
Popular Tags