"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/test/groovy/GroovyInterceptableTest.groovy" between
apache-groovy-src-3.0.8.zip and apache-groovy-src-3.0.9.zip

About: Groovy is an agile dynamic (scripting) language for the Java Platform. It has features similar to those of Python, Ruby, Perl, and Smalltalk. Source release.

GroovyInterceptableTest.groovy  (apache-groovy-src-3.0.8):GroovyInterceptableTest.groovy  (apache-groovy-src-3.0.9)
skipping to change at line 24 skipping to change at line 24
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package groovy package groovy
import groovy.test.GroovyTestCase import groovy.test.GroovyTestCase
import org.codehaus.groovy.runtime.ReflectionMethodInvoker import org.codehaus.groovy.runtime.ReflectionMethodInvoker
class GroovyInterceptableTest extends GroovyTestCase { final class GroovyInterceptableTest extends GroovyTestCase {
void testMethodInterception() { void testMethodIntercept1() {
def g = new GI() def g = new GI()
assert g.someInt() == 2806 assert g.someInt() == 2806
assert g.someUnexistingMethod() == 1 assert g.someUnexistingMethod() == 1
assert g.toString() == "invokeMethodToString" assert g.toString() == "invokeMethodToString"
} }
void testProperties() { void testMethodIntercept2() {
def g = new GI() def g = new GI()
assert g.foo == 89 assert g.foo == 89
g.foo = 90 g.foo = 90
assert g.foo == 90 assert g.foo == 90
// should this be 1 or 90? // Should this be 1 or 90?
assert g.getFoo() == 1 assert g.getFoo() == 1
} }
void testCallMissingMethod() { // GROOVY-3015
void testMethodIntercept3() {
String shared = '''\
import org.codehaus.groovy.runtime.InvokerHelper
import org.codehaus.groovy.runtime.StringBufferWriter
import static groovy.test.GroovyTestCase.assertEquals
class Traceable implements GroovyInterceptable {
private static int indent = 1
Writer writer = new PrintWriter(System.out)
Object invokeMethod(String name, Object args) {
writer.write('\\n' + (' ' * indent) + 'Enter ' + name)
indent += 1
def result = InvokerHelper.getMetaClass(this).invokeMethod(t
his, name, args)
indent -= 1
writer.write('\\n' + (' ' * indent) + 'Leave ' + name)
return result
}
}
class Whatever extends Traceable {
int inner() { return 1 }
int outer() { return inner() }
int shouldTraceOuterAndInnerMethod() { return outer() }
def shouldTraceOuterAndInnerClosure = { -> return outer() }
}
def log = new StringBuffer()
def obj = new Whatever(writer: new StringBufferWriter(log))
'''
assertScript shared + '''
obj.shouldTraceOuterAndInnerMethod()
assertEquals """
| Enter shouldTraceOuterAndInnerMethod
| Enter outer
| Enter inner
| Leave inner
| Leave outer
| Leave shouldTraceOuterAndInnerMethod""".stripMargin(), log.toStri
ng()
'''
assertScript shared + '''
obj.shouldTraceOuterAndInnerClosure()
assertEquals """
| Enter shouldTraceOuterAndInnerClosure
| Enter outer
| Enter inner
| Leave inner
| Leave outer
| Leave shouldTraceOuterAndInnerClosure""".stripMargin(), log.toStr
ing()
'''
}
void testMissingMethod1() {
def obj = new GI2() def obj = new GI2()
shouldFail { obj.notAMethod() } shouldFail { obj.notAMethod() }
assert 'missing' == obj.result assert 'missing' == obj.result
} }
void testCallMissingMethodFromInstance() { void testMissingMethod2() {
def obj = new GI2() def obj = new GI2()
shouldFail { obj.method() } shouldFail { obj.method() }
assert 'missing' == obj.result assert 'missing' == obj.result
} }
} }
class GI implements GroovyInterceptable { //------------------------------------------------------------------------------
class GI implements GroovyInterceptable {
def foo = 89 def foo = 89
int someInt() { 2806 } int someInt() { 2806 }
@Override
String toString() { "originalToString" } String toString() { "originalToString" }
@Override
Object invokeMethod(String name, Object args) { Object invokeMethod(String name, Object args) {
if ("toString" == name) if ("toString" == name)
return "invokeMethodToString" return "invokeMethodToString"
else if ("someInt" == name) else if ("someInt" == name)
return ReflectionMethodInvoker.invoke(this, name, args) return ReflectionMethodInvoker.invoke(this, name, args)
else else
return 1 return 1
} }
} }
class GI2 implements GroovyInterceptable { class GI2 implements GroovyInterceptable {
def result = "" def result = ""
def invokeMethod(String name, args) { @Override
def metaMethod = Foo.metaClass.getMetaMethod(name, args) def invokeMethod(String name, args) {
if (metaMethod != null) return metaMethod.invoke(this, args) def metaMethod = Foo.metaClass.getMetaMethod(name, args)
result += "missing" if (metaMethod != null) return metaMethod.invoke(this, args)
throw new MissingMethodException(name, Foo.class, args) result += "missing"
} throw new MissingMethodException(name, Foo.class, args)
}
def method() { def method() {
notAMethod() notAMethod()
} }
} }
 End of changes. 13 change blocks. 
21 lines changed or deleted 81 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)