KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > TestUtilTest


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.java.source;
21
22 import java.util.Collections JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.io.File JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.SortedSet JavaDoc;
30 import java.util.TreeSet JavaDoc;
31 import java.util.jar.JarFile JavaDoc;
32 import java.util.zip.ZipEntry JavaDoc;
33 import junit.framework.*;
34
35
36 /** Makes sure the TestUtility class works as expected.
37  *
38  * @author Petr Hrebejk
39  */

40 public class TestUtilTest extends TestCase {
41
42     public TestUtilTest( String JavaDoc testName ) {
43         super( testName );
44     }
45
46     protected void setUp() throws Exception JavaDoc {
47     }
48
49     protected void tearDown() throws Exception JavaDoc {
50     }
51
52     public static Test suite() {
53         TestSuite suite = new TestSuite( TestUtilTest.class );
54         return suite;
55     }
56
57     public void testCreateAndRemoveWorkDir() throws Exception JavaDoc {
58     
59     
60         File JavaDoc workDir = TestUtil.createWorkFolder();
61         
62         assertEquals( "WorkDir must exist", true, workDir.exists() );
63         assertEquals( "WorkDir must be readable", true, workDir.canRead() );
64         assertEquals( "WorkDir must be writeable", true, workDir.canWrite() );
65         
66         TestUtil.removeWorkFolder( workDir );
67         assertEquals( "WorkDir must disapear", false, workDir.exists() );
68                 
69     }
70     
71     public void testCopyResourceFile() throws Exception JavaDoc {
72         
73     String JavaDoc SAMPLE_FILE = "samples1/EmptyClass.java";
74     
75         File JavaDoc workDir = TestUtil.createWorkFolder();
76         
77         TestUtil.copyFiles( workDir, SAMPLE_FILE );
78         File JavaDoc sf = new File JavaDoc( workDir, SAMPLE_FILE );
79         
80         assertEquals( "WorkDir must exist", true, sf.exists() );
81         assertEquals( "WorkDir must be readable", true, sf.canRead() );
82         assertEquals( "WorkDir must be writeable", true, sf.canWrite() );
83         
84     TestUtil.copyFiles( TestUtil.getJdkDir(), workDir, TestUtil.RT_JAR );
85         File JavaDoc rt = new File JavaDoc( workDir, TestUtil.RT_JAR );
86         
87         assertEquals( "WorkDir must exist", true, rt.exists() );
88         assertEquals( "WorkDir must be readable", true, rt.canRead() );
89         assertEquals( "WorkDir must be writeable", true, rt.canWrite() );
90     
91         TestUtil.removeWorkFolder( workDir );
92         assertEquals( "WorkDir must disapear", false, workDir.exists() );
93         
94     }
95     
96     public void testCopySampleFile() throws Exception JavaDoc {
97         
98         File JavaDoc workDir = TestUtil.createWorkFolder();
99         
100         TestUtil.copyFiles( workDir, "samples1/EmptyClass.java" );
101         File JavaDoc sample = new File JavaDoc( workDir, "samples1/EmptyClass.java" );
102         
103         assertEquals( "WorkDir must exist", true, sample.exists() );
104         assertEquals( "WorkDir must be readable", true, sample.canRead() );
105         assertEquals( "WorkDir must be writeable", true, sample.canWrite() );
106         
107         TestUtil.removeWorkFolder( workDir );
108         assertEquals( "WorkDir must disapear", false, workDir.exists() );
109         
110     }
111     
112     public void testUnzip() throws Exception JavaDoc {
113         
114         File JavaDoc workDir = TestUtil.createWorkFolder();
115         
116         TestUtil.copyFiles( TestUtil.getJdkDir(), workDir, TestUtil.RT_JAR );
117         File JavaDoc rt = new File JavaDoc( workDir, TestUtil.RT_JAR );
118         JarFile JavaDoc rtJar = new JarFile JavaDoc( rt );
119         
120         File JavaDoc dest = new File JavaDoc( workDir, "dest" );
121         TestUtil.unzip( rtJar, dest ); // Unzip Jar file
122

123         Set JavaDoc<String JavaDoc> entryNames = new TreeSet JavaDoc<String JavaDoc>();
124         for( Enumeration JavaDoc<? extends ZipEntry JavaDoc> e = rtJar.entries(); e.hasMoreElements(); ) {
125             addNames( e.nextElement(), entryNames );
126         }
127         
128         Set JavaDoc<String JavaDoc> fileNames = new TreeSet JavaDoc<String JavaDoc>();
129         addNamesRecursively( dest, fileNames, dest.getPath().length() + 1 );
130                 
131         // TestUtil.collectionDiff( fileNames, entryNames );
132

133         assertEquals( "Sets should have the same size", entryNames.size(), fileNames.size() );
134         assertEquals( "Lists should be identical", entryNames, fileNames );
135         
136         TestUtil.removeWorkFolder( workDir );
137         assertEquals( "WorkDir must disapear", false, workDir.exists() );
138         
139     }
140     
141     // Private methods ---------------------------------------------------------
142

143     private void addNames( ZipEntry JavaDoc entry, Set JavaDoc<String JavaDoc> dest ) {
144     
145         
146         String JavaDoc name = entry.getName();
147         if ( entry.isDirectory() ) {
148             name = name.substring( name.length() - 1 );
149         }
150         
151         int index = name.indexOf( '/', 0 );
152         while( index != -1 ) {
153             if ( index != 0 ) {
154                 dest.add( name.substring( 0, index ) );
155             }
156             index = name.indexOf( '/', index + 1 );
157         }
158         
159         if ( !"/".equals( name ) ) {
160             dest.add( name );
161         }
162                 
163     }
164     
165     private void addNamesRecursively( File JavaDoc folder, Set JavaDoc<String JavaDoc> list, int beginIndex ) {
166         
167         File JavaDoc[] files = folder.listFiles();
168         for( File JavaDoc f : files ) {
169             list.add( f.getPath().substring( beginIndex ) );
170             if ( f.isDirectory() ) {
171                 addNamesRecursively( f, list, beginIndex );
172             }
173         }
174                 
175     }
176     
177     
178 }
179
Popular Tags