package test;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import rainteen.jnadll.ENUM_LOG_LEVEL;
import rainteen.jnadll.FpCallback;
import rainteen.jnadll.IJnaDll;
import rainteen.jnadll.JnaDllService;
import rainteen.jnadll.KEYSTORE_FILE_STRU;
import rainteen.jnadll.STRU_FP_CALLBACK;
import rainteen.jnadll.STRU_NOTIFY_INFO;
import rainteen.jnadll.STRU_USER_INFO;
import rainteen.util.TypeUtil;
import com.sun.jna.Native;
public class TestJnaDll extends TestCase {
private static final Logger logger = Logger.getLogger(TestJnaDll.class);
private JnaDllService jnaDllService = null;
private IJnaDll jnaDll = null;
@Before
public void setUp() {
this.jnaDllService = JnaDllService.getInstance();
this.jnaDll = this.jnaDllService.getJnaDll();
}
@After
public void tearDown() {
}
@Test
public void testWriteStruct() {
KEYSTORE_FILE_STRU.ByReference fileStruct = new KEYSTORE_FILE_STRU.ByReference();
byte[] data = Native.toByteArray("F:/OK/testFile");
fileStruct.keyStoreFile[0].init(data);
byte[] dataFile = Native.toByteArray("F:/OK/dataFile");
fileStruct.keyStoreFile[1].init(dataFile);
byte[] test = Native.toByteArray("F:/OK/keyFile");
fileStruct.keyFile[0].init(test);
byte[] testFile = Native.toByteArray("F:/OK/keyDataFile");
fileStruct.keyFile[1].init(testFile);
int res = this.jnaDll.writeStruct(fileStruct);
byte[] data0 = fileStruct.keyStoreFile[0].getData();
System.out.println(TypeUtil.toString(data0));
byte[] data1 = fileStruct.keyStoreFile[1].getData();
System.out.println(TypeUtil.toString(data1));
byte[] key0 = fileStruct.keyFile[0].getData();
System.out.println(TypeUtil.toString(key0));
byte[] key1 = fileStruct.keyFile[1].getData();
System.out.println(TypeUtil.toString(key1));
Assert.assertTrue(res == 1);
}
@Test
public void testWriteByteArray() {
String name = "This is a test.";
String buff = "Oh, my God.";
int len = buff.length();
byte[] outBuff = new byte[512];
int outLen = 512;
int res = this.jnaDll.writeByteArray(name, buff, len, outBuff, outLen);
logger.info("name=" + name);
logger.info("buff=" + buff);
logger.info("outBuff=" + TypeUtil.toString(outBuff));
logger.info("res=" + res);
Assert.assertTrue(res == 1);
}
@Test
public void testStruFpCallback() {
STRU_FP_CALLBACK.ByReference fpCallback = new STRU_FP_CALLBACK.ByReference();
fpCallback.write();
STRU_NOTIFY_INFO.ByReference pNotifyInfo = new STRU_NOTIFY_INFO.ByReference();
pNotifyInfo.id = 0;
pNotifyInfo.info = "OMG";
pNotifyInfo.msg = "CMB";
pNotifyInfo.write();
int level = ENUM_LOG_LEVEL.LOG_DEBUG.getValue();
int res = this.jnaDll.writeLog(level, "callback log info.", fpCallback,
pNotifyInfo);
logger.info("testCallback() res=" + res);
}
@Test
public void testFpCallback() {
STRU_USER_INFO.ByReference pUserInfo = new STRU_USER_INFO.ByReference();
String szName = "Morpheus";
pUserInfo.szName = szName;
pUserInfo.age = 100;
pUserInfo.gender = 'M';
String sign = "I come from the Matrix";
pUserInfo.sign = sign;
pUserInfo.write();
//STRU_FP_CALLBACK.ByReference struFpCallback = new STRU_FP_CALLBACK.ByReference();
FpCallback fpCallback = new STRU_FP_CALLBACK.FpCallbackImpl();
int res = this.jnaDll.callback(fpCallback, pUserInfo);
logger.info("res=" + res);
logger.info("TestJnaDll.testFpCallback() pUserInfo.szName="
+ pUserInfo.szName);
logger.info("TestJnaDll.testFpCallback() pUserInfo.age="
+ pUserInfo.age);
logger.info("TestJnaDll.testFpCallback() pUserInfo.gender="
+ (char) pUserInfo.gender);
logger.info("TestJnaDll.testFpCallback() pUserInfo.sign="
+ pUserInfo.sign);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
JNA示例的Java工程
共59个文件
class:30个
java:17个
jar:4个
需积分: 9 23 下载量 152 浏览量
2015-03-03
22:40:53
上传
评论
收藏 2.46MB ZIP 举报
温馨提示
JNA 示例 的 java工程, 包含了 结构体指针、回调、char* 映射、byte[]映射等等。
资源推荐
资源详情
资源评论
收起资源包目录
JNAProj.zip (59个子文件)
JNAProj
log
jna.log 81KB
bin
rainteen
jnadll
KEYSTORE_FILE_STRU$ByValue.class 458B
STRU_USER_INFO.class 920B
STRU_USER_INFO$ByValue.class 442B
STRU_FP_CALLBACK.class 1KB
ENUM_LOG_LEVEL.class 2KB
IJnaDll.class 939B
STRU_NOTIFY_INFO$ByReference.class 466B
FpNotifyCallback.class 221B
STRU_FP_CALLBACK$ByValue.class 450B
KEYSTORE_FILE_STRU.class 1KB
FpWriteLogCallback.class 205B
JnaDllService.class 2KB
STRU_FP_CALLBACK$FpNotifyImpl.class 2KB
STRU_FP_CALLBACK$ByReference.class 466B
STRU_FP_CALLBACK$FpWriteLogImpl.class 2KB
STRU_NOTIFY_INFO.class 927B
STRU_NOTIFY_INFO$ByValue.class 450B
KEYSTORE_FILE_STRU$ByReference.class 474B
FpCallback.class 219B
STRU_USER_INFO$ByReference.class 458B
STRU_FP_CALLBACK$FpCallbackImpl.class 2KB
ByteArray$ByValue.class 401B
ByteArray.class 1KB
ByteArray$ByReference.class 417B
util
TypeUtil.class 727B
kernel
IKernel32.class 504B
LPFN_ISWOW64PROCESS.class 252B
Kernel32Service.class 1KB
test
TestKernel.class 2KB
TestJnaDll.class 5KB
log4j.properties 843B
JnaDllProject.dll 33KB
test
test
TestKernel.java 1KB
TestJnaDll.java 4KB
.settings
org.eclipse.jdt.core.prefs 629B
src
rainteen
jnadll
IJnaDll.java 882B
STRU_USER_INFO.java 710B
KEYSTORE_FILE_STRU.java 1KB
STRU_FP_CALLBACK.java 3KB
FpWriteLogCallback.java 244B
STRU_NOTIFY_INFO.java 711B
ENUM_LOG_LEVEL.java 645B
JnaDllService.java 861B
FpCallback.java 324B
FpNotifyCallback.java 300B
ByteArray.java 899B
util
TypeUtil.java 421B
kernel
LPFN_ISWOW64PROCESS.java 261B
IKernel32.java 586B
Kernel32Service.java 788B
log4j.properties 843B
.project 383B
.classpath 510B
lib
junit-4.10.jar 247KB
jna.jar 1.05MB
jna-platform.jar 1.4MB
log4j-1.2.17.jar 478KB
kernel32.dll 1.06MB
共 59 条
- 1
资源评论
rainteen
- 粉丝: 7
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功