com.boxedfolder.domain.util.DateTimeSerializerTests.java Source code

Java tutorial

Introduction

Here is the source code for com.boxedfolder.domain.util.DateTimeSerializerTests.java

Source

/*
 * Carrot - beacon content management
 * Copyright (C) 2016 Heiko Dreyer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.boxedfolder.domain.util;

import com.boxedfolder.carrot.domain.util.DateTimeSerializer;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import java.io.StringWriter;
import java.io.Writer;

import static junit.framework.Assert.assertEquals;

/**
 * @author Heiko Dreyer (heiko@boxedfolder.com)
 */
@RunWith(MockitoJUnitRunner.class)
public class DateTimeSerializerTests {
    private ObjectMapper mapper;
    private DateTimeSerializer serializer;
    private DateTime dateTime;

    @Before
    public void setUp() {
        mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        serializer = new DateTimeSerializer();
        dateTime = new DateTime(1437550756302l); // Timestamp
    }

    @Test
    public void testSerializer() throws Exception {
        Writer writer = new StringWriter();
        JsonGenerator generator = new JsonFactory().createGenerator(writer);
        serializer.serialize(dateTime, generator, mapper.getSerializerProvider());
        generator.close();
        assertEquals("\"2015-07-22T07:39:16.302Z\"", writer.toString()); // Output
    }
}