Package com.thoughtworks.xstream.converters

Examples of com.thoughtworks.xstream.converters.SingleValueConverter


*/
public class DurationConverterTest extends TestCase {
    private static final String[] STRINGS = {"-P1Y2M3DT4H5M6.7S", "P1Y", "PT1H2M"};

    public void testConversion() throws Exception {
        final SingleValueConverter converter = new DurationConverter();
        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


    }

    public void testCanBeUsedForCustomTypes() {
        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"));
    }
View Full Code Here

        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);

        final Map exceptions = new HashMap();
        final ThreadGroup tg = new ThreadGroup(getName()) {
            public void uncaughtException(Thread t, Throwable e) {
                exceptions.put(e, t.getName());
                super.uncaughtException(t, e);
            }
        };

        final Map references = new HashMap();
        final int[] counter = new int[1];
        counter[0] = 0;
        final Thread[] threads = new Thread[10];
        for (int i = 0; i < threads.length; ++i) {
            final String name = "JUnit Thread:" + i;
            references.put(name, new Software("JUnit Thread", Integer.toString(i)));
            threads[i] = new Thread(tg, name) {

                public void run() {
                    int i = 0;
                    try {
                        synchronized (this) {
                            notifyAll();
                            wait();
                        }
                        final Software software = (Software)references.get(Thread
                            .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

    private Converter newInstance(Class<? extends ConverterMatcher> type) {
        Converter converter;
        // TODO: We need a separate exception for runtime initialization.
        try {
            if (SingleValueConverter.class.isAssignableFrom(type)) {
                final SingleValueConverter svc = (SingleValueConverter)type.getConstructor().newInstance();
                converter = new SingleValueConverterWrapper(svc);
            } else {
                converter = (Converter)type.getConstructor().newInstance();
            }
        } catch (InvocationTargetException e) {
View Full Code Here

                    //    lookupType = definedIn;
                    // }
                    defaultFieldDefinition.put(fieldName, reflectionProvider.getField(lookupType, fieldName));
                }
               
                SingleValueConverter converter = mapper.getConverterFromItemType(fieldName, type, definedIn);
                if (converter != null) {
                    final String attribute = mapper.aliasForAttribute(mapper.serializedMember(definedIn, fieldName));
                    if (value != null) {
                        if (writtenAttributes.contains(fieldName)) { // TODO: use attribute
                            throw new ConversionException("Cannot write field with name '" + fieldName
                                + "' twice as attribute for object of type " + source.getClass().getName());
                        }
                        final String str = converter.toString(value);
                        if (str != null) {
                            writer.addAttribute(attribute, str);
                        }
                    }
                    writtenAttributes.add(fieldName); // TODO: use attribute
View Full Code Here

                }
                Class classDefiningField = field.getDeclaringClass();
                if (!mapper.shouldSerializeMember(classDefiningField, attrName)) {
                    continue;
                }
                SingleValueConverter converter = mapper.getConverterFromAttribute(classDefiningField, attrName, field.getType());
                Class type = field.getType();
                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

        final Set seenAsAttributes = new HashSet();

        // Attributes might be preferred to child elements ...
         reflectionProvider.visitSerializableFields(source, new ReflectionProvider.Visitor() {
            public void visit(String fieldName, Class type, Class definedIn, Object value) {
                SingleValueConverter converter = mapper.getConverterFromItemType(fieldName, type);
                if (converter == null) {
                    converter = mapper.getConverterFromItemType(type);
                }
                if (converter != null) {
                    final String str = converter.toString(value);
                    if (str != null) {
                        writer.addAttribute(mapper.aliasForAttribute(fieldName), str);
                    }
                    seenAsAttributes.add(fieldName);
                }
View Full Code Here

            String attrAlias = (String) it.next();
            String attrName = mapper.attributeForAlias(attrAlias);
            Class classDefiningField = determineWhichClassDefinesField(reader);
            boolean fieldExistsInClass = reflectionProvider.fieldDefinedInClass(attrName, result.getClass());
            if (fieldExistsInClass) {
                SingleValueConverter converter = mapper.getConverterFromAttribute(attrName);
                if (converter == null) {
                    converter = mapper.getConverterFromItemType(reflectionProvider.getFieldType(result, attrName, classDefiningField));
                }
                if (converter != null) {
                    Object value = converter.fromString(reader.getAttribute(attrAlias));
                    reflectionProvider.writeField(result, attrName, value, classDefiningField);
                    seenFields.add(classDefiningField, attrName);
                }
            }
        }
View Full Code Here

    private Converter newInstance(Class<? extends ConverterMatcher> type) {
        Converter converter;
        // TODO: We need a separate exception for runtime initialization.
        try {
            if (SingleValueConverter.class.isAssignableFrom(type)) {
                final SingleValueConverter svc = (SingleValueConverter)type.getConstructor().newInstance();
                converter = new SingleValueConverterWrapper(svc);
            } else {
                converter = (Converter)type.getConstructor().newInstance();
            }
        } catch (InvocationTargetException e) {
View Full Code Here

        return (Converter)localConverters.get(new FastField(definedIn, fieldName));
    }

    public SingleValueConverter getConverterFromAttribute(Class definedIn, String attribute,
        Class type) {
        SingleValueConverter converter = getLocalSingleValueConverter(
            definedIn, attribute, type);
        return converter == null
            ? super.getConverterFromAttribute(definedIn, attribute, type)
            : converter;
    }
View Full Code Here

TOP

Related Classes of com.thoughtworks.xstream.converters.SingleValueConverter

Copyright © 2018 www.massapicom. 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.