Skip to content

Commit 1390689

Browse files
Merge pull request #1005 from matthiasblaesing/enum_struct
Improve test for using TypeMapper with Enums
2 parents 0aacf71 + a761649 commit 1390689

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

build.xml

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project name="JNA" default="default" basedir="." xmlns:artifact="antlib:org.apache.maven.artifact.ant">
2+
<project name="JNA" default="default" basedir="." xmlns:artifact="antlib:org.apache.maven.artifact.ant" xmlns:if="ant:if">
33
<description>Builds and tests JNA</description>
44

55
<!-- Default build compiles all platform-independent stuff as well
@@ -1127,6 +1127,12 @@ cd ..
11271127
<target name="test" depends="-enable-native,jar,compile-tests" unless="cross-compile"
11281128
description="Run all unit tests">
11291129
<property name="test.fork" value="yes"/>
1130+
<property name="test.forkmode" value="perTest"/>
1131+
1132+
<condition property="test.jdwp" value="-Xrunjdwp:transport=dt_socket,address=${test.debugport},server=y,suspend=y">
1133+
<isset property="test.debugport" />
1134+
</condition>
1135+
11301136
<property name="reports.junit" location="${reports}/junit/${os.prefix}"/>
11311137
<property name="results.junit" location="${build}/junit-results/${os.prefix}"/>
11321138
<mkdir dir="${results.junit}"/>
@@ -1148,6 +1154,7 @@ cd ..
11481154
</and>
11491155
</condition>
11501156
<property name="tests.platform" value=""/>
1157+
<property name="tests.include" value="com/sun/jna/*Test.java"/>
11511158
<property name="tests.exclude" value=""/>
11521159
<property name="tests.exclude-patterns" value=""/>
11531160
<condition property="java.awt.headless" value="true">
@@ -1156,8 +1163,8 @@ cd ..
11561163
<propertyset id="headless">
11571164
<propertyref prefix="java.awt.headless"/>
11581165
</propertyset>
1159-
<junit fork="${test.fork}" failureproperty="testfailure" tempdir="${build}">
1160-
<!--<jvmarg value="-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y" />-->
1166+
<junit fork="${test.fork}" forkmode="${test.forkmode}" failureproperty="testfailure" tempdir="${build}">
1167+
<jvmarg if:set="test.jdwp" value="${test.jdwp}" />
11611168
<!-- optionally run headless -->
11621169
<syspropertyset refid="headless"/>
11631170
<!-- avoid VM conflicts with JNA protected mode -->
@@ -1173,7 +1180,7 @@ cd ..
11731180
<formatter type="xml"/>
11741181
<batchtest todir="${results.junit}">
11751182
<fileset dir="${test.src}" excludes="${tests.exclude-patterns}">
1176-
<include name="com/sun/jna/*Test.java"/>
1183+
<include name="${tests.include}"/>
11771184
<include name="${tests.platform}"/>
11781185
<exclude name="${tests.exclude}"/>
11791186
</fileset>

test/com/sun/jna/TypeMapperTest.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -232,28 +232,49 @@ public static Enumeration fromCode(int code) {
232232
}
233233
}
234234
}
235+
235236
public static interface EnumerationTestLibrary extends Library {
236237
Enumeration returnInt32Argument(Enumeration arg);
238+
239+
@Structure.FieldOrder({"field"})
240+
class MinTestStructure extends Structure {
241+
public Enumeration field;
242+
}
243+
MinTestStructure testStructurePointerArgument(MinTestStructure s);
237244
}
245+
238246
public void testEnumConversion() throws Exception {
239247
DefaultTypeMapper mapper = new DefaultTypeMapper();
240248
TypeConverter converter = new TypeConverter() {
241249
@Override
242250
public Object toNative(Object value, ToNativeContext ctx) {
243-
return Integer.valueOf(((Enumeration)value).getCode());
251+
if(value == null) {
252+
// NULL needs to be explicityl handled for size calculation
253+
// in structure use
254+
return Enumeration.STATUS_ERROR.getCode();
255+
} else {
256+
return ((Enumeration)value).getCode();
257+
}
244258
}
245259
@Override
246260
public Object fromNative(Object value, FromNativeContext context) {
247-
return Enumeration.fromCode(((Integer)value).intValue());
261+
return Enumeration.fromCode(((Integer)value));
248262
}
249263
@Override
250264
public Class<?> nativeType() {
251265
return Integer.class;
252266
}
253267
};
268+
254269
mapper.addTypeConverter(Enumeration.class, converter);
255270
EnumerationTestLibrary lib = Native.load("testlib", EnumerationTestLibrary.class, Collections.singletonMap(Library.OPTION_TYPE_MAPPER, mapper));
271+
assertEquals("Enumeration improperly converted", Enumeration.STATUS_0, lib.returnInt32Argument(Enumeration.STATUS_0));
256272
assertEquals("Enumeration improperly converted", Enumeration.STATUS_1, lib.returnInt32Argument(Enumeration.STATUS_1));
273+
EnumerationTestLibrary.MinTestStructure struct = new EnumerationTestLibrary.MinTestStructure();
274+
struct.field = Enumeration.STATUS_0;
275+
assertEquals("Enumeration in structure improperly converted", Enumeration.STATUS_0, lib.testStructurePointerArgument(struct).field);
276+
struct.field = Enumeration.STATUS_1;
277+
assertEquals("Enumeration in structure improperly converted", Enumeration.STATUS_1, lib.testStructurePointerArgument(struct).field);
257278
}
258279

259280
public static void main(String[] args) {

0 commit comments

Comments
 (0)