1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
/* Copyright (c) 2007 Wayne Meissner, All Rights Reserved
*
* The contents of this file is dual-licensed under 2
* alternative Open Source/Free licenses: LGPL 2.1 or later and
* Apache License 2.0. (starting with JNA version 4.0.0).
*
* You can freely decide which license you want to apply to
* the project.
*
* You may obtain a copy of the LGPL License at:
*
* http://www.gnu.org/licenses/licenses.html
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "LGPL2.1".
*
* You may obtain a copy of the Apache License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/
package com.sun.jna;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import junit.framework.TestCase;
//@SuppressWarnings("unused")
public class TypeMapperTest extends TestCase {
private static final String UNICODE = "[\0444]";
public static interface TestLibrary extends Library {
int returnInt32Argument(boolean b);
int returnInt32Argument(String s);
int returnInt32Argument(Number n);
WString returnWStringArgument(String s);
String returnWStringArgument(WString s);
}
public void testBooleanToIntArgumentConversion() {
final int MAGIC = 0xABEDCF23;
DefaultTypeMapper mapper = new DefaultTypeMapper();
mapper.addToNativeConverter(Boolean.class, new ToNativeConverter() {
@Override
public Object toNative(Object arg, ToNativeContext ctx) {
return Integer.valueOf(Boolean.TRUE.equals(arg) ? MAGIC : 0);
}
@Override
public Class<?> nativeType() {
return Integer.class;
}
});
TestLibrary lib = Native.load("testlib", TestLibrary.class, Collections.singletonMap(Library.OPTION_TYPE_MAPPER, mapper));
assertEquals("Failed to convert Boolean argument to Int", MAGIC, lib.returnInt32Argument(true));
}
public void testStringToIntArgumentConversion() {
DefaultTypeMapper mapper = new DefaultTypeMapper();
mapper.addToNativeConverter(String.class, new ToNativeConverter() {
@Override
public Object toNative(Object arg, ToNativeContext ctx) {
return Integer.valueOf((String) arg, 16);
}
@Override
public Class<?> nativeType() {
return Integer.class;
}
});
final int MAGIC = 0x7BEDCF23;
TestLibrary lib = Native.load("testlib", TestLibrary.class, Collections.singletonMap(Library.OPTION_TYPE_MAPPER, mapper));
assertEquals("Failed to convert String argument to Int", MAGIC,
lib.returnInt32Argument(Integer.toHexString(MAGIC)));
}
public void testStringToWStringArgumentConversion() {
DefaultTypeMapper mapper = new DefaultTypeMapper();
mapper.addToNativeConverter(String.class, new ToNativeConverter() {
@Override
public Object toNative(Object arg, ToNativeContext ctx) {
return new WString(arg.toString());
}
@Override
public Class<?> nativeType() {
return WString.class;
}
});
final String MAGIC = "magic" + UNICODE;
TestLibrary lib = Native.load("testlib", TestLibrary.class, Collections.singletonMap(Library.OPTION_TYPE_MAPPER, mapper));
assertEquals("Failed to convert String argument to WString", new WString(MAGIC),
lib.returnWStringArgument(MAGIC));
}
public void testCharSequenceToIntArgumentConversion() {
DefaultTypeMapper mapper = new DefaultTypeMapper();
mapper.addToNativeConverter(CharSequence.class, new ToNativeConverter() {
@Override
public Object toNative(Object arg, ToNativeContext ctx) {
return Integer.valueOf(((CharSequence)arg).toString(), 16);
}
@Override
public Class<?> nativeType() {
return Integer.class;
}
});
final int MAGIC = 0x7BEDCF23;
TestLibrary lib = Native.load("testlib", TestLibrary.class, Collections.singletonMap(Library.OPTION_TYPE_MAPPER, mapper));
assertEquals("Failed to convert String argument to Int", MAGIC, lib.returnInt32Argument(Integer.toHexString(MAGIC)));
}
public void testNumberToIntArgumentConversion() {
DefaultTypeMapper mapper = new DefaultTypeMapper();
mapper.addToNativeConverter(Double.class, new ToNativeConverter() {
@Override
public Object toNative(Object arg, ToNativeContext ctx) {
return Integer.valueOf(((Double)arg).intValue());
}
@Override
public Class<?> nativeType() {
return Integer.class;
}
});
final int MAGIC = 0x7BEDCF23;
TestLibrary lib = Native.load("testlib", TestLibrary.class, Collections.singletonMap(Library.OPTION_TYPE_MAPPER, mapper));
assertEquals("Failed to convert Double argument to Int", MAGIC,
lib.returnInt32Argument(Double.valueOf(MAGIC)));
}
public void testWStringToStringResultConversion() throws Exception {
final String MAGIC = "magic" + UNICODE;
DefaultTypeMapper mapper = new DefaultTypeMapper();
mapper.addFromNativeConverter(String.class, new FromNativeConverter() {
@Override
public Object fromNative(Object value, FromNativeContext ctx) {
if (value == null) {
return null;
}
return value.toString();
}
@Override
public Class<?> nativeType() {
return WString.class;
}
});
TestLibrary lib = Native.load("testlib", TestLibrary.class, Collections.singletonMap(Library.OPTION_TYPE_MAPPER, mapper));
assertEquals("Failed to convert WString result to String", MAGIC, lib.returnWStringArgument(new WString(MAGIC)));
}
public static interface BooleanTestLibrary extends Library {
boolean returnInt32Argument(boolean b);
}
public void testIntegerToBooleanResultConversion() throws Exception {
final int MAGIC = 0xABEDCF23;
DefaultTypeMapper mapper = new DefaultTypeMapper();
mapper.addToNativeConverter(Boolean.class, new ToNativeConverter() {
@Override
public Object toNative(Object value, ToNativeContext ctx) {
return Integer.valueOf(Boolean.TRUE.equals(value) ? MAGIC : 0);
}
@Override
public Class<?> nativeType() {
return Integer.class;
}
});
mapper.addFromNativeConverter(Boolean.class, new FromNativeConverter() {
@Override
public Object fromNative(Object value, FromNativeContext context) {
return Boolean.valueOf(((Integer) value).intValue() == MAGIC);
}
@Override
public Class<?> nativeType() {
return Integer.class;
}
});
BooleanTestLibrary lib = Native.load("testlib", BooleanTestLibrary.class, Collections.singletonMap(Library.OPTION_TYPE_MAPPER, mapper));
assertEquals("Failed to convert integer return to boolean TRUE", true, lib.returnInt32Argument(true));
assertEquals("Failed to convert integer return to boolean FALSE", false, lib.returnInt32Argument(false));
}
public static interface StructureTestLibrary extends Library {
public static class TestStructure extends Structure {
public TestStructure(TypeMapper mapper) {
super(mapper);
}
public boolean data;
@Override
protected List<String> getFieldOrder() {
return Arrays.asList("data");
}
}
}
public void testStructureConversion() throws Exception {
DefaultTypeMapper mapper = new DefaultTypeMapper();
TypeConverter converter = new TypeConverter() {
@Override
public Object toNative(Object value, ToNativeContext ctx) {
return Integer.valueOf(Boolean.TRUE.equals(value) ? 1 : 0);
}
@Override
public Object fromNative(Object value, FromNativeContext context) {
return Boolean.valueOf(((Integer)value).intValue() == 1);
}
@Override
public Class<?> nativeType() {
return Integer.class;
}
};
mapper.addTypeConverter(Boolean.class, converter);
Native.load("testlib", StructureTestLibrary.class, Collections.singletonMap(Library.OPTION_TYPE_MAPPER, mapper));
StructureTestLibrary.TestStructure s = new StructureTestLibrary.TestStructure(mapper);
assertEquals("Wrong native size", 4, s.size());
s.data = true;
s.write();
assertEquals("Wrong value written", 1, s.getPointer().getInt(0));
s.getPointer().setInt(0, 0);
s.read();
assertFalse("Wrong value read", s.data);
}
public static enum Enumeration {
STATUS_0(0), STATUS_1(1), STATUS_ERROR(-1);
private final int code;
Enumeration(int code) { this.code = code; }
public int getCode() { return code; }
public static Enumeration fromCode(int code) {
switch(code) {
case 0:
return STATUS_0;
case 1:
return STATUS_1;
default:
return STATUS_ERROR;
}
}
}
public static interface EnumerationTestLibrary extends Library {
Enumeration returnInt32Argument(Enumeration arg);
@Structure.FieldOrder({"field"})
class MinTestStructure extends Structure {
public Enumeration field;
}
MinTestStructure testStructurePointerArgument(MinTestStructure s);
}
public void testEnumConversion() throws Exception {
DefaultTypeMapper mapper = new DefaultTypeMapper();
TypeConverter converter = new TypeConverter() {
@Override
public Object toNative(Object value, ToNativeContext ctx) {
if(value == null) {
// NULL needs to be explicityl handled for size calculation
// in structure use
return Enumeration.STATUS_ERROR.getCode();
} else {
return ((Enumeration)value).getCode();
}
}
@Override
public Object fromNative(Object value, FromNativeContext context) {
return Enumeration.fromCode(((Integer)value));
}
@Override
public Class<?> nativeType() {
return Integer.class;
}
};
mapper.addTypeConverter(Enumeration.class, converter);
EnumerationTestLibrary lib = Native.load("testlib", EnumerationTestLibrary.class, Collections.singletonMap(Library.OPTION_TYPE_MAPPER, mapper));
assertEquals("Enumeration improperly converted", Enumeration.STATUS_0, lib.returnInt32Argument(Enumeration.STATUS_0));
assertEquals("Enumeration improperly converted", Enumeration.STATUS_1, lib.returnInt32Argument(Enumeration.STATUS_1));
EnumerationTestLibrary.MinTestStructure struct = new EnumerationTestLibrary.MinTestStructure();
struct.field = Enumeration.STATUS_0;
assertEquals("Enumeration in structure improperly converted", Enumeration.STATUS_0, lib.testStructurePointerArgument(struct).field);
struct.field = Enumeration.STATUS_1;
assertEquals("Enumeration in structure improperly converted", Enumeration.STATUS_1, lib.testStructurePointerArgument(struct).field);
}
public static void main(String[] args) {
junit.textui.TestRunner.run(TypeMapperTest.class);
}
}
|