[groovy-user] groovy lists and java arrays

1 view
Skip to first unread message

John Sanabria

unread,
Nov 17, 2007, 2:09:10 AM11/17/07
to us...@groovy.codehaus.org
Hi,

I'm trying to implement a java interface using groovy and then using
GroovyTestCase validate my implementation.

That is my implementation of the Enumeration interface:
-----------------------------------
package paquete

import paquete.ImplMatrixElementI

class ImplEnumeration implements Enumeration {
ImplMatrixElementI vector = []
int pointer = 0
public ImplEnumeration(x) {
(0..<(x.size)) { item ->
def y = new ImplMatrixElementI()
y.value = x[item]
vector << y
}
}
public void setElementAt(int index,double value) {
vector[index] = new ImplMatrixElementI(value: value)
}
public void setElements(double[] x) {
(0..<(x.size)) { item ->
vector[item] = x[item]
}
}
public boolean hasMoreElements() {
if (pointer < vector.size)
return true
return false
}
public Object nextElement() {
if (pointer < vector.size)
return vector[pointer++]
return null
}
}
----------------------------------
import groovy.util.GroovyTestCase as GTC
import paquete.ImplEnumeration

class ImplEnumerationTestCase extends GTC {
void test() {
double y = new double[3]

y[0] = 1.0
y[1] = 2.0
y[2] = 3.0
ImplEnumeration x = new ImplEnumeration(y)
assert x.hasMoreElements() == true : "Failed has more
elements"
assert x.nextElement() == 1.0 : "Failing retrieving 1.0"
assert x.nextElement() == 2.0 : "Failing retrieveing 2.0"
assert x.hasMoreElements() == true : "Failed has more
elements"
}
}
----------------------------
Now, when I try to test the test case, I got this:

groovy -cp
lib/or124.jar:lib/liblp.jar:lib/junit-4.4.jar:/usr/local/apps/groovy/embeddable/groovy-all-1.0.jar
ImplEnumerationTestCase.groovy
.E
Time: 0.005
There was 1 error:
1)
test(ImplEnumerationTestCase)org.codehaus.groovy.runtime.typehandling.GroovyCastException:
Cannot cast object '[D@11b9fb1' with class '[D' to class 'java.lang.Number'
at
org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:130)
at
org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:234)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:656)
at ImplEnumerationTestCase.test(ImplEnumerationTestCase.groovy:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at
org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:69)
at
org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at
groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:664)
at
org.codehaus.groovy.runtime.Invoker.invokeStaticMethod(Invoker.java:158)
at
org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:131)
at groovy.lang.GroovyShell.runTest(GroovyShell.java:333)
at
groovy.lang.GroovyShell.runMainOrTestOrRunnable(GroovyShell.java:273)
at groovy.lang.GroovyShell.run(GroovyShell.java:244)
at groovy.lang.GroovyShell.run(GroovyShell.java:173)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:483)
at groovy.ui.GroovyMain.run(GroovyMain.java:293)
at groovy.ui.GroovyMain.process(GroovyMain.java:279)
at groovy.ui.GroovyMain.main(GroovyMain.java:145)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at
org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:131)
at
org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:160)

FAILURES!!!
Tests run: 1, Failures: 0, Errors: 1

Thanks for your help,

Regards

---------------------------------------------------------------------
To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Russel Winder

unread,
Nov 17, 2007, 2:38:28 AM11/17/07
to us...@groovy.codehaus.org

On Sat, 2007-11-17 at 03:09 -0400, John Sanabria wrote:

> class ImplEnumerationTestCase extends GTC {
> void test() {
> double y = new double[3]

Isn't there a problem here?

def y = new double[3]
or


double[] y = new double[3]

> y[0] = 1.0
> y[1] = 2.0
> y[2] = 3.0
> ImplEnumeration x = new ImplEnumeration(y)
> assert x.hasMoreElements() == true : "Failed has more
> elements"
> assert x.nextElement() == 1.0 : "Failing retrieving 1.0"
> assert x.nextElement() == 2.0 : "Failing retrieveing 2.0"
> assert x.hasMoreElements() == true : "Failed has more
> elements"
> }
> }

> groovy -cp

> lib/or124.jar:lib/liblp.jar:lib/junit-4.4.jar:/usr/local/apps/groovy/embeddable/groovy-all-1.0.jar
> ImplEnumerationTestCase.groovy

GroovyTestCase is a JUnit 3.8.x thing does JUnit 4.4 support 'old style'
JUnit?

--
Russel.
====================================================
Dr Russel Winder Partner

Concertant LLP t: +44 20 7193 9203
41 Buckmaster Road, f: +44 8700 516 084
London SW11 1EN, UK. m: +44 7770 465 077

signature.asc
Reply all
Reply to author
Forward
0 new messages