1 29 30 package com.eaio.nativecall; 31 32 import java.io.File ; 33 import java.security.AccessController ; 34 import java.util.Arrays ; 35 36 import junit.framework.TestCase; 37 import sun.security.action.GetPropertyAction; 38 39 45 public class FileManagementTest1Win32 extends TestCase { 46 47 static { 48 try { 49 NativeCall.init(); 50 } 51 catch (Throwable thrw) { 52 thrw.printStackTrace(); 53 fail(thrw.getLocalizedMessage()); 54 } 55 } 56 57 61 public FileManagementTest1Win32(String arg0) { 62 super(arg0); 63 } 64 65 public static void main(String [] args) { 66 junit.awtui.TestRunner.run(FileManagementTest1Win32.class); 67 } 68 69 public void testGetTempPath() { 70 71 IntCall getTempPathA = new IntCall("GetTempPathA"); 72 73 byte[] byteBuf = new byte[256]; 74 75 int bufLength = 76 getTempPathA.executeCall( 77 new Object [] { new Integer (byteBuf.length), byteBuf }); 78 79 String path = new String (byteBuf, 0, bufLength); 80 81 File tempDir = new File (path); 82 assertTrue(tempDir.exists()); 83 assertTrue(tempDir.isDirectory()); 84 85 GetPropertyAction a = new GetPropertyAction("java.io.tmpdir"); 86 87 assertEquals( 88 tempDir, 89 new File (((String ) AccessController.doPrivileged(a)))); 90 91 getTempPathA.destroy(); 92 93 IntCall getTempPathW = new IntCall("kernel32", "GetTempPathW"); 94 95 char[] charBuf = new char[256]; 96 97 bufLength = 98 getTempPathW.executeCall( 99 new Object [] { new Integer (charBuf.length), charBuf }); 100 assertEquals(0, getTempPathW.getLastErrorCode()); 101 102 path = new String (charBuf, 0, bufLength); 103 104 tempDir = new File (path); 105 assertTrue(tempDir.exists()); 106 assertTrue(tempDir.isDirectory()); 107 108 assertEquals( 109 tempDir, 110 new File (((String ) AccessController.doPrivileged(a)))); 111 112 getTempPathW.destroy(); 113 assertEquals(0, getTempPathW.getLastErrorCode()); 114 115 } 116 117 public void testCreateFile() { 118 119 File target = new File ("documents/file_management_test.txt"); 120 121 IntCall createFileA = new IntCall("CreateFileA"); 122 IntCall closeHandle = new IntCall("CloseHandle"); 123 124 byte[] securityAttributes = new byte[12]; 125 securityAttributes[0] = (byte) 12; 126 127 129 int fileHandle = 0; 130 131 try { 132 133 fileHandle = 134 createFileA.executeCall( 135 new Object [] { 136 "documents/file_management_test.txt", 137 new Integer (0x80000000 | 0x40000000 | 0x00010000), 138 null, 139 securityAttributes, 140 new Integer (2), 141 null, 142 null }); 143 144 } 145 catch (NullPointerException ex) { 146 ex.printStackTrace(); 147 fail(ex.getLocalizedMessage()); 148 } 149 150 assertEquals(0, createFileA.getLastErrorCode()); 151 assertTrue(target.exists()); 152 153 boolean closed = 154 closeHandle.executeBooleanCall(new Integer (fileHandle)); 155 assertEquals(0, closeHandle.getLastErrorCode()); 156 157 assertTrue(closed); 158 159 createFileA.destroy(); 160 assertEquals(0, createFileA.getLastErrorCode()); 161 closeHandle.destroy(); 162 assertEquals(0, closeHandle.getLastErrorCode()); 163 164 target.delete(); 165 166 assertFalse(target.exists()); 167 } 168 169 public void testGetVolumeInformation() { 170 171 IntCall getVolumeInformationA = new IntCall("GetVolumeInformationA"); 172 173 Holder volumeSerialNumber = new Holder(new Integer (0)); 174 Holder maximumComponentLength = new Holder(new Integer (0)); 175 Holder fileSystemFlags = new Holder(new Integer (0)); 176 177 int result = 178 getVolumeInformationA.executeCall( 179 new Object [] { 180 "c:\\", 181 null, 182 null, 183 volumeSerialNumber, 184 maximumComponentLength, 185 fileSystemFlags, 186 null, 187 null }); 188 assertEquals(0, getVolumeInformationA.getLastErrorCode()); 189 assertNull(getVolumeInformationA.getLastError()); 190 getVolumeInformationA.destroy(); 191 assertEquals(0, getVolumeInformationA.getLastErrorCode()); 192 assertNull(getVolumeInformationA.getLastError()); 193 194 assertTrue(result != 0); 195 assertTrue(((Integer ) volumeSerialNumber.get()).intValue() != 0); 196 197 } 198 199 public void testGetFileAttributesEx() { 200 201 IntCall getFileAttributesExA = new IntCall("GetFileAttributesExA"); 202 203 byte[] buf = new byte[36]; 204 Arrays.fill(buf, (byte) 0xff); 205 206 int success = 207 getFileAttributesExA.executeCall( 208 new Object [] { "c:\\autoexec.bat", new Integer (0), buf }); 209 getFileAttributesExA.destroy(); 210 assertTrue(0 != success); 211 212 226 } 227 228 } 229 | Popular Tags |