/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. package org.hudsonci.utils.marshal.xref;
  25. import com.thoughtworks.xstream.XStream;
  26. import com.thoughtworks.xstream.annotations.XStreamAlias;
  27. import org.hudsonci.utils.marshal.XStreamMarshaller;
  28. import org.hudsonci.utils.test.TestUtil;
  29. import org.junit.Test;
  30. import java.io.StringReader;
  31. import java.io.StringWriter;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. import static org.junit.Assert.assertEquals;
  35. import static org.junit.Assert.assertNotNull;
  36. import static org.junit.Assert.assertNull;
  37. import static org.junit.Assert.assertTrue;
  38. /**
  39. * Tests for {@link XReferenceStoreConverter}.
  40. */
  41. public class XReferenceStoreConverterTest
  42. {
  43. private final TestUtil util = new TestUtil(this);
  44. @Test
  45. public void testChewDocument() throws Exception {
  46. XStream xs = new XStream();
  47. xs.autodetectAnnotations(true);
  48. XStreamMarshaller marshaller = new XStreamMarshaller(xs);
  49. XReferenceStore store = new FileXReferenceStore(marshaller, util.resolveFile("target/test-xref"));
  50. XReferenceStoreConverter converter = new XReferenceStoreConverter(store, xs.getMapper(), xs.getReflectionProvider());
  51. xs.registerConverter(converter);
  52. Document<Record> doc1 = new Document<Record>();
  53. Record rec1 = new Record("1");
  54. Entity ent1 = new Entity("1");
  55. rec1.set(ent1);
  56. assertNotNull(rec1.entity.holder);
  57. assertTrue(rec1.entity.holder instanceof XReference.InstanceHolder);
  58. XReference.InstanceHolder holder1 = (XReference.InstanceHolder)rec1.entity.holder;
  59. assertNotNull(holder1.instance);
  60. doc1.records.add(rec1);
  61. StringWriter buff = new StringWriter();
  62. marshaller.marshal(doc1, buff);
  63. System.out.println("XML:\n" + buff);
  64. Document<Record> doc2 = (Document<Record>) marshaller.unmarshal(new StringReader(buff.toString()));
  65. assertNotNull(doc2);
  66. assertNotNull(doc2.records);
  67. assertEquals(1, doc2.records.size());
  68. Record rec2 = doc2.records.get(0);
  69. assertNotNull(rec2.entity);
  70. System.out.println("ENT REF: " + rec2.entity);
  71. assertNotNull(rec2.entity.holder);
  72. assertTrue(rec2.entity.holder instanceof XReferenceStoreConverter.UnmarshalHolder);
  73. XReferenceStoreConverter.UnmarshalHolder holder2 = (XReferenceStoreConverter.UnmarshalHolder)rec2.entity.holder;
  74. assertNull(holder2.instance);
  75. Entity ent2 = rec2.get();
  76. System.out.println("ENT: " + ent2);
  77. assertNotNull(ent2);
  78. assertNotNull(holder2.instance);
  79. buff = new StringWriter();
  80. marshaller.marshal(doc2, buff);
  81. System.out.println("XML\n" + buff);
  82. }
  83. @XStreamAlias("document")
  84. private static class Document<T>
  85. {
  86. public final List<T> records = new ArrayList<T>();
  87. }
  88. @XStreamAlias("record")
  89. private static class Record
  90. {
  91. private final String name;
  92. private XReference entity;
  93. private Record(String name) {
  94. this.name = name;
  95. }
  96. public void set(Entity value) {
  97. entity = new EntityReference(value);
  98. }
  99. public Entity get() {
  100. if (entity != null) {
  101. return (Entity) entity.get();
  102. }
  103. return null;
  104. }
  105. @XStreamAlias("entity-xref")
  106. private class EntityReference
  107. extends XReference
  108. {
  109. private EntityReference(final Entity value) {
  110. super(value);
  111. }
  112. @Override
  113. public String getPath() {
  114. return "test-" + name + ".xml";
  115. }
  116. }
  117. }
  118. @XStreamAlias("entity")
  119. private static class Entity
  120. {
  121. public final String name;
  122. private Entity(String name) {
  123. this.name = name;
  124. }
  125. @Override
  126. public String toString() {
  127. return "Entity{" +
  128. "name='" + name + '\'' +
  129. '}';
  130. }
  131. }
  132. }