??xml version="1.0" encoding="utf-8" standalone="yes"?> 明天打算l那些组件所有public的方法写上测试用?-虽然花时间给别h的代码写试是极为无聊的行ؓQ但是现在我也认了,不能再这h心吊胆得写代码和手工试了,宁可׃一个上午甚x多的旉来写用例搭徏环境。Rod Johnson说得Ҏ?牛仔式真是编E害死:满天飞的工厂模式、单例、静态变量,什么注释都没有的监听接?---最重要的是Q没有测试用?-乃还程序员吗?知道老子׃多少旉来拜M攚w构step in,step over您的"大作"Qholy shit. springframework.org论坛里有条评论,译q来大概是_srcpE序说到?是用几个jar和配|文件把你的swing应用l织h的有着复杂的设计结构的xx"Q好吧我不理解ؕ译Q,说vswingQؓ了兼个方面的因素(主要是concurrency)Qswing的开发者们不得不让swing的用者们在一个迂回曲折的环境中编写他们的应用。这好像在一座庞大的金字塔内部观察它的徏{结构一P你会发出阵阵惊叹Q也会绉L头迷惑不?-开始的时候后者是q远多过前者的。回q来看看现在如火如荼的web开发,xQweb开发是不是在走10?0q前的老\Q什么才是程序的真谛呢?
接下来准备学习一Ҏ据挖掘方面的知识Q具体点是语义分析。因为公司在数据采集q方面还相当原始的spider+手工收割Q和st谈到q个时也提到个可选的ҎQ天知道那个看上M良少q一L家伙脑袋里到底装了多东ѝ好了,q周打算拖拖拉拉地把自动更新updater写完Q顺便看看有没机会弄成一个通用的updater。第一期的业务逻辑不复杂,所以也暂时没我什么事Q正好可以一只脚站在门外l心瞅瞅Q其他等评估l果出来了再说。刚才收信时候看到有?4Loffer letterQ密密麻ȝ英文搞得foxmail都把它丢到垃圄里去了?br/>
原题要求用javaQ用python只是Z方便。用回溯法快Q但是还是坚持用遍历林来写Q这也是面试时没惛_的思\Q呵呵,我就是自N烦的石头性格?/p>
最先想到用无向q通图q行深度优先搜烦Q但是没有考虑到结束条件。对于N个待排列的数字,每个节点都有N-1个出口和入口Q而用树状l构每个节点只有一个父节点Q存在递归q回的条件。但是这个方法的实用性只限制在当排列数很(N < 8Q时。当N>8时算法消耗的旉明显增加(一?*7*6*5*4*3*2*1=40320U组?Q当N>1000?当然Q这U情冉|不敢惛_?׃辑ֈpython的递归极限。所以真正如果要q点什么的?当然Q高中生都知道全排列?2张扑克牌出来排一下结果集是个天文数?Q这昄不是个好法?/p>
.
Issue 1: Poor integration tests, as everything is being tested in isolation
I’ve had good experience with mock-based testing. However, it’s obvious that mocks will only test classes in isolation. I use both unit tests and integration tests (sometimes called system or acceptance tests) together. The need for integration tests is not an issue for mock-based techniques and is not a good reason to use less mocks. It’s just a different aspect of testing. A more common issue in my experience is that people in the agile community who are new to testing often don’t understand these different aspects of testing and seem to believe that mock-based unit testing and integration testing are mutually exclusive options. The lack of common terminology in the community only worsens the problem. For some people, a “unit?is a class or small group of tightly coupled classes. For others, it’s a large portion of the software product. Most agile developers seem to call every test they write a unit test. It’s become so confusing for some teams that I’ve seen terminology like “integration unit tests?being used to describe testing strategies.
But, back to the topic. Poor integration testing is simply the result of lack of integration tests. Mocks do not cause a lack of integration tests. A team makes that choice, probably based on a weak understanding of the tradeoffs between isolation (unit) and integration (system) testing.
Issue 2: Mocks add complexity to the software design.
“I’ve seen numerous occasions where the introduction of mocks has added a large amount of complexity to an otherwise simple design. This complexity leads to higher implementation costs, a higher cognitive load on the developers working on the system, and higher maintenance costs (as there’s more code to maintain). ?/p>
The author appears to focused on the increased use of interfaces when using mock-based testing and expresses the opinion that interfaces should only be used where we’d want to be able to replace one implementation with another. First, there are other reasons to use interfaces. In general, interfaces are useful for managing dependencies between software components or subsystems. This can be beneficial even if the implementations do not change (see The Dependency Inversion Principle).
A modular software design will generally make it easier to use mock-based testing without altering the design specifically for the mocks. However, there are times when the software must be modified to support testing. Fortunately, the changes needed to support testing often, if done well, support the modularity goal.
In my experience, extra interfaces don’t add a significant maintenance overhead. Most effort is spent implementing the interface. The time writing the interface itself (or extracting it using an IDE’s refactoring tools) is negligible.
My Conclusions
In almost every case, I see the “simplicity?gained by not using mocks overshadowed by complex test setups to initialize large groups of dependent objects. The dark side of integration testing is that it’s often very slow for large numbers of tests. Some teams are using continuous integration tools like Cruise Control to run their “unit tests?(usually they are actually integration tests). This delays the feedback about broken builds but is often necessary because the tests run so slow. I realize there other reasons for using CC, but this is a common one from what I’ve seen and heard.
I’ve worked on teams where we had thousands of tests that ran in less than 15-20 seconds total on a developer workstation. This was a direct result of heavy use of mock-based testing. We also had a slower suite of integration tests that required 4-5 minutes to run. We didn’t need a continuous integration server because we were able to integrate and run our unit tests before every commit to the source control system. The team integrated 10-20 times/day and broken builds were practically nonexistent over the several years I worked with them. In the very rare cases when the build did break, it was typically fixed in a matter of minutes.
The other benefit of the isolation testing enabled by mocks is the ability to pinpoint problems much more quickly. It’s a form of the divide and conquer problem solving strategy, only the divide part is already done. The conquering is relatively easy compared to tracking down the cause of test failures when many classes are being exercised in a test.
My experience was that our mock based unit tests caught about 98% of the code problems before the code was ever committed to source control. The integration tests caught about another 1% beyond that (almost always because of a flaw in the mock-based testing) and manual testing caught the other 1%.
2006-05-22 增加 5.1 初始?velocity
2006-05-21 增加 4.1 - 4.2 HttpSession操作
更新note:
1.HttpServletRequest?a href="file:///D:/jakarta-tomcat-4.1.30/servlet%20api/javax/servlet/ServletRequest.html#getParameterNames()">getParameterNames() Ҏq回的Enumeration相对于页面参数的物理序刚好是相反的,所以实现@ToList标签时候需要先用一个Stack来对所有参数进行反?不知tomcat在这个apiҎ上是怎么实现?Z么就倒过来。c语言函数可以Ҏ不同的callcd来指定不同的压栈的顺序,莫非q个惯例也被用到servlet里面来了Q挺有意思的东西Q看来需要留意一下?br />
问题和解x?
1.关于@ToSession标签.如果是登陆的?那么有个判断条件决定某个login token是否应该被加到session?q时候可以在@ToSession标记的getterҎ中加入逻辑判断,来决定返回一个token实体或者null(q样做有点古?.).
2.路径的问?比如一?a href="http://host/logic/bbs/admin/ListUsers.wff">http://host/logic/bbs/admin/ListUsers.wff 摸版的相兌源目?比如,囄,css,js{?必d在webproject/bbs/admin响应的目录下,有点像struts的多模块.
开?
Ruby on Rails有个设计思想是用~码规定代替J琐的配|文件。jvmq_已经有一些类似ror的实玎ͼ比如
grails(http://docs.codehaus.org/display/GRAILS/2006/03/29/Groovy+on+Rails+(Grails)+0.1+Released)
虽然׃java自n的局限,它很隑ց出像ruby或者groovy那样动态语a那样随心所Ʋ的动作Q但是利用它的运行时反射、动态代理等Ҏ来可能实现“用~码规定代替J琐的配|文件”这一思想?br />下面转入正题?/p>
ServletAPI对HTTP协议q行了封?通过配置Web.xml来把不同的请求{发给不同的servlet来处理。Web框架则用一个ActionServletQ根据自q对Action的定义来转发h?br />抛开那些J琐的配|文Ӟ设想一下这样一U方?
1.Web动作的处理和响应
假设q样一个POSTh:
<form action="logic/group/NewTopic.wff" method="post">
Web动作实现Bean:
org.qqsns.web.logic.group.NewTopic
注意后面的logic/group/NewTopic和logic.group.NewTopic, 动作cdWeb动作是通过h路径和包名相互关联?br />q样Q对Web动作的响应就依赖于编译期的代码的l织l构而不是执行期的配|文件。这L好处是避免了l护J琐的配|文?特别是在没有IDE支持的情况下?/p>
org.qqsns.web.logic.group.NewTopiccL一个实现net.wff.servlet.WebAction接口的POJOQ下面是NewTopic中execute的方法片D?
//Only method must be implemented for interface net.wff.servlet.WebAction
public String execute(WebParam param, VelocityView view)
throws ServletException, IOException{
...
//return "redirect /success.html"; //h重定?br /> return "/success.jsp"; //h转发
}
executeҎ的返回值手动指定了一个{发或重定向的路径?/p>
2.输入验证
普通的Web框架都带数据输入验证功能,一般复杂程度和功能强大与否成正比?br />q里单地要求从setterҎ里抛Z个包含验证信息的异常Q以此来实现输入异常处理?br />
普通setterҎ
public void setName(String name){
this.name = name;
}
d输入验证后的setterҎ
public void setName(String name) throws InputException{
if(name.length()<3)
throw new InputException("Topic name must has a length greater than 3");
this.name = name;
}
在WaterFallServlet如何中处理验证信?
WebAction wa =
(WebAction)Class.forName(classPath).newInstance();
//procces forwarding
try {
ActionHelper.setProperties(request,wa);
} catch (InputException e) {
//return to input view
//header:referer
String rtn = request.getHeader("referer");
//clear old errors
if(rtn.indexOf("?")!=1){
rtn = rtn.substring(0,rtn.indexOf("?"));
}
rtn=rtn+"?error="+URLEncoder.encode(e.getMessage(),"UTF-8");
response.sendRedirect(rtn);
return;
}
q样验证信息通过h参数传回到输入页?
3.数据l定
假设有这Lhtml输入:
<input type="text" name="name"/>
<input type="text" name="number"/>
<input type="text" name="price"/>
下面是NewTopic中execute的方法全?
public String execute(WebParam param, VelocityView view)
throws ServletException, IOException{
System.out.println(getName());
System.out.println(getNumber());
System.out.println(getPrice());
System.out.println(getLength());
return "/success.html";
}
自动从request注入parameter,q也是Struts DynamicActionForm的好处之一?br />不过q里实现更类似多了类型{换的<jsp:setProperty name="bean" property="*"/>
因ؓName的类型是String,Number的类型是Integer,Price的类型是float,length的类型是double.至于其他复杂的类?也许jsf的{换器是个更好的主意?br />q样初步解决了数据的输入绑定和验证。余下的是业务逻辑的问题?/p>
4.如何操作Session中的信息
获取session中的信息:
@FromSession(
sessionKey=User.key
)
public void setCurrentUser(String currentUser) {
this.currentUser = currentUser;
}
信息保存到session?
@ToSession(
sessionKey = User.key
)
public String getName() {
if(login())return name;
else return null;
}
q?个方法定义在WebAction的实现类中?br /> 框架在execute()之前执行@FromSession动作Q在execute()之后执行@ToSession动作?br /> q里使用了annotation,所以要求必Ljdk5.0以上版本.
5.1 Velocity
waterfall启动时候自动在WEB-INF目录下寻扑ƈ初始化Velocity.properties文g
操作:
public String execute(WebParam param, VelocityView view)
throws ServletException, IOException{
view.addElement("msg","Hello");
view.go("index.vm");
return null;
}
ServletAPI对HTTP协议q行了封?通过配置Web.xml来把不同的请求{发给不同的servlet来处理。Web框架则用一个ActionServletQ根据自q对Action的定义来转发h?br />抛开那些J琐的配|文Ӟ设想一下这样一U方?
1.Web动作的处理和响应
假设q样一个POSTh:
Web动作实现Bean:
注意后面?em>logic/group/NewTopic?em>logic.group.NewTopic, 动作cdWeb动作是通过h路径和包名相互关联?br />q样Q对Web动作的响应就依赖?strong>~译期的代码的组l结?/strong>而不?strong>执行期的配置文g。这L好处是避免了l护J琐的配|文?特别是在没有IDE支持的情况下?/p>
org.qqsns.web.logic.group.NewTopiccL一个实现net.wff.servlet.WebAction接口的POJOQ下面是NewTopic中execute的方法片D?
executeҎ的返回值手动指定了一个{发或重定向的路径?/p>
2.输入验证
普通的Web框架都带数据输入验证功能,一般复杂程度和功能强大与否成正比?br />q里单地要求从setterҎ里抛Z个包含验证信息的异常Q以此来实现输入异常处理?br />
普通setterҎ
d输入验证后的setterҎ
WaterFallServlet是如何处理验证信息的:
q样验证信息通过h参数传回到输入页?
3.数据l定
假设有这Lhtml输入:
下面是NewTopic中execute的方法全?
自动从request注入parameter,q也许是很多人喜ƢStruts DynamicActionForm的原因之一?br />不过q里实现更类似多了类型{换的<jsp:setProperty name="bean" property="*"/>
因ؓName的类型是String,Number的类型是Integer,Price的类型是float,length的类型是double.至于其他复杂的类?也许jsf的{换器是个更好的主意?br />q样初步解决了数据的输入绑定和验证。余下的是业务逻辑的问题。WaterfallWebFramework源代?
http://www.aygfsteel.com/Files/zqc/WaterfallWebFramework.rar (只有一个配|文?其中只有1行配|信?)
以上是框架的主要功能?strong>用编码规定代曉K|文?/strong>Q也许这会是一U更加高效率的开发方式?br />