我的一个实现类是如下:
package com.anhuily.callcenter.gps.deviceinfo;import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import com.cms.db.dao.NoSuchColumnException;
import com.cms.db.dao.Row;
import com.cms.db.dao.SQLCommandBean;
import com.cms.db.dao.UnsupportedTypeException;/**
 * <p>
 * Title: EMSFLOW流程管理平台
 * </p>
 * <p>
 * Description: 此代码由EMSFLOW代码生成工具产生
 * </p>
 *  
 * <p>
 * EMSFLOW SoftWare:www.emsflow.com
 * </p>
 * 
 * 
 *    用来处理数据库访问
 */public class DeviceinfoBZ {
private int totalRow; public int getTotalRow() {
return totalRow;
} public List<DeviceInfo> findgpsbyzrrmc(String zrr, String condition,String tablename,
int offset, int limit) {
String sql = "select g.no,c.zrrmc,t.carnum,t.snno,t.simid,realtime,map_x,map_y,acc,wjdy from t_gps_position_"
+ tablename
+ " g  "
+ "inner join (select no,snno,simid,carnum,carid from T_GPS_CAR_SIMID_CONFIG) t on t.no=g.carid  "
+ "inner join (select zrrmc,clid,no from V_SYS_CAR where zrr='"
+ zrr
+ "') c on c.no=t.carid "
+ "where "+condition
+ " order by realtime";
List<DeviceInfo> listinfo = new ArrayList<DeviceInfo>(); SQLCommandBean sBean = new SQLCommandBean("GPS");
Collection c = null;
DeviceInfo dInfo = null;
int intTemp = 0;
int i = 1;
try {
String sqlString = "select count(1) from sysobjects where id = object_id('t_gps_position_"
+ tablename + "') and OBJECTPROPERTY(id, N'IsUserTable') = 1";
sBean.setSqlValue(sqlString);
Collection collection = sBean.executeQuery();
Iterator iterator = collection.iterator();
if (iterator.hasNext()) {
Row row = (Row) iterator.next(); if (!"0".equals(row.getString(1))
&& !"".equals(row.getString(1))) {
sBean.setSqlValue(sql);
c = sBean.executeQuery(offset, limit);
this.setTotalRow(sBean.getTotalRow());
Iterator iter = c.iterator();
while (iter.hasNext()) {
row = (Row) iter.next();
dInfo = new DeviceInfo();
dInfo.setId(row.getString(1));
dInfo.setZrrmc(row.getString(2));
dInfo.setCarnum(row.getString(3));
dInfo.setSnno(row.getString(4));
dInfo.setSimid(row.getString(5));
dInfo.setRealtime(row.getString(6));
dInfo.setMap_x(row.getString(7));
dInfo.setMap_y(row.getString(8));
dInfo.setAcc(row.getString(9));
dInfo.setWjdy(row.getString(10)); if (offset != 0) {
intTemp = ((limit * offset) / limit);
}
dInfo.setId(String.valueOf(i + intTemp));
i++; listinfo.add(dInfo);
}
} } } catch (SQLException e) {
e.printStackTrace();
} catch (UnsupportedTypeException e) {
e.printStackTrace();
} catch (NoSuchColumnException e) {
e.printStackTrace();
} finally {
c = null;
sBean = null;
}
return listinfo;
}
 /**
  * 导成excel表查询
  * **/
public List<DeviceInfo> expgpsbyzrrmc(String zrr, String condition,String tablename) { 
String sql = "select g.no,c.zrrmc,t.carnum,t.snno,t.simid,realtime,map_x,map_y,acc,wjdy from t_gps_position_"
+ tablename
+ " g  "
+ "inner join (select no,snno,simid,carnum,carid from T_GPS_CAR_SIMID_CONFIG) t on t.no=g.carid  "
+ "inner join (select zrrmc,clid,no from V_SYS_CAR where zrr='"
+ zrr
+ "') c on c.no=t.carid "
+ "where "+condition
+ "order by realtime"; 
List<DeviceInfo> listinfo = new ArrayList<DeviceInfo>(); 
SQLCommandBean sBean = new SQLCommandBean("GPS"); 
Collection c = null;
DeviceInfo dInfo = null; 
try {
String sqlString = "select count(1) from sysobjects where id = object_id('t_gps_position_"
+ tablename + "') and OBJECTPROPERTY(id, N'IsUserTable') = 1";
sBean.setSqlValue(sqlString);
Collection collection = sBean.executeQuery();
Iterator iterator = collection.iterator();
if (iterator.hasNext()) {
Row row = (Row) iterator.next(); 
if (!"0".equals(row.getString(1))
&& !"".equals(row.getString(1))) {
sBean.setSqlValue(sql);
c = sBean.executeQuery();
this.setTotalRow(sBean.getTotalRow());
Iterator iter = c.iterator();
while (iter.hasNext()) {
row = (Row) iter.next();
dInfo = new DeviceInfo();
dInfo.setId(row.getString(1));
dInfo.setZrrmc(row.getString(2));
dInfo.setCarnum(row.getString(3));
dInfo.setSnno(row.getString(4));
dInfo.setSimid(row.getString(5));
dInfo.setRealtime(row.getString(6));
dInfo.setMap_x(row.getString(7));
dInfo.setMap_y(row.getString(8));
dInfo.setAcc(row.getString(9));
dInfo.setWjdy(row.getString(10));
listinfo.add(dInfo);
}


} catch (SQLException e) {
e.printStackTrace();
} catch (UnsupportedTypeException e) {
e.printStackTrace();
} catch (NoSuchColumnException e) {
e.printStackTrace();
} finally {
c = null;
sBean = null;

return listinfo; 

public void setTotalRow(int totalRow) {
this.totalRow = totalRow;
}}

解决方案 »

  1.   

    action如下:
    package com.anhuily.callcenter.gps.deviceinfo;import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.List;import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import jxl.Workbook;
    import jxl.format.Alignment;
    import jxl.format.VerticalAlignment;
    import jxl.write.Label;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableFont;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;import com.anhuily.callcenter.fuwu.common.DateUtils;
    import com.cms.action.BaseAction;
    import com.cms.common.Pager;
    import com.cms.common.exception.BusinessException;
    import com.cms.common.exception.SystemException;public class DeviceinfoAction extends BaseAction {
    private static int PAGE_LENGTH = 30; @Override
    public ActionForward executeAction(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws BusinessException, SystemException, ServletException,
    IOException {
    ActionForward aForward = null;
    String amapping = mapping.getParameter();
    if ("".equalsIgnoreCase(amapping)) {
    aForward = mapping.findForward("failure");
    } else if ("devicelist".equalsIgnoreCase(amapping)) {
    aForward = deviceList(mapping, form, request, response);
    } else if ("exportexcel".equalsIgnoreCase(amapping)) {
    exportexcel(mapping, form, request, response);
    }
    return aForward;
    } public ActionForward deviceList(ActionMapping mapping, ActionForm form2,
    HttpServletRequest request, HttpServletResponse response) {
    String date = request.getParameter("dplanDate");
    String firsttime = request.getParameter("dplanDateQuriy2");
    String endtime = request.getParameter("dplanDateQuriy22");
    String zrr = request.getParameter("yid");
    if (date == null || "".equals(date)) {
    date = DateUtils.toady();
    }
    if (firsttime == null || "".equals(firsttime)) {
    firsttime = "00:00:00";
    }
    if (endtime == null || "".equals(endtime)) {
    endtime = "01:00:00";
    }
    DeviceinfoBZ dinfoBZ = new DeviceinfoBZ();
    String url = request.getContextPath() + request.getServletPath();
    int offset;
    int length = PAGE_LENGTH;
    String pageOffset = request.getParameter("pager.offset");
    if (pageOffset == null || pageOffset.equals("")) {
    offset = 0;
    } else {
    offset = Integer.parseInt(pageOffset);
    } String condition = " realtime>='"+date + " "+firsttime+"' and realtime<='" +date+ " "+endtime+"' ";
    String tablename = date.substring(0, 4) + "_"+ date.substring(4, 6) + "_" + date.substring(6, 8); List<DeviceInfo> list = dinfoBZ.findgpsbyzrrmc(zrr, condition,tablename, offset,
    length);
    String pagerHeader = Pager.generate(offset, dinfoBZ.getTotalRow(),
    length, url + "?yid=" + zrr + "&dplanDate=" + date
    + "&dplanDateQuriy2=" + firsttime
    + "&dplanDateQuriy22=" + endtime + "", condition);
    request.setAttribute("offset", new Integer(0));
    request.setAttribute("pagerHeader", pagerHeader);
    request.setAttribute("length", new Integer(length));
    request.setAttribute("list", list); request.setAttribute("yid", zrr); 
    request.setAttribute("date", date);
    request.setAttribute("firsttime", firsttime);
    request.setAttribute("endtime", endtime);
    return mapping.findForward("success"); 
    }
    /**
     *  导成excel表
     * **/
    public void exportexcel(ActionMapping mapping, ActionForm form2,
    HttpServletRequest request, HttpServletResponse response) {
    String date = request.getParameter("dplanDate");
    String firsttime = request.getParameter("dplanDateQuriy2");
    String endtime = request.getParameter("dplanDateQuriy22"); 

    String zrr = request.getParameter("yid");
    if (date == null || "".equals(date)) {
    date = DateUtils.toady();
    }
    if (firsttime == null || "".equals(firsttime)) {
    firsttime = "00:00:00";
    }
    if (endtime == null || "".equals(endtime)) {
    endtime = "01:00:00";

    DeviceinfoBZ dinfoBZ = new DeviceinfoBZ();  
    String condition = " realtime>='"+date + " "+firsttime+"' and realtime<='" +date+ " "+endtime+"' ";
    String tablename = date.substring(0, 4) + "_"+ date.substring(4, 6) + "_" + date.substring(6, 8); 
    File file = null,file1 = null;
    try {
    //操作excel
    String rootPath = this.getServlet().getServletContext()
    .getRealPath("/");
    file = new File(rootPath + "\\download\\gpsdevice.xls");
    file1 = new File(rootPath + "\\download\\gpsdevice1.xls");
    Workbook wb = Workbook.getWorkbook(file);
    WritableWorkbook wwb = Workbook.createWorkbook(file1, wb);
    WritableSheet sheet = wwb.getSheet(0);
    WritableFont titleFont2 = new WritableFont(WritableFont.ARIAL, 10,
    WritableFont.NO_BOLD, false);
    WritableCellFormat cTitleFont2 = new WritableCellFormat(titleFont2); jxl.write.NumberFormat nf1 = new jxl.write.NumberFormat("###,###");
    WritableCellFormat wcf_n = new WritableCellFormat(nf1);
    wcf_n.setAlignment(Alignment.CENTRE);
    wcf_n.setVerticalAlignment(VerticalAlignment.CENTRE); List<DeviceInfo> l = dinfoBZ.expgpsbyzrrmc(zrr, condition,tablename);
    for (int i = 0; i < l.size(); i++) { DeviceInfo d = (DeviceInfo) l.get(i);
    sheet.addCell(new jxl.write.Number(0, i + 2, (i + 1), wcf_n));
    // sheet.addCell(new Label(0,i+2,d.getId(),cTitleFont2));                  
    sheet.addCell(new Label(1, i + 2, d.getZrrmc(), cTitleFont2));
    sheet.addCell(new Label(2, i + 2, d.getCarnum(), cTitleFont2));
    sheet.addCell(new Label(3, i + 2, d.getSnno(), cTitleFont2));
    sheet.addCell(new Label(4, i + 2, d.getSimid(), cTitleFont2));
    sheet
    .addCell(new Label(5, i + 2, d.getRealtime(),
    cTitleFont2));
    sheet.addCell(new Label(6, i + 2, d.getMap_x(), cTitleFont2));
    sheet.addCell(new Label(7, i + 2, d.getMap_y(), cTitleFont2));
    sheet.addCell(new Label(8, i + 2, d.getWjdy(), cTitleFont2));
    sheet.addCell(new Label(9, i + 2, d.getAcc(), cTitleFont2));

    wwb.write();
    wwb.close();
    wb.close(); 
    if (file1.exists()) {
    response.reset();
    response
    .setContentType("application/vnd.ms-excel;charset=gb2312");
    response.addHeader("Content-Disposition",
    "attachment; filename=\""
    + new String(("gps信息技术管理报表.xls")
    .getBytes("gb2312"), "ISO8859_1")
    + "\"");
    int fileLength = (int) file1.length();
    response.setContentLength(fileLength);
    if (fileLength != 0) {
    InputStream inStream = new FileInputStream(file1);
    byte[] buf = new byte[4096];
    ServletOutputStream servletOS = response.getOutputStream();
    int readLength;
    while (((readLength = inStream.read(buf)) != -1)) {
    servletOS.write(buf, 0, readLength);
    }
    inStream.close();
    servletOS.flush();
    servletOS.close();
    }
    }
    file1.delete(); 
    } catch (Exception e) {
    e.printStackTrace();
    }finally{
    file = null;
    file1 = null;
    }
    }
    }我想用junit进行测试 我该怎样写junit呢?
    请高手指点一下
    谢谢
      

  2.   

    1。导入jUnit包
    2。public class DeviceinfoBZ extends TestCase{
      

  3.   

    导入junit包, 4比较好用点,在你需要测试的方法上面加个@Test的注解就可以了,! 
      

  4.   

    myeclipse下 选中要测试的class点击 new -> Junit test case
      

  5.   

    1。导入jUnit包
    2。public class DeviceinfoBZTest extends TestCase{DeviceinfoBZ testDeviceinfoBZ; public void setup(){
     super.setup();
      testDeviceinfoBZ =new DeviceinfoBZ ();
     }
      
     public void test方法名(){
      assertTure或者assertNotNull或者assertEquals(testDeviceinfoBZ.方法名)
     }不过你这里是struts1里面的action,没有分离数据库连接代码, 最好是把连接代码写到一个类中,然后进行测试,这样好点。}
      

  6.   

    package com.anhuily.callcenter.gps.deviceinfo;import static org.junit.Assert.*;import java.util.ArrayList;
    import java.util.List;import org.junit.BeforeClass;
    import org.junit.Test; 
    public class DeviceinforTest {
    static DeviceinfoBZ db=null;
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    db=new DeviceinfoBZ();
    } @Test
    public void testGetTotalRow() {
    fail("Not yet implemented");
    } @Test
    public void testFindgpsbyzrrmc() { 
    db.findgpsbyzrrmc("160700010"
    ," realtime>='20100609 00:00:00' and realtime<='20100609 01:00:00' "
    ,"2010_06_09"
    , 0, 30);
    fail("Not yet implemented");
    } @Test
    public void testExpgpsbyzrrmc() {
    fail("Not yet implemented");
    } @Test
    public void testSetTotalRow() {
    fail("Not yet implemented");
    }}
    这是我写的junit类
    可是它报空指针异常啊
      

  7.   

    异常为:
    connect error : java.lang.NullPointerException
    java.sql.SQLException: connect error : java.lang.NullPointerException
    at com.cms.db.dao.SQLCommandBean.getConnection(SQLCommandBean.java:152)
    at com.cms.db.dao.SQLCommandBean.executeQuery(SQLCommandBean.java:259)
    at com.anhuily.callcenter.gps.deviceinfo.DeviceinfoBZ.findgpsbyzrrmc(DeviceinfoBZ.java:60)
    at com.anhuily.callcenter.gps.deviceinfo.DeviceinforTest.testFindgpsbyzrrmc(DeviceinforTest.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
    at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
    at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
    at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
    at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
    at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
      

  8.   

    看下这几行对应的代码,应该是getConnection的问题
    at com.cms.db.dao.SQLCommandBean.getConnection(SQLCommandBean.java:152)
    at com.cms.db.dao.SQLCommandBean.executeQuery(SQLCommandBean.java:259)
    at com.anhuily.callcenter.gps.deviceinfo.DeviceinfoBZ.findgpsbyzrrmc(DeviceinfoBZ.java:60)
    at com.anhuily.callcenter.gps.deviceinfo.DeviceinforTest.testFindgpsbyzrrmc(DeviceinforTest.java:24)