1 | /* |
2 | * IntCallTest1Win32.java |
3 | * |
4 | * Created on 17.09.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 copy |
11 | * of this software and associated documentation files (the "Software"), to deal |
12 | * in the Software without restriction, including without limitation the rights |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
14 | * copies of the Software, and to permit persons to whom the Software is |
15 | * furnished to do so, subject to the following conditions: |
16 | * |
17 | * The above copyright notice and this permission notice shall be included in |
18 | * all copies or substantial portions of the Software. |
19 | * |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
26 | * SOFTWARE. |
27 | * |
28 | */ |
29 | package com.eaio.nativecall; |
30 | import java.io.File; |
31 | |
32 | import junit.framework.TestCase; |
33 | |
34 | /** |
35 | * Test case for the {@link com.eaio.nativecall.IntCall} class. |
36 | * |
37 | * @author <a href="mailto:jb@eaio.com">Johann Burkard</a> |
38 | * @version $Id: IntCallTest1Win32.java,v 1.3 2006/04/12 22:11:41 grnull Exp $ |
39 | */ |
40 | public class IntCallTest1Win32 extends TestCase { |
41 | |
42 | static { |
43 | try { |
44 | NativeCall.init(); |
45 | } |
46 | catch (Throwable thrw) { |
47 | thrw.printStackTrace(); |
48 | fail(thrw.getLocalizedMessage()); |
49 | } |
50 | } |
51 | |
52 | /** |
53 | * Constructor for IntCallTest1Win32. |
54 | * @param arg0 |
55 | */ |
56 | public IntCallTest1Win32(String arg0) { |
57 | super(arg0); |
58 | } |
59 | |
60 | public static void main(String[] args) { |
61 | junit.awtui.TestRunner.run(IntCallTest1Win32.class); |
62 | } |
63 | |
64 | /* |
65 | * Test for int executeCall() |
66 | */ |
67 | public void testExecuteCall() { |
68 | IntCall cT = new IntCall("GetCurrentThread"); |
69 | assertEquals(0, cT.getLastErrorCode()); |
70 | int threadHandle = cT.executeCall(); |
71 | assertEquals(0, cT.getLastErrorCode()); |
72 | assertTrue(threadHandle != 0); |
73 | cT.destroy(); |
74 | assertEquals(0, cT.getLastErrorCode()); |
75 | } |
76 | |
77 | /* |
78 | * Test for int executeCall(Object) |
79 | */ |
80 | public void testExecuteCallObject() { |
81 | |
82 | IntCall cT = new IntCall("GetCurrentThread"); |
83 | |
84 | int threadHandle = cT.executeCall(); |
85 | |
86 | IntCall tP = new IntCall("GetThreadPriority"); |
87 | |
88 | int threadPriority = tP.executeCall(new Integer(threadHandle)); |
89 | |
90 | Thread.currentThread().setPriority(Thread.MAX_PRIORITY); |
91 | |
92 | Object o = new Object(); |
93 | synchronized (o) { |
94 | try { |
95 | o.wait(1000); |
96 | } |
97 | catch (InterruptedException ex) {} |
98 | } |
99 | |
100 | tP.destroy(); |
101 | cT.destroy(); |
102 | |
103 | synchronized (o) { |
104 | try { |
105 | o.wait(1000); |
106 | } |
107 | catch (InterruptedException ex) {} |
108 | } |
109 | |
110 | } |
111 | |
112 | /* |
113 | * Test for int executeCall(Object[]) |
114 | */ |
115 | public void testExecuteCallObjectArray() { |
116 | |
117 | // Copying files for fun |
118 | |
119 | IntCall cF = new IntCall("kernel32", "CopyFileA"); |
120 | |
121 | assertEquals("{ IntCall: module = kernel32, function = CopyFileA }", cF.toString()); |
122 | |
123 | File copy = new File("documentation_copy.txt"); |
124 | if (copy.exists()) |
125 | copy.delete(); |
126 | |
127 | boolean copied = |
128 | cF.executeBooleanCall( |
129 | new Object[] { |
130 | "documentation/copying.txt", |
131 | "documentation_copy.txt", |
132 | Boolean.FALSE }); |
133 | |
134 | cF.destroy(); |
135 | |
136 | assertTrue(copied); |
137 | assertTrue(copy.exists()); |
138 | assertTrue( |
139 | copy.length() == new File("documentation/copying.txt").length()); |
140 | |
141 | copy.delete(); |
142 | |
143 | // Setting file attributes |
144 | |
145 | File copying = new File("documentation/copying.txt"); |
146 | assertTrue(copying.exists()); |
147 | |
148 | IntCall getFileAttributesA = new IntCall("GetFileAttributesA"); |
149 | |
150 | int attributes = |
151 | getFileAttributesA.executeCall(copying.getAbsolutePath()); |
152 | |
153 | assertEquals(0, attributes & 0xdf); // might be compressed |
154 | |
155 | IntCall setFileAttributesA = new IntCall("SetFileAttributesA"); |
156 | |
157 | assertTrue( |
158 | setFileAttributesA.executeBooleanCall( |
159 | new Object[] { |
160 | copying.getAbsolutePath(), |
161 | new Integer(attributes | 2)})); |
162 | |
163 | assertEquals( |
164 | 2, |
165 | getFileAttributesA.executeCall(copying.getAbsolutePath()) & 0xdf); |
166 | |
167 | assertTrue( |
168 | setFileAttributesA.executeBooleanCall( |
169 | new Object[] { |
170 | copying.getAbsolutePath(), |
171 | new Integer(attributes)})); |
172 | |
173 | assertEquals( |
174 | 0, |
175 | getFileAttributesA.executeCall(copying.getAbsolutePath()) & 0xdf); |
176 | |
177 | getFileAttributesA.destroy(); |
178 | |
179 | setFileAttributesA.destroy(); |
180 | |
181 | // Setting file attributes (Unicode) |
182 | |
183 | IntCall getFileAttributesW = new IntCall("GetFileAttributesW"); |
184 | |
185 | attributes = |
186 | getFileAttributesW.executeCall( |
187 | "\\\\?\\" + copying.getAbsolutePath()); |
188 | |
189 | assertEquals(0, attributes & 0xdf); // might be compressed |
190 | |
191 | IntCall setFileAttributesW = new IntCall("SetFileAttributesW"); |
192 | |
193 | assertTrue( |
194 | setFileAttributesW.executeBooleanCall( |
195 | new Object[] { |
196 | "\\\\?\\" + copying.getAbsolutePath(), |
197 | new Integer(attributes | 2)})); |
198 | |
199 | assertEquals( |
200 | 2, |
201 | getFileAttributesW.executeCall( |
202 | "\\\\?\\" + copying.getAbsolutePath()) |
203 | & 0xdf); |
204 | |
205 | assertTrue( |
206 | setFileAttributesW.executeBooleanCall( |
207 | new Object[] { |
208 | copying.getAbsolutePath(), |
209 | new Integer(attributes)})); |
210 | |
211 | assertEquals( |
212 | 0, |
213 | getFileAttributesW.executeCall( |
214 | "\\\\?\\" + copying.getAbsolutePath()) |
215 | & 0xdf); |
216 | |
217 | getFileAttributesW.destroy(); |
218 | |
219 | setFileAttributesW.destroy(); |
220 | |
221 | IntCall messageBoxA = new IntCall("user32", "MessageBoxA"); |
222 | |
223 | messageBoxA.executeCall( |
224 | new Object[] { |
225 | new Integer(0), |
226 | "No native code required!", |
227 | "NativeCall", |
228 | new Integer(0)}); |
229 | |
230 | messageBoxA.destroy(); |
231 | |
232 | // Enumerating process IDs |
233 | |
234 | IntCall enumProcesses = new IntCall("psapi", "EnumProcesses"); |
235 | |
236 | byte[] buffer = new byte[1024]; |
237 | Holder length = new Holder(new Integer(0)); |
238 | |
239 | boolean success = |
240 | enumProcesses.executeBooleanCall( |
241 | new Object[] { buffer, new Integer(buffer.length), length }); |
242 | |
243 | assertTrue(success); |
244 | assertTrue(((Integer) length.get()).intValue() > 0); |
245 | |
246 | enumProcesses.destroy(); |
247 | |
248 | } |
249 | |
250 | } |