#
PyDev - Python IDE (Python Development Enviroment for Eclipse) version 0.9.8.2 has been released.
Check the homepage (http://pydev.sourceforge.net/) for more details.
Download
-----------------------------------------------------
Current release: 0.9.8.2.
- Use the Eclipse update manager: http://pydev.sf.net/updates/
- Get zip from SourceForge :http://www.sourceforge.net/projects/pydev/
Requirements:
-----------------------------------------------------
- Eclipse 3.1 (the SDK is required)
- Python 2.3.x or 2.4.x
- Java 1.4 or higher
Instructions:
-----------------------------------------------------
Fast install: just go to the update manager (inside the help menu) and add update site: http://pydev.sf.net/updates/ (eclipse should do the rest)
An alternative is just getting the zip file and extracting it yourself in eclipse.
If you choose to do it, just make sure the plugins folder is extracted on top of the eclipse plugins folder.
Important:
-----------------------------------------------------
If you have version 0.9.6 or earlier installed, you must remove it and all previous versions
before installing the latest version.
一直没有什么机会采用ww2去做项目,都是在用struts,正好下周有个项目,准备采用webwork/xwork,放弃struts.同时也开始我的ww2之旅.
I like it o_o
将上面的代码重构了一下,并且添加了cookies,这样我们就不需要每次登陆了
1#!/usr/bin/python
2# -*- coding: UTF-8 -*-
3
4import urllib
5import cookielib, urllib2
6import os
7import re
8
9class Fun:
10 def __init__(self,username,password,times=1,\ dataFileName='fun.data',loginurl='',posturl='',\ proxies={},pform=None,cookies=None):
11 print '初始化'
12 self.username=username
13 self.password=password
14 self.times = times
15 self.loginurl=loginurl
16 self.posturl = posturl
17 self.proxies=proxies
18 self.pform=pform
19 self.dataFileName=dataFileName
20 self.cookies=cookielib.CookieJar()
21 self.opener=None
22
23 def login(self):
24 """
25 登陆
26 """
27 print '登陆'
28 loginparams = urllib.urlencode({'j_username':self.username,\ 'j_password':self.password,\
29 'UPC_REQUEST_URI':'*.do',\ 'UPC_LOGIN_FLAG':'UPC_LOGIN_FLAG'})
30 self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookies))
31
32 self.pform = self.opener.open(self.loginurl,loginparams)
33 print '登陆成功'
34
35 def post(self):
36 """
37 解析登陆完成的HTML页面,获取用户相应的信息
38 """
39 print '解析html页面,获取服务器端返回的内容'
40 content = '\n'.join(self.pform.readlines())
41 items = ["employeeName","department","workArea","workCity","fillInDate"]
42 values = []
43 for item in items:
44 pattern = "name=\""+item+"\"\s*(size=\"\d*\")?\s*\ value=\"(\S*(\s*\S*)?)\"\s*id=\""+item+"\""
45 matcher = re.search(pattern,content)
46
47 if matcher!=None:
48 values.append(matcher.groups()[1])
49 print values
50 else:
51 values.append('')
52 print '解析完成'
53 """
54 提交日报
55 """
56
57 print '读取data文件:'+self.dataFileName
58 f = open(self.dataFileName)
59 contents = f.readlines()
60
61 #读取data文件,判断是否采用交互界面,还是采用data文件的数据
62
63 if contents[0]=='n':
64 c = raw_input('是否采用提交当天日志(y/n)?:')
65 if c=='n':
66 self.fillInDate = raw_input('请输入提交日期(格式yyyy-mm-dd):')
67 projectId = raw_input('项目名称代码:')
68 workId = raw_input('具体事项:')
69 workContent = raw_input('工作内容:')
70 workNum = raw_input('正常工作时长:')
71 overtimeNum = raw_input('加班工作时长:')
72 else:
73
74 #是否采用文件中的时间,如果不采用,则采用当前时间
75 if contents[1]=='y':
76 self.fillInDate = contents[2]
77
78 projectId = contents[3]
79 workId = contents[4]
80 workContent = contents[5]
81 workNum = contents[6]
82 overtimeNum = contents[7]
83
84 #拼凑参数 o_o
85 paramsmap = {'employeeId':self.username,\ 'employeeName':values[0],\
86 'department':values[1],\ 'workArea':values[2],\ 'workCity':values[3],\ 'fillInDate':values[4],\
87 'detailList.projectId':[projectId],\ 'detailList.workId':[workId],\
88 'detailList.workContent':[workContent],\ 'detailList.workNum':[0], 'detailList.overtimeNum':[8]}
89
90 keys = paramsmap.keys()
91 for key in keys:
92 print key,':',paramsmap[key]
93
94 postparams = urllib.urlencode(paramsmap)
95 print 'postparams:'+postparams
96 #提交
97 #opener = urllib2.build_opener(self.cookies)
98 print '开始提交日报'
99 result = self.opener.open(self.posturl,postparams)
100 #result = urllib2.urlopen(posturl,postparams)
101 #todo:解析提过完成页面,获取信息
102 #
103 result.close()
104 print '日报填写成功'
105
106 def close(self):
107 """
108 """
109 if not self.pform:
110 self.pform.close()
111
112
113
114if __name__=='__main__':
115 #测试一下
116 try:
117 test = Fun(username='',password='',loginurl='',posturl = '')
118 test.login()
119 test.post()
120 test.close()
121 except IOError:
122 raise
初始化
登陆...
登陆成功
解析html页面,获取服务器端返回的内容
['\xd0\xec\xcf\xe9\xbe\xfc']
['\xd0\xec\xcf\xe9\xbe\xfc', '\xc4\xcf\xbe\xa9']
['\xd0\xec\xcf\xe9\xbe\xfc', '\xc4\xcf\xbe\xa9', '\xbb\xaa\xb6\xab']
['\xd0\xec\xcf\xe9\xbe\xfc', '\xc4\xcf\xbe\xa9', '\xbb\xaa\xb6\xab', '\xc4\xcf\x
be\xa9']
['\xd0\xec\xcf\xe9\xbe\xfc', '\xc4\xcf\xbe\xa9', '\xbb\xaa\xb6\xab', '\xc4\xcf\x
be\xa9', '2005-09-24 17:45:40.0']
解析完成
读取data文件:fun.data
detailList.workId : ['10003102\n']
employeeId : 404
workCity : 南京
detailList.projectId : ['JX0001\n']
detailList.overtimeNum : [8]
detailList.workNum : [0]
detailList.workContent : ['\xbb\xfc\xb2\xe9\xd0\xe8\xc7\xf3\xb7\xd6\xce\xf6\n']
fillInDate : 2005-09-24 17:45:40.0
department : 南京
workArea : 华东
employeeName : 徐祥军
postparams:detailList.workId=%5B%2710003102%5Cn%27%5D&employeeId=404&workCity=%C
4%CF%BE%A9&detailList.projectId=%5B%27JX0001%5Cn%27%5D&detailList.overtimeNum=%5
B8%5D&detailList.workNum=%5B0%5D&detailList.workContent=%5B%27%5Cxbb%5Cxfc%5Cxb2
%5Cxe9%5Cxd0%5Cxe8%5Cxc7%5Cxf3%5Cxb7%5Cxd6%5Cxce%5Cxf6%5Cn%27%5D&fillInDate=2005
-09-24+17%3A45%3A40.0&department=%C4%CF%BE%A9&workArea=%BB%AA%B6%AB&employeeName
=%D0%EC%CF%E9%BE%FC
开始提交日报
日报填写成功
摘要: 问题: 公司的日报,每天填写的时候总是那么巨慢,都有点恐惧它了!
解决方案: 让代码帮我们每天定时提交,这样就可以省下精力取吹牛去了
好了,开始吧: 有两种思路,大概的描述一下如下...
阅读全文
在上面篇文章,我们的测试代码如下:
1package com.martin.pdo;
2
3import org.springframework.context.ApplicationContext;
4
5import junit.framework.Assert;
6import junit.framework.TestCase;
7
8public class UserTest extends TestCase {
9
10 public void testUser() {
11 ApplicationContext context = JUnitTestHelper.getContext();
12 User user = (User) context.getBean("user");
13 User _u1 = new User();
14 _u1.setName("martin xus");
15 _u1.setSex('F');
16 _u1.setAge(100);
17 user.add(_u1);
18
19 Assert.assertNotNull(_u1.getId());
20
21 User _u2 = (User) user.loadByName("martin xus").get(0);
22 Assert.assertEquals(_u1.getId(), _u2.getId());
23
24 user.remove(_u2);
25 Assert.assertTrue(user.loadByName("martin xus").size() == 0);
26 }
27
28}
29
而这些,必须要和spring&hibernate一起工作,必须保证数据库操作正确,而我们需要的只是测试这个类的本身!
有必要连hibernate,spring一起测试吗?当然没有,测试,只需要保证本代码完成相应的功能,即可.
那么就有必要更换测试方法:
mock便是首选,在这里我们选择了jmock (http://www.jmock.org)
1package com.martin.mock;
2
3import org.jmock.Mock;
4import org.jmock.MockObjectTestCase;
5
6import java.util.List;
7
8/** *//**
9 * @author martin.xus
10 */
11public class UserTest extends MockObjectTestCase {
12
13 public void testAdd() {
14 Mock mock = mock(UserManager.class);
15 User user = new User();
16 mock.expects(once()).method("add").with(isA(User.class));
17 UserManager manager = (UserManager) mock.proxy();
18
19 user.setManager(manager);
20 user.add(new User());
21 mock.verify();
22 }
23
24 public void testLoadByName() {
25 Mock mock = mock(UserManager.class);
26 User user = new User();
27
28 mock.expects(once()).method("loadByName").with(isA(String.class)).will(returnValue(List.class));
29 UserManager manager = (UserManager) mock.proxy();
30
31 user.setManager(manager);
32 user.loadByName("martin");
33 mock.verify();
34 }
35
36 public void testRemove(){
37 Mock mock = mock(UserManager.class);
38 User user = new User();
39
40 mock.expects(once()).method("remove").with(isA(User.class));
41 UserManager manager = (UserManager) mock.proxy();
42
43 user.setManager(manager);
44 user.remove(user);
45 mock.verify();
46 }
47}
ok,测试通过