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.projectimport.eclipse;
043:
044: import java.io.BufferedInputStream;
045: import java.io.BufferedOutputStream;
046: import java.io.File;
047: import java.io.FileInputStream;
048: import java.io.FileOutputStream;
049: import java.io.IOException;
050: import java.util.Collection;
051: import java.util.Iterator;
052: import java.util.zip.ZipEntry;
053: import java.util.zip.ZipInputStream;
054: import org.netbeans.junit.NbTestCase;
055:
056: /**
057: * Provides basic functionality for ProjectImporter tests.
058: *
059: * @author mkrauskopf
060: */
061: public class ProjectImporterTestCase extends NbTestCase {
062:
063: private static final int BUFFER = 2048;
064:
065: /*
066: * If true a lot of information about parsed project will be written to a
067: * console.
068: */
069: private static boolean verbose;
070:
071: /** Creates a new instance of ProjectImporterTestCase */
072: public ProjectImporterTestCase(String name) {
073: super (name);
074: }
075:
076: protected void setUp() throws Exception {
077: super .setUp();
078: /* comment this out to see verbose info */
079: // setVerbose(true);
080: clearWorkDir();
081: }
082:
083: protected void tearDown() throws Exception {
084: super .tearDown();
085: clearWorkDir();
086: }
087:
088: protected void setVerbose(boolean verbose) {
089: ProjectImporterTestCase.verbose = verbose;
090: }
091:
092: /*
093: * XXX - doesn't similar method already exist somewhere in the API?
094: * XXX - If not replace with JarFileSystem as hinted by Radek :)
095: */
096: protected File extractToWorkDir(String archiveFile)
097: throws IOException {
098: ZipInputStream zis = null;
099: BufferedOutputStream dest = null;
100: try {
101: FileInputStream fis = new FileInputStream(new File(
102: getDataDir(), archiveFile));
103: zis = new ZipInputStream(new BufferedInputStream(fis));
104: ZipEntry entry;
105: while ((entry = zis.getNextEntry()) != null) {
106: byte data[] = new byte[BUFFER];
107: File entryFile = new File(getWorkDir(), entry.getName());
108: if (entry.isDirectory()) {
109: entryFile.mkdirs();
110: } else {
111: FileOutputStream fos = new FileOutputStream(
112: entryFile);
113: dest = new BufferedOutputStream(fos, BUFFER);
114: int count;
115: while ((count = zis.read(data, 0, BUFFER)) != -1) {
116: dest.write(data, 0, count);
117: }
118: dest.flush();
119: }
120: }
121: } finally {
122: if (zis != null) {
123: zis.close();
124: }
125: if (dest != null) {
126: dest.close();
127: }
128: }
129: // return the directory (without ".zip" - convention used here)
130: return new File(getWorkDir(), archiveFile.substring(0,
131: archiveFile.length() - 4));
132: }
133:
134: protected static void printMessage(String message, boolean newLine) {
135: if (verbose) {
136: if (newLine) {
137: System.out.println(message);
138: } else {
139: System.out.print(message);
140: }
141: }
142: }
143:
144: protected static void printMessage(String message) {
145: printMessage(message, true);
146: }
147:
148: protected static void printCollection(String name, Collection col) {
149: if (col != null && !col.isEmpty()) {
150: printMessage(" " + name + ":");
151: for (Iterator it = col.iterator(); it.hasNext();) {
152: ClassPathEntry entry = (ClassPathEntry) it.next();
153: printMessage(" \"" + entry.getRawPath() + "\" ",
154: false);
155: if (entry.getAbsolutePath() != null) {
156: printMessage("converted to \""
157: + entry.getAbsolutePath() + "\"");
158: } else {
159: printMessage("cannot be resolved !!!!");
160: }
161: }
162: }
163: }
164: }
|