Skip to content

Commit

Permalink
Doesn't need to extend from collection converter.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed May 1, 2011
1 parent def41be commit ef34fbc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
10 changes: 3 additions & 7 deletions src/main/java/org/jenkinsci/jruby/RubyArrayConverter.java
@@ -1,10 +1,8 @@
package org.jenkinsci.jruby;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
import com.thoughtworks.xstream.io.ExtendedHierarchicalStreamWriterHelper;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import org.jruby.Ruby;
Expand All @@ -14,11 +12,10 @@
/**
* @author Kohsuke Kawaguchi
*/
public class RubyArrayConverter extends AbstractCollectionConverter {
public class RubyArrayConverter implements Converter {
private final Ruby runtime;

public RubyArrayConverter(XStream xs, Ruby runtime) {
super(xs.getMapper());
public RubyArrayConverter(Ruby runtime) {
this.runtime = runtime;
}

Expand All @@ -39,7 +36,6 @@ public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContex
}
}

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
RubyArray a = RubyArray.newArray(runtime);

Expand Down
18 changes: 14 additions & 4 deletions src/test/java/org/jenkinsci/jruby/BasicTest.java
Expand Up @@ -5,6 +5,7 @@
import junit.framework.TestCase;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyHash;
import org.jruby.RubyObject;
import org.jruby.embed.ScriptingContainer;

Expand All @@ -31,7 +32,7 @@ protected MapperWrapper wrapMapper(MapperWrapper next) {
xs.registerConverter(new RubyFixnumConverter(runtime));
xs.registerConverter(new RubyIntegerConverter(runtime));
xs.registerConverter(new RubyBooleanConverter(runtime));
xs.registerConverter(new RubyArrayConverter(xs,runtime));
xs.registerConverter(new RubyArrayConverter(runtime));
xs.registerConverter(new JRubyXStreamConverter(xs,runtime), XStream.PRIORITY_LOW);
}

Expand All @@ -44,12 +45,21 @@ public void test1() {

public void testArray() {
RubyArray before = (RubyArray)jruby.runScriptlet("[1,\"abc\",nil]");
String xml = xs.toXML(before);
System.out.println(xml);
RubyArray after = (RubyArray) xs.fromXML(xml);
RubyArray after = roundtrip(before);

assertEquals(before.length(), after.length());
for (int i=0; i<before.getLength(); i++)
assertEquals(before.entry(i), after.entry(i));
}

public void testHash() {
RubyHash before = (RubyHash)jruby.runScriptlet("{ 1 => 5, \"foo\" => \"bar\", :abc => :def, \"d\" => [nil,nil]}");
RubyHash after = roundtrip(before);
}

private <T> T roundtrip(T before) {
String xml = xs.toXML(before);
System.out.println(xml);
return (T) xs.fromXML(xml);
}
}

0 comments on commit ef34fbc

Please sign in to comment.