锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
avg_3000 wrote:
> Hi,
>
> Can you suggest a tutorial site for junit and jcoverage that covers
> from the grassroot level since I have no idea on how to use this tools
> in java.
jcoverage is as good as dead. Use Cobertura, which is its replacement.
Cobertura has documentation for running it in Ant; it's even easier in
Maven.
Take a look at <http://cobertura.sourceforge.net/anttaskreference.html>,
since that's what Google would probably have pulled up if you'd searched.
緇撳悎浣跨敤JUnit鍜孋obertura鍙兘鏄笉閿欑殑閫夋嫨銆?/p>
鈼嬨傝儗鏅笌鑳屾櫙鐭ヨ瘑
榪欐槸鏄ㄥぉ鍦≦Q涓婁婦鐨勪竴涓緥瀛愩傛湰鏂囧茍涓嶆槸浠庡ご璁睩actory Method妯″紡錛屼粎瀵瑰叾瀹炵幇緇嗚妭榪涜璁ㄨ銆傚叧浜庤繖涓ā寮忓彲浠ュ弬鑰?a target="_blank">wayfarer鍜?a target="_blank">鍚曢渿瀹?/a>鐨勬枃绔犮?/p>
涓銆傚垎鏋?/p>
鍥犱負Factory Method錛堝ぇ鍐欑殑Factory Method琛ㄧず妯″紡鍚嶇О錛夋ā寮忕殑涓涓洰鐨勫氨鏄嬌寰楀垱寤鴻涓猴紙factory method錛夛紙灝忓啓鐨刦actory method琛ㄧず鍒涘緩琛屼負鏂規硶錛夋娊璞″寲錛岀敱瀛愮被瀹炵幇錛屼粠鑰屽鏄撴墿灞曘備竴涓函鐨凢actory Method妯″紡錛屽叾factory method鏄笉鑳界敤static鏉ュ疄鐜扮殑銆傞氬父Simple Factory妯″紡鎵嶄細鐢眘tatic鏉ュ疄鐜般?/p>
浣嗘槸錛屼竴涓狥actory鎷ユ湁涓涓猻tatic鐨勫垱寤烘柟娉曟槸寰堣浜虹殑銆傜敤鎴峰湪浠諱綍鏃跺欏彧瑕佺敤綾誨悕錛團actory鐨勭被鍚嶏級鏃㈠彲浠ュ垱寤哄嚭闇瑕佺殑Product銆?a target="_blank">wayfarer聽鐨勬枃绔犳渶鍚庣粰鍑虹殑DotNet鐨勫疄鐜伴儴鍒嗗氨鏄繖鏍風殑渚嬪瓙銆傚悤闇囧畤鐨勫彟涓綃?a >鏂囩珷聽錛堝叧浜嶴imple Factory妯″紡錛夌殑鍥炲涓?a >Walkdan緇欏嚭鐨勮В鍐蟲柟妗堜篃鏄繖鏍風殑銆備粠鏈川涓婏紝榪欎袱涓緥瀛愰兘涓嶆槸綰殑Factory Method妯″紡鎴栬匰imple Factory妯″紡錛岃屾槸Simple Factory妯″紡鍜孎actory Method妯″紡鐨勭粍鍚堝簲鐢ㄣ?/p>
寮曠敤Walkdan鐨勫洖澶嶏細
Simple Factory鏈澶х殑濂藉鏄妸鍙樺寲闆嗕腑鍒頒簡涓澶勩傚彟澶栵紝LightSimpleFactory.Create()閲岄潰鐨勪緷璧栧叧緋諱篃鍙互娑堥櫎錛屾垜鐨勫仛娉曟槸:
1. 澹版槑鏋勯犲瓙
public interface聽ILightCreator
{
聽聽聽聽Light聽Create();
}public class聽BulbLightCreator:聽ILightCreator
{
public聽Light聽Create()
聽聽聽聽{
return new聽BulbLight();
聽聽聽聽}
}.
2. 娉ㄥ唽鏋勯犲瓙
creators.Register("Bulb",聽new聽BulbLightCreator());
creators.Register("Tube",聽new聽TubeLightCreator());.
3.聽Simple Factory涓垱寤哄璞÷?/em>
public class聽LightSimpleFactory.Create(string聽lightType)
{
聽聽聽聽ILightCreator聽creator聽=聽creators.Find(lightType);
return聽creator.Create();
}鏋勯犲瓙鍏跺疄灝辨槸Factory Method銆傝繖鏍? 閫氳繃娉ㄥ唽鏋勯犲瓙錛?.涓師鏉ョ殑switch()榪囩▼琚秷闄わ紝渚濊禆鍏崇郴闅忎箣瑙i櫎銆傚叾涓殑Register(), Find()瀹規槗鐞嗚В錛屾柟娉曞氨涓嶅啓浜嗐?/em>
鏂扮殑綾誨瀷鍙氳繃鏂版敞鍐屾瀯閫犲瓙鏉ュ疄鐜版墿灞曘傚綋鐒?涓殑娉ㄥ唽浠g爜浠呬粎鏄ず渚嬶紝瀹屽叏鍙互鏀懼埌閰嶇疆鏂囦歡涓幓瀹炵幇錛屾瘮濡傦細
<lightCreators>
<add聽name="Bulb"聽type="BulbLightCreator,"/>
<add聽name="Tube"聽type="TubeLightCreator,"/>
</lightCreators>
鍏禝LightCreator緇ф壙浣撶郴錛屾槸Factory Method妯″紡銆侺ightSimpleFactory鍜宑reators鐨勫叧緋籛alkdan娌℃湁璇存槑錛屼篃鍙疄鐜頒負Strategy妯″紡銆?/p>
浜屻傚紩鐢抽棶棰?/p>
涓夈傜粨璁?/p>
涓涓函鐨凢actory Method妯″紡鍏秄actory method涓嶅彲浠ョ敤static瀹炵幇錛岄氳繃涓庡叾浠栨ā寮忕粨鍚堜嬌鐢ㄥ彲浠ヨ繘琛屽彉閫氥傝屼笖閫氬父鎯呭喌涓婩actory Method妯″紡閮戒細涓庡叾浠栨ā寮忕浉緇撳悎銆侫bstract Factory妯″紡閫氬父灝辨槸鐢‵actory Method妯″紡鏉ュ疄鐜般?/p>
1.娓呮櫚鎬у拰綆浠嬫ф槸鏈涓洪噸瑕佺殑錛氫竴涓ā鍧楃殑鐢ㄦ埛姘歌繙涔熶笉搴旇琚ā鍧楃殑琛屼負鎵榪鋒儜錛堥偅鏍峰氨涓嶆竻鏅頒簡錛夛紱妯″潡瑕佸敖鍙兘鐨勫皬錛屼絾鍙堜笉鑳藉お灝忋旀湳璇ā鍧楋紙module錛夊湪鏈功涓殑鐢ㄦ硶錛屾槸鎸囦換浣曞彲閲嶇敤鐨勮蔣浠剁粍浠訛紝浠庡崟涓柟娉曪紝鍒板寘鍚涓寘鐨勫鏉傜郴緇熼兘鍙互鏄竴涓ā鍧椼曘?/p>
2.浠g爜搴旇琚噸鐢紝鑰屼笉鏄鎷瘋礉銆?/p>
3.妯″潡涔嬮棿鐨勭浉渚濇у簲璇ュ敖鍙兘闄嶄綆鍒版渶灝忋?/p>
4.閿欒搴旇灝芥棭琚嫻嬪嚭鏉ワ紝鐞嗘兂鎯呭喌涓嬫槸鍦ㄧ紪璇戞椂鍒匯?/p>
聽聽聽 /** * Returns whether the specified node is a leaf node. * The way the test is performed depends on the * <code>askAllowsChildren</code> setting. * * @param node the node to check * @return true if the node is a leaf node * * @see #asksAllowsChildren * @see TreeModel#isLeaf */ publicboolean isLeaf(Object node) { if(asksAllowsChildren) return !((TreeNode)node).getAllowsChildren(); return ((TreeNode)node).isLeaf(); }鑰宎sksAllowsChildren鐢辨柟娉晄etAsksAllowsChildren璁懼畾
聽聽聽 /** * Sets whether or not to test leafness by asking getAllowsChildren() * or isLeaf() to the TreeNodes. If newvalue is true, getAllowsChildren() * is messaged, otherwise isLeaf() is messaged. */ publicvoid setAsksAllowsChildren(boolean newValue) { asksAllowsChildren = newValue; }娉ㄦ剰錛屽湪setRoot鏃跺彲浠ュ彧璁懼畾涓嬈?br />
聽聽聽聽聽聽 model.setRoot(root); /* * Since nodes are added dynamically in this application, the only true * leaf nodes are nodes that don't allow children to be added. (By * default, askAllowsChildren is false and all nodes without children * are considered to be leaves.) * * But there's a complication: when the tree structure changes, JTree * pre-expands the root node unless it's a leaf. To avoid having the * root pre-expanded, we set askAllowsChildren *after* assigning the * new root. */ model.setAsksAllowsChildren(true);
鑰屽湪璋冪敤model.nodeStructureChanged(node)鍓嶅悗瑕佸垎鍒緗負false鍜宼rue銆傝繖鏄洜涓簄odeStructureChanged浼氳嚜鍔ㄧ殑璇曞浘灞曞紑鑺傜偣node錛屽鏋渋sLeaf榪斿洖涓篺alse銆傝瘯鎯籌紝asksAllowsChildren榪斿洖true錛屽垯涓涓粨鐐瑰嵆浣挎病鏈夊瓙緇撶偣錛屽彧瑕佽緗簡allowsChildren錛屽搴旂殑treeNodeModel鐨刬sLeaf灝變細榪斿洖true銆傜劧鑰屽綋鑷姩灞曞紑鑺傜偣鐨勬椂鍊欏彂鐜板茍娌℃湁瀛愮粨鐐癸紝褰撶劧灝變細鎶婂浘鏍?img height="10" alt="ParentNode.jpg" src="http://www.aygfsteel.com/images/blogjava_net/pragmatic/JTips/ParentNode.jpg" width="15" border="0" />鍘繪帀浜嗐傝屽厛鎶奱sksAllowsChildren璁句負false錛屽氨涓嶄細鍘昏瘯鍥懼睍寮璇ヨ妭鐐癸紝鐒跺悗璁句負true錛岃鑺傜偣灝變細鏄劇ず涓?img height="10" alt="ParentNode.jpg" src="http://www.aygfsteel.com/images/blogjava_net/pragmatic/JTips/ParentNode.jpg" width="15" border="0" />銆?/font>聽聽聽
聽聽聽 /** * Determines whether or not this node is allowed to have children. * If <code>allows</code> is false, all of this node's children are * removed. * <p> * Note: By default, a node allows children. * * @param allows true if this node is allowed to have children */ publicvoid setAllowsChildren(boolean allows) { if (allows != allowsChildren) { allowsChildren = allows; if (!allowsChildren) { removeAllChildren(); } } }
node.removeAllChildren(); DefaultTreeModel model = (DefaultTreeModel) cardTreeView.getModel(); /* * To avoid having JTree re-expand the root node, we disable * ask-allows-children when we notify JTree about the new node * structure. */ model.setAsksAllowsChildren(false); model.nodeStructureChanged(node); model.setAsksAllowsChildren(true);