com.basistech.rosette.dm.json.plain.NameTest.java Source code

Java tutorial

Introduction

Here is the source code for com.basistech.rosette.dm.json.plain.NameTest.java

Source

/*
* Copyright 2014 Basis Technology Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.basistech.rosette.dm.json.plain;

import com.basistech.rosette.dm.Name;
import com.basistech.util.ISO15924;
import com.basistech.util.LanguageCode;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.List;

/**
 * Test for serialization etc. on {@link com.basistech.rosette.dm.Name}.
 */
public class NameTest extends AdmAssert {
    @Test
    public void name() throws Exception {
        List<Name> names = Lists.newArrayList();
        Name.Builder builder = new Name.Builder("Fred");
        names.add(builder.build());
        builder = new Name.Builder("George");
        builder.languageOfOrigin(LanguageCode.ENGLISH).script(ISO15924.Latn).languageOfUse(LanguageCode.FRENCH);
        names.add(builder.build());
        ObjectMapper mapper = objectMapper();
        String json = mapper.writeValueAsString(names);
        // one way to inspect the works is to read it back in _without_ our customized mapper.
        ObjectMapper plainMapper = new ObjectMapper();
        JsonNode tree = plainMapper.readTree(json);
        assertTrue(tree.isArray());
        assertEquals(2, tree.size());
        JsonNode node = tree.get(0);
        assertTrue(node.has("text"));
        assertEquals("Fred", node.get("text").asText());
        assertFalse(node.has("script"));
        assertFalse(node.has("languageOfOrigin"));
        assertFalse(node.has("languageOfUse"));

        List<Name> readBack = mapper.readValue(json, new TypeReference<List<Name>>() {
        });
        assertEquals(names, readBack);
    }
}