/* Copyright (c) 2009 Timothy Wall, All Rights Reserved
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.  
 */
package com.sun.jna;

import java.util.Map;
import java.util.HashMap;
import java.lang.reflect.Method;

import junit.framework.TestCase;

public class LastErrorTest extends TestCase {
    
    public interface TestLibrary extends Library {
        void noThrowLastError(int code);
        void throwLastError(int code) throws LastErrorException;
    }

    public static class DirectTestLibrary implements TestLibrary {
        public native void noThrowLastError(int code);
        public native void throwLastError(int code) throws LastErrorException;
        static {
            Native.register("testlib");
        }
    }

    public void testThrowLastError() {
        Map options = new HashMap();
        options.put(Library.OPTION_FUNCTION_MAPPER, new FunctionMapper() {
            public String getFunctionName(NativeLibrary library, Method m) {
                if (m.getName().equals("noThrowLastError")
                    || m.getName().equals("throwLastError")) {
                    return "setLastError";
                }
                return m.getName();
            }
        });
        TestLibrary lib = (TestLibrary)Native.loadLibrary("testlib", TestLibrary.class, options);

        final int ERROR = -1;
        lib.noThrowLastError(ERROR);
        assertEquals("Last error not preserved", ERROR, Native.getLastError());
        try {
            lib.throwLastError(ERROR);
            fail("Method should throw LastErrorException");
        }
        catch(LastErrorException e) {
            assertEquals("Exception should contain error code", ERROR, e.errorCode);
        }
    }

    public void testThrowLastErrorDirect() {
        TestLibrary lib = new DirectTestLibrary();

        final int ERROR = -1;
        lib.noThrowLastError(ERROR);
        assertEquals("Last error not preserved", ERROR, Native.getLastError());
        try {
            lib.throwLastError(ERROR);
            fail("Method should throw LastErrorException");
        }
        catch(LastErrorException e) {
            assertEquals("Exception should contain error code", ERROR, e.errorCode);
        }
    }

    public static void main(String[] args) {
        junit.textui.TestRunner.run(LastErrorTest.class);
    }
}

@@ -766,16 +766,470 @@
  %7B%0A    %0A
+    private static final Map OPTIONS = new HashMap() %7B%7B%0A        put(Library.OPTION_FUNCTION_MAPPER, new FunctionMapper() %7B%0A            public String getFunctionName(NativeLibrary library, Method m) %7B%0A                if (m.getName().equals(%22noThrowLastError%22)%0A                    %7C%7C m.getName().equals(%22throwLastError%22)) %7B%0A                    return %22setLastError%22;%0A                %7D%0A                return m.getName();%0A            %7D%0A        %7D);%0A    %7D%7D;%0A%0A
     publ
@@ -1263,32 +1263,32 @@
 tends Library %7B%0A
-
         void noT
@@ -1630,25 +1630,61 @@
 egister(
-%22testlib%22
+NativeLibrary.getInstance(%22testlib%22, OPTIONS)
 );%0A     
@@ -1775,405 +1775,8 @@
 ();%0A
-        options.put(Library.OPTION_FUNCTION_MAPPER, new FunctionMapper() %7B%0A            public String getFunctionName(NativeLibrary library, Method m) %7B%0A                if (m.getName().equals(%22noThrowLastError%22)%0A                    %7C%7C m.getName().equals(%22throwLastError%22)) %7B%0A                    return %22setLastError%22;%0A                %7D%0A                return m.getName();%0A            %7D%0A        %7D);%0A
     
@@ -1859,23 +1859,23 @@
 .class, 
-options
+OPTIONS
 );%0A%0A    

make tests independent of load order