2014年4月30日 星期三

ANT - build junit時出現Errors while applying transformations: Fatal error during transformation...


今天播點時間來實作『一年前』(沒錯...我拖了一年...)上課學習的東西,將我的TestCase搬到Jenkins上,並安裝emma plugin觀看Junit Testing Report,
首先第一件事情當然是要寫一個ANT Build,但執行的時候出現以下錯誤:

[junitreport] Processing /Users/wuanne/workspace/eclipse/XXXXTest/build/junitReport/TESTS-TestSuites.xml to /var/folders/59/jk74nmqj48n_6wksbv0lccw80000gn/T/null1033290394
[junitreport] Loading stylesheet jar:file:/Applications/eclipse/plugins/org.apache.ant_1.8.4.v201303080030/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] : Error! 非靜態 Java 函數 'replace' 的第一個引數不是有效的物件參照。
[junitreport] : Error! 無法編譯樣式表
[junitreport] : Fatal Error! 無法轉換 data-type 'void' 為 'reference'。 Cause: 無法轉換 data-type 'void' 為 'reference'。
[junitreport] Failed to process /Users/wuanne/workspace/eclipse/XXXXTest/build/junitReport/TESTS-TestSuites.xml

BUILD FAILED

/Users/wuanne/workspace/eclipse/XXXXTest/build/build.xml:90: Errors while applying transformations: Fatal error during transformation

報錯的build.xml:90為:
<target name="junit-report">
<junitreport todir="${junitReport.dir}">
...
</target>

當然這行看起來實在沒什麼問題,google了一下,參考上述討論網站的解答:
不要使用eclipse自帶的ANT,自行安裝ANT,並在eclipse->preference->Ant->Runtime,按下『Ant Home』設定自行安裝的ANT,重新build,就可以成功了~

ESAPI - 出現 Failed to load ESAPI.properties as a classloader resource...

參考:Configuring ESAPI for use with a Java Web Application

客戶需要使用ESAPI來驗證輸出入的參數是安全的,但我一開始嘗試使用就報錯:

org.owasp.esapi.errors.ConfigurationException: java.lang.reflect.InvocationTargetException SecurityConfiguration class (org.owasp.esapi.reference.DefaultSecurityConfiguration) CTOR threw exception.
at org.owasp.esapi.util.ObjFactory.make(ObjFactory.java:129)
at org.owasp.esapi.ESAPI.securityConfiguration(ESAPI.java:184)
at org.owasp.esapi.ESAPI.httpUtilities(ESAPI.java:121)
at com.ctbc.mi.web.util.ServletUtil.getESAPIRequestParameter(ServletUtil.java:169)

at com.ctbc.mi.web.servlet.query.MIQueryServletMain.doProcess(MIQueryServletMain.java:35)
.......
.......
Caused by: java.lang.IllegalArgumentException: Failed to load ESAPI.properties as a classloader resource.
at org.owasp.esapi.reference.DefaultSecurityConfiguration.loadConfigurationFromClasspath(DefaultSecurityConfiguration.java:667)
at org.owasp.esapi.reference.DefaultSecurityConfiguration.loadConfiguration(DefaultSecurityConfiguration.java:436)

... 48 more


查看root exception:
Caused by: java.lang.IllegalArgumentException: Failed to load ESAPI.properties as a classloader resource.
很明顯是設定錯誤,少了ESAPI.properties這個檔案,依照上述參考網站所述,建議放在src root下:
  1. Create a ESAPI.properties file in the root source directory of your web application. Do not place it in a package inside the root source directory because the DefaultSecurityConfiguration will not find it.
更改置放的位置後就可以順利執行了~



2014年4月19日 星期六

mybatis錯誤:The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,plugins?,environments?,databaseIdProvider?,mappers?)".

Myibatis 配置问题
這樣的mybatis-config.xml:

 
          ...  
 
 
 
          ...
 
 
          ...
            
 
            ....     
 

錯誤:
The content of element type "configuration" must match 

 "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,plugins?,environments?,databaseIdProvider?,mappers?)".

原來mybatis的xml宣告是有順序的,務必要按照錯誤上所說的:
properties 再來是 settings 再來 typeAliases... 依此類推。

因此以我的問題來說,只要將settings改到properties下面environments之上就可以囉(也就是中間)
更改後如下:


 
          ...  
 
 
 
          ...
        

 
          ...
 
    
 
            ....     
 

2014年4月4日 星期五

Junit - 如何比較JsonArray是否相同?

參考:http://stackoverflow.com/questions/2253750/compare-two-json-objects-in-java

好久沒寫blog了,這陣子還是忙到昏天地暗,但也寫了一些HttpUnit的練習,有機會再來分享:)

json是很常使用的格式,我自己的專案也大量的運用它來傳遞訊息,那麼,如何比較兩個jsonArray是否相等呢?(或json)

用junit的assertEquals()是無法正確比較其中內容的,因此我們需要額外的方式:

  1. 下載JSONassert From Maven
  2. 使用:
    JSONAssert.assertEquals(expected,actual,false);
上述的第三個參數:boolean strict,說明,可參考doc上所寫:

There are two modes, strict and non-strict. In most cases, you will probably want to set strict to false, since that will make the tests less brittle.
Strict tests require all of the elements requested to be returned, and only those elements (ie, the tests are non-extensible). Arrays of elements must be returned in the same order as expected. For example, say I'm expecting:
{id:123,things['a','b','c']}The following would match when doing non-strict checking, but would fail on strict checking:
{id:123,things['c','b','a'],anotherfield:'blah'}

何時該使用strict(==true)就端看個人需求選擇囉~