|
| 1 | +/* Copyright (c) 2024 Matthias Bläsing, All Rights Reserved |
| 2 | + * |
| 3 | + * The contents of this file is dual-licensed under 2 |
| 4 | + * alternative Open Source/Free licenses: LGPL 2.1 or later and |
| 5 | + * Apache License 2.0. (starting with JNA version 4.0.0). |
| 6 | + * |
| 7 | + * You can freely decide which license you want to apply to |
| 8 | + * the project. |
| 9 | + * |
| 10 | + * You may obtain a copy of the LGPL License at: |
| 11 | + * |
| 12 | + * http://www.gnu.org/licenses/licenses.html |
| 13 | + * |
| 14 | + * A copy is also included in the downloadable source code package |
| 15 | + * containing JNA, in file "LGPL2.1". |
| 16 | + * |
| 17 | + * You may obtain a copy of the Apache License at: |
| 18 | + * |
| 19 | + * http://www.apache.org/licenses/ |
| 20 | + * |
| 21 | + * A copy is also included in the downloadable source code package |
| 22 | + * containing JNA, in file "AL2.0". |
| 23 | + */ |
| 24 | +package com.sun.jna; |
| 25 | + |
| 26 | +import java.util.Collections; |
| 27 | +import junit.framework.TestCase; |
| 28 | + |
| 29 | +/** |
| 30 | + * Check getNativeLibrary functions in Native |
| 31 | + */ |
| 32 | +public class NativeGetNativeLibraryTest extends TestCase { |
| 33 | + |
| 34 | + private NativeLibrary libUTF8; |
| 35 | + private TestLib libUTF8Interface; |
| 36 | + |
| 37 | + @Override |
| 38 | + protected void setUp() { |
| 39 | + libUTF8 = NativeLibrary.getInstance("testlib", |
| 40 | + Collections.singletonMap(Library.OPTION_STRING_ENCODING, "UTF-8")); |
| 41 | + Native.register(TestLibUTF8.class, libUTF8); |
| 42 | + libUTF8Interface = Native.load("testlib", TestLib.class, |
| 43 | + Collections.singletonMap(Library.OPTION_STRING_ENCODING, "UTF-8")); |
| 44 | + } |
| 45 | + |
| 46 | + public void testGetNativeLibraryInterface() { |
| 47 | + NativeLibrary nl = Native.getNativeLibrary(libUTF8Interface); |
| 48 | + assertTrue(nl instanceof NativeLibrary); |
| 49 | + } |
| 50 | + |
| 51 | + public void testGetNativeLibraryDirect() { |
| 52 | + NativeLibrary nl = Native.getNativeLibrary(TestLibUTF8.class); |
| 53 | + assertTrue(nl instanceof NativeLibrary); |
| 54 | + // This only makes sense for the direct case, as that directly wraps |
| 55 | + // a supplied instance |
| 56 | + assertEquals(libUTF8, nl); |
| 57 | + } |
| 58 | + |
| 59 | + public void testGetNativeLibraryOnUnboundShouldFail() { |
| 60 | + try { |
| 61 | + Native.getNativeLibrary(new TestLib() { |
| 62 | + @Override |
| 63 | + public String returnStringArgument(Pointer input) { |
| 64 | + return ""; |
| 65 | + } |
| 66 | + }); |
| 67 | + assertTrue("Exception not thrown", false); |
| 68 | + } catch (IllegalArgumentException ex) { |
| 69 | + // This should be reached |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public void testGetNativeLibraryOnNullShouldFail() { |
| 74 | + try { |
| 75 | + Native.getNativeLibrary((Class) null); |
| 76 | + assertTrue("Exception not thrown", false); |
| 77 | + } catch (IllegalArgumentException ex) { |
| 78 | + // This should be reached |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public static void main(java.lang.String[] argList) { |
| 83 | + junit.textui.TestRunner.run(NativeGetNativeLibraryTest.class); |
| 84 | + } |
| 85 | + |
| 86 | + private static class TestLibUTF8 implements Library { |
| 87 | + native String returnStringArgument(Pointer input); |
| 88 | + } |
| 89 | + |
| 90 | + private interface TestLib extends Library { |
| 91 | + public String returnStringArgument(Pointer input); |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments