Search in sources :

Example 11 with ValueList

use of eu.esdihumboldt.hale.common.core.io.ValueList in project hale by halestudio.

the class ValueListTypeTest method testValueListValueListJsonGroovy.

/**
 * Test if a nested list is the same when converted to JSON and back again.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testValueListValueListJsonGroovy() throws Exception {
    ValueList values1 = new ValueList();
    values1.add(Value.of(1));
    values1.add(Value.of(2));
    ValueList values2 = new ValueList();
    values2.add(Value.of("a"));
    values2.add(Value.of("b"));
    values2.add(Value.of("c"));
    ValueList values = new ValueList();
    values.add(new ComplexValue(values1));
    values.add(new ComplexValue(values2));
    // converter
    ValueListType vlt = new ValueListType();
    // convert to Json
    Object json = vlt.toJson(values);
    System.out.println(new JsonBuilder(json).toString());
    // convert back
    ValueList conv = vlt.fromJson(json, null);
    assertEquals("List size does not match", 2, conv.size());
    assertEquals(values, conv);
}
Also used : JsonBuilder(groovy.json.JsonBuilder) ValueList(eu.esdihumboldt.hale.common.core.io.ValueList) Test(org.junit.Test)

Example 12 with ValueList

use of eu.esdihumboldt.hale.common.core.io.ValueList in project hale by halestudio.

the class ValueListTypeTest method testRead.

/**
 * Test reading a value list from XML.
 */
@SuppressWarnings("javadoc")
@Test
public void testRead() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    ByteArrayInputStream inputStream = new ByteArrayInputStream(XML_SAMPLE.getBytes());
    Element root = builder.parse(inputStream).getDocumentElement();
    // read
    ValueList conv = HaleIO.getComplexValue(root, ValueList.class, null);
    assertEquals(3, conv.size());
    assertEquals("foo", conv.get(0).as(String.class));
    assertEquals("bar", conv.get(1).as(String.class));
    assertEquals("hello", conv.get(2).as(ValueList.class).get(0).as(String.class));
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ValueList(eu.esdihumboldt.hale.common.core.io.ValueList) Element(org.w3c.dom.Element) Test(org.junit.Test)

Example 13 with ValueList

use of eu.esdihumboldt.hale.common.core.io.ValueList in project hale by halestudio.

the class ValueListTypeTest method testValueListValueList.

/**
 * Test if a nested list is the same when converted to DOM and back again.
 */
@Test
public void testValueListValueList() {
    ValueList values1 = new ValueList();
    values1.add(Value.of(1));
    values1.add(Value.of(2));
    ValueList values2 = new ValueList();
    values2.add(Value.of("a"));
    values2.add(Value.of("b"));
    values2.add(Value.of("c"));
    ValueList values = new ValueList();
    values.add(new ComplexValue(values1));
    values.add(new ComplexValue(values2));
    // convert to DOM
    Element fragment = HaleIO.getComplexElement(values);
    // convert back
    ValueList conv = HaleIO.getComplexValue(fragment, ValueList.class, null);
    assertEquals("List size does not match", 2, conv.size());
    assertEquals(values, conv);
}
Also used : ValueList(eu.esdihumboldt.hale.common.core.io.ValueList) Element(org.w3c.dom.Element) Test(org.junit.Test)

Example 14 with ValueList

use of eu.esdihumboldt.hale.common.core.io.ValueList in project hale by halestudio.

the class ValueListTypeTest method testValueListValueListJson.

/**
 * Test if a nested list is the same when converted to JSON and back again.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testValueListValueListJson() throws Exception {
    ValueList values1 = new ValueList();
    values1.add(Value.of(1));
    values1.add(Value.of(2));
    ValueList values2 = new ValueList();
    values2.add(Value.of("a"));
    values2.add(Value.of("b"));
    values2.add(Value.of("c"));
    ValueList values = new ValueList();
    values.add(new ComplexValue(values1));
    values.add(new ComplexValue(values2));
    // converter
    ValueListType vlt = new ValueListType();
    // convert to Json
    StringWriter writer = new StringWriter();
    vlt.toJson(values, writer);
    System.out.println(writer.toString());
    // convert back
    ValueList conv = vlt.fromJson(new StringReader(writer.toString()), null);
    assertEquals("List size does not match", 2, conv.size());
    assertEquals(values, conv);
}
Also used : StringWriter(java.io.StringWriter) ValueList(eu.esdihumboldt.hale.common.core.io.ValueList) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 15 with ValueList

use of eu.esdihumboldt.hale.common.core.io.ValueList in project hale by halestudio.

the class ValueListTypeTest method testStringValueList.

/**
 * Test if a simple list containing only {@link StringValue}s is the same
 * when converted to DOM and back again.
 */
@Test
public void testStringValueList() {
    ValueList values = new ValueList();
    values.add(Value.of(1));
    values.add(Value.of(2));
    values.add(Value.of(3));
    values.add(Value.of(4));
    values.add(Value.of(5));
    // convert to DOM
    Element fragment = HaleIO.getComplexElement(values);
    // convert back
    ValueList conv = HaleIO.getComplexValue(fragment, ValueList.class, null);
    assertEquals("List size does not match", values.size(), conv.size());
    for (int i = 0; i < values.size(); i++) {
        assertEquals(values.get(i), conv.get(i));
    }
}
Also used : ValueList(eu.esdihumboldt.hale.common.core.io.ValueList) Element(org.w3c.dom.Element) Test(org.junit.Test)

Aggregations

ValueList (eu.esdihumboldt.hale.common.core.io.ValueList)24 Value (eu.esdihumboldt.hale.common.core.io.Value)12 ValueProperties (eu.esdihumboldt.hale.common.core.io.ValueProperties)8 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 Element (org.w3c.dom.Element)4 QName (javax.xml.namespace.QName)3 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)2 CustomTypeContentAssociation (eu.esdihumboldt.hale.io.xsd.anytype.CustomTypeContentAssociation)2 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)2 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2 Function (com.google.common.base.Function)1 Definition (eu.esdihumboldt.hale.common.schema.model.Definition)1 ClassResolver (eu.esdihumboldt.hale.common.schema.model.constraint.factory.ClassResolver)1 TypeReferenceBuilder (eu.esdihumboldt.hale.common.schema.model.constraint.factory.TypeReferenceBuilder)1 TypeResolver (eu.esdihumboldt.hale.common.schema.model.constraint.factory.TypeResolver)1 ValueConstraintFactory (eu.esdihumboldt.hale.common.schema.model.constraint.factory.ValueConstraintFactory)1 CodeListAssociation (eu.esdihumboldt.hale.common.schema.model.constraint.property.CodeListAssociation)1 Reference (eu.esdihumboldt.hale.common.schema.model.constraint.property.Reference)1