KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > eaio > nativecall > FileManagementTest1Win32


1 /*
2  * FileManagementTest1Win32.java
3  *
4  * Created on 11.12.2004
5  *
6  * eaio: NativeCall - calling operating system methods from Java
7  * Copyright (c) 2004-2006 Johann Burkard (<mailto:jb@eaio.com>)
8  * <http://eaio.com>
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
23  * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
24  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26  * USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  */

29
30 package com.eaio.nativecall;
31
32 import java.io.File JavaDoc;
33 import java.security.AccessController JavaDoc;
34 import java.util.Arrays JavaDoc;
35
36 import junit.framework.TestCase;
37 import sun.security.action.GetPropertyAction;
38
39 /**
40  * Some more complicated tests for general file management functions.
41  *
42  * @author <a HREF="mailto:jb@eaio.com">Johann Burkard</a>
43  * @version $Id: FileManagementTest1Win32.java,v 1.3 2006/04/12 22:11:41 grnull Exp $
44  */

45 public class FileManagementTest1Win32 extends TestCase {
46
47     static {
48         try {
49             NativeCall.init();
50         }
51         catch (Throwable JavaDoc thrw) {
52             thrw.printStackTrace();
53             fail(thrw.getLocalizedMessage());
54         }
55     }
56
57     /**
58      * Constructor for FileManagementTest1Win32.
59      * @param arg0
60      */

61     public FileManagementTest1Win32(String JavaDoc arg0) {
62         super(arg0);
63     }
64
65     public static void main(String JavaDoc[] 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 JavaDoc[] { new Integer JavaDoc(byteBuf.length), byteBuf });
78
79         String JavaDoc path = new String JavaDoc(byteBuf, 0, bufLength);
80
81         File JavaDoc tempDir = new File JavaDoc(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 JavaDoc(((String JavaDoc) 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 JavaDoc[] { new Integer JavaDoc(charBuf.length), charBuf });
100         assertEquals(0, getTempPathW.getLastErrorCode());
101
102         path = new String JavaDoc(charBuf, 0, bufLength);
103
104         tempDir = new File JavaDoc(path);
105         assertTrue(tempDir.exists());
106         assertTrue(tempDir.isDirectory());
107
108         assertEquals(
109             tempDir,
110             new File JavaDoc(((String JavaDoc) AccessController.doPrivileged(a))));
111
112         getTempPathW.destroy();
113         assertEquals(0, getTempPathW.getLastErrorCode());
114
115     }
116
117     public void testCreateFile() {
118
119         File JavaDoc target = new File JavaDoc("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         // Different methods to do the same
128

129         int fileHandle = 0;
130
131         try {
132
133             fileHandle =
134                 createFileA.executeCall(
135                     new Object JavaDoc[] {
136                         "documents/file_management_test.txt",
137                         new Integer JavaDoc(0x80000000 | 0x40000000 | 0x00010000),
138                         null,
139                         securityAttributes,
140                         new Integer JavaDoc(2),
141                         null,
142                         null });
143
144         }
145         catch (NullPointerException JavaDoc 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 JavaDoc(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 JavaDoc(0));
174         Holder maximumComponentLength = new Holder(new Integer JavaDoc(0));
175         Holder fileSystemFlags = new Holder(new Integer JavaDoc(0));
176
177         int result =
178             getVolumeInformationA.executeCall(
179                 new Object JavaDoc[] {
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 JavaDoc) 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 JavaDoc[] { "c:\\autoexec.bat", new Integer JavaDoc(0), buf });
209         getFileAttributesExA.destroy();
210         assertTrue(0 != success);
211
212 // try {
213
// new HexDumpEncoder().encodeBuffer(buf, System.out);
214
// }
215
// catch (IOException ex) {
216
// ex.printStackTrace();
217
// fail(ex.toString());
218
// }
219
//
220
// if (success == 0) {
221
// IntCall getLastError = new IntCall("GetLastError");
222
// System.out.println(getLastError.executeCall());
223
// getLastError.destroy();
224
// }
225

226     }
227
228 }
229
Popular Tags