Java tutorial
/* * Copyright 2014 the original author or authors. * * 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.carlomicieli.jtrains.value.objects; import com.carlomicieli.jtrains.test.AbstractJsonSerializationJUnit4Tests; import com.fasterxml.jackson.databind.JavaType; import com.jayway.jsonpath.JsonModel; import org.junit.Test; import java.io.IOException; import java.util.Locale; import static com.carlomicieli.jtrains.test.assertj.CustomAssertions.assertThat; /** * @author Carlo Micieli */ public class LocalizedFieldSerializationTests extends AbstractJsonSerializationJUnit4Tests { @Test public void shouldSerializeBrands() throws IOException { JsonModel json = toJson(LocalizedField.of("Hello")); assertThat(json).isNotNull().containsPair("en", "Hello"); } @Test public void shouldDeserializeLocalizedFieldsContainStrings() throws IOException { LocalizedField<String> field = parseLocalizedField("{\"en\": \"Hello\", \"it\": \"Ciao\"}", String.class); assertThat(field).hasSize(2).containsEntry(Locale.ENGLISH, "Hello").containsEntry(Locale.ITALIAN, "Ciao"); } @Test public void shouldDeserializeLocalizedFieldsContainNumbers() throws IOException { LocalizedField<Integer> field = parseLocalizedField("{\"en\": 24, \"it\": 42}", Integer.class); assertThat(field).hasSize(2).containsEntry(Locale.ENGLISH, 24).containsEntry(Locale.ITALIAN, 42); } public <T> LocalizedField<T> parseLocalizedField(String json, Class<T> subtype) throws IOException { JavaType type = objectMapper.getTypeFactory().constructParametricType(LocalizedField.class, subtype); return objectMapper.readValue(json, type); } }