Search in sources :

Example 6 with TypeReference

use of org.osgi.util.converter.TypeReference in project felix by apache.

the class SchemaTest method testNullValues.

@Test
public void testNullValues() {
    Schema s = schematizer.schematize("MyDTO", new TypeReference<MyDTO3<MyEmbeddedDTO2<String>>>() {
    }).get("MyDTO");
    assertNotNull(s);
    MyEmbeddedDTO2<String> embedded1 = new MyEmbeddedDTO2<>();
    MyEmbeddedDTO2<String> embedded2 = new MyEmbeddedDTO2<>();
    MyEmbeddedDTO2<String> embedded3 = new MyEmbeddedDTO2<>();
    MyDTO3<MyEmbeddedDTO2<String>> dto = new MyDTO3<>();
    dto.ping = "lalala";
    dto.pong = Long.MIN_VALUE;
    dto.count = MyDTO3.Count.ONE;
    dto.embedded = new ArrayList<>();
    dto.embedded.add(embedded1);
    dto.embedded.add(embedded2);
    dto.embedded.add(embedded3);
    assertEquals("lalala", s.valuesAt("/ping", dto).iterator().next());
    assertEquals(Long.MIN_VALUE, s.valuesAt("/pong", dto).iterator().next());
    assertEquals(MyDTO3.Count.ONE, s.valuesAt("/count", dto).iterator().next());
    assertNotNull(s.valuesAt("/embedded", dto));
    assertListEquals(Arrays.asList(new String[] { null, null, null }), s.valuesAt("/embedded/value", dto));
}
Also used : Schema(org.apache.felix.schematizer.Schema) TypeReference(org.osgi.util.converter.TypeReference) Test(org.junit.Test)

Example 7 with TypeReference

use of org.osgi.util.converter.TypeReference in project felix by apache.

the class SchemaTest method testValues.

@Test
public void testValues() {
    final Schema s = schematizer.schematize("MyDTO", new TypeReference<MyDTO3<MyEmbeddedDTO2<String>>>() {
    }).get("MyDTO");
    assertNotNull(s);
    MyEmbeddedDTO2<String> embedded1 = new MyEmbeddedDTO2<>();
    embedded1.value = "value1";
    MyEmbeddedDTO2<String> embedded2 = new MyEmbeddedDTO2<>();
    embedded2.value = "value2";
    MyEmbeddedDTO2<String> embedded3 = new MyEmbeddedDTO2<>();
    embedded3.value = "value3";
    MyDTO3<MyEmbeddedDTO2<String>> dto = new MyDTO3<>();
    dto.ping = "lalala";
    dto.pong = Long.MIN_VALUE;
    dto.count = MyDTO3.Count.ONE;
    dto.embedded = new ArrayList<>();
    dto.embedded.add(embedded1);
    dto.embedded.add(embedded2);
    dto.embedded.add(embedded3);
    assertEquals("lalala", s.valuesAt("/ping", dto).iterator().next());
    assertEquals(Long.MIN_VALUE, s.valuesAt("/pong", dto).iterator().next());
    assertEquals(MyDTO3.Count.ONE, s.valuesAt("/count", dto).iterator().next());
    assertNotNull(s.valuesAt("/embedded", dto));
    Object embeddedList = s.valuesAt("/embedded", dto).iterator().next();
    assertNotNull(embeddedList);
    assertTrue(embeddedList instanceof List);
    assertFalse(((List<?>) embeddedList).isEmpty());
    Object embeddedObject = ((List<?>) embeddedList).get(0);
    assertTrue(embeddedObject instanceof MyEmbeddedDTO2);
    assertListEquals(Arrays.asList(new String[] { "value1", "value2", "value3" }), s.valuesAt("/embedded/value", dto));
}
Also used : Schema(org.apache.felix.schematizer.Schema) List(java.util.List) ArrayList(java.util.ArrayList) TypeReference(org.osgi.util.converter.TypeReference) Test(org.junit.Test)

Example 8 with TypeReference

use of org.osgi.util.converter.TypeReference in project felix by apache.

the class SchemaTest method testEmbeddedValues.

@Test
public void testEmbeddedValues() {
    Schema s = schematizer.schematize("MyDTO", new TypeReference<MyDTO>() {
    }).get("MyDTO");
    assertNotNull(s);
    MyEmbeddedDTO embedded = new MyEmbeddedDTO();
    embedded.alpha = Alpha.A;
    embedded.marco = "mmmm";
    embedded.polo = 66;
    MyDTO dto = new MyDTO();
    dto.ping = "lalala";
    dto.pong = Long.MIN_VALUE;
    dto.count = MyDTO.Count.ONE;
    dto.embedded = embedded;
    assertEquals("lalala", s.valuesAt("/ping", dto).iterator().next());
    assertEquals(Long.MIN_VALUE, s.valuesAt("/pong", dto).iterator().next());
    assertEquals(MyDTO.Count.ONE, s.valuesAt("/count", dto).iterator().next());
    assertNotNull(s.valuesAt("/embedded", dto));
    Object embeddedObject = s.valuesAt("/embedded", dto).iterator().next();
    assertTrue(embeddedObject instanceof MyEmbeddedDTO);
    assertEquals(Alpha.A, s.valuesAt("/embedded/alpha", dto).iterator().next());
    assertEquals("mmmm", s.valuesAt("/embedded/marco", dto).iterator().next());
    assertEquals(66L, s.valuesAt("/embedded/polo", dto).iterator().next());
}
Also used : Schema(org.apache.felix.schematizer.Schema) TypeReference(org.osgi.util.converter.TypeReference) Test(org.junit.Test)

Example 9 with TypeReference

use of org.osgi.util.converter.TypeReference in project felix by apache.

the class SchematizerServiceTest method testSchematizeToMap.

@Test
public void testSchematizeToMap() {
    Schema s = schematizer.schematize("MyDTO", new TypeReference<MyDTO>() {
    }).get("MyDTO");
    Map<String, Node.DTO> map = s.toMap();
    testMapValues(map);
}
Also used : Schema(org.apache.felix.schematizer.Schema) TypeReference(org.osgi.util.converter.TypeReference) Test(org.junit.Test)

Example 10 with TypeReference

use of org.osgi.util.converter.TypeReference in project felix by apache.

the class SchematizerServiceTest method testVisitor.

@Test
public void testVisitor() {
    Schema s = schematizer.schematize("MyDTO", new TypeReference<MyDTO>() {
    }).get("MyDTO");
    StringBuilder sb = new StringBuilder();
    s.visit(n -> sb.append("::").append(n.name()));
    assertEquals("::::count::embedded::alpha::marco::polo::ping::pong", sb.toString());
}
Also used : Schema(org.apache.felix.schematizer.Schema) TypeReference(org.osgi.util.converter.TypeReference) Test(org.junit.Test)

Aggregations

TypeReference (org.osgi.util.converter.TypeReference)15 Test (org.junit.Test)13 Schema (org.apache.felix.schematizer.Schema)11 Node (org.apache.felix.schematizer.Node)6 ArrayList (java.util.ArrayList)2 List (java.util.List)2 SchematizerImpl (org.apache.felix.schematizer.impl.SchematizerImpl)2 Converter (org.osgi.util.converter.Converter)2 File (java.io.File)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Ignore (org.junit.Ignore)1