Package com.thoughtworks.xstream.converters

Examples of com.thoughtworks.xstream.converters.SingleValueConverter.fromString()


                Class type = field.getType();
                if (converter == null) {
                    converter = mapper.getConverterFromItemType(type);
                }
                if (converter != null) {
                    Object value = converter.fromString(reader.getAttribute(attrAlias));
                    if (type.isPrimitive()) {
                        type = Primitives.box(type);
                    }
                    if (value != null && !type.isAssignableFrom(value.getClass())) {
                        throw new ConversionException("Cannot convert type " + value.getClass().getName() + " to type " + type.getName());
View Full Code Here


                }

                if (valueAsAttribute && valueConverter != null) {
                    final String attribute = reader.getAttribute(valueName);
                    if (attribute != null) {
                        value = valueConverter.fromString(attribute);
                    }
                }
            }

            if (keyConverter == null) {
View Full Code Here

                // we need a converter that produces a string representation only
                final SingleValueConverter converter = mapper.getConverterFromAttribute(classDefiningField, attrName,
                    field.getType());
                Class<?> type = field.getType();
                if (converter != null) {
                    final Object value = converter.fromString(reader.getAttribute(attrAlias));
                    if (type.isPrimitive()) {
                        type = Primitives.box(type);
                    }
                    if (value != null && !type.isAssignableFrom(value.getClass())) {
                        throw new ConversionException("Cannot convert type "
View Full Code Here

        final Class<?> type = getMapper().realClass(key.substring(0, idx));
        final Converter converter = getConverterLookup().lookupConverterForType(type);
        if (converter instanceof SingleValueConverter) {
            final SingleValueConverter svConverter = (SingleValueConverter)converter;
            @SuppressWarnings("unchecked")
            final K k = (K)svConverter.fromString(key.substring(idx + 1));
            return k;
        } else {
            throw new StreamException("No SingleValueConverter for type " + type.getName() + " available");
        }
    }
View Full Code Here

        }
    }

    public void testCanConvertRightType() throws NoSuchMethodException {
        SingleValueConverter converter = new ToStringConverter(Foo.class);
        assertTrue(converter.fromString("hello") instanceof Foo);
        assertEquals("hello", ((Foo) converter.fromString("hello")).foo);
    }

    public void testCanInnocentlyConvertWrongTypeToString() throws NoSuchMethodException {
        SingleValueConverter converter = new ToStringConverter(Foo.class);
View Full Code Here

    }

    public void testCanConvertRightType() throws NoSuchMethodException {
        SingleValueConverter converter = new ToStringConverter(Foo.class);
        assertTrue(converter.fromString("hello") instanceof Foo);
        assertEquals("hello", ((Foo) converter.fromString("hello")).foo);
    }

    public void testCanInnocentlyConvertWrongTypeToString() throws NoSuchMethodException {
        SingleValueConverter converter = new ToStringConverter(Foo.class);
        assertEquals("whoa", converter.toString("whoa"));
View Full Code Here

    }

    public void testCantConvertWrongType() throws NoSuchMethodException {
        SingleValueConverter converter = new ToStringConverter(BadFoo1.class);
        try {
            converter.fromString("whoa");
            fail("shoulda barfed");
        } catch (ConversionException e) {
            assertTrue(e.getMessage().startsWith("Unable to target single String param constructor"));
            assertTrue(e.getCause() instanceof NullPointerException);
        }
View Full Code Here

        DatatypeFactory factory = DatatypeFactory.newInstance();
        for (int i = 0; i < STRINGS.length; i++) {
            final String s = STRINGS[i];
            Duration o = factory.newDuration(s);
            assertEquals(s, converter.toString(o));
            assertEquals(o, converter.fromString(s));
        }
    }

}
View Full Code Here

        Software software = new Software("Joe Walnes", "XStream");
        SingleValueConverter converter = new PropertyEditorCapableConverter(
            SoftwarePropertyEditor.class, Software.class);
        assertTrue(converter.canConvert(Software.class));
        assertEquals("Joe Walnes:XStream", converter.toString(software));
        assertEquals(software, converter.fromString("Joe Walnes:XStream"));
    }

    public void testConcurrentConversion() throws InterruptedException {
        final SingleValueConverter converter = new PropertyEditorCapableConverter(
            SoftwarePropertyEditor.class, Software.class);
View Full Code Here

                            .currentThread()
                            .getName());
                        while (i < 1000 && !interrupted()) {
                            String formatted = converter.toString(software);
                            Thread.yield();
                            assertEquals(software, converter.fromString(formatted));
                            ++i;
                        }
                    } catch (InterruptedException e) {
                        fail("Unexpected InterruptedException");
                    }
View Full Code Here

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.