001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import java.io.BufferedReader;
022: import java.io.File;
023: import java.io.FileReader;
024: import java.io.IOException;
025: import java.util.Enumeration;
026: import java.util.HashSet;
027: import java.util.Set;
028:
029: import org.apache.tools.ant.BuildFileTest;
030: import org.apache.tools.ant.Project;
031:
032: /**
033: * Testcase for the Manifest class used in the jar task.
034: *
035: */
036: public class ManifestTest extends BuildFileTest {
037:
038: public static final String EXPANDED_MANIFEST = "src/etc/testcases/taskdefs/manifests/META-INF/MANIFEST.MF";
039:
040: public static final String LONG_LINE = "AReallyLongLineToTestLineBreakingInManifests-ACapabilityWhich"
041: + "IsSureToLeadToHundredsOfQuestionsAboutWhyAntMungesManifests"
042: + "OfCourseTheAnswerIsThatIsWhatTheSpecRequiresAndIfAnythingHas"
043: + "AProblemWithThatItIsNotABugInAnt";
044:
045: public static final String LONG_70_NAME = "ThisNameIsJustSeventyCharactersWhichIsAllowedAccordingToTheSpecsFiller";
046: public static final String LONG_68_NAME = "ThisNameIsJustSixtyEightCharactersWhichIsAllowedAccordingToTheSpecsX";
047: public static final String NOT_LONG_NAME = "NameIsJustUnderSeventyCharactersWhichIsAllowedAccordingTheSpec";
048:
049: public static final String VALUE = "NOT_LONG";
050:
051: public ManifestTest(String name) {
052: super (name);
053: }
054:
055: public void setUp() {
056: configureProject("src/etc/testcases/taskdefs/manifest.xml");
057: }
058:
059: public void tearDown() {
060: executeTarget("clean");
061: }
062:
063: /**
064: * Empty manifest - is OK
065: */
066: public void test1() throws ManifestException, IOException {
067: executeTarget("test1");
068: Manifest manifest = getManifest(EXPANDED_MANIFEST);
069: String version = manifest.getManifestVersion();
070: assertEquals(
071: "Manifest was not created with correct version - ",
072: "1.0", version);
073: }
074:
075: /**
076: * Simple Manifest with version 2.0
077: */
078: public void test2() throws ManifestException, IOException {
079: executeTarget("test2");
080: Manifest manifest = getManifest(EXPANDED_MANIFEST);
081: String version = manifest.getManifestVersion();
082: assertEquals(
083: "Manifest was not created with correct version - ",
084: "2.0", version);
085: }
086:
087: /**
088: * Malformed manifest - no : on the line
089: */
090: public void test3() {
091: expectBuildExceptionContaining("test3",
092: "Manifest is invalid - no colon on header line",
093: "Invalid Manifest");
094: }
095:
096: /**
097: * Malformed manifest - starts with continuation line
098: */
099: public void test4() {
100: expectBuildExceptionContaining(
101: "test4",
102: "Manifest is invalid - section starts with continuation line",
103: "Invalid Manifest");
104: }
105:
106: /**
107: * Malformed manifest - Name attribute in main section
108: */
109: public void test5() {
110: executeTarget("test5");
111: String output = getLog();
112: boolean hasWarning = output
113: .indexOf("Manifest warning: \"Name\" attributes should not occur in the main section") != -1;
114: assertTrue("Expected warning about Name in main section",
115: hasWarning);
116: }
117:
118: /**
119: * New Section not starting with Name attribute.
120: */
121: public void test6() {
122: expectBuildExceptionContaining(
123: "test6",
124: "Manifest is invalid - section starts with incorrect attribute",
125: "Invalid Manifest");
126: String output = getLog();
127: boolean hasWarning = output
128: .indexOf("Manifest sections should start with a \"Name\" attribute") != -1;
129: assertTrue(
130: "Expected warning about section not starting with Name: attribute",
131: hasWarning);
132: }
133:
134: /**
135: * From attribute is illegal
136: */
137: public void test7() {
138: executeTarget("test7");
139:
140: boolean hasWarning = getLog().indexOf(
141: Manifest.ERROR_FROM_FORBIDDEN) != -1;
142: assertTrue("Expected warning about From: attribute", hasWarning);
143: }
144:
145: /**
146: * Inline manifest - OK
147: */
148: public void test8() throws IOException, ManifestException {
149: executeTarget("test8");
150: Manifest manifest = getManifest(EXPANDED_MANIFEST);
151: Manifest.Section mainSection = manifest.getMainSection();
152: String classpath = mainSection.getAttributeValue("class-path");
153: assertEquals("Class-Path attribute was not set correctly - ",
154: "fubar", classpath);
155:
156: Manifest.Section testSection = manifest.getSection("Test");
157: String testAttr = testSection.getAttributeValue("TestAttr");
158: assertEquals("TestAttr attribute was not set correctly - ",
159: "Test", testAttr);
160: }
161:
162: /**
163: * Inline manifest - Invalid since has a Name attribute in the section element
164: */
165: public void test9() {
166: expectBuildExceptionContaining(
167: "test9",
168: "Construction is invalid - Name attribute should not be used",
169: "Specify the section name using the \"name\" attribute of the <section> element");
170: }
171:
172: /**
173: * Inline manifest - Invalid attribute without name
174: */
175: public void test10() {
176: expectBuildExceptionContaining("test10",
177: "Attribute has no name",
178: "Attributes must have name and value");
179: }
180:
181: /**
182: * Inline manifest - Invalid attribute without value
183: */
184: public void test11() {
185: expectBuildExceptionContaining("test11",
186: "Attribute has no value",
187: "Attributes must have name and value");
188: }
189:
190: /**
191: * Inline manifest - Invalid attribute without value
192: */
193: public void test12() {
194: expectBuildExceptionContaining("test12",
195: "Section with no name", "Sections must have a name");
196: }
197:
198: /**
199: * Inline manifest - Duplicate attribute
200: */
201: public void test13() {
202: expectBuildExceptionContaining("test13", "Duplicate Attribute",
203: "The attribute \"Test\" may not occur more than once in the same section");
204: }
205:
206: /**
207: * Inline manifest - OK since classpath entries can be duplicated.
208: */
209: public void test14() throws IOException, ManifestException {
210: executeTarget("test14");
211: Manifest manifest = getManifest(EXPANDED_MANIFEST);
212: Manifest.Section mainSection = manifest.getMainSection();
213: String classpath = mainSection.getAttributeValue("class-path");
214: assertEquals("Class-Path attribute was not set correctly - ",
215: "Test1 Test2 Test3 Test4", classpath);
216: }
217:
218: /**
219: * Tets long line wrapping
220: */
221: public void testLongLine() throws IOException, ManifestException {
222: Project p = getProject();
223: p.setUserProperty("test.longline", LONG_LINE);
224: p.setUserProperty("test.long68name", LONG_68_NAME);
225: p.setUserProperty("test.long70name", LONG_70_NAME);
226: p.setUserProperty("test.notlongname", NOT_LONG_NAME);
227: p.setUserProperty("test.value", VALUE);
228: executeTarget("testLongLine");
229:
230: Manifest manifest = getManifest(EXPANDED_MANIFEST);
231: Manifest.Section mainSection = manifest.getMainSection();
232: String classpath = mainSection.getAttributeValue("class-path");
233: assertEquals("Class-Path attribute was not set correctly - ",
234: LONG_LINE, classpath);
235:
236: String value = mainSection.getAttributeValue(LONG_68_NAME);
237: assertEquals("LONG_68_NAME_VALUE_MISMATCH", VALUE, value);
238: value = mainSection.getAttributeValue(LONG_70_NAME);
239: assertEquals("LONG_70_NAME_VALUE_MISMATCH", VALUE, value);
240: value = mainSection.getAttributeValue(NOT_LONG_NAME);
241: assertEquals("NOT_LONG_NAME_VALUE_MISMATCH", VALUE, value);
242:
243: BufferedReader in = new BufferedReader(new FileReader(
244: EXPANDED_MANIFEST));
245:
246: Set set = new HashSet();
247: String read = in.readLine();
248: while (read != null) {
249: set.add(read);
250: read = in.readLine();
251: }
252:
253: assertTrue("Manifest file should have contained string ", set
254: .remove(" NOT_LONG"));
255: assertTrue("Manifest file should have contained string ", set
256: .remove(" NG"));
257: assertTrue("Manifest file should have contained string ", set
258: .remove(LONG_70_NAME + ": "));
259: assertTrue("Manifest file should have contained string ", set
260: .remove(NOT_LONG_NAME + ": NOT_LO"));
261: }
262:
263: /**
264: * Tests ordering of sections
265: */
266: public void testOrder1() throws IOException, ManifestException {
267: executeTarget("testOrder1");
268:
269: Manifest manifest = getManifest(EXPANDED_MANIFEST);
270: Enumeration e = manifest.getSectionNames();
271: String section1 = (String) e.nextElement();
272: String section2 = (String) e.nextElement();
273: assertEquals("First section name unexpected", "Test1", section1);
274: assertEquals("Second section name unexpected", "Test2",
275: section2);
276:
277: Manifest.Section section = manifest.getSection("Test1");
278: e = section.getAttributeKeys();
279: String attr1Key = (String) e.nextElement();
280: String attr2Key = (String) e.nextElement();
281: String attr1 = section.getAttribute(attr1Key).getName();
282: String attr2 = section.getAttribute(attr2Key).getName();
283: assertEquals("First attribute name unexpected", "TestAttr1",
284: attr1);
285: assertEquals("Second attribute name unexpected", "TestAttr2",
286: attr2);
287: }
288:
289: /**
290: * Tests ordering of sections
291: */
292: public void testOrder2() throws IOException, ManifestException {
293: executeTarget("testOrder2");
294:
295: Manifest manifest = getManifest(EXPANDED_MANIFEST);
296: Enumeration e = manifest.getSectionNames();
297: String section1 = (String) e.nextElement();
298: String section2 = (String) e.nextElement();
299: assertEquals("First section name unexpected", "Test2", section1);
300: assertEquals("Second section name unexpected", "Test1",
301: section2);
302:
303: Manifest.Section section = manifest.getSection("Test1");
304: e = section.getAttributeKeys();
305: String attr1Key = (String) e.nextElement();
306: String attr2Key = (String) e.nextElement();
307: String attr1 = section.getAttribute(attr1Key).getName();
308: String attr2 = section.getAttribute(attr2Key).getName();
309: assertEquals("First attribute name unexpected", "TestAttr2",
310: attr1);
311: assertEquals("Second attribute name unexpected", "TestAttr1",
312: attr2);
313: }
314:
315: /**
316: * file attribute for manifest task is required.
317: */
318: public void testNoFile() {
319: expectBuildException("testNoFile", "file is required");
320: }
321:
322: /**
323: * replace changes Manifest-Version from 2.0 to 1.0
324: */
325: public void testReplace() throws IOException, ManifestException {
326: executeTarget("testReplace");
327: Manifest mf = getManifest("src/etc/testcases/taskdefs/mftest.mf");
328: assertNotNull(mf);
329: assertEquals(Manifest.getDefaultManifest(), mf);
330: }
331:
332: /**
333: * update keeps the Manifest-Version and adds a new attribute Foo
334: */
335: public void testUpdate() throws IOException, ManifestException {
336: executeTarget("testUpdate");
337: Manifest mf = getManifest("src/etc/testcases/taskdefs/mftest.mf");
338: assertNotNull(mf);
339: assertTrue(!Manifest.getDefaultManifest().equals(mf));
340: String mfAsString = mf.toString();
341: assertNotNull(mfAsString);
342: assertTrue(mfAsString.startsWith("Manifest-Version: 2.0"));
343: assertTrue(mfAsString.indexOf("Foo: Bar") > -1);
344:
345: mf = getManifest("src/etc/testcases/taskdefs/mftest2.mf");
346: assertNotNull(mf);
347: mfAsString = mf.toString();
348: assertNotNull(mfAsString);
349: assertEquals(-1, mfAsString.indexOf("Foo: Bar"));
350: assertTrue(mfAsString.indexOf("Foo: Baz") > -1);
351: }
352:
353: public void testFrom() {
354: expectLogContaining("testFrom", Manifest.ERROR_FROM_FORBIDDEN);
355: }
356:
357: /**
358: * Reads mftest.mf.
359: */
360: private Manifest getManifest(String filename) throws IOException,
361: ManifestException {
362: FileReader r = new FileReader(new File(System
363: .getProperty("root"), filename));
364: try {
365: return new Manifest(r);
366: } finally {
367: r.close();
368: }
369: }
370: }
|