hibernate的单步测试。

03-09-09 neuhawk
看了hibernate的大部分文档,像把\src\net\sf\hibernate\test

里的例子运行一遍,刚开始我是用main写的,后来觉得太麻烦,于是就学了

junit。可是运行它自带的例子就出错了。

如NewPerformanceTest:

import java.io.Serializable;

import java.util.List;

import junit.framework.Test;

import junit.framework.TestSuite;

import net.sf.hibernate.Session;

public class NewPerformanceTest extends TestCase {

public NewPerformanceTest(String arg0) {

super(arg0);

}

public void testPerformance() throws Exception {

for ( int n=2; n<4000; n*=2 ) {

Simple[] simples = new Simple[n];

Serializable[] ids = new Serializable[n];

for ( int i=0; i<n; i++ ) {

simples = new Simple();

simples.init();

simples.setCount(i);

ids = new Long(i);

}

Session s = sessions.openSession();

prepare(s, simples, ids, n);

s.close();

long find = 0;

long flush = 0;

for ( int i=0; i<100; i++ ) {

s = sessions.openSession();

long time = System.currentTimeMillis();

List list = s.find("from s in class Simple where not s.name='osama bin laden' and s.other is null");

find += System.currentTimeMillis() - time;

assertTrue( list.size()==n );

time = System.currentTimeMillis();

s.flush();

flush += System.currentTimeMillis() - time;

time = System.currentTimeMillis();

s.connection().commit();

find += System.currentTimeMillis() - time;

s.close();

}

System.out.println( "Objects: " + n + " - find(): " + find + "ms / flush(): " + flush + "ms / Ratio: " + ( (float) flush )/find );

s = sessions.openSession();

delete(s);

s.close();

}

}

private void prepare(Session s, Simple[] simples, Serializable[] ids, int N) throws Exception {

for ( int i=0; i<N; i++ ) {

s.save( simples, ids );

}

s.flush();

s.connection().commit();

}

private void delete(Session s) throws Exception {

s.delete("from s in class Simple");

s.flush();

s.connection().commit();

}

public static Test suite() throws Exception {

TestCase.exportSchema( new String[] { "Simple.hbm.xml" } );

return new TestSuite(NewPerformanceTest.class);

}

}

/////////////////////////////////////

jbuilder 提示错误:

sessions(没有定义),TestCase.exportSchema( new String[] { "Simple.hbm.xml" } );(无该方法)。

晕。

neuhawk
2003-09-09 15:42
我把它改成:

public class NewPerformanceTest

extends TestCase {

SessionFactory sessions = null;

public NewPerformanceTest(String arg0) {

super(arg0);

}

public void testPerformance() throws Exception {

Configuration conf = new Configuration()

.addClass(Simple.class);

//生成并输出sql到文件(当前目录)和数据库

SchemaExport dbExport = new SchemaExport(conf);

dbExport.setOutputFile("classes\\sql.txt");

dbExport.create(true, true);

sessions = conf.buildSessionFactory();

//以上都是些固定格式的环境配置

//start......

for (int n = 2; n < 4000; n *= 2) {

Simple[] simples = new Simple[n];

Serializable[] ids = new Serializable[n];

for (int i = 0; i < n; i++) {

simples = new Simple();

simples.init();

simples.setCount(i);

ids = new Long(i);

}

Session s = sessions.openSession();

prepare(s, simples, ids, n);

s.close();

long find = 0;

long flush = 0;

for (int i = 0; i < 100; i++) {

s = sessions.openSession();

long time = System.currentTimeMillis();

List list = s.find(

"from s in class Simple where not s.name='osama bin laden' and s.other is null");

find += System.currentTimeMillis() - time;

assertTrue(list.size() == n);

time = System.currentTimeMillis();

s.flush();

flush += System.currentTimeMillis() - time;

time = System.currentTimeMillis();

s.connection().commit();

find += System.currentTimeMillis() - time;

s.close();

}

System.out.println("Objects: " + n + " - find(): " + find +

"ms / flush(): " + flush + "ms / Ratio: " +

( (float) flush) / find);

s = sessions.openSession();

delete(s);

s.close();

}

}

private void prepare(Session s, Simple[] simples, Serializable[] ids, int N) throws

Exception {

for (int i = 0; i < N; i++) {

s.save(simples, ids);

}

s.flush();

s.connection().commit();

}

private void delete(Session s) throws Exception {

s.delete("from s in class Simple");

s.flush();

s.connection().commit();

}

// public static Test suite() throws Exception {

// TestCase.exportSchema( new String[] { "Simple.hbm.xml" } );

// return new TestSuite(NewPerformanceTest.class);

// }

}

/////////////////////////////////////////////

语法没有错了,可是junit说它有错。

于是我做了个main函数:

public class Main {

public static void main(String[] args) {

NewPerformanceTest test = new NewPerformanceTest("hi");

try {

test.testPerformance();

}

catch (Exception e) {

}

}

}

晕,居然运行成功。

真是摸不着头脑了。

neuhawk
2003-09-09 15:50
哦,打错了,是单元测试,而不是单步测试。

neuhawk
2003-09-09 16:12
OK.问题部分得到解决。

如果我在jbuilder里把junit SwingUI Test Runner改成junit TextUI Test Runner,来编译我修改的程序,那么就顺利通过测试了。

sigh,,,,,,莫名其妙!

TestCase.exportSchema()这个方法从哪里来的?

猜你喜欢