PageRenderTime 40ms CodeModel.GetById 17ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-utils/src/test/java/org/hudsonci/utils/marshal/xref/XReferenceStoreConverterTest.java

http://github.com/hudson/hudson
Java | 160 lines | 106 code | 28 blank | 26 comment | 2 complexity | c8d76015fa9e8c8743f474eeb369d697 MD5 | raw file
  1/**
  2 * The MIT License
  3 *
  4 * Copyright (c) 2010-2011 Sonatype, Inc. All rights reserved.
  5 *
  6 * Permission is hereby granted, free of charge, to any person obtaining a copy
  7 * of this software and associated documentation files (the "Software"), to deal
  8 * in the Software without restriction, including without limitation the rights
  9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10 * copies of the Software, and to permit persons to whom the Software is
 11 * furnished to do so, subject to the following conditions:
 12 *
 13 * The above copyright notice and this permission notice shall be included in
 14 * all copies or substantial portions of the Software.
 15 *
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22 * THE SOFTWARE.
 23 */
 24
 25package org.hudsonci.utils.marshal.xref;
 26
 27import com.thoughtworks.xstream.XStream;
 28import com.thoughtworks.xstream.annotations.XStreamAlias;
 29
 30import org.hudsonci.utils.marshal.XStreamMarshaller;
 31import org.hudsonci.utils.test.TestUtil;
 32import org.junit.Test;
 33
 34import java.io.StringReader;
 35import java.io.StringWriter;
 36import java.util.ArrayList;
 37import java.util.List;
 38
 39import static org.junit.Assert.assertEquals;
 40import static org.junit.Assert.assertNotNull;
 41import static org.junit.Assert.assertNull;
 42import static org.junit.Assert.assertTrue;
 43
 44
 45/**
 46 * Tests for {@link XReferenceStoreConverter}.
 47 */
 48public class XReferenceStoreConverterTest
 49{
 50    private final TestUtil util = new TestUtil(this);
 51
 52    @Test
 53    public void testChewDocument() throws Exception {
 54        XStream xs = new XStream();
 55        xs.autodetectAnnotations(true);
 56        XStreamMarshaller marshaller = new XStreamMarshaller(xs);
 57
 58        XReferenceStore store = new FileXReferenceStore(marshaller, util.resolveFile("target/test-xref"));
 59        XReferenceStoreConverter converter = new XReferenceStoreConverter(store, xs.getMapper(), xs.getReflectionProvider());
 60        xs.registerConverter(converter);
 61
 62        Document<Record> doc1 = new Document<Record>();
 63        Record rec1 = new Record("1");
 64        Entity ent1 = new Entity("1");
 65        rec1.set(ent1);
 66        assertNotNull(rec1.entity.holder);
 67        assertTrue(rec1.entity.holder instanceof XReference.InstanceHolder);
 68        XReference.InstanceHolder holder1 = (XReference.InstanceHolder)rec1.entity.holder;
 69        assertNotNull(holder1.instance);
 70
 71        doc1.records.add(rec1);
 72
 73        StringWriter buff = new StringWriter();
 74        marshaller.marshal(doc1, buff);
 75        System.out.println("XML:\n" + buff);
 76
 77        Document<Record> doc2 = (Document<Record>) marshaller.unmarshal(new StringReader(buff.toString()));
 78        assertNotNull(doc2);
 79        assertNotNull(doc2.records);
 80        assertEquals(1, doc2.records.size());
 81
 82        Record rec2 = doc2.records.get(0);
 83        assertNotNull(rec2.entity);
 84        System.out.println("ENT REF: " + rec2.entity);
 85        assertNotNull(rec2.entity.holder);
 86        assertTrue(rec2.entity.holder instanceof XReferenceStoreConverter.UnmarshalHolder);
 87
 88        XReferenceStoreConverter.UnmarshalHolder holder2 = (XReferenceStoreConverter.UnmarshalHolder)rec2.entity.holder;
 89        assertNull(holder2.instance);
 90
 91        Entity ent2 = rec2.get();
 92        System.out.println("ENT: " + ent2);
 93        assertNotNull(ent2);
 94        assertNotNull(holder2.instance);
 95
 96        buff = new StringWriter();
 97        marshaller.marshal(doc2, buff);
 98        System.out.println("XML\n" + buff);
 99    }
100
101    @XStreamAlias("document")
102    private static class Document<T>
103    {
104        public final List<T> records = new ArrayList<T>();
105    }
106
107    @XStreamAlias("record")
108    private static class Record
109    {
110        private final String name;
111
112        private XReference entity;
113
114        private Record(String name) {
115            this.name = name;
116        }
117
118        public void set(Entity value) {
119            entity = new EntityReference(value);
120        }
121
122        public Entity get() {
123            if (entity != null) {
124                return (Entity) entity.get();
125            }
126            return null;
127        }
128
129        @XStreamAlias("entity-xref")
130        private class EntityReference
131            extends XReference
132        {
133            private EntityReference(final Entity value) {
134                super(value);
135            }
136
137            @Override
138            public String getPath() {
139                return "test-" + name + ".xml";
140            }
141        }
142    }
143
144    @XStreamAlias("entity")
145    private static class Entity
146    {
147        public final String name;
148
149        private Entity(String name) {
150            this.name = name;
151        }
152
153        @Override
154        public String toString() {
155            return "Entity{" +
156                "name='" + name + '\'' +
157                '}';
158        }
159    }
160}