KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > ManifestTest


1 /*
2  * Copyright 2001-2002,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.tools.ant.taskdefs;
19
20 import java.io.File JavaDoc;
21 import java.io.FileReader JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.Vector JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import org.apache.tools.ant.BuildFileTest;
27 import org.apache.tools.ant.Project;
28
29 /**
30  * Testcase for the Manifest class used in the jar task.
31  *
32  */

33 public class ManifestTest extends BuildFileTest {
34
35     public static final String JavaDoc EXPANDED_MANIFEST
36         = "src/etc/testcases/taskdefs/manifests/META-INF/MANIFEST.MF";
37
38     public static final String JavaDoc LONG_LINE
39         = "AReallyLongLineToTestLineBreakingInManifests-ACapabilityWhich" +
40           "IsSureToLeadToHundredsOfQuestionsAboutWhyAntMungesManifests" +
41           "OfCourseTheAnswerIsThatIsWhatTheSpecRequiresAndIfAnythingHas" +
42           "AProblemWithThatItIsNotABugInAnt";
43
44     public ManifestTest(String JavaDoc name) {
45         super(name);
46     }
47
48     public void setUp() {
49         configureProject("src/etc/testcases/taskdefs/manifest.xml");
50     }
51
52     public void tearDown() {
53         executeTarget("clean");
54     }
55
56     /**
57      * Empty manifest - is OK
58      */

59     public void test1() throws ManifestException, IOException JavaDoc {
60         executeTarget("test1");
61         Manifest manifest = getManifest(EXPANDED_MANIFEST);
62         String JavaDoc version = manifest.getManifestVersion();
63         assertEquals("Manifest was not created with correct version - ", "1.0", version);
64     }
65
66     /**
67      * Simple Manifest with version 2.0
68      */

69     public void test2() throws ManifestException, IOException JavaDoc {
70         executeTarget("test2");
71         Manifest manifest = getManifest(EXPANDED_MANIFEST);
72         String JavaDoc version = manifest.getManifestVersion();
73         assertEquals("Manifest was not created with correct version - ", "2.0", version);
74     }
75
76     /**
77      * Malformed manifest - no : on the line
78      */

79     public void test3() {
80         expectBuildExceptionContaining("test3", "Manifest is invalid - no colon on header line",
81                                        "Invalid Manifest");
82     }
83
84     /**
85      * Malformed manifest - starts with continuation line
86      */

87     public void test4() {
88         expectBuildExceptionContaining("test4", "Manifest is invalid - section starts with continuation line",
89                                        "Invalid Manifest");
90    }
91
92     /**
93      * Malformed manifest - Name attribute in main section
94      */

95     public void test5() {
96         executeTarget("test5");
97         String JavaDoc output = getLog();
98         boolean hasWarning = output.indexOf("Manifest warning: \"Name\" attributes should not occur in the main section") != -1;
99         assertEquals("Expected warning about Name in main section", true, hasWarning);
100     }
101
102     /**
103      * New Section not starting with Name attribute.
104      */

105     public void test6() {
106         expectBuildExceptionContaining("test6", "Manifest is invalid - section starts with incorrect attribute",
107                                        "Invalid Manifest");
108         String JavaDoc output = getLog();
109         boolean hasWarning = output.indexOf("Manifest sections should start with a \"Name\" attribute") != -1;
110         assertEquals("Expected warning about section not starting with Name: attribute", true, hasWarning);
111     }
112
113     /**
114      * From attribute is illegal
115      */

116     public void test7() {
117         executeTarget("test7");
118
119         boolean hasWarning = getLog().indexOf("Manifest attributes should not start with \"From\"") != -1;
120         assertEquals("Expected warning about From: attribute", true, hasWarning);
121     }
122
123     /**
124      * Inline manifest - OK
125      */

126     public void test8() throws IOException JavaDoc, ManifestException {
127         executeTarget("test8");
128         Manifest manifest = getManifest(EXPANDED_MANIFEST);
129         Manifest.Section mainSection = manifest.getMainSection();
130         String JavaDoc classpath = mainSection.getAttributeValue("class-path");
131         assertEquals("Class-Path attribute was not set correctly - ", "fubar", classpath);
132
133         Manifest.Section testSection = manifest.getSection("Test");
134         String JavaDoc testAttr = testSection.getAttributeValue("TestAttr");
135         assertEquals("TestAttr attribute was not set correctly - ", "Test", testAttr);
136     }
137
138     /**
139      * Inline manifest - Invalid since has a Name attribute in the section element
140      */

141     public void test9() {
142         expectBuildExceptionContaining("test9", "Construction is invalid - Name attribute should not be used",
143                                        "Specify the section name using the \"name\" attribute of the <section> element");
144     }
145
146     /**
147      * Inline manifest - Invalid attribute without name
148      */

149     public void test10() {
150         expectBuildExceptionContaining("test10", "Attribute has no name",
151                                        "Attributes must have name and value");
152     }
153
154     /**
155      * Inline manifest - Invalid attribute without value
156      */

157     public void test11() {
158         expectBuildExceptionContaining("test11", "Attribute has no value",
159                                        "Attributes must have name and value");
160     }
161
162     /**
163      * Inline manifest - Invalid attribute without value
164      */

165     public void test12() {
166         expectBuildExceptionContaining("test12", "Section with no name",
167                                        "Sections must have a name");
168     }
169
170     /**
171      * Inline manifest - Duplicate attribute
172      */

173     public void test13() {
174         expectBuildExceptionContaining("test13", "Duplicate Attribute",
175                                        "The attribute \"Test\" may not occur more than once in the same section");
176     }
177
178     /**
179      * Inline manifest - OK since classpath entries can be duplicated.
180      */

181     public void test14() throws IOException JavaDoc, ManifestException {
182         executeTarget("test14");
183         Manifest manifest = getManifest(EXPANDED_MANIFEST);
184         Manifest.Section mainSection = manifest.getMainSection();
185         String JavaDoc classpath = mainSection.getAttributeValue("class-path");
186         assertEquals("Class-Path attribute was not set correctly - ",
187             "Test1 Test2 Test3 Test4", classpath);
188     }
189
190     /**
191      * Tets long line wrapping
192      */

193     public void testLongLine() throws IOException JavaDoc, ManifestException {
194         Project project = getProject();
195         project.setUserProperty("test.longline", LONG_LINE);
196         executeTarget("testLongLine");
197
198         Manifest manifest = getManifest(EXPANDED_MANIFEST);
199         Manifest.Section mainSection = manifest.getMainSection();
200         String JavaDoc classpath = mainSection.getAttributeValue("class-path");
201         assertEquals("Class-Path attribute was not set correctly - ",
202             LONG_LINE, classpath);
203     }
204
205     /**
206      * Tests ordering of sections
207      */

208     public void testOrder1() throws IOException JavaDoc, ManifestException {
209         executeTarget("testOrder1");
210
211         Manifest manifest = getManifest(EXPANDED_MANIFEST);
212         Enumeration JavaDoc e = manifest.getSectionNames();
213         String JavaDoc section1 = (String JavaDoc)e.nextElement();
214         String JavaDoc section2 = (String JavaDoc)e.nextElement();
215         assertEquals("First section name unexpected", "Test1", section1);
216         assertEquals("Second section name unexpected", "Test2", section2);
217
218         Manifest.Section section = manifest.getSection("Test1");
219         e = section.getAttributeKeys();
220         String JavaDoc attr1Key = (String JavaDoc)e.nextElement();
221         String JavaDoc attr2Key = (String JavaDoc)e.nextElement();
222         String JavaDoc attr1 = section.getAttribute(attr1Key).getName();
223         String JavaDoc attr2 = section.getAttribute(attr2Key).getName();
224         assertEquals("First attribute name unexpected", "TestAttr1", attr1);
225         assertEquals("Second attribute name unexpected", "TestAttr2", attr2);
226     }
227
228     /**
229      * Tests ordering of sections
230      */

231     public void testOrder2() throws IOException JavaDoc, ManifestException {
232         executeTarget("testOrder2");
233
234         Manifest manifest = getManifest(EXPANDED_MANIFEST);
235         Enumeration JavaDoc e = manifest.getSectionNames();
236         String JavaDoc section1 = (String JavaDoc)e.nextElement();
237         String JavaDoc section2 = (String JavaDoc)e.nextElement();
238         assertEquals("First section name unexpected", "Test2", section1);
239         assertEquals("Second section name unexpected", "Test1", section2);
240
241         Manifest.Section section = manifest.getSection("Test1");
242         e = section.getAttributeKeys();
243         String JavaDoc attr1Key = (String JavaDoc)e.nextElement();
244         String JavaDoc attr2Key = (String JavaDoc)e.nextElement();
245         String JavaDoc attr1 = section.getAttribute(attr1Key).getName();
246         String JavaDoc attr2 = section.getAttribute(attr2Key).getName();
247         assertEquals("First attribute name unexpected", "TestAttr2", attr1);
248         assertEquals("Second attribute name unexpected", "TestAttr1", attr2);
249     }
250
251     /**
252      * file attribute for manifest task is required.
253      */

254     public void testNoFile() {
255         expectBuildException("testNoFile", "file is required");
256     }
257
258     /**
259      * replace changes Manifest-Version from 2.0 to 1.0
260      */

261     public void testReplace() throws IOException JavaDoc, ManifestException {
262         executeTarget("testReplace");
263         Manifest mf = getManifest("src/etc/testcases/taskdefs/mftest.mf");
264         assertNotNull(mf);
265         assertEquals(Manifest.getDefaultManifest(), mf);
266     }
267
268     /**
269      * update keeps the Manifest-Version and adds a new attribute Foo
270      */

271     public void testUpdate() throws IOException JavaDoc, ManifestException {
272         executeTarget("testUpdate");
273         Manifest mf = getManifest("src/etc/testcases/taskdefs/mftest.mf");
274         assertNotNull(mf);
275         assertTrue(!Manifest.getDefaultManifest().equals(mf));
276         String JavaDoc mfAsString = mf.toString();
277         assertNotNull(mfAsString);
278         assertTrue(mfAsString.startsWith("Manifest-Version: 2.0"));
279         assertTrue(mfAsString.indexOf("Foo: Bar") > -1);
280
281         mf = getManifest("src/etc/testcases/taskdefs/mftest2.mf");
282         assertNotNull(mf);
283         mfAsString = mf.toString();
284         assertNotNull(mfAsString);
285         assertEquals(-1, mfAsString.indexOf("Foo: Bar"));
286         assertTrue(mfAsString.indexOf("Foo: Baz") > -1);
287     }
288
289     /**
290      * Reads mftest.mf.
291      */

292     private Manifest getManifest(String JavaDoc filename) throws IOException JavaDoc, ManifestException {
293         FileReader JavaDoc r = new FileReader JavaDoc(filename);
294         try {
295             return new Manifest(r);
296         } finally {
297             r.close();
298         }
299     }
300 }
301
Popular Tags