1 19 20 package org.netbeans.modules.java.source; 21 22 import java.util.Collections ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.io.File ; 26 import java.util.Enumeration ; 27 import java.util.LinkedList ; 28 import java.util.Set ; 29 import java.util.SortedSet ; 30 import java.util.TreeSet ; 31 import java.util.jar.JarFile ; 32 import java.util.zip.ZipEntry ; 33 import junit.framework.*; 34 35 36 40 public class TestUtilTest extends TestCase { 41 42 public TestUtilTest( String testName ) { 43 super( testName ); 44 } 45 46 protected void setUp() throws Exception { 47 } 48 49 protected void tearDown() throws Exception { 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 { 58 59 60 File 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 { 72 73 String SAMPLE_FILE = "samples1/EmptyClass.java"; 74 75 File workDir = TestUtil.createWorkFolder(); 76 77 TestUtil.copyFiles( workDir, SAMPLE_FILE ); 78 File sf = new File ( 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 rt = new File ( 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 { 97 98 File workDir = TestUtil.createWorkFolder(); 99 100 TestUtil.copyFiles( workDir, "samples1/EmptyClass.java" ); 101 File sample = new File ( 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 { 113 114 File workDir = TestUtil.createWorkFolder(); 115 116 TestUtil.copyFiles( TestUtil.getJdkDir(), workDir, TestUtil.RT_JAR ); 117 File rt = new File ( workDir, TestUtil.RT_JAR ); 118 JarFile rtJar = new JarFile ( rt ); 119 120 File dest = new File ( workDir, "dest" ); 121 TestUtil.unzip( rtJar, dest ); 123 Set <String > entryNames = new TreeSet <String >(); 124 for( Enumeration <? extends ZipEntry > e = rtJar.entries(); e.hasMoreElements(); ) { 125 addNames( e.nextElement(), entryNames ); 126 } 127 128 Set <String > fileNames = new TreeSet <String >(); 129 addNamesRecursively( dest, fileNames, dest.getPath().length() + 1 ); 130 131 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 143 private void addNames( ZipEntry entry, Set <String > dest ) { 144 145 146 String 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 folder, Set <String > list, int beginIndex ) { 166 167 File [] files = folder.listFiles(); 168 for( File 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 |