KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > projectimport > eclipse > ProjectImporterTestCase


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.projectimport.eclipse;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.BufferedOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.zip.ZipEntry JavaDoc;
31 import java.util.zip.ZipInputStream JavaDoc;
32 import org.netbeans.junit.NbTestCase;
33
34 /**
35  * Provides basic functionality for ProjectImporter tests.
36  *
37  * @author mkrauskopf
38  */

39 public class ProjectImporterTestCase extends NbTestCase {
40     
41     private static final int BUFFER = 2048;
42     
43     /*
44      * If true a lot of information about parsed project will be written to a
45      * console.
46      */

47     private static boolean verbose;
48     
49     /** Creates a new instance of ProjectImporterTestCase */
50     public ProjectImporterTestCase(String JavaDoc name) {
51         super(name);
52     }
53     
54     protected void setUp() throws Exception JavaDoc {
55         super.setUp();
56         /* comment this out to see verbose info */
57         // setVerbose(true);
58
clearWorkDir();
59     }
60     
61     protected void tearDown() throws Exception JavaDoc {
62         super.tearDown();
63         clearWorkDir();
64     }
65     
66     protected void setVerbose(boolean verbose) {
67         ProjectImporterTestCase.verbose = verbose;
68     }
69     
70     /*
71      * XXX - doesn't similar method already exist somewhere in the API?
72      * XXX - If not replace with JarFileSystem as hinted by Radek :)
73      */

74     protected File JavaDoc extractToWorkDir(String JavaDoc archiveFile) throws IOException JavaDoc {
75         ZipInputStream JavaDoc zis = null;
76         BufferedOutputStream JavaDoc dest = null;
77         try {
78             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(new File JavaDoc(getDataDir(), archiveFile));
79             zis = new ZipInputStream JavaDoc(new BufferedInputStream JavaDoc(fis));
80             ZipEntry JavaDoc entry;
81             while((entry = zis.getNextEntry()) != null) {
82                 byte data[] = new byte[BUFFER];
83                 File JavaDoc entryFile = new File JavaDoc(getWorkDir(), entry.getName());
84                 if (entry.isDirectory()) {
85                     entryFile.mkdirs();
86                 } else {
87                     FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(entryFile);
88                     dest = new BufferedOutputStream JavaDoc(fos, BUFFER);
89                     int count;
90                     while ((count = zis.read(data, 0, BUFFER)) != -1) {
91                         dest.write(data, 0, count);
92                     }
93                     dest.flush();
94                 }
95             }
96         } finally {
97             if (zis != null) { zis.close(); }
98             if (dest != null) { dest.close(); }
99         }
100         // return the directory (without ".zip" - convention used here)
101
return new File JavaDoc(getWorkDir(), archiveFile.substring(0, archiveFile.length() - 4));
102     }
103     
104     protected static void printMessage(String JavaDoc message, boolean newLine) {
105         if (verbose) {
106             if (newLine) {
107                 System.out.println(message);
108             } else {
109                 System.out.print(message);
110             }
111         }
112     }
113     
114     protected static void printMessage(String JavaDoc message) {
115         printMessage(message, true);
116     }
117     
118     protected static void printCollection(String JavaDoc name, Collection JavaDoc col) {
119         if (col != null && !col.isEmpty()) {
120             printMessage(" " + name + ":");
121             for (Iterator JavaDoc it = col.iterator(); it.hasNext(); ) {
122                 ClassPathEntry entry = (ClassPathEntry) it.next();
123                 printMessage(" \"" + entry.getRawPath() + "\" ", false);
124                 if (entry.getAbsolutePath() != null) {
125                     printMessage("converted to \"" + entry.getAbsolutePath() + "\"");
126                 } else {
127                     printMessage("cannot be resolved !!!!");
128                 }
129             }
130         }
131     }
132 }
133
Popular Tags