licel / jcardsim

https://jcardsim.org
222 stars 123 forks source link

Best Practice of running JavaCard api.jar side by side with jcardsim #35

Open martinpaljak opened 10 years ago

martinpaljak commented 10 years ago

It would be nice if some background easy classloader-fu magic (or something similar) allowed to have api.jar in the eclipse classpath (as normal) for development and then bring in the jcardsim only for junit tests.

Putting some thought into how this could be done easily and "by default" would be really nice.

licel commented 10 years ago

Hi,

It is simple:

1) You need to add the code initializing jCardSim SmartCard Provider in the TestCase into the testSetup method:

    @BeforeClass
    public static void testSetup() throws Exception {
        System.setProperty("com.licel.jcardsim.card.applet.0.AID",
                TEST_APPLET_AID);
        System.setProperty("com.licel.jcardsim.card.applet.0.Class",
                "com.licel.jcardsim.samples.HelloWorldApplet1");
        if (Security.getProvider("jCardSim") == null) {
            try {
            Provider provider = (Provider) HelloWorldAppletTest.class.
                    getClassLoader().
                    loadClass("com.licel.jcardsim.smartcardio.JCardSimProvider").
                    newInstance();
            Security.addProvider(provider);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }       
    }

2) Follow this procedure Run/Debug Settings -> JUnit Test -> ClassPath -> User Entries -> Add JARs and select a path to jcardsim-xxx-all.jar. Note that jcardsim-xxx-all.jar must be before the default class path entry in the list.

It is done, now you are able to test applications with jCardSim and use standard api.jar.

There is a more interesting way to use jCardSim: client-server mode and remote loading of an applet to be tested. It require a bit more of code, but opens up interesting possibilities of writing integration tests.

martinpaljak commented 10 years ago

Okay, while system properties is one method of initiating applets I wish there was something better in addition - more programmatic.

FYI: I intend to integrate jCardSim in my "Applet PLayground": https://github.com/martinpaljak/AppletPlayground/ One of the burning issues is the lack of extended APDUs. I'm looking into it but it is not a blocking issue per se.

I hope that patches, if I generate any, will be welcome in upstream :)