001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.apisupport.project;
043:
044: import java.io.ByteArrayInputStream;
045: import java.io.ByteArrayOutputStream;
046: import java.io.IOException;
047: import java.util.Arrays;
048: import java.util.Collections;
049: import java.util.HashSet;
050: import org.netbeans.junit.NbTestCase;
051:
052: /**
053: * Tests {@link EditableManifest} semantics.
054: * @author Jesse Glick
055: */
056: public class EditableManifestTest extends NbTestCase {
057:
058: static {
059: // Make sure this test runs the same on Windows as on Unix.
060: System.setProperty("line.separator", "\n");
061: }
062:
063: public EditableManifestTest(String name) {
064: super (name);
065: }
066:
067: public void testCreateNew() throws Exception {
068: EditableManifest m = new EditableManifest();
069: assertEquals("Manifest-Version: 1.0\n\n", manifest2String(m));
070: m.setAttribute("hello", "dolly", null);
071: m.setAttribute("although", "earlier", null);
072: m.setAttribute("later", "OK", null);
073: m.setAttribute("unicode", "\u0950", null);
074: m.setAttribute("later", "rewritten", null);
075: m.addSection("some/section");
076: m.setAttribute("whatever", "value", "some/section");
077: m.addSection("earlier/section");
078: m.setAttribute("some", "value", "earlier/section");
079: m.setAttribute("whatever", "new value", "some/section");
080: m.setAttribute("different", "value", "earlier/section");
081: assertEquals("Manifest-Version: 1.0\n" + "although: earlier\n"
082: + "hello: dolly\n" + "later: rewritten\n"
083: + "unicode: \u0950\n" + "\n"
084: + "Name: earlier/section\n" + "different: value\n"
085: + "some: value\n" + "\n" + "Name: some/section\n"
086: + "whatever: new value\n" + "\n", manifest2String(m));
087: }
088:
089: public void testMalformedManifest() throws Exception {
090: try {
091: string2Manifest("something\n");
092: fail("no value (main section)");
093: } catch (IOException e) {
094: }
095: try {
096: string2Manifest("key: val\n\nName: foo\nsomething\n");
097: fail("no value (named section)");
098: } catch (IOException e) {
099: }
100: try {
101: string2Manifest("Name: foo\nValue: bar\n");
102: fail("cannot start with section");
103: } catch (IOException e) {
104: }
105: try {
106: string2Manifest("something: here\nName: foo\n");
107: fail("no blank line before section");
108: } catch (IOException e) {
109: }
110: try {
111: string2Manifest("something: here\n\nName: foo\nName: bar\n");
112: fail("no blank line between sections");
113: } catch (IOException e) {
114: }
115: try {
116: string2Manifest("something: here\nsomething: again\n");
117: fail("duplicated attrs");
118: } catch (IOException e) {
119: }
120: try {
121: string2Manifest("something: here\nSomething: again\n");
122: fail("duplicated attrs (mixed case)");
123: } catch (IOException e) {
124: }
125: try {
126: string2Manifest("something: here\n\nName: foo\n\nName: foo\n");
127: fail("duplicated sections");
128: } catch (IOException e) {
129: }
130: try {
131: string2Manifest("something: here\n\nName:\n\n");
132: fail("blank Name not permitted");
133: } catch (IOException e) {
134: }
135: try {
136: string2Manifest("something: here\n\nbogus: val\n\n");
137: fail("section must contain Name");
138: } catch (IOException e) {
139: }
140: try {
141: string2Manifest("something: here\n\nbogus: val\nName: x\n\n");
142: fail("section must start with Name");
143: } catch (IOException e) {
144: }
145: }
146:
147: public void testReadGeneral() throws Exception {
148: EditableManifest m = string2Manifest("");
149: assertEquals(Collections.EMPTY_SET, m.getAttributeNames(null));
150: assertEquals(Collections.EMPTY_SET, m.getSectionNames());
151: m = string2Manifest("Foo: val1\nBar: val2");
152: assertEquals(new HashSet<String>(Arrays.asList("Foo", "Bar")),
153: m.getAttributeNames(null));
154: assertEquals("val1", m.getAttribute("Foo", null));
155: assertEquals("val2", m.getAttribute("Bar", null));
156: assertEquals(Collections.emptySet(), m.getSectionNames());
157: m = string2Manifest("Foo: val1\nBar: val2\n\nName: something.class\nAttr: val\n\nName: other.class\nAttr: val2\n\n");
158: assertEquals(new HashSet<String>(Arrays.asList("Foo", "Bar")),
159: m.getAttributeNames(null));
160: assertEquals("val1", m.getAttribute("foo", null));
161: assertEquals("val2", m.getAttribute("bar", null));
162: assertEquals(new HashSet<String>(Arrays.asList(
163: "something.class", "other.class")), m.getSectionNames());
164: assertEquals(Collections.singleton("Attr"), m
165: .getAttributeNames("something.class"));
166: assertEquals("val", m.getAttribute("Attr", "something.class"));
167: assertEquals(Collections.singleton("Attr"), m
168: .getAttributeNames("other.class"));
169: assertEquals("val2", m.getAttribute("Attr", "other.class"));
170: m = string2Manifest("Foo : bar \nBaz:quux");
171: assertEquals(new HashSet<String>(Arrays.asList("Foo", "Baz")),
172: m.getAttributeNames(null));
173: assertEquals("bar ", m.getAttribute("Foo", null));
174: assertEquals("quux", m.getAttribute("Baz", null));
175: }
176:
177: public void testReadMissingSectionsAndAttributes() throws Exception {
178: EditableManifest m = string2Manifest("Foo: val1\n\nName: something.class\nAttr: val\n\n");
179: assertEquals(null, m.getAttribute("dummy", null));
180: assertEquals(null, m.getAttribute("dummy", "something.class"));
181: try {
182: m.getAttribute("dummy", "nonexistent.class");
183: fail("nonexistent section");
184: } catch (IllegalArgumentException e) {
185: }
186: }
187:
188: public void testReadContinuationLines() throws Exception {
189: EditableManifest m = string2Manifest("Attr: some long value which\n cannot fit on one line\n\nName: foo\nAttr: again \n here");
190: assertEquals("some long value which cannot fit on one line", m
191: .getAttribute("Attr", null));
192: assertEquals("again here", m.getAttribute("Attr", "foo"));
193: }
194:
195: public void testEdit() throws Exception {
196: EditableManifest m = string2Manifest("A1: v1\nA2: v2\n\nName: n1\nA1: v1\nA2: v2\n\nName: n2\nA1: v1\n\n");
197: m.removeSection("n2");
198: try {
199: m.removeSection("n3");
200: fail("Cannot remove nonexistent section");
201: } catch (IllegalArgumentException e) {
202: }
203: m.removeAttribute("A2", null);
204: try {
205: m.removeAttribute("A3", null);
206: fail("Cannot remove nonexistent attr");
207: } catch (IllegalArgumentException e) {
208: }
209: m.removeAttribute("A1", "n1");
210: try {
211: m.removeAttribute("A3", "n1");
212: fail("Cannot remove nonexistent attr");
213: } catch (IllegalArgumentException e) {
214: }
215: try {
216: m.removeAttribute("A1", "n2");
217: fail("Cannot remove attr from nonexistent section");
218: } catch (IllegalArgumentException e) {
219: }
220: m.addSection("n3");
221: m.setAttribute("A3", "v3", null);
222: m.setAttribute("A3", "v3", "n1");
223: m.setAttribute("A3", "v3", "n3");
224: m.setAttribute("A3", "v3a", "n3");
225: try {
226: m.setAttribute("A1", "v1", "n2");
227: fail("cannot set attr in nonexistent section");
228: } catch (IllegalArgumentException e) {
229: }
230: assertEquals("A1: v1\n" + "A3: v3\n" + "\n" + "Name: n1\n"
231: + "A2: v2\n" + "A3: v3\n" + "\n" + "Name: n3\n"
232: + "A3: v3a\n" + "\n", manifest2String(m));
233: }
234:
235: public void testModifyOutOfOrderAttr() throws Exception {
236: EditableManifest m = string2Manifest("A2: v2\nA1: v1\n\n");
237: m.setAttribute("A1", "v1a", null);
238: assertEquals("A2: v2\nA1: v1a\n\n", manifest2String(m));
239: m.setAttribute("A1", "v1a", null);
240: assertEquals("A2: v2\nA1: v1a\n\n", manifest2String(m));
241: }
242:
243: public void testAlphabetization() throws Exception {
244: EditableManifest m = string2Manifest("aa: x\nM: x\nz: x\n\nName: aa\n\nName: m\n\nName: z\n\n");
245: m.setAttribute("a", "x", null);
246: m.setAttribute("B", "x", null);
247: m.setAttribute("n", "x", null);
248: m.addSection("a");
249: m.addSection("k");
250: m.addSection("z2");
251: m.setAttribute("z", "x", "m");
252: m.setAttribute("a", "x", "m");
253: assertEquals("a: x\n" + "aa: x\n" + "B: x\n" + "M: x\n"
254: + "n: x\n" + "z: x\n" + "\n" + "Name: a\n" + "\n"
255: + "Name: aa\n" + "\n" + "Name: k\n" + "\n"
256: + "Name: m\n" + "a: x\n" + "z: x\n" + "\n"
257: + "Name: z\n" + "\n" + "Name: z2\n" + "\n",
258: manifest2String(m));
259: }
260:
261: public void testCaseInsensitivityOfAttributeNames()
262: throws Exception {
263: EditableManifest m = string2Manifest("a: x\nB: x\n\nName: n\na: x\nB: x\n\n");
264: assertEquals("x", m.getAttribute("A", null));
265: assertEquals("x", m.getAttribute("b", null));
266: assertEquals("x", m.getAttribute("A", "n"));
267: assertEquals("x", m.getAttribute("b", "n"));
268: try {
269: m.getAttribute("a", "N");
270: fail("section names case sensitive");
271: } catch (IllegalArgumentException e) {
272: }
273: m.setAttribute("A", "x2", null);
274: m.setAttribute("b", "x2", "n");
275: m.removeAttribute("b", null);
276: m.removeAttribute("A", "n");
277: assertEquals("A: x2\n\nName: n\nb: x2\n\n", manifest2String(m));
278: }
279:
280: public void testManifestVersionAlwaysInsertedFirst()
281: throws Exception {
282: EditableManifest m = string2Manifest("b: x\n\n");
283: m.setAttribute("Manifest-Version", "1.0", null);
284: assertEquals("Manifest-Version: 1.0\nb: x\n\n",
285: manifest2String(m));
286: m.setAttribute("a", "x", null);
287: assertEquals("Manifest-Version: 1.0\na: x\nb: x\n\n",
288: manifest2String(m));
289: m.setAttribute("Manifest-Version", "1.1", null);
290: assertEquals("Manifest-Version: 1.1\na: x\nb: x\n\n",
291: manifest2String(m));
292: }
293:
294: public void testPreserveFormatting() throws Exception {
295: EditableManifest m = string2Manifest("A:x\nB : x\nC: lo\n ng \n as heck\nD: x\nE :lo\n ng\n\n\nName: n");
296: m.setAttribute("a", "x", null);
297: m.setAttribute("c", "long as heck", null);
298: m.setAttribute("d", "x2", null);
299: m.setAttribute("E", "longer", null);
300: assertEquals(
301: "A:x\nB : x\nC: lo\n ng \n as heck\nd: x2\nE: longer\n\n\nName: n\n",
302: manifest2String(m));
303: }
304:
305: public void test66341() throws Exception {
306: EditableManifest m = string2Manifest("A: x\nB: y\n");
307: m.addSection("x");
308: //System.err.println(manifest2String(m));
309: assertEquals("adding a section always inserts a blank line",
310: "A: x\nB: y\n\nName: x\n\n", manifest2String(m));
311: }
312:
313: private static EditableManifest string2Manifest(String text)
314: throws Exception {
315: return new EditableManifest(new ByteArrayInputStream(text
316: .getBytes("UTF-8")));
317: }
318:
319: private static String manifest2String(EditableManifest em)
320: throws Exception {
321: ByteArrayOutputStream baos = new ByteArrayOutputStream();
322: em.write(baos);
323: return baos.toString("UTF-8");
324: }
325:
326: }
|