做者:chszsjava
1. 斷言
Java開發者經常使用JUnit或TestNG作單元測試,因此對斷言是很清楚的。斷言是用於驗證假設的條件是否爲真。在Groovy的斷言中,若是假設的條件不爲真,那麼就會拋出java.lang.AssertionError異常。使用Groovy表達式來測試假設條件。
好比:
groovy:000> assert 1==2 : "One isn't Two"
ERROR java.lang.AssertionError:
One isn't Two. Expression: (1 == 2)
at groovysh_evaluate.run (groovysh_evaluate:2)
...
Java的斷言也是這麼寫的:
assert 1==2 : "One isn't Two";
可見,Groovy的斷言語法與Java的斷言相同,Groovy的斷言能夠省略分號。
斷言的使用技巧:
當你使用斷言時,你應用包含一條消息。此消息能夠幫助其餘人維護和理解你的代碼,理清你的意圖。
正則表達式
2. 強斷言
Groovy的斷言assert關鍵字僅僅是檢查表達式是否爲真。若是爲假,斷言僅僅告訴開發者,表達式不成立,以及表達式使用的變量值,除此之外什麼都沒有。若是使用強斷言,斷言的輸出會提供表達式的每個子表達式的可視化展示。下面我將詳細說明強斷言。
groovy:000> assert 1==1: "One isn't Two"
===> null
groovy:000> assert new File('HelloWorld.txt')==new File('Hello.txt')
ERROR org.codehaus.groovy.runtime.powerassert.PowerAssertionError:
assert new File('HelloWorld.txt')==new File('Hello.txt')
| | |
HelloWorld.txt | Hello.txt
false
at groovysh_evaluate.run (groovysh_evaluate:2)
...
斷言能夠方便地對代碼進行測試。
編程
3. Strings
Groovy支持兩種字符串:常規的Java字符串和GStrings。
在Groovy中,用單引號或雙引號括起來的字符串是java.lang.String類的一個實例,
GStrings是groovy.lang.GString類的一個實例,它容許在文本中包含佔位符。GStrings並非String的子類,由於String類是final類型,並且是不能被擴充的。
GString很像普通的字符串,可是他容許在其內使用${..}嵌入變量。若是內嵌的變量只是佔位符,那麼能夠省略{}花括號。
Groovy支持一些編程語言如Perl或Ruby所謂的字符串插入(String Interpolation)。字符串插入是字符串內的表達式或變量的替代。若是你熟悉Unix Shell編程或Ruby、Perl編程,那麼你應該很熟悉這一點。
Java不支持字符串插入,你必須手動的進行值連結。
Java的字符串操做例子:
String name = "Jim";
String helloName = "Hello " + name;
System.out.println(helloName);
若是是使用Groovy的GString,那麼能夠這樣:
groovy:000> str1 = "Li Sir"
groovy:000> str2 = "Hello "
groovy:000> println "$str2$str1"
Hello Li Sir
可見,正如前面所講,若是內嵌的變量只是佔位符,那麼能夠省略{}花括號。
當Groovy看到表達式內的內嵌變量時,Groovy會構造一個org.codehaus.groovy.runtime.GStringImpl來代替java.lang.String。當訪問GString時,表達式會被從新計算。
注意,你能夠在${}內包含任意有效的Groovy表達式,能夠是方法調用或變量名。
windows
4. 單行字符串
單行字符串能夠用單引號或雙引號包圍,可是二者是有區別的。
單引號包圍的表達式,裏面的字符串插值是不會被識別的。雙引號包圍的表達式正好相反。
舉例:
name = "LiSir"
s1 = 'Hello $name'
println s1
輸出:
Hello $name
例子:
name = "LiSir"
s1 = "Hello $name"
println s1
輸出:
Hello LiSir
可見,由雙引號定義的字符串,其內嵌的表達式會被解釋。
編程語言
5. 多行字符串
Groovy支持字符串跨越多行,多行字符串由三個雙引號或者三個單引號定義。
多行字符串對於建立模板(如XML模板)或內嵌的文檔(SQL語句、HTML等)是很是有用的。還有不少用途。好比,可使用多行字符串和字符串插值創建電子郵件的主體內容。
好比:
def name = "Jim"
def multiLineQuote = """
Hello, ${name}
This is a multiline string with double quotes
"""
println multiLineQuote
println multiLineQuote.class.name
def multiLineSingleQuote = '''
Hello, ${name}
This is a multiline string with single quotes
'''
println multiLineSingleQuote
println multiLineSingleQuote.class.name
輸出:
Hello, Jim
This is a multiline string with double quotes
org.codehaus.groovy.runtime.GStringImpl
Hello, ${name}
This is a multiline string with single quotes
java.lang.String單元測試
6. 斜線字符串
正如早先提到的,斜線能夠用於定義字符串。用斜線定義字符串有一個優勢:那就是,字符串自己的內容無需轉義反斜線。
好比:
def winpathQuoted = 'C:\\windows\\system32'
def winpathSlashy = /C:\windows\system32/
println winpathSlashy // C:\windows\system32
assert winpathSlashy ==~ '\\w{1}:\\\\.+\\\\.+'
assert winpathSlashy ==~ /\w{1}:\\.+\\.+/
測試
7. 多行斜線字符串
斜線字符串還可以跨越多行。這一點對於多行正則表達式很是有用。
好比:
def name = "vishal"
def path = "c:/groovy/"
def multilineslashy = /
Hello $name
path $path
dollar = $
path = c:\/groovy
/
println multilineslashy
輸出爲:
Hello vishal
path c:/groovy/
dollar = $
path = c:/groovy
ui
8. 斜線字符串中的美圓符號$
在多行斜線字符串中,若是字符串內出現斜線/,那麼它須要被轉義。另外,若是字符串內出現了$美圓符號(沒有用於表達式時),也須要轉義,不然會報MissingPropertyException異常。
好比:
def name = "vishal"
def path = "c:/groovy"
def multilineSlashy = /
Hello $name
path = $path
dollar = $test
path = c:\/groovy
/
println multilineSlashy
輸出:
Exception thrown
四月 17, 2013 9:11:24 下午 org.codehaus.groovy.runtime.StackTraceUtils sanitize
WARNING: Sanitizing stacktrace:
groovy.lang.MissingPropertyException: No such property: test for class: ConsoleScript3
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
at ConsoleScript3.run(ConsoleScript3:3)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:257)
at groovy.lang.GroovyShell.run(GroovyShell.java:481)
at groovy.lang.GroovyShell.run(GroovyShell.java:163)
at groovy.lang.GroovyShell$run.call(Unknown Source)
at groovy.ui.Console$_runScriptImpl_closure17.doCall(Console.groovy:951)
at groovy.ui.Console$_runScriptImpl_closure17.doCall(Console.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:909)
at groovy.lang.Closure.call(Closure.java:411)
at groovy.lang.Closure.call(Closure.java:405)
at groovy.lang.Closure.run(Closure.java:492)
at java.lang.Thread.run(Thread.java:722)
groovy.lang.MissingPropertyException: No such property: test for class: ConsoleScript3
at ConsoleScript3.run(ConsoleScript3:3)lua