如何将本地库路径添加到junit任务? [英] How to add a native library path to the JUNIT task?

查看:249
本文介绍了如何将本地库路径添加到junit任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用串行通信这个驱动的Java项目。该驱动程序使用Windows下一个dll创建串行端口。

该项目包含完整的成功使用运行方式 - > JUnit测试几个JUnit测试。但是,引用本地库测试失败运行ant(这不引用本地库通测试)时。

我最好的猜测,到目前为止是添加包含本地库到的java.library.path目录,但我还没有成功地通过build.xml文件这样做。

有人可以告诉(干净)解决方案?

下面是我的build.xml:

 <路径ID =compile.classpath>
    <文件集DIR =$ {} lib目录>
        <包括姓名=** / *罐子。/>
    < /文件集>
    <文件集DIR =$ {} JUNIT_HOME>
        <包括姓名=** / *罐子。/>
    < /文件集>
< /路径>&所述;路径ID =test.classpath>
    < pathelement位置=$ {}斌/>
    <文件集DIR =$ {} lib目录>
         <包括姓名=** / *罐子。/>
     < /文件集>
    <文件集DIR =$ {} JUNIT_HOME>
        <包括姓名=** / *罐子。/>
    < /文件集>
< /路径><目标名称=编译>
    < MKDIR DIR =$ {}斌/>
    <回声消息=编译src文件夹.../>
    < javac的includeantruntime =无classpathref =compile.classpathSRCDIR =$ {src}字符串DESTDIR =$ {}斌/>
    <回声消息=编译test文件夹.../>
    < javac的includeantruntime =无classpathref =compile.classpathSRCDIR =$ {}测试DESTDIR =$ {}斌/>
< /目标与GT;<目标名称=测试>
    < MKDIR DIR =$ {} test.reports/>
    < JUnit的叉=YESprintsummary =YEShaltonfailure =YES>
        <试验NAME =$ {} test.class.nametodir =$ {} test.reports/>
        <格式化类型=XML/>
        <类路径REFID =test.classpath/>
    < / JUnit的>
< /目标与GT;

这里是测试报告(XML格式)的一部分:

 <测试用例类名=nl.timo.comport.test.buildservertests.ComportFactoryTestNAME =testGetInstance时间=0.0/>
  <测试用例类名=nl.timo.comport.test.buildservertests.ComportFactoryTestNAME =testCreateDefaultComport时间=0.016>
    <错误信息=giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava /朗/字符串;)Ljava /朗/字符串;键入=java.lang.UnsatisfiedLinkError中> java.lang.UnsatisfiedLinkError中:giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava /朗/字符串;)Ljava /朗/字符串;
    在giovynet.nativelink.SerialPort.getStateSerialPortC(本机方法)
    在giovynet.nativelink.SerialPort.getFreeSerialPort(SerialPort.java:50)
    在package.comport.GioComport.getFreeSerialPorts(来源不明)
    在package.comport.GioComport.findDevice(来源不明)
    。在package.comport.GioComport&放大器; LT;初始化和放大器; GT;(未知来源)
    在package.comport.ComportFactory.createNewPort(来源不明)
    在package.comport.ComportFactory.createComport(来源不明)
    在package.comport.test.buildservertests.ComportFactoryTest.testCreateDefaultComport(来源不明)
< /错误>
  < /测试用例>
  <测试用例类名=nl.timo.comport.test.buildservertests.ComportFactoryTestNAME =testCreateComportWithWrongSettings时间=0.0>
    <错误信息=giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava /朗/字符串;)Ljava /朗/字符串;键入=java.lang.UnsatisfiedLinkError中> java.lang.UnsatisfiedLinkError中:giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava /朗/字符串;)Ljava /朗/字符串;
    在giovynet.nativelink.SerialPort.getStateSerialPortC(本机方法)
    在giovynet.nativelink.SerialPort.getFreeSerialPort(SerialPort.java:50)
    在package.comport.GioComport.getFreeSerialPorts(来源不明)
    在package.comport.GioComport.findDevice(来源不明)
    。在package.comport.GioComport&放大器; LT;初始化和放大器; GT;(未知来源)
    在package.comport.ComportFactory.createNewPort(来源不明)
    在package.comport.ComportFactory.createComport(来源不明)
    在package.comport.test.buildservertests.ComportFactoryTest.testCreateComportWithWrongSettings(来源不明)
< /错误>
  < /测试用例>
  <系统出><![CDATA []]>< /系统出>
  <系统-ERR>&下; [CDATA [java.lang.UnsatisfiedLinkError中:在的java.library.path没有libSerialPort
    在java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)


解决方案

在蚂蚁的 junit任务,允许设置系统属性,就像一些其他任务。你需要指定在 sysproperty 嵌套元素作为的java.library.path 值:

 < JUnit的叉=YESprintsummary =YEShaltonfailure =YES>
    <试验NAME =$ {} test.class.nametodir =$ {} test.reports/>
    <格式化类型=XML/>
    <类路径REFID =test.classpath/>
    < sysproperty键=的java.library.pathVALUE =把你的库路径在这里/>
< / JUnit的>

I have a Java project that uses this driver for serial communication. The driver uses a dll under Windows to create serial ports.

The project contains several JUnit tests which complete successfully using the "Run as -> JUnit Test". However, the tests referencing the native library fail when running ant (and tests that do not reference the native library pass).

My best guess so far is to add the directory that contains the native library to the java.library.path, but I haven't succeeded in doing so through the build.xml file.

Can somebody tell a (clean) solution?

Here is my build.xml:

<path id="compile.classpath">
    <fileset dir="${lib}">
        <include name="**/*.jar"/>
    </fileset>
    <fileset dir="${junit_home}">
        <include name="**/*.jar"/>
    </fileset>
</path>

<path id="test.classpath">
    <pathelement location="${bin}" />
    <fileset dir="${lib}">
         <include name="**/*.jar"/>
     </fileset>
    <fileset dir="${junit_home}">
        <include name="**/*.jar"/>
    </fileset>
</path>

<target name="compile">
    <mkdir dir="${bin}" />
    <echo Message="Compiling src folder..." />
    <javac includeantruntime="no" classpathref="compile.classpath" srcdir="${src}" destdir="${bin}" />
    <echo Message="Compiling test folder..." />
    <javac includeantruntime="no" classpathref="compile.classpath" srcdir="${test}" destdir="${bin}" />
</target>

<target name="test">
    <mkdir dir="${test.reports}" />
    <junit fork="yes" printsummary="yes" haltonfailure="yes">
        <test name="${test.class.name}" todir="${test.reports}" />
        <formatter type="xml" />
        <classpath refid="test.classpath" />
    </junit>
</target>

And here is a part of the test report (in XML):

    <testcase classname="nl.timo.comport.test.buildservertests.ComportFactoryTest" name="testGetInstance" time="0.0" />
  <testcase classname="nl.timo.comport.test.buildservertests.ComportFactoryTest" name="testCreateDefaultComport" time="0.016">
    <error message="giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava/lang/String;)Ljava/lang/String;" type="java.lang.UnsatisfiedLinkError">java.lang.UnsatisfiedLinkError: giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava/lang/String;)Ljava/lang/String;
    at giovynet.nativelink.SerialPort.getStateSerialPortC(Native Method)
    at giovynet.nativelink.SerialPort.getFreeSerialPort(SerialPort.java:50)
    at package.comport.GioComport.getFreeSerialPorts(Unknown Source)
    at package.comport.GioComport.findDevice(Unknown Source)
    at package.comport.GioComport.&lt;init&gt;(Unknown Source)
    at package.comport.ComportFactory.createNewPort(Unknown Source)
    at package.comport.ComportFactory.createComport(Unknown Source)
    at package.comport.test.buildservertests.ComportFactoryTest.testCreateDefaultComport(Unknown Source)
</error>
  </testcase>
  <testcase classname="nl.timo.comport.test.buildservertests.ComportFactoryTest" name="testCreateComportWithWrongSettings" time="0.0">
    <error message="giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava/lang/String;)Ljava/lang/String;" type="java.lang.UnsatisfiedLinkError">java.lang.UnsatisfiedLinkError: giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava/lang/String;)Ljava/lang/String;
    at giovynet.nativelink.SerialPort.getStateSerialPortC(Native Method)
    at giovynet.nativelink.SerialPort.getFreeSerialPort(SerialPort.java:50)
    at package.comport.GioComport.getFreeSerialPorts(Unknown Source)
    at package.comport.GioComport.findDevice(Unknown Source)
    at package.comport.GioComport.&lt;init&gt;(Unknown Source)
    at package.comport.ComportFactory.createNewPort(Unknown Source)
    at package.comport.ComportFactory.createComport(Unknown Source)
    at package.comport.test.buildservertests.ComportFactoryTest.testCreateComportWithWrongSettings(Unknown Source)
</error>
  </testcase>
  <system-out><![CDATA[]]></system-out>
  <system-err><![CDATA[java.lang.UnsatisfiedLinkError: no libSerialPort in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)

解决方案

The junit task in Ant, allow setting system properties, just like some of the other tasks. You'll need to specify the java.library.path value in the sysproperty nested element as:

<junit fork="yes" printsummary="yes" haltonfailure="yes">
    <test name="${test.class.name}" todir="${test.reports}" />
    <formatter type="xml" />
    <classpath refid="test.classpath" />
    <sysproperty key="java.library.path" value="put your library path here"/>
</junit>

这篇关于如何将本地库路径添加到junit任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆