commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joachim Maes <joachim.m...@pandora.be>
Subject jxpath : BasicTypeConverter problem
Date Sat, 22 Mar 2003 15:08:34 GMT
Hi,

I started using JXPath last week (and I am very excited about it!), and
think found a problem in the conversions to String types in
BasicTypeConverter.
I ran into it using an extension function that refers to the commons
StringUtils class.  Basically I use the expression
"stringutils:getChomp(type,'.')".  Due to a problem in the type
conversion from a NodeSet to a String, the actual method gets called
with String arguments
"org.apache.commons.jxpath.ri.EvalContext$SimpleNodeSet@443226" and ".".

I added tests to the BasicTypeConverterTest class to illustrate the
problem, and then I made a simple change to BasicTypeConverter to make
the adapted test succeed.  Presumably there are other approaches
possible to resolve the problem (like implementing toString in
EvalContext$SimpleNodeSet).

Please find both classes/changes in attachment.

Regards,
Joachim Maes


1) added tests BasicTypeConverterTest :

      public void testSingletonCollectionToString() {
          assertConversion(Collections.singleton("Earth"), String.class,
"Earth");
      }

      public void testSingletonArrayToString() {
          assertConversion(new String[] {"Earth"}, String.class, "Earth");
      }

      public void testPointerToString() {
          assertConversion(
                  new Pointer() {
                      public Object getValue() {
                          return "value";
                      }
                      public Object getNode() {
                          return null;
                      }
                      public void setValue(Object value) {
                      }
                      public Object getRootNode() {
                          return null;
                      }
                      public String asPath() {
                          return null;
                      }
                      public Object clone() {
                          return null;
                      }
                      public int compareTo(Object o) {
                          return 0;
                      }
                  },
                  String.class,
                  "value");
      }

      public void testNodeSetToString() {
          assertConversion(
                  new NodeSet() {
                      public List getNodes() {
                          return null;
                      }
                      public List getPointers() {
                          return null;
                      }
                      public List getValues() {
                          return Collections.singletonList("hello");
                      }
                  },
                  String.class,
                  "hello");
      }

      // succeeds in current version
      public void testNodeSetToInteger() {
          assertConversion(
                  new NodeSet() {
                      public List getNodes() {
                          return null;
                      }
                      public List getPointers() {
                          return null;
                      }
                      public List getValues() {
                          return Collections.singletonList("9");
                      }
                  },
                  Integer.class,
                  new Integer(9));
      }



2) change to BasicTypeConverter at line 223:

          if (toType == String.class
                  && !(object instanceof Collection)
                  && !fromType.isArray()
                  && !(object instanceof NodeSet)
                  && !(object instanceof Pointer)) {

              return object.toString();
          }




Mime
View raw message