Package de.timefinder.data.util

Source Code of de.timefinder.data.util.HelperTest$MyTester

/*
*  Copyright 2009 Peter Karich, peat_hal ‘at’ users ‘dot’ sourceforge ‘dot’ net.
*
*  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.
*  under the License.
*/
package de.timefinder.data.util;

import de.timefinder.data.Event;
import de.timefinder.data.Person;
import java.io.InputStream;
import java.io.StringWriter;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;
import java.util.logging.Logger;
import org.junit.Test;
import static org.junit.Assert.*;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

/**
*
* @author Peter Karich, peat_hal ‘at’ users ‘dot’ sourceforge ‘dot’ net
*/
public class HelperTest {

    private final Logger log = Logger.getLogger(getClass().getSimpleName());

    @Test
    public void testfromDateTime1() throws ParseException {
        //System.out.println(Helper.toDateTime(new Date()));
        Calendar cal = getExpectedCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 11 + 4 + 2);
        // hour + 4h (because +f -04:00) + 2h (because of GMT)
        Date date = Helper.fromDateTime("2008-05-19T11:00:02-04:00");
        assertEquals(date, cal.getTime());
    }

    @Test
    public void testfromDateTime2() throws ParseException {
        Calendar cal = getExpectedCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 11);

        Date date = Helper.fromDateTime("2008-05-19T09:00:02Z");
        assertEquals(date, cal.getTime());
    }

    @Test
    public void testfromDateTime3() throws ParseException {
        Calendar cal = getExpectedCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 11);

        Date date = Helper.fromDateTime("2008-05-19T11:00:02+0200");
        assertEquals(date, cal.getTime());
    }

    @Test
    public void testfromDateTime4() throws ParseException {
        Calendar cal = getExpectedCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 11);

        Date date = Helper.fromDateTime("2008-05-19T09:00:02");
        assertEquals(date, cal.getTime());
    }

    @Test
    public void testFirstElement() throws Exception {
        String str = "<root>dflg<firstelement/></root>";
        Document doc = Helper.getAsDocument(str);
        Node root = Helper.getFirstElement(doc.getElementsByTagName("root"));
        assertEquals(root.getNodeName(), "root");

        assertEquals(
                Helper.getFirstElement(root.getChildNodes()).getNodeName(),
                "firstelement");
    }

    @Test
    public void testToLocalDateTime() throws ParseException {
        Calendar cal = getExpectedCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 11);

        assertEquals(Helper.toLocalDateTime(cal.getTime()),
                "2008-05-19T11:00:02");
    }

    @Test
    public void testFromLocalDateTime() throws ParseException {
        Calendar cal = getExpectedCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 11);

        // Methode sollte immer das Gleiche zurückliefern unabhängig vom offset.
        Date date = Helper.fromLocalDateTime("2008-05-19T11:00:02+0200");
        assertEquals(date, cal.getTime());

        date = Helper.fromLocalDateTime("2008-05-19T11:00:02-06:00");
        assertEquals(date, cal.getTime());

        date = Helper.fromLocalDateTime("2008-05-19T11:00:02");
        assertEquals(date, cal.getTime());
    }

    private Calendar getExpectedCalendar() {
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin"));
        // Falls man dies haben will:  Calendar cal = Calendar.getInstance();
        // muss man dies aufrufen: cal.set(Calendar.HOUR, 11 - 4);

        cal.set(Calendar.YEAR, 2008);
        cal.set(Calendar.MONTH, 4);
        cal.set(Calendar.DAY_OF_MONTH, 19);
        // Die nächste Zeile wird beim Aufrufer überschrieben
        // cal.set(Calendar.HOUR, 11);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 2);
        cal.set(Calendar.MILLISECOND, 0);
        return cal;
    }

    @Test
    public void testPatternCounter() throws Exception {
        String str = "<root>dflg<firstelement/></root>";
        assertEquals(Helper.countPattern(str, "root"), 2);
        assertEquals(Helper.countPattern(str, "element"), 1);
    }

    public void createJavaStringFromXMLString() throws Exception {
        InputStream is = getClass().getResourceAsStream(
                "HelperTestFormattingFile.xml");
        StringWriter sw = new StringWriter();
        Helper.readStreamIntoWriter(is, sw);
        String str = sw.toString();

        log.info(Helper.createJavaStringFromXMLString(str));
    }

    @Test
    public void testCopyProperties() throws Exception {
        MyTester source = new MyTester("a", "b", "c");
        source.setArr(new String[]{"Arr1", "Arr2"});
        source.setSet(new HashSet<String>());
        source.getSet().add("set1");
        source.getSet().add("set2");
        MyTester dest = new MyTester(null, null, null);

        Helper.copyProperties(source, dest);

        assertEquals("a", dest.getA());
        // no getter
        assertEquals(null, dest.b);
        // no setter
        assertEquals(null, dest.getC());

        // cloned the array?
        source.getArr()[1] = "ARR2";
        assertEquals("ARR2", source.getArr()[1]);
        assertEquals("Arr2", dest.getArr()[1]);

        // cloned the set?
        assertEquals(2, dest.getSet().size());
        source.getSet().add("set3");
        assertEquals(3, source.getSet().size());
        assertEquals(2, dest.getSet().size());
    }

    @Test
    public void testCopyPropertiesOfEvent() throws Exception {
        Event source = new Event(4, 2);
        source.addPerson(new Person(), true);
        Event dest = new Event();
        Helper.copyProperties(source, dest);

        assertEquals(2, source.getDuration());
        assertEquals(2, dest.getDuration());

        assertEquals(4, source.getStart());
        assertEquals(4, dest.getStart());

        assertTrue(source.getPersons() != dest.getPersons());

        source.getPersons().clear();
        assertEquals(0, source.getPersons().size());
        assertEquals(1, dest.getPersons().size());
    }

    class MyTester {

        private String a;
        public String b;
        private String c;
        private String[] arr;
        private Set<String> set;

        public MyTester(String a, String b, String c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }

        public String getA() {
            return a;
        }

        public void setA(String a) {
            this.a = a;
        }

        public void setB(String b) {
            this.b = b;
        }

        public String getC() {
            return c;
        }

        public String[] getArr() {
            return arr;
        }

        public void setArr(String[] arr) {
            this.arr = arr;
        }

        public Set<String> getSet() {
            return set;
        }

        public void setSet(Set<String> set) {
            this.set = set;
        }
    }
}
TOP

Related Classes of de.timefinder.data.util.HelperTest$MyTester

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.