Created new DataTypeTest unit test. Moved some tests there
This commit is contained in:
parent
b82775ec18
commit
374bb9cc7b
@ -37,7 +37,6 @@
|
||||
package org.jooq.test;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static org.jooq.JoinType.LEFT_OUTER_JOIN;
|
||||
import static org.jooq.impl.Factory.any;
|
||||
import static org.jooq.impl.Factory.avg;
|
||||
@ -123,9 +122,6 @@ import org.jooq.impl.CustomField;
|
||||
import org.jooq.impl.Factory;
|
||||
import org.jooq.test.data.Table1Record;
|
||||
import org.jooq.test.data.TestDataType;
|
||||
import org.jooq.types.DayToSecond;
|
||||
import org.jooq.types.Interval;
|
||||
import org.jooq.types.YearToMonth;
|
||||
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
@ -2544,54 +2540,4 @@ public class BasicTest extends AbstractTest {
|
||||
assertEquals("select :1 from \"TABLE1\" where \"TABLE1\".\"ID1\" = :2", r_refP.render(q));
|
||||
assertEquals("select ? from \"TABLE1\" where \"TABLE1\".\"ID1\" = ?", r_ref.render(q));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testYearToMonth() {
|
||||
for (int i = 0; i <= 5; i++) {
|
||||
intervalChecks(i * 12, new YearToMonth(i));
|
||||
intervalChecks(i * -12, new YearToMonth(i).neg());
|
||||
intervalChecks(i, new YearToMonth(0, i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDayToSecond() {
|
||||
for (double i = -1394892834972.0; i <= 23487289374987.0; i += 283749827.3839293) {
|
||||
intervalChecks(i, DayToSecond.valueOf(i));
|
||||
}
|
||||
|
||||
for (int i = 0; i <= 5; i++) {
|
||||
intervalChecks(i * 1000 * 86400.0, new DayToSecond(i));
|
||||
intervalChecks(i * 1000 * 3600.0, new DayToSecond(0, i));
|
||||
intervalChecks(i * 1000 * 60.0, new DayToSecond(0, 0, i));
|
||||
intervalChecks(i * 1000, new DayToSecond(0, 0, 0, i));
|
||||
intervalChecks(i / 1000000.0, new DayToSecond(0, 0, 0, 0, i));
|
||||
}
|
||||
}
|
||||
|
||||
private <I extends Number & Interval> void intervalChecks(Number expected, I interval) {
|
||||
// Allow some floating point arithmetic inaccuracy
|
||||
assertTrue(Math.abs(Double.doubleToLongBits(expected.doubleValue()) - Double.doubleToLongBits(interval.doubleValue())) < 50);
|
||||
assertTrue(Math.abs(Float.floatToIntBits(expected.floatValue()) - Float.floatToIntBits(interval.floatValue())) < 5);
|
||||
|
||||
assertEquals(expected.byteValue(), interval.byteValue());
|
||||
assertEquals(expected.shortValue(), interval.shortValue());
|
||||
assertEquals(expected.intValue(), interval.intValue());
|
||||
assertEquals(expected.longValue(), interval.longValue());
|
||||
|
||||
if (interval instanceof YearToMonth) {
|
||||
YearToMonth y = YearToMonth.valueOf(interval.toString());
|
||||
assertEquals(interval, y);
|
||||
}
|
||||
else {
|
||||
DayToSecond m = DayToSecond.valueOf(interval.toString());
|
||||
assertEquals(interval, m);
|
||||
assertEquals(m.getDays(),
|
||||
m.getSign() * (int) m.getTotalDays());
|
||||
assertEquals(m.getDays() * 24 + m.getHours(),
|
||||
m.getSign() * (int) m.getTotalHours());
|
||||
assertEquals(m.getDays() * 24 * 60 + m.getHours() * 60 + m.getMinutes(),
|
||||
m.getSign() * (int) m.getTotalMinutes());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
106
jOOQ/src/test/java/org/jooq/test/DataTypeTest.java
Normal file
106
jOOQ/src/test/java/org/jooq/test/DataTypeTest.java
Normal file
@ -0,0 +1,106 @@
|
||||
/**
|
||||
* Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed to you under the Apache License, Version 2.0
|
||||
* (the "License"); You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* . Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* . Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* . Neither the name "jOOQ" nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.jooq.test;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
|
||||
import org.jooq.types.DayToSecond;
|
||||
import org.jooq.types.Interval;
|
||||
import org.jooq.types.YearToMonth;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
/**
|
||||
* A test suite for jOOQ functionality related to data types
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public class DataTypeTest extends AbstractTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void testYearToMonth() {
|
||||
for (int i = 0; i <= 5; i++) {
|
||||
intervalChecks(i * 12, new YearToMonth(i));
|
||||
intervalChecks(i * -12, new YearToMonth(i).neg());
|
||||
intervalChecks(i, new YearToMonth(0, i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDayToSecond() {
|
||||
for (double i = -1394892834972.0; i <= 23487289374987.0; i += 283749827.3839293) {
|
||||
intervalChecks(i, DayToSecond.valueOf(i));
|
||||
}
|
||||
|
||||
for (int i = 0; i <= 5; i++) {
|
||||
intervalChecks(i * 1000 * 86400.0, new DayToSecond(i));
|
||||
intervalChecks(i * 1000 * 3600.0, new DayToSecond(0, i));
|
||||
intervalChecks(i * 1000 * 60.0, new DayToSecond(0, 0, i));
|
||||
intervalChecks(i * 1000, new DayToSecond(0, 0, 0, i));
|
||||
intervalChecks(i / 1000000.0, new DayToSecond(0, 0, 0, 0, i));
|
||||
}
|
||||
}
|
||||
|
||||
private <I extends Number & Interval> void intervalChecks(Number expected, I interval) {
|
||||
// Allow some floating point arithmetic inaccuracy
|
||||
assertTrue(Math.abs(Double.doubleToLongBits(expected.doubleValue()) - Double.doubleToLongBits(interval.doubleValue())) < 50);
|
||||
assertTrue(Math.abs(Float.floatToIntBits(expected.floatValue()) - Float.floatToIntBits(interval.floatValue())) < 5);
|
||||
|
||||
assertEquals(expected.byteValue(), interval.byteValue());
|
||||
assertEquals(expected.shortValue(), interval.shortValue());
|
||||
assertEquals(expected.intValue(), interval.intValue());
|
||||
assertEquals(expected.longValue(), interval.longValue());
|
||||
|
||||
if (interval instanceof YearToMonth) {
|
||||
YearToMonth y = YearToMonth.valueOf(interval.toString());
|
||||
assertEquals(interval, y);
|
||||
}
|
||||
else {
|
||||
DayToSecond m = DayToSecond.valueOf(interval.toString());
|
||||
assertEquals(interval, m);
|
||||
assertEquals(m.getDays(),
|
||||
m.getSign() * (int) m.getTotalDays());
|
||||
assertEquals(m.getDays() * 24 + m.getHours(),
|
||||
m.getSign() * (int) m.getTotalHours());
|
||||
assertEquals(m.getDays() * 24 * 60 + m.getHours() * 60 + m.getMinutes(),
|
||||
m.getSign() * (int) m.getTotalMinutes());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user