net.sourceforge.jaulp.lang.ObjectUtilsTest.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.jaulp.lang.ObjectUtilsTest.java

Source

/**
 * Copyright (C) 2007 Asterios Raptis
 *
 * 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 net.sourceforge.jaulp.lang;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sourceforge.jaulp.date.CreateDateUtils;
import net.sourceforge.jaulp.io.ChangedAttributeResult;

import org.apache.commons.beanutils.BeanComparator;
import org.jaulp.test.objects.A;
import org.jaulp.test.objects.Gender;
import org.jaulp.test.objects.Person;
import org.testng.AssertJUnit;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import de.alpharogroup.date.DateDecorator;
import de.alpharogroup.date.SqlTimestampDecorator;

/**
 * The Class ObjectUtilsTest.
 */
public class ObjectUtilsTest {

    /**
     * Sets the up.
     *
     * @throws Exception
     *             the exception
     */
    @BeforeMethod
    public void setUp() throws Exception {
    }

    /**
     * Tear down.
     *
     * @throws Exception
     *             the exception
     */
    @AfterMethod
    public void tearDown() throws Exception {
    }

    /**
     * Test clone object.
     */
    @Test(enabled = false)
    public void testCloneObject() {
        Date past = CreateDateUtils.newDate(2009, 3, 26, 10, 37, 04);
        Object otherCopy = ObjectUtils.cloneObjectQuietly(past);

        boolean result = past.equals(otherCopy);
        AssertJUnit.assertTrue("Cloned object should be equal with the source object.", result);

        String aString = "Hy there...";

        otherCopy = ObjectUtils.cloneObjectQuietly(aString);

        result = aString.equals(otherCopy);
        AssertJUnit.assertTrue("Cloned object should be equal with the source object.", result);

        A a = new A();
        a.setA("a");

        Object anotherCopy = ObjectUtils.cloneObjectQuietly(a);

        result = a.equals(anotherCopy);
        AssertJUnit.assertTrue("Cloned object should be equal with the source object.", result);
    }

    /**
     * Test generic clone method.
     */
    @Test(enabled = false)
    public void testClone() {
        Date past = CreateDateUtils.newDate(2009, 3, 26, 10, 37, 04);
        Date otherCopy = ObjectUtils.clone(past);

        boolean result = past.equals(otherCopy);
        AssertJUnit.assertTrue("Cloned object should be equal with the source object.", result);

        String aString = "Hy there...";

        String clonedString = ObjectUtils.clone(aString);

        result = aString.equals(clonedString);
        AssertJUnit.assertTrue("Cloned object should be equal with the source object.", result);

        A a = new A();
        a.setA("a");

        A anotherCopy = ObjectUtils.clone(a);

        result = a.equals(anotherCopy);
        AssertJUnit.assertTrue("Cloned object should be equal with the source object.", result);
    }

    /**
     * Test compare.
     *
     * @throws IllegalAccessException
     *             the illegal access exception
     * @throws InvocationTargetException
     *             the invocation target exception
     * @throws NoSuchMethodException
     *             the no such method exception
     */
    @Test(enabled = false)
    public void testCompare() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        Person sourceOjbect = new Person();
        sourceOjbect.setGender(Gender.MALE);
        sourceOjbect.setName("obelix");

        Person objectToCompare = (Person) ObjectUtils.cloneObjectQuietly(sourceOjbect);

        boolean result = ObjectUtils.compare(sourceOjbect, objectToCompare);
        AssertJUnit.assertTrue("Cloned object should be equal with the source object.", result);

        objectToCompare.setGender(Gender.FEMALE);
        result = ObjectUtils.compare(sourceOjbect, objectToCompare);
        AssertJUnit.assertFalse(
                "Object to compare should be not equal with the source object because it has changed.", result);
    }

    /**
     * Test get changed data.
     *
     * @throws IllegalAccessException
     *             the illegal access exception
     * @throws InvocationTargetException
     *             the invocation target exception
     * @throws NoSuchMethodException
     *             the no such method exception
     */
    @Test(enabled = false)
    public void testGetChangedData()
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        Person sourceOjbect = new Person();
        sourceOjbect.setGender(Gender.MALE);
        sourceOjbect.setName("obelix");

        Person objectToCompare = (Person) ObjectUtils.cloneObjectQuietly(sourceOjbect);

        Map<Object, ChangedAttributeResult> result = ObjectUtils.getChangedDataMap(sourceOjbect, objectToCompare);
        AssertJUnit.assertTrue("Size should be 0 but is " + result.size(), result.size() == 0);
        // Change the gender from the objectToCompare...
        objectToCompare.setGender(Gender.FEMALE);
        // and get the changed data...
        result = ObjectUtils.getChangedDataMap(sourceOjbect, objectToCompare);
        AssertJUnit.assertFalse("Size should be 1 but is " + result.size(), result.size() == 0);
        AssertJUnit.assertTrue("", result.containsKey("gender"));
        ChangedAttributeResult changed = result.get("gender");
        Object sourceAttribute = changed.getSourceAttribute();
        Object changedAttribute = changed.getChangedAttribute();
        AssertJUnit.assertTrue("", sourceAttribute.equals(Gender.MALE.name()));
        AssertJUnit.assertTrue("", changedAttribute.equals(Gender.FEMALE.name()));
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Test(enabled = false)
    public void testCompareTo() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        List<Person> persons = new ArrayList<>();
        Person obelix = new Person();
        obelix.setGender(Gender.MALE);
        obelix.setName("obelix");

        Person asterix = new Person();
        asterix.setGender(Gender.MALE);
        asterix.setName("asterix");

        Person miraculix = new Person();
        miraculix.setGender(Gender.MALE);
        miraculix.setName("miraculix");

        int i = ObjectUtils.compareTo(asterix, obelix, "name");

        System.out.println(i);

        persons.add(obelix);
        persons.add(asterix);
        persons.add(miraculix);
        System.out.println("Unsorted Persons:");
        System.out.println(persons.toString());
        Comparator defaultComparator = new BeanComparator("name");
        Collections.sort(persons, defaultComparator);
        System.out.println("Sorted Persons by name:");
        System.out.println(persons.toString());
        Collections.reverse(persons);
        System.out.println("Sorted Persons by name reversed:");
        System.out.println(persons.toString());
    }

    @Test(enabled = true)
    public void testCopy() throws IllegalAccessException, InvocationTargetException {
        DateDecorator dateDecorator = new DateDecorator();
        Date now = CreateDateUtils.now();
        dateDecorator.setDate(now);

        SqlTimestampDecorator timestampDecorator = new SqlTimestampDecorator();

        ObjectUtils.copy(dateDecorator, timestampDecorator);
        System.out.println(timestampDecorator);

    }

}