Source Code Cross Referenced for JarCreationTest.java in  » IDE » DrJava » edu » rice » cs » util » jar » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE » DrJava » edu.rice.cs.util.jar 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*BEGIN_COPYRIGHT_BLOCK
002:         *
003:         * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004:         * All rights reserved.
005:         * 
006:         * Redistribution and use in source and binary forms, with or without
007:         * modification, are permitted provided that the following conditions are met:
008:         *    * Redistributions of source code must retain the above copyright
009:         *      notice, this list of conditions and the following disclaimer.
010:         *    * Redistributions in binary form must reproduce the above copyright
011:         *      notice, this list of conditions and the following disclaimer in the
012:         *      documentation and/or other materials provided with the distribution.
013:         *    * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014:         *      names of its contributors may be used to endorse or promote products
015:         *      derived from this software without specific prior written permission.
016:         * 
017:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018:         * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019:         * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020:         * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021:         * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022:         * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023:         * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024:         * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025:         * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026:         * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027:         * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028:         *
029:         * This software is Open Source Initiative approved Open Source Software.
030:         * Open Source Initative Approved is a trademark of the Open Source Initiative.
031:         * 
032:         * This file is part of DrJava.  Download the current version of this project
033:         * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034:         * 
035:         * END_COPYRIGHT_BLOCK*/
036:
037:        package edu.rice.cs.util.jar;
038:
039:        import edu.rice.cs.drjava.DrJavaTestCase;
040:
041:        import java.io.*;
042:        import java.util.Arrays;
043:        import java.util.Set;
044:        import java.util.TreeSet;
045:        import java.util.jar.Attributes;
046:        import java.util.jar.JarEntry;
047:        import java.util.jar.JarInputStream;
048:        import java.util.jar.Manifest;
049:
050:        public class JarCreationTest extends DrJavaTestCase {
051:            /**
052:             * Tests the creation of manifest files through the ManifestWriter class
053:             */
054:            public void testCreateManifest() {
055:                ManifestWriter mw = new ManifestWriter();
056:                Manifest manifest = mw.getManifest();
057:                assertTrue("should have version attribute", manifest
058:                        .getMainAttributes().containsKey(
059:                                Attributes.Name.MANIFEST_VERSION));
060:                assertEquals("should have version attribute", "1.0", manifest
061:                        .getMainAttributes().get(
062:                                Attributes.Name.MANIFEST_VERSION));
063:                assertEquals("should only have manifest attribute", 1, manifest
064:                        .getMainAttributes().size());
065:
066:                mw.setMainClass("edu.rice.cs.drjava.DrJava");
067:                manifest = mw.getManifest();
068:                assertTrue("should have version attribute", manifest
069:                        .getMainAttributes().containsKey(
070:                                Attributes.Name.MANIFEST_VERSION));
071:                assertEquals("should have version attribute", "1.0", manifest
072:                        .getMainAttributes().get(
073:                                Attributes.Name.MANIFEST_VERSION));
074:                assertTrue("should have main class attribute", manifest
075:                        .getMainAttributes().containsKey(
076:                                Attributes.Name.MAIN_CLASS));
077:                assertEquals("should have main class attribute",
078:                        "edu.rice.cs.drjava.DrJava", manifest
079:                                .getMainAttributes().get(
080:                                        Attributes.Name.MAIN_CLASS));
081:                assertEquals("should only have manifest attribute", 2, manifest
082:                        .getMainAttributes().size());
083:
084:                mw = new ManifestWriter();
085:                mw.addClassPath("koala.dynamicjava");
086:                manifest = mw.getManifest();
087:                assertTrue("should have version attribute", manifest
088:                        .getMainAttributes().containsKey(
089:                                Attributes.Name.MANIFEST_VERSION));
090:                assertEquals("should have version attribute", "1.0", manifest
091:                        .getMainAttributes().get(
092:                                Attributes.Name.MANIFEST_VERSION));
093:                assertTrue("should have classpath attribute", manifest
094:                        .getMainAttributes().containsKey(
095:                                Attributes.Name.CLASS_PATH));
096:                assertEquals("should have correct classpath",
097:                        "koala.dynamicjava", manifest.getMainAttributes().get(
098:                                Attributes.Name.CLASS_PATH));
099:                assertEquals("have version and classpath", 2, manifest
100:                        .getMainAttributes().size());
101:
102:                mw.addClassPath("edu.rice.cs.util");
103:                manifest = mw.getManifest();
104:                assertTrue("should have version attribute", manifest
105:                        .getMainAttributes().containsKey(
106:                                Attributes.Name.MANIFEST_VERSION));
107:                assertEquals("should have version attribute", "1.0", manifest
108:                        .getMainAttributes().get(
109:                                Attributes.Name.MANIFEST_VERSION));
110:                assertTrue("should have classpath attribute", manifest
111:                        .getMainAttributes().containsKey(
112:                                Attributes.Name.CLASS_PATH));
113:                assertEquals("should have correct classpath",
114:                        "koala.dynamicjava edu.rice.cs.util", manifest
115:                                .getMainAttributes().get(
116:                                        Attributes.Name.CLASS_PATH));
117:                assertEquals("have version and classpath", 2, manifest
118:                        .getMainAttributes().size());
119:
120:                mw.setMainClass("edu.rice.cs.drjava.DrJava");
121:                manifest = mw.getManifest();
122:                assertTrue("should have version attribute", manifest
123:                        .getMainAttributes().containsKey(
124:                                Attributes.Name.MANIFEST_VERSION));
125:                assertEquals("should have version attribute", "1.0", manifest
126:                        .getMainAttributes().get(
127:                                Attributes.Name.MANIFEST_VERSION));
128:                assertTrue("should have classpath attribute", manifest
129:                        .getMainAttributes().containsKey(
130:                                Attributes.Name.CLASS_PATH));
131:                assertEquals("should have correct classpath",
132:                        "koala.dynamicjava edu.rice.cs.util", manifest
133:                                .getMainAttributes().get(
134:                                        Attributes.Name.CLASS_PATH));
135:                assertTrue("should have main class attribute", manifest
136:                        .getMainAttributes().containsKey(
137:                                Attributes.Name.MAIN_CLASS));
138:                assertEquals("should have main class attribute",
139:                        "edu.rice.cs.drjava.DrJava", manifest
140:                                .getMainAttributes().get(
141:                                        Attributes.Name.MAIN_CLASS));
142:                assertEquals("have version and classpath", 3, manifest
143:                        .getMainAttributes().size());
144:            }
145:
146:            /**
147:             * Test create addDirectoryRecursive
148:             */
149:            public void testCreateJarFromDirectoryRecursive() {
150:                File dir = new File("temp_dir");
151:                dir.mkdir();
152:                File dir1 = new File(dir, "dir");
153:                dir1.mkdir();
154:                File[] files = new File[] { new File(dir, "test.java"),
155:                        new File(dir, "test.class"), new File(dir, "p1.tmp"),
156:                        new File(dir1, "test1.java"),
157:                        new File(dir1, "out.class"),
158:                        new File(dir1, "out.out.out.class"),
159:                        new File(dir1, "that.java") };
160:                dir.deleteOnExit();
161:                dir1.deleteOnExit();
162:                for (int i = 0; i < files.length; i++)
163:                    files[i].deleteOnExit();
164:                try {
165:
166:                    PrintWriter pw = null;
167:                    for (int i = 0; i < files.length; i++) {
168:                        pw = new PrintWriter(new FileOutputStream(files[i]));
169:                        pw.write(files[i].getName());
170:                        pw.close();
171:                    }
172:
173:                    File f = new File("test~.jar");
174:                    f.deleteOnExit();
175:                    JarBuilder jb = new JarBuilder(f);
176:                    jb.addDirectoryRecursive(dir, "");
177:                    jb.close();
178:
179:                    testArchive(f, new TreeSet<String>(Arrays
180:                            .asList(new String[] { files[0].getName(),
181:                                    files[1].getName(), files[2].getName(),
182:                                    "dir/" + files[3].getName(),
183:                                    "dir/" + files[4].getName(),
184:                                    "dir/" + files[5].getName(),
185:                                    "dir/" + files[6].getName() })));
186:
187:                    jb = new JarBuilder(f);
188:                    jb.addDirectoryRecursive(dir, "", new FileFilter() {
189:                        public boolean accept(File pathname) {
190:                            return pathname.getName().endsWith(".class")
191:                                    || pathname.isDirectory();
192:                        }
193:                    });
194:                    jb.close();
195:
196:                    testArchive(f, new TreeSet<String>(Arrays
197:                            .asList(new String[] { files[1].getName(),
198:                                    "dir/" + files[4].getName(),
199:                                    "dir/" + files[5].getName() })));
200:
201:                    jb = new JarBuilder(f);
202:                    jb.addDirectoryRecursive(dir, "", new FileFilter() {
203:                        public boolean accept(File pathname) {
204:                            return pathname.getName().endsWith(".java")
205:                                    || pathname.isDirectory();
206:                        }
207:                    });
208:                    jb.close();
209:
210:                    testArchive(f, new TreeSet<String>(Arrays
211:                            .asList(new String[] { files[0].getName(),
212:                                    "dir/" + files[3].getName(),
213:                                    "dir/" + files[6].getName() })));
214:
215:                } catch (FileNotFoundException e) {
216:                    e.printStackTrace();
217:                } catch (IOException e) {
218:                    e.printStackTrace();
219:                }
220:
221:            }
222:
223:            /**
224:             * Test the manual creation of jar files
225:             */
226:            public void testCreateJar() {
227:                File f = new File("test.jar");
228:                f.deleteOnExit();
229:                File add = null;
230:                try {
231:                    String fileContents = "public class JarTest {"
232:                            + "\tpublic String getClassName() {"
233:                            + "\t\treturn \"JarTest\";" + "\t}" + "}";
234:                    byte[] b = new byte[fileContents.getBytes("UTF-8").length];
235:
236:                    add = File.createTempFile("JarTest", ".java")
237:                            .getCanonicalFile();
238:                    add.deleteOnExit();
239:
240:                    PrintWriter pw = new PrintWriter(new FileOutputStream(add));
241:                    pw.write(fileContents);
242:                    pw.close();
243:
244:                    JarBuilder jb = new JarBuilder(f);
245:                    jb.addFile(add, "", "JarTest.java");
246:                    jb.addFile(add, "dir", "JarTest.java");
247:                    jb.close();
248:
249:                    testArchive(f, new TreeSet<String>(Arrays
250:                            .asList(new String[] { "JarTest.java",
251:                                    "dir/JarTest.java" })));
252:
253:                    JarInputStream jarStream = new JarInputStream(
254:                            new FileInputStream(f), true);
255:
256:                    JarEntry ent = jarStream.getNextJarEntry();
257:                    assertTrue("should have JarTest", ent != null);
258:                    assertEquals("names should match", "JarTest.java", ent
259:                            .getName());
260:
261:                    ent = jarStream.getNextJarEntry();
262:                    assertTrue("should have JarTest", ent != null);
263:                    assertEquals("names should match", "dir/JarTest.java", ent
264:                            .getName());
265:                } catch (IOException e) {
266:                    e.printStackTrace();
267:                    fail("failed test");
268:                }
269:            }
270:
271:            /**
272:             * Check that all files in an a Set are in the jar file
273:             * @param jar the jar file to check
274:             * @param fileNames the set of the names of files
275:             */
276:            private void testArchive(File jar, Set<String> fileNames) {
277:                JarInputStream jarStream = null;
278:                try {
279:                    jarStream = new JarInputStream(new FileInputStream(jar),
280:                            true);
281:
282:                    JarEntry ent = null;
283:                    while ((ent = jarStream.getNextJarEntry()) != null) {
284:                        assertTrue("found " + ent.getName()
285:                                + " should be in list", fileNames.contains(ent
286:                                .getName()));
287:                        fileNames.remove(ent.getName());
288:                    }
289:                } catch (IOException e) {
290:                    fail("couldn't open file");
291:                } finally {
292:                    if (jarStream != null)
293:                        try {
294:                            jarStream.close();
295:                        } catch (IOException e) {
296:                            e.printStackTrace();
297:                        }
298:                }
299:                assertEquals("all listed files should have been in archive", 0,
300:                        fileNames.size());
301:            }
302:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.