View Javadoc

1   /* ANVLRecordTest
2   *
3   * $Id: ANVLRecordTest.java 4545 2006-08-26 00:33:38Z stack-sf $
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;
28  import java.io.IOException;
29  import java.io.UnsupportedEncodingException;
30  import java.util.Map;
31  
32  import junit.framework.TestCase;
33  
34  public class ANVLRecordTest extends TestCase {
35      public void testAdd() throws Exception {
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 m = am.asMap();
52          System.out.println(m.toString());
53      }
54      
55      public void testEmptyRecord() throws Exception {
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 {
63          ANVLRecord am = new ANVLRecord();
64          Exception e = null;
65          try {
66              am.addLabel("Label with \n in it");
67          } catch (IllegalArgumentException iae) {
68              e = iae;
69          }
70          assertTrue(e != null && e instanceof IllegalArgumentException);
71          am.addLabelValue("label", "value with \n in it");
72      }
73      
74      public void testParse() throws UnsupportedEncodingException, IOException {
75          String 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(
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(
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(
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, IOException {
94      	final String 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(
100         		sample.getBytes("ISO-8859-1")));
101         System.out.println(r);
102     }
103     
104     public void testPoundLabel()
105     throws UnsupportedEncodingException, IOException {
106     	final String 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, 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" +
120     		"\t\tthe Guard\r\n" +
121     		"when/created:\t 1888\r\n\r\n";
122     	IllegalArgumentException iae = null;
123     	try {
124     		ANVLRecord.load(sample);
125     	} catch(IllegalArgumentException e) {
126     		iae = e;
127     	}
128     	assertTrue(iae != null);
129     }
130 }