proxytest.groovy
来自「Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业」· GROOVY 代码 · 共 65 行
GROOVY
65 行
package groovy.util
/**
* @author Dierk Koenig
**/
class ProxyTest extends GroovyTestCase {
void testStringDecoration(){
def original = 'decorated String'
def proxy = new StringDecorator().wrap(original)
// method, that is only known on proxy
assertSame original, proxy.adaptee
// method, that is only known on adaptee is relayed through the proxy
assertEquals original.size(), proxy.size()
// method, that is availabe in both objects should come from proxy
assertEquals 0, proxy.length()
// method, that is availabe in both objects
// but should come from adaptee needs explicit relay
assertEquals original, proxy.toString()
// method from decorator, that is not in adaptee
assertEquals 'new Method reached', proxy.someNewMethod()
}
/*
* Some test cases to probe perceived problems with each and collect on Proxy objects.
* cf. GROOVY-1461. Jonathan Carlson <Jonathan.Carlson@katun.com> made a proposal for a test
* as a single method, Russel Winder <russel@russel.org.uk> split things up when entering
* them so that there is only a single assert per method to try and maximize the benefit of
* the tests.
*/
void testProxyCollect ( ) {
def collection = [ 1 , 2 , 3 ]
def proxy = ( new Proxy ( ) ).wrap ( collection )
assertEquals ( [ 2 , 3 , 4 ] , proxy.collect { it + 1 } )
}
void testProxyAny ( ) {
def collection = [ 1 , 2 , 3 ]
def proxy = ( new Proxy ( ) ).wrap ( collection )
assertEquals ( true , proxy.any { it == 2 } )
}
void testProxyFind ( ) {
def collection = [ 1 , 2 , 3 ]
def proxy = ( new Proxy ( ) ).wrap ( collection )
assertEquals ( 2 , proxy.find { it == 2 } )
}
void testProxyEach ( ) {
def collection = [ 1 , 2 , 3 ]
def proxy = ( new Proxy ( ) ).wrap ( collection )
def testString = ''
proxy.each { testString += it }
assertEquals ( '123' , testString )
}
}
class StringDecorator extends Proxy{
int length() { 0 }
String toString() { adaptee.toString()}
String someNewMethod(){ 'new Method reached' }
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?