/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package com.xpn.xwiki.objects.classes;
import java.util.Random;
import org.junit.Assert;
import org.junit.Test;
/**
* Unit tests for the base {@link PropertyClass} class.
*
* @version $Id: 29f283a4a3602e1290a7ee62ba42ccf039132771 $
* @since 2.4M2
*/
public class PropertyClassTest
{
/** Test the {@link PropertyClass#compareTo(PropertyClass)} method. */
@Test
public void testCompareTo()
{
PropertyClass one = new PropertyClass();
PropertyClass two = new PropertyClass();
// Random numbers to be used as property indexes.
int n1, n2;
one.setName("first");
two.setName("second");
// Since the test might randomly succeed, run it several times to be safer.
Random random = new Random();
for (int i = 0; i < 20; ++i) {
n1 = random.nextInt();
n2 = random.nextInt();
one.setNumber(n1);
two.setNumber(n2);
if (n1 == n2) {
Assert.assertEquals(Math.signum(one.compareTo(two)), -1.0, 0);
Assert.assertEquals(Math.signum(two.compareTo(one)), 1.0, 0);
} else {
Assert.assertEquals(0, Float.compare(Math.signum(one.compareTo(two)), Math.signum(n1 - n2)));
Assert.assertEquals(0, Float.compare(Math.signum(two.compareTo(one)), Math.signum(n2 - n1)));
}
}
// Also test that the comparison takes into account the name in case the two numbers are identical
one.setNumber(42);
two.setNumber(42);
Assert.assertEquals(Math.signum(one.compareTo(two)), -1.0, 0);
Assert.assertEquals(Math.signum(two.compareTo(one)), 1.0, 0);
}
}