au.id.hazelwood.xmltvguidebuilder.utils.DateTimeUtilsUnitTest.java Source code

Java tutorial

Introduction

Here is the source code for au.id.hazelwood.xmltvguidebuilder.utils.DateTimeUtilsUnitTest.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 au.id.hazelwood.xmltvguidebuilder.utils;

import au.id.hazelwood.xmltvguidebuilder.framework.XmltvGuideBuilderTestWatchman;

import org.joda.time.DateTime;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Constructor;
import java.util.Calendar;
import java.util.GregorianCalendar;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

public class DateTimeUtilsUnitTest {
    @Rule
    public final MethodRule watchman = new XmltvGuideBuilderTestWatchman();

    @Test
    public void testConstructor() throws Exception {
        Constructor<DateTimeUtils> constructor = DateTimeUtils.class.getDeclaredConstructor();
        assertFalse(constructor.isAccessible());
        ReflectionUtils.makeAccessible(constructor);
        assertNotNull(constructor.newInstance());
    }

    @Test
    public void testToISODateTime() throws Exception {
        DateTime dateTime = new DateTime(2010, 1, 8, 9, 20, 0, 0);
        assertEquals("2010-01-08T09:20", DateTimeUtils.toISODateTime(dateTime));
    }

    @Test
    public void testToDateTime() throws Exception {
        Calendar calendar = new GregorianCalendar(2010, 0, 8, 9, 20, 0);
        assertEquals(new DateTime(2010, 1, 8, 9, 20, 0, 0).getMillis(),
                DateTimeUtils.toDateTime(calendar).getMillis());
    }

    @Test
    public void testFormatDurationWords() throws Exception {
        assertEquals("30 seconds", DateTimeUtils.formatDurationWords(30 * 1000));
        assertEquals("5 minutes 30 seconds", DateTimeUtils.formatDurationWords(330 * 1000));
        assertEquals("16 hours 40 minutes 30 seconds", DateTimeUtils.formatDurationWords(60030 * 1000));
    }

    @Test
    public void testFormatDurationDHM() throws Exception {
        assertEquals("00d00h01m", DateTimeUtils.formatDurationDHM(90 * 1000));
        assertEquals("00d00h05m", DateTimeUtils.formatDurationDHM(300 * 1000));
        assertEquals("00d16h40m", DateTimeUtils.formatDurationDHM(60000 * 1000));
    }
}