KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > EditableManifestTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.project;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import org.netbeans.junit.NbTestCase;
29
30 /**
31  * Tests {@link EditableManifest} semantics.
32  * @author Jesse Glick
33  */

34 public class EditableManifestTest extends NbTestCase {
35
36     static {
37          // Make sure this test runs the same on Windows as on Unix.
38
System.setProperty("line.separator", "\n");
39     }
40
41     public EditableManifestTest(String JavaDoc name) {
42         super(name);
43     }
44     
45     public void testCreateNew() throws Exception JavaDoc {
46         EditableManifest m = new EditableManifest();
47         assertEquals("Manifest-Version: 1.0\n\n", manifest2String(m));
48         m.setAttribute("hello", "dolly", null);
49         m.setAttribute("although", "earlier", null);
50         m.setAttribute("later", "OK", null);
51         m.setAttribute("unicode", "\u0950", null);
52         m.setAttribute("later", "rewritten", null);
53         m.addSection("some/section");
54         m.setAttribute("whatever", "value", "some/section");
55         m.addSection("earlier/section");
56         m.setAttribute("some", "value", "earlier/section");
57         m.setAttribute("whatever", "new value", "some/section");
58         m.setAttribute("different", "value", "earlier/section");
59         assertEquals("Manifest-Version: 1.0\n" +
60                      "although: earlier\n" +
61                      "hello: dolly\n" +
62                      "later: rewritten\n" +
63                      "unicode: \u0950\n" +
64                      "\n" +
65                      "Name: earlier/section\n" +
66                      "different: value\n" +
67                      "some: value\n" +
68                      "\n" +
69                      "Name: some/section\n" +
70                      "whatever: new value\n" +
71                      "\n",
72             manifest2String(m));
73     }
74     
75     public void testMalformedManifest() throws Exception JavaDoc {
76         try {
77             string2Manifest("something\n");
78             fail("no value (main section)");
79         } catch (IOException JavaDoc e) {}
80         try {
81             string2Manifest("key: val\n\nName: foo\nsomething\n");
82             fail("no value (named section)");
83         } catch (IOException JavaDoc e) {}
84         try {
85             string2Manifest("Name: foo\nValue: bar\n");
86             fail("cannot start with section");
87         } catch (IOException JavaDoc e) {}
88         try {
89             string2Manifest("something: here\nName: foo\n");
90             fail("no blank line before section");
91         } catch (IOException JavaDoc e) {}
92         try {
93             string2Manifest("something: here\n\nName: foo\nName: bar\n");
94             fail("no blank line between sections");
95         } catch (IOException JavaDoc e) {}
96         try {
97             string2Manifest("something: here\nsomething: again\n");
98             fail("duplicated attrs");
99         } catch (IOException JavaDoc e) {}
100         try {
101             string2Manifest("something: here\nSomething: again\n");
102             fail("duplicated attrs (mixed case)");
103         } catch (IOException JavaDoc e) {}
104         try {
105             string2Manifest("something: here\n\nName: foo\n\nName: foo\n");
106             fail("duplicated sections");
107         } catch (IOException JavaDoc e) {}
108         try {
109             string2Manifest("something: here\n\nName:\n\n");
110             fail("blank Name not permitted");
111         } catch (IOException JavaDoc e) {}
112         try {
113             string2Manifest("something: here\n\nbogus: val\n\n");
114             fail("section must contain Name");
115         } catch (IOException JavaDoc e) {}
116         try {
117             string2Manifest("something: here\n\nbogus: val\nName: x\n\n");
118             fail("section must start with Name");
119         } catch (IOException JavaDoc e) {}
120     }
121     
122     public void testReadGeneral() throws Exception JavaDoc {
123         EditableManifest m = string2Manifest("");
124         assertEquals(Collections.EMPTY_SET, m.getAttributeNames(null));
125         assertEquals(Collections.EMPTY_SET, m.getSectionNames());
126         m = string2Manifest("Foo: val1\nBar: val2");
127         assertEquals(new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] {"Foo", "Bar"})), m.getAttributeNames(null));
128         assertEquals("val1", m.getAttribute("Foo", null));
129         assertEquals("val2", m.getAttribute("Bar", null));
130         assertEquals(Collections.EMPTY_SET, m.getSectionNames());
131         m = string2Manifest("Foo: val1\nBar: val2\n\nName: something.class\nAttr: val\n\nName: other.class\nAttr: val2\n\n");
132         assertEquals(new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] {"Foo", "Bar"})), m.getAttributeNames(null));
133         assertEquals("val1", m.getAttribute("foo", null));
134         assertEquals("val2", m.getAttribute("bar", null));
135         assertEquals(new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] {"something.class", "other.class"})), m.getSectionNames());
136         assertEquals(Collections.singleton("Attr"), m.getAttributeNames("something.class"));
137         assertEquals("val", m.getAttribute("Attr", "something.class"));
138         assertEquals(Collections.singleton("Attr"), m.getAttributeNames("other.class"));
139         assertEquals("val2", m.getAttribute("Attr", "other.class"));
140         m = string2Manifest("Foo : bar \nBaz:quux");
141         assertEquals(new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] {"Foo", "Baz"})), m.getAttributeNames(null));
142         assertEquals("bar ", m.getAttribute("Foo", null));
143         assertEquals("quux", m.getAttribute("Baz", null));
144     }
145     
146     public void testReadMissingSectionsAndAttributes() throws Exception JavaDoc {
147         EditableManifest m = string2Manifest("Foo: val1\n\nName: something.class\nAttr: val\n\n");
148         assertEquals(null, m.getAttribute("dummy", null));
149         assertEquals(null, m.getAttribute("dummy", "something.class"));
150         try {
151             m.getAttribute("dummy", "nonexistent.class");
152             fail("nonexistent section");
153         } catch (IllegalArgumentException JavaDoc e) {}
154     }
155     
156     public void testReadContinuationLines() throws Exception JavaDoc {
157         EditableManifest m = string2Manifest("Attr: some long value which\n cannot fit on one line\n\nName: foo\nAttr: again \n here");
158         assertEquals("some long value which cannot fit on one line", m.getAttribute("Attr", null));
159         assertEquals("again here", m.getAttribute("Attr", "foo"));
160     }
161     
162     public void testEdit() throws Exception JavaDoc {
163         EditableManifest m = string2Manifest("A1: v1\nA2: v2\n\nName: n1\nA1: v1\nA2: v2\n\nName: n2\nA1: v1\n\n");
164         m.removeSection("n2");
165         try {
166             m.removeSection("n3");
167             fail("Cannot remove nonexistent section");
168         } catch (IllegalArgumentException JavaDoc e) {}
169         m.removeAttribute("A2", null);
170         try {
171             m.removeAttribute("A3", null);
172             fail("Cannot remove nonexistent attr");
173         } catch (IllegalArgumentException JavaDoc e) {}
174         m.removeAttribute("A1", "n1");
175         try {
176             m.removeAttribute("A3", "n1");
177             fail("Cannot remove nonexistent attr");
178         } catch (IllegalArgumentException JavaDoc e) {}
179         try {
180             m.removeAttribute("A1", "n2");
181             fail("Cannot remove attr from nonexistent section");
182         } catch (IllegalArgumentException JavaDoc e) {}
183         m.addSection("n3");
184         m.setAttribute("A3", "v3", null);
185         m.setAttribute("A3", "v3", "n1");
186         m.setAttribute("A3", "v3", "n3");
187         m.setAttribute("A3", "v3a", "n3");
188         try {
189             m.setAttribute("A1", "v1", "n2");
190             fail("cannot set attr in nonexistent section");
191         } catch (IllegalArgumentException JavaDoc e) {}
192         assertEquals("A1: v1\n" +
193                      "A3: v3\n" +
194                      "\n" +
195                      "Name: n1\n" +
196                      "A2: v2\n" +
197                      "A3: v3\n" +
198                      "\n" +
199                      "Name: n3\n" +
200                      "A3: v3a\n" +
201                      "\n",
202             manifest2String(m));
203     }
204     
205     public void testModifyOutOfOrderAttr() throws Exception JavaDoc {
206         EditableManifest m = string2Manifest("A2: v2\nA1: v1\n\n");
207         m.setAttribute("A1", "v1a", null);
208         assertEquals("A2: v2\nA1: v1a\n\n", manifest2String(m));
209         m.setAttribute("A1", "v1a", null);
210         assertEquals("A2: v2\nA1: v1a\n\n", manifest2String(m));
211     }
212     
213     public void testAlphabetization() throws Exception JavaDoc {
214         EditableManifest m = string2Manifest("aa: x\nM: x\nz: x\n\nName: aa\n\nName: m\n\nName: z\n\n");
215         m.setAttribute("a", "x", null);
216         m.setAttribute("B", "x", null);
217         m.setAttribute("n", "x", null);
218         m.addSection("a");
219         m.addSection("k");
220         m.addSection("z2");
221         m.setAttribute("z", "x", "m");
222         m.setAttribute("a", "x", "m");
223         assertEquals("a: x\n" +
224                      "aa: x\n" +
225                      "B: x\n" +
226                      "M: x\n" +
227                      "n: x\n" +
228                      "z: x\n" +
229                      "\n" +
230                      "Name: a\n" +
231                      "\n" +
232                      "Name: aa\n" +
233                      "\n" +
234                      "Name: k\n" +
235                      "\n" +
236                      "Name: m\n" +
237                      "a: x\n" +
238                      "z: x\n" +
239                      "\n" +
240                      "Name: z\n" +
241                      "\n" +
242                      "Name: z2\n" +
243                      "\n",
244             manifest2String(m));
245     }
246     
247     public void testCaseInsensitivityOfAttributeNames() throws Exception JavaDoc {
248         EditableManifest m = string2Manifest("a: x\nB: x\n\nName: n\na: x\nB: x\n\n");
249         assertEquals("x", m.getAttribute("A", null));
250         assertEquals("x", m.getAttribute("b", null));
251         assertEquals("x", m.getAttribute("A", "n"));
252         assertEquals("x", m.getAttribute("b", "n"));
253         try {
254             m.getAttribute("a", "N");
255             fail("section names case sensitive");
256         } catch (IllegalArgumentException JavaDoc e) {}
257         m.setAttribute("A", "x2", null);
258         m.setAttribute("b", "x2", "n");
259         m.removeAttribute("b", null);
260         m.removeAttribute("A", "n");
261         assertEquals("A: x2\n\nName: n\nb: x2\n\n", manifest2String(m));
262     }
263     
264     public void testManifestVersionAlwaysInsertedFirst() throws Exception JavaDoc {
265         EditableManifest m = string2Manifest("b: x\n\n");
266         m.setAttribute("Manifest-Version", "1.0", null);
267         assertEquals("Manifest-Version: 1.0\nb: x\n\n", manifest2String(m));
268         m.setAttribute("a", "x", null);
269         assertEquals("Manifest-Version: 1.0\na: x\nb: x\n\n", manifest2String(m));
270         m.setAttribute("Manifest-Version", "1.1", null);
271         assertEquals("Manifest-Version: 1.1\na: x\nb: x\n\n", manifest2String(m));
272     }
273     
274     public void testPreserveFormatting() throws Exception JavaDoc {
275         EditableManifest m = string2Manifest("A:x\nB : x\nC: lo\n ng \n as heck\nD: x\nE :lo\n ng\n\n\nName: n");
276         m.setAttribute("a", "x", null);
277         m.setAttribute("c", "long as heck", null);
278         m.setAttribute("d", "x2", null);
279         m.setAttribute("E", "longer", null);
280         assertEquals("A:x\nB : x\nC: lo\n ng \n as heck\nd: x2\nE: longer\n\n\nName: n\n", manifest2String(m));
281     }
282     
283     public void test66341() throws Exception JavaDoc {
284         EditableManifest m = string2Manifest("A: x\nB: y\n");
285         m.addSection("x");
286         //System.err.println(manifest2String(m));
287
assertEquals("adding a section always inserts a blank line", "A: x\nB: y\n\nName: x\n\n", manifest2String(m));
288     }
289     
290     private static EditableManifest string2Manifest(String JavaDoc text) throws Exception JavaDoc {
291         return new EditableManifest(new ByteArrayInputStream JavaDoc(text.getBytes("UTF-8")));
292     }
293     
294     private static String JavaDoc manifest2String(EditableManifest em) throws Exception JavaDoc {
295         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
296         em.write(baos);
297         return baos.toString("UTF-8");
298     }
299     
300 }
301
Popular Tags