Set forbidden APIs target compatibility to compiler java version (#32935)

Set forbidden apis target compatibility to compiler version

Fix outstanding deprecation
This commit is contained in:
Alpar Torok 2018-08-20 09:27:02 +03:00 committed by GitHub
parent de92d2ef1f
commit 4b34b3f4aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 166 additions and 156 deletions

View File

@ -21,6 +21,7 @@ package org.elasticsearch.gradle.precommit
import de.thetaphi.forbiddenapis.gradle.CheckForbiddenApis
import de.thetaphi.forbiddenapis.gradle.ForbiddenApisPlugin
import org.elasticsearch.gradle.ExportElasticsearchBuildResourcesTask
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.FileCollection
@ -101,6 +102,11 @@ class PrecommitTasks {
signaturesURLs = project.forbiddenApis.signaturesURLs +
[ getClass().getResource('/forbidden/es-server-signatures.txt') ]
}
// forbidden apis doesn't support Java 11, so stop at 10
String targetMajorVersion = (project.compilerJavaVersion.compareTo(JavaVersion.VERSION_1_10) > 0 ?
JavaVersion.VERSION_1_10 :
project.compilerJavaVersion).getMajorVersion()
targetCompatibility = Integer.parseInt(targetMajorVersion) >= 9 ?targetMajorVersion : "1.${targetMajorVersion}"
}
Task forbiddenApis = project.tasks.findByName('forbiddenApis')
forbiddenApis.group = "" // clear group, so this does not show up under verification tasks

View File

@ -53,7 +53,7 @@ public class InitializerTests extends ScriptTestCase {
"Object[] x = new Object[] {y, z, 1 + s, s + 'aaa'}; return x;");
assertEquals(4, objects.length);
assertEquals(new Integer(2), objects[0]);
assertEquals(Integer.valueOf(2), objects[0]);
assertEquals(new ArrayList(), objects[1]);
assertEquals("1aaa", objects[2]);
assertEquals("aaaaaa", objects[3]);
@ -85,7 +85,7 @@ public class InitializerTests extends ScriptTestCase {
list = (List)exec("int y = 2; List z = new ArrayList(); String s = 'aaa'; List x = [y, z, 1 + s, s + 'aaa']; return x;");
assertEquals(4, list.size());
assertEquals(new Integer(2), list.get(0));
assertEquals(Integer.valueOf(2), list.get(0));
assertEquals(new ArrayList(), list.get(1));
assertEquals("1aaa", list.get(2));
assertEquals("aaaaaa", list.get(3));
@ -100,15 +100,15 @@ public class InitializerTests extends ScriptTestCase {
map = (Map)exec("[5 : 7, -1 : 14]");
assertEquals(2, map.size());
assertEquals(new Integer(7), map.get(5));
assertEquals(new Integer(14), map.get(-1));
assertEquals(Integer.valueOf(7), map.get(5));
assertEquals(Integer.valueOf(14), map.get(-1));
map = (Map)exec("int y = 2; int z = 3; Map x = [y*z : y + z, y - z : y, z : z]; return x;");
assertEquals(3, map.size());
assertEquals(new Integer(5), map.get(6));
assertEquals(new Integer(2), map.get(-1));
assertEquals(new Integer(3), map.get(3));
assertEquals(Integer.valueOf(5), map.get(6));
assertEquals(Integer.valueOf(2), map.get(-1));
assertEquals(Integer.valueOf(3), map.get(3));
map = (Map)exec("int y = 2; List z = new ArrayList(); String s = 'aaa';" +
"def x = [y : z, 1 + s : s + 'aaa']; return x;");
@ -139,7 +139,7 @@ public class InitializerTests extends ScriptTestCase {
list3.add(9);
assertEquals(3, map.size());
assertEquals(new Integer(5), map.get(6));
assertEquals(Integer.valueOf(5), map.get(6));
assertEquals(list2, map.get("s"));
assertEquals(list3, map.get(3));
}

View File

@ -16,6 +16,8 @@
package org.elasticsearch.common.inject.matcher;
import org.elasticsearch.common.SuppressForbidden;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@ -327,7 +329,9 @@ public class Matchers {
return "inPackage(" + targetPackage.getName() + ")";
}
@SuppressForbidden(reason = "ClassLoader.getDefinedPackage not available yet")
public Object readResolve() {
// TODO minJava >= 9 : use ClassLoader.getDefinedPackage and remove @SuppressForbidden
return inPackage(Package.getPackage(packageName));
}
}

View File

@ -59,7 +59,7 @@ public class FuzzinessTests extends ESTestCase {
Float floatRep = randomFloat();
Number value = intValue;
if (randomBoolean()) {
value = new Float(floatRep += intValue);
value = Float.valueOf(floatRep += intValue);
}
XContentBuilder json = jsonBuilder().startObject()
.field(Fuzziness.X_FIELD_NAME, randomBoolean() ? value.toString() : value)

View File

@ -337,7 +337,7 @@ public class EsExecutorsTests extends ESTestCase {
final CountDownLatch executed = new CountDownLatch(1);
threadContext.putHeader("foo", "bar");
final Integer one = new Integer(1);
final Integer one = Integer.valueOf(1);
threadContext.putTransient("foo", one);
EsThreadPoolExecutor executor =
EsExecutors.newFixed(getName(), pool, queue, EsExecutors.daemonThreadFactory("dummy"), threadContext);

View File

@ -42,7 +42,7 @@ public class ThreadContextTests extends ESTestCase {
threadContext.putHeader("foo", "bar");
threadContext.putTransient("ctx.foo", 1);
assertEquals("bar", threadContext.getHeader("foo"));
assertEquals(new Integer(1), threadContext.getTransient("ctx.foo"));
assertEquals(Integer.valueOf(1), threadContext.getTransient("ctx.foo"));
assertEquals("1", threadContext.getHeader("default"));
try (ThreadContext.StoredContext ctx = threadContext.stashContext()) {
assertNull(threadContext.getHeader("foo"));
@ -61,7 +61,7 @@ public class ThreadContextTests extends ESTestCase {
threadContext.putHeader("foo", "bar");
threadContext.putTransient("ctx.foo", 1);
assertEquals("bar", threadContext.getHeader("foo"));
assertEquals(new Integer(1), threadContext.getTransient("ctx.foo"));
assertEquals(Integer.valueOf(1), threadContext.getTransient("ctx.foo"));
assertEquals("1", threadContext.getHeader("default"));
HashMap<String, String> toMerge = new HashMap<>();
toMerge.put("foo", "baz");

View File

@ -601,10 +601,10 @@ public class HighlightBuilderTests extends ESTestCase {
value = randomAlphaOfLengthBetween(1, 10);
break;
case 1:
value = new Integer(randomInt(1000));
value = Integer.valueOf(randomInt(1000));
break;
case 2:
value = new Boolean(randomBoolean());
value = Boolean.valueOf(randomBoolean());
break;
}
options.put(randomAlphaOfLengthBetween(1, 10), value);

View File

@ -33,7 +33,7 @@ import static org.junit.Assert.assertTrue;
*/
public class EqualsHashCodeTestUtils {
private static Object[] someObjects = new Object[] { "some string", new Integer(1), new Double(1.0) };
private static Object[] someObjects = new Object[] { "some string", Integer.valueOf(1), Double.valueOf(1.0) };
/**
* A function that makes a copy of its input argument

View File

@ -392,7 +392,7 @@ public class ScrollDataExtractorTests extends ESTestCase {
assertThat(extractor.hasNext(), is(true));
output = extractor.next();
assertThat(output.isPresent(), is(true));
assertEquals(new Long(1400L), extractor.getLastTimestamp());
assertEquals(Long.valueOf(1400L), extractor.getLastTimestamp());
// A second failure is not tolerated
assertThat(extractor.hasNext(), is(true));
expectThrows(SearchPhaseExecutionException.class, extractor::next);

View File

@ -125,7 +125,7 @@ public class JobResultsProviderIT extends MlSingleNodeTestCase {
Long matchedCount = queryResult.stream().filter(
c -> c.getId().equals("foo calendar") || c.getId().equals("foo bar calendar") || c.getId().equals("cat foo calendar"))
.count();
assertEquals(new Long(3), matchedCount);
assertEquals(Long.valueOf(3), matchedCount);
queryResult = getCalendars("bar");
assertThat(queryResult, hasSize(1));

View File

@ -58,7 +58,7 @@ final class TypeConverter {
private static final long DAY_IN_MILLIS = 60 * 60 * 24;
private static final Map<Class<?>, JDBCType> javaToJDBC;
static {
Map<Class<?>, JDBCType> aMap = Arrays.stream(DataType.values())
.filter(dataType -> dataType.javaClass() != null
@ -119,7 +119,7 @@ final class TypeConverter {
c.setTimeInMillis(initial);
}
}
static long convertFromCalendarToUTC(long value, Calendar cal) {
if (cal == null) {
return value;
@ -142,7 +142,7 @@ final class TypeConverter {
if (type == null) {
return (T) convert(val, columnType);
}
if (type.isInstance(val)) {
try {
return type.cast(val);
@ -150,7 +150,7 @@ final class TypeConverter {
throw new SQLDataException("Unable to convert " + val.getClass().getName() + " to " + columnType, cce);
}
}
if (type == String.class) {
return (T) asString(convert(val, columnType));
}
@ -276,8 +276,8 @@ final class TypeConverter {
}
return dataType.isSigned();
}
static JDBCType fromJavaToJDBC(Class<?> clazz) throws SQLException {
for (Entry<Class<?>, JDBCType> e : javaToJDBC.entrySet()) {
// java.util.Calendar from {@code javaToJDBC} is an abstract class and this method can be used with concrete classes as well
@ -285,7 +285,7 @@ final class TypeConverter {
return e.getValue();
}
}
throw new SQLFeatureNotSupportedException("Objects of type " + clazz.getName() + " are not supported");
}
@ -432,7 +432,7 @@ final class TypeConverter {
case REAL:
case FLOAT:
case DOUBLE:
return new Float(((Number) val).doubleValue());
return Float.valueOf((((float) ((Number) val).doubleValue())));
default:
}
@ -451,7 +451,7 @@ final class TypeConverter {
case REAL:
case FLOAT:
case DOUBLE:
return new Double(((Number) val).doubleValue());
return Double.valueOf(((Number) val).doubleValue());
default:
}
@ -539,4 +539,4 @@ final class TypeConverter {
}
return Math.round(x);
}
}
}

View File

@ -38,27 +38,27 @@ import static java.sql.JDBCType.VARBINARY;
import static java.sql.JDBCType.VARCHAR;
public class JdbcPreparedStatementTests extends ESTestCase {
public void testSettingBooleanValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
jps.setBoolean(1, true);
assertEquals(true, value(jps));
assertEquals(BOOLEAN, jdbcType(jps));
jps.setObject(1, false);
assertEquals(false, value(jps));
assertEquals(BOOLEAN, jdbcType(jps));
jps.setObject(1, true, Types.BOOLEAN);
assertEquals(true, value(jps));
assertEquals(BOOLEAN, jdbcType(jps));
assertTrue(value(jps) instanceof Boolean);
jps.setObject(1, true, Types.INTEGER);
assertEquals(1, value(jps));
assertEquals(INTEGER, jdbcType(jps));
jps.setObject(1, true, Types.VARCHAR);
assertEquals("true", value(jps));
assertEquals(VARCHAR, jdbcType(jps));
@ -66,264 +66,264 @@ public class JdbcPreparedStatementTests extends ESTestCase {
public void testThrownExceptionsWhenSettingBooleanValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
SQLException sqle = expectThrows(SQLException.class, () -> jps.setObject(1, true, Types.TIMESTAMP));
assertEquals("Conversion from type [BOOLEAN] to [Timestamp] not supported", sqle.getMessage());
}
public void testSettingStringValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
jps.setString(1, "foo bar");
assertEquals("foo bar", value(jps));
assertEquals(VARCHAR, jdbcType(jps));
jps.setObject(1, "foo bar");
assertEquals("foo bar", value(jps));
assertEquals(VARCHAR, jdbcType(jps));
jps.setObject(1, "foo bar", Types.VARCHAR);
assertEquals("foo bar", value(jps));
assertEquals(VARCHAR, jdbcType(jps));
assertTrue(value(jps) instanceof String);
}
public void testThrownExceptionsWhenSettingStringValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
SQLException sqle = expectThrows(SQLException.class, () -> jps.setObject(1, "foo bar", Types.INTEGER));
assertEquals("Conversion from type [VARCHAR] to [Integer] not supported", sqle.getMessage());
}
public void testSettingByteTypeValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
jps.setByte(1, (byte) 6);
assertEquals((byte) 6, value(jps));
assertEquals(TINYINT, jdbcType(jps));
jps.setObject(1, (byte) 6);
assertEquals((byte) 6, value(jps));
assertEquals(TINYINT, jdbcType(jps));
assertTrue(value(jps) instanceof Byte);
jps.setObject(1, (byte) 0, Types.BOOLEAN);
assertEquals(false, value(jps));
assertEquals(BOOLEAN, jdbcType(jps));
jps.setObject(1, (byte) 123, Types.BOOLEAN);
assertEquals(true, value(jps));
assertEquals(BOOLEAN, jdbcType(jps));
jps.setObject(1, (byte) 123, Types.INTEGER);
assertEquals(123, value(jps));
assertEquals(INTEGER, jdbcType(jps));
jps.setObject(1, (byte) -128, Types.DOUBLE);
assertEquals(-128.0, value(jps));
assertEquals(DOUBLE, jdbcType(jps));
}
public void testThrownExceptionsWhenSettingByteTypeValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
SQLException sqle = expectThrows(SQLException.class, () -> jps.setObject(1, (byte) 6, Types.TIMESTAMP));
assertEquals("Conversion from type [TINYINT] to [Timestamp] not supported", sqle.getMessage());
}
public void testSettingShortTypeValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
short someShort = randomShort();
jps.setShort(1, someShort);
assertEquals(someShort, value(jps));
assertEquals(SMALLINT, jdbcType(jps));
jps.setObject(1, someShort);
assertEquals(someShort, value(jps));
assertEquals(SMALLINT, jdbcType(jps));
assertTrue(value(jps) instanceof Short);
jps.setObject(1, (short) 1, Types.BOOLEAN);
assertEquals(true, value(jps));
assertEquals(BOOLEAN, jdbcType(jps));
jps.setObject(1, (short) -32700, Types.DOUBLE);
assertEquals(-32700.0, value(jps));
assertEquals(DOUBLE, jdbcType(jps));
jps.setObject(1, someShort, Types.INTEGER);
assertEquals((int) someShort, value(jps));
assertEquals(INTEGER, jdbcType(jps));
}
public void testThrownExceptionsWhenSettingShortTypeValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
SQLException sqle = expectThrows(SQLException.class, () -> jps.setObject(1, (short) 6, Types.TIMESTAMP));
assertEquals("Conversion from type [SMALLINT] to [Timestamp] not supported", sqle.getMessage());
sqle = expectThrows(SQLException.class, () -> jps.setObject(1, 256, Types.TINYINT));
assertEquals("Numeric " + 256 + " out of range", sqle.getMessage());
}
public void testSettingIntegerValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
int someInt = randomInt();
jps.setInt(1, someInt);
assertEquals(someInt, value(jps));
assertEquals(INTEGER, jdbcType(jps));
jps.setObject(1, someInt);
assertEquals(someInt, value(jps));
assertEquals(INTEGER, jdbcType(jps));
assertTrue(value(jps) instanceof Integer);
jps.setObject(1, someInt, Types.VARCHAR);
assertEquals(String.valueOf(someInt), value(jps));
assertEquals(VARCHAR, jdbcType(jps));
jps.setObject(1, someInt, Types.FLOAT);
assertEquals(Double.valueOf(someInt), value(jps));
assertTrue(value(jps) instanceof Double);
assertEquals(FLOAT, jdbcType(jps));
}
public void testThrownExceptionsWhenSettingIntegerValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
int someInt = randomInt();
SQLException sqle = expectThrows(SQLException.class, () -> jps.setObject(1, someInt, Types.TIMESTAMP));
assertEquals("Conversion from type [INTEGER] to [Timestamp] not supported", sqle.getMessage());
Integer randomIntNotShort = randomIntBetween(32768, Integer.MAX_VALUE);
sqle = expectThrows(SQLException.class, () -> jps.setObject(1, randomIntNotShort, Types.SMALLINT));
assertEquals("Numeric " + randomIntNotShort + " out of range", sqle.getMessage());
sqle = expectThrows(SQLException.class, () -> jps.setObject(1, randomIntNotShort, Types.TINYINT));
assertEquals("Numeric " + randomIntNotShort + " out of range", sqle.getMessage());
}
public void testSettingLongValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
long someLong = randomLong();
jps.setLong(1, someLong);
assertEquals(someLong, value(jps));
assertEquals(BIGINT, jdbcType(jps));
jps.setObject(1, someLong);
assertEquals(someLong, value(jps));
assertEquals(BIGINT, jdbcType(jps));
assertTrue(value(jps) instanceof Long);
jps.setObject(1, someLong, Types.VARCHAR);
assertEquals(String.valueOf(someLong), value(jps));
assertEquals(VARCHAR, jdbcType(jps));
jps.setObject(1, someLong, Types.DOUBLE);
assertEquals((double) someLong, value(jps));
assertEquals(DOUBLE, jdbcType(jps));
jps.setObject(1, someLong, Types.FLOAT);
assertEquals((double) someLong, value(jps));
assertEquals(FLOAT, jdbcType(jps));
}
public void testThrownExceptionsWhenSettingLongValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
long someLong = randomLong();
SQLException sqle = expectThrows(SQLException.class, () -> jps.setObject(1, someLong, Types.TIMESTAMP));
assertEquals("Conversion from type [BIGINT] to [Timestamp] not supported", sqle.getMessage());
Long randomLongNotShort = randomLongBetween(Integer.MAX_VALUE + 1, Long.MAX_VALUE);
sqle = expectThrows(SQLException.class, () -> jps.setObject(1, randomLongNotShort, Types.INTEGER));
assertEquals("Numeric " + randomLongNotShort + " out of range", sqle.getMessage());
sqle = expectThrows(SQLException.class, () -> jps.setObject(1, randomLongNotShort, Types.SMALLINT));
assertEquals("Numeric " + randomLongNotShort + " out of range", sqle.getMessage());
}
public void testSettingFloatValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
float someFloat = randomFloat();
jps.setFloat(1, someFloat);
assertEquals(someFloat, value(jps));
assertEquals(REAL, jdbcType(jps));
jps.setObject(1, someFloat);
assertEquals(someFloat, value(jps));
assertEquals(REAL, jdbcType(jps));
assertTrue(value(jps) instanceof Float);
jps.setObject(1, someFloat, Types.VARCHAR);
assertEquals(String.valueOf(someFloat), value(jps));
assertEquals(VARCHAR, jdbcType(jps));
jps.setObject(1, someFloat, Types.DOUBLE);
assertEquals((double) someFloat, value(jps));
assertEquals(DOUBLE, jdbcType(jps));
jps.setObject(1, someFloat, Types.FLOAT);
assertEquals((double) someFloat, value(jps));
assertEquals(FLOAT, jdbcType(jps));
}
public void testThrownExceptionsWhenSettingFloatValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
float someFloat = randomFloat();
SQLException sqle = expectThrows(SQLException.class, () -> jps.setObject(1, someFloat, Types.TIMESTAMP));
assertEquals("Conversion from type [REAL] to [Timestamp] not supported", sqle.getMessage());
Float floatNotInt = 5_155_000_000f;
sqle = expectThrows(SQLException.class, () -> jps.setObject(1, floatNotInt, Types.INTEGER));
assertEquals(String.format(Locale.ROOT, "Numeric %s out of range",
assertEquals(String.format(Locale.ROOT, "Numeric %s out of range",
Long.toString(Math.round(floatNotInt.doubleValue()))), sqle.getMessage());
sqle = expectThrows(SQLException.class, () -> jps.setObject(1, floatNotInt, Types.SMALLINT));
assertEquals(String.format(Locale.ROOT, "Numeric %s out of range",
assertEquals(String.format(Locale.ROOT, "Numeric %s out of range",
Long.toString(Math.round(floatNotInt.doubleValue()))), sqle.getMessage());
}
public void testSettingDoubleValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
double someDouble = randomDouble();
jps.setDouble(1, someDouble);
assertEquals(someDouble, value(jps));
assertEquals(DOUBLE, jdbcType(jps));
jps.setObject(1, someDouble);
assertEquals(someDouble, value(jps));
assertEquals(DOUBLE, jdbcType(jps));
assertTrue(value(jps) instanceof Double);
jps.setObject(1, someDouble, Types.VARCHAR);
assertEquals(String.valueOf(someDouble), value(jps));
assertEquals(VARCHAR, jdbcType(jps));
jps.setObject(1, someDouble, Types.REAL);
assertEquals(new Float(someDouble), value(jps));
assertEquals((float) someDouble, value(jps));
assertEquals(REAL, jdbcType(jps));
}
public void testThrownExceptionsWhenSettingDoubleValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
double someDouble = randomDouble();
SQLException sqle = expectThrows(SQLException.class, () -> jps.setObject(1, someDouble, Types.TIMESTAMP));
assertEquals("Conversion from type [DOUBLE] to [Timestamp] not supported", sqle.getMessage());
Double doubleNotInt = 5_155_000_000d;
sqle = expectThrows(SQLException.class, () -> jps.setObject(1, doubleNotInt, Types.INTEGER));
assertEquals(String.format(Locale.ROOT, "Numeric %s out of range",
assertEquals(String.format(Locale.ROOT, "Numeric %s out of range",
Long.toString(((Number) doubleNotInt).longValue())), sqle.getMessage());
}
public void testUnsupportedClasses() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
SQLFeatureNotSupportedException sfnse = expectThrows(SQLFeatureNotSupportedException.class, () -> jps.setObject(1, new Struct() {
@ -341,23 +341,23 @@ public class JdbcPreparedStatementTests extends ESTestCase {
}
}));
assertEquals("Objects of type java.sql.Struct are not supported", sfnse.getMessage());
sfnse = expectThrows(SQLFeatureNotSupportedException.class, () -> jps.setObject(1, new URL("http://test")));
assertEquals("Objects of type java.net.URL are not supported", sfnse.getMessage());
sfnse = expectThrows(SQLFeatureNotSupportedException.class, () -> jps.setURL(1, new URL("http://test")));
assertEquals("Objects of type java.net.URL are not supported", sfnse.getMessage());
sfnse = expectThrows(SQLFeatureNotSupportedException.class, () -> jps.setObject(1, this, Types.TIMESTAMP));
assertEquals("Conversion from type " + this.getClass().getName() + " to TIMESTAMP not supported", sfnse.getMessage());
SQLException se = expectThrows(SQLException.class, () -> jps.setObject(1, this, 1_000_000));
assertEquals("Type:1000000 is not a valid Types.java value.", se.getMessage());
IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> jps.setObject(1, randomShort(), Types.CHAR));
assertEquals("Unsupported JDBC type [CHAR]", iae.getMessage());
}
public void testSettingTimestampValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
@ -365,186 +365,186 @@ public class JdbcPreparedStatementTests extends ESTestCase {
jps.setTimestamp(1, someTimestamp);
assertEquals(someTimestamp.getTime(), ((Date)value(jps)).getTime());
assertEquals(TIMESTAMP, jdbcType(jps));
Calendar nonDefaultCal = randomCalendar();
// February 29th, 2016. 01:17:55 GMT = 1456708675000 millis since epoch
jps.setTimestamp(1, new Timestamp(1456708675000L), nonDefaultCal);
assertEquals(1456708675000L, convertFromUTCtoCalendar(((Date)value(jps)), nonDefaultCal));
assertEquals(TIMESTAMP, jdbcType(jps));
long beforeEpochTime = -randomMillisSinceEpoch();
jps.setTimestamp(1, new Timestamp(beforeEpochTime), nonDefaultCal);
assertEquals(beforeEpochTime, convertFromUTCtoCalendar(((Date)value(jps)), nonDefaultCal));
assertTrue(value(jps) instanceof java.util.Date);
jps.setObject(1, someTimestamp, Types.VARCHAR);
assertEquals(someTimestamp.toString(), value(jps).toString());
assertEquals(VARCHAR, jdbcType(jps));
}
public void testThrownExceptionsWhenSettingTimestampValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
Timestamp someTimestamp = new Timestamp(randomMillisSinceEpoch());
SQLException sqle = expectThrows(SQLFeatureNotSupportedException.class, () -> jps.setObject(1, someTimestamp, Types.INTEGER));
assertEquals("Conversion from type java.sql.Timestamp to INTEGER not supported", sqle.getMessage());
}
public void testSettingTimeValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
Time time = new Time(4675000);
Calendar nonDefaultCal = randomCalendar();
jps.setTime(1, time, nonDefaultCal);
assertEquals(4675000, convertFromUTCtoCalendar(((Date)value(jps)), nonDefaultCal));
assertEquals(TIMESTAMP, jdbcType(jps));
assertTrue(value(jps) instanceof java.util.Date);
jps.setObject(1, time, Types.VARCHAR);
assertEquals(time.toString(), value(jps).toString());
assertEquals(VARCHAR, jdbcType(jps));
}
public void testThrownExceptionsWhenSettingTimeValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
Time time = new Time(4675000);
SQLException sqle = expectThrows(SQLFeatureNotSupportedException.class, () -> jps.setObject(1, time, Types.INTEGER));
assertEquals("Conversion from type java.sql.Time to INTEGER not supported", sqle.getMessage());
}
public void testSettingSqlDateValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
java.sql.Date someSqlDate = new java.sql.Date(randomMillisSinceEpoch());
jps.setDate(1, someSqlDate);
assertEquals(someSqlDate.getTime(), ((Date)value(jps)).getTime());
assertEquals(TIMESTAMP, jdbcType(jps));
someSqlDate = new java.sql.Date(randomMillisSinceEpoch());
Calendar nonDefaultCal = randomCalendar();
jps.setDate(1, someSqlDate, nonDefaultCal);
assertEquals(someSqlDate.getTime(), convertFromUTCtoCalendar(((Date)value(jps)), nonDefaultCal));
assertEquals(TIMESTAMP, jdbcType(jps));
assertTrue(value(jps) instanceof java.util.Date);
jps.setObject(1, someSqlDate, Types.VARCHAR);
assertEquals(someSqlDate.toString(), value(jps).toString());
assertEquals(VARCHAR, jdbcType(jps));
}
public void testThrownExceptionsWhenSettingSqlDateValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
java.sql.Date someSqlDate = new java.sql.Date(randomMillisSinceEpoch());
SQLException sqle = expectThrows(SQLFeatureNotSupportedException.class,
SQLException sqle = expectThrows(SQLFeatureNotSupportedException.class,
() -> jps.setObject(1, new java.sql.Date(randomMillisSinceEpoch()), Types.DOUBLE));
assertEquals("Conversion from type " + someSqlDate.getClass().getName() + " to DOUBLE not supported", sqle.getMessage());
}
public void testSettingCalendarValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
Calendar someCalendar = randomCalendar();
someCalendar.setTimeInMillis(randomMillisSinceEpoch());
jps.setObject(1, someCalendar);
assertEquals(someCalendar.getTime(), (Date) value(jps));
assertEquals(TIMESTAMP, jdbcType(jps));
assertTrue(value(jps) instanceof java.util.Date);
jps.setObject(1, someCalendar, Types.VARCHAR);
assertEquals(someCalendar.toString(), value(jps).toString());
assertEquals(VARCHAR, jdbcType(jps));
Calendar nonDefaultCal = randomCalendar();
jps.setObject(1, nonDefaultCal);
assertEquals(nonDefaultCal.getTime(), (Date) value(jps));
assertEquals(TIMESTAMP, jdbcType(jps));
}
public void testThrownExceptionsWhenSettingCalendarValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
Calendar someCalendar = randomCalendar();
SQLException sqle = expectThrows(SQLFeatureNotSupportedException.class, () -> jps.setObject(1, someCalendar, Types.DOUBLE));
assertEquals("Conversion from type " + someCalendar.getClass().getName() + " to DOUBLE not supported", sqle.getMessage());
}
public void testSettingDateValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
Date someDate = new Date(randomMillisSinceEpoch());
jps.setObject(1, someDate);
assertEquals(someDate, (Date) value(jps));
assertEquals(TIMESTAMP, jdbcType(jps));
assertTrue(value(jps) instanceof java.util.Date);
jps.setObject(1, someDate, Types.VARCHAR);
assertEquals(someDate.toString(), value(jps).toString());
assertEquals(VARCHAR, jdbcType(jps));
}
public void testThrownExceptionsWhenSettingDateValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
Date someDate = new Date(randomMillisSinceEpoch());
SQLException sqle = expectThrows(SQLFeatureNotSupportedException.class, () -> jps.setObject(1, someDate, Types.BIGINT));
assertEquals("Conversion from type " + someDate.getClass().getName() + " to BIGINT not supported", sqle.getMessage());
}
public void testSettingLocalDateTimeValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
LocalDateTime ldt = LocalDateTime.now(Clock.systemDefaultZone());
jps.setObject(1, ldt);
assertEquals(Date.class, value(jps).getClass());
assertEquals(TIMESTAMP, jdbcType(jps));
assertTrue(value(jps) instanceof java.util.Date);
jps.setObject(1, ldt, Types.VARCHAR);
assertEquals(ldt.toString(), value(jps).toString());
assertEquals(VARCHAR, jdbcType(jps));
}
public void testThrownExceptionsWhenSettingLocalDateTimeValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
LocalDateTime ldt = LocalDateTime.now(Clock.systemDefaultZone());
SQLException sqle = expectThrows(SQLFeatureNotSupportedException.class, () -> jps.setObject(1, ldt, Types.BIGINT));
assertEquals("Conversion from type " + ldt.getClass().getName() + " to BIGINT not supported", sqle.getMessage());
}
public void testSettingByteArrayValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
byte[] buffer = "some data".getBytes(StandardCharsets.UTF_8);
jps.setBytes(1, buffer);
assertEquals(byte[].class, value(jps).getClass());
assertEquals(VARBINARY, jdbcType(jps));
jps.setObject(1, buffer);
assertEquals(byte[].class, value(jps).getClass());
assertEquals(VARBINARY, jdbcType(jps));
assertTrue(value(jps) instanceof byte[]);
jps.setObject(1, buffer, Types.VARBINARY);
assertEquals((byte[]) value(jps), buffer);
assertEquals(VARBINARY, jdbcType(jps));
SQLException sqle = expectThrows(SQLFeatureNotSupportedException.class, () -> jps.setObject(1, buffer, Types.VARCHAR));
assertEquals("Conversion from type byte[] to VARCHAR not supported", sqle.getMessage());
sqle = expectThrows(SQLFeatureNotSupportedException.class, () -> jps.setObject(1, buffer, Types.DOUBLE));
assertEquals("Conversion from type byte[] to DOUBLE not supported", sqle.getMessage());
}
public void testThrownExceptionsWhenSettingByteArrayValues() throws SQLException {
JdbcPreparedStatement jps = createJdbcPreparedStatement();
byte[] buffer = "foo".getBytes(StandardCharsets.UTF_8);
SQLException sqle = expectThrows(SQLFeatureNotSupportedException.class, () -> jps.setObject(1, buffer, Types.VARCHAR));
assertEquals("Conversion from type byte[] to VARCHAR not supported", sqle.getMessage());
sqle = expectThrows(SQLFeatureNotSupportedException.class, () -> jps.setObject(1, buffer, Types.DOUBLE));
assertEquals("Conversion from type byte[] to DOUBLE not supported", sqle.getMessage());
}
@ -564,14 +564,14 @@ public class JdbcPreparedStatementTests extends ESTestCase {
private Object value(JdbcPreparedStatement jps) throws SQLException {
return jps.query.getParam(1).value;
}
private Calendar randomCalendar() {
return Calendar.getInstance(randomTimeZone(), Locale.ROOT);
}
/*
* Converts from UTC to the provided Calendar.
* Helps checking if the converted date/time values using Calendars in set*(...,Calendar) methods did convert
* Helps checking if the converted date/time values using Calendars in set*(...,Calendar) methods did convert
* the values correctly to UTC.
*/
private long convertFromUTCtoCalendar(Date date, Calendar nonDefaultCal) throws SQLException {

View File

@ -33,7 +33,7 @@ public class CompareConditionTests extends ESTestCase {
assertThat(CompareCondition.Op.EQ.eval(null, null), is(true));
assertThat(CompareCondition.Op.EQ.eval(4, 3.0), is(false));
assertThat(CompareCondition.Op.EQ.eval(3, 3.0), is(true));
assertThat(CompareCondition.Op.EQ.eval(2, new Float(3.0)), is(false));
assertThat(CompareCondition.Op.EQ.eval(2, Float.valueOf((float)3.0)), is(false));
assertThat(CompareCondition.Op.EQ.eval(3, null), is(false));
assertThat(CompareCondition.Op.EQ.eval(2, "2"), is(true)); // comparing as strings
assertThat(CompareCondition.Op.EQ.eval(3, "4"), is(false)); // comparing as strings
@ -59,7 +59,7 @@ public class CompareConditionTests extends ESTestCase {
assertThat(CompareCondition.Op.NOT_EQ.eval(null, null), is(false));
assertThat(CompareCondition.Op.NOT_EQ.eval(4, 3.0), is(true));
assertThat(CompareCondition.Op.NOT_EQ.eval(3, 3.0), is(false));
assertThat(CompareCondition.Op.NOT_EQ.eval(2, new Float(3.0)), is(true));
assertThat(CompareCondition.Op.NOT_EQ.eval(2, Float.valueOf((float)3.0)), is(true));
assertThat(CompareCondition.Op.NOT_EQ.eval(3, null), is(true));
assertThat(CompareCondition.Op.NOT_EQ.eval(2, "2"), is(false)); // comparing as strings
assertThat(CompareCondition.Op.NOT_EQ.eval(3, "4"), is(true)); // comparing as strings
@ -83,7 +83,7 @@ public class CompareConditionTests extends ESTestCase {
public void testOpEvalGTE() throws Exception {
assertThat(CompareCondition.Op.GTE.eval(4, 3.0), is(true));
assertThat(CompareCondition.Op.GTE.eval(3, 3.0), is(true));
assertThat(CompareCondition.Op.GTE.eval(2, new Float(3.0)), is(false));
assertThat(CompareCondition.Op.GTE.eval(2, Float.valueOf((float)3.0)), is(false));
assertThat(CompareCondition.Op.GTE.eval(3, null), is(false));
assertThat(CompareCondition.Op.GTE.eval(3, "2"), is(true)); // comparing as strings
assertThat(CompareCondition.Op.GTE.eval(3, "4"), is(false)); // comparing as strings
@ -103,7 +103,7 @@ public class CompareConditionTests extends ESTestCase {
public void testOpEvalGT() throws Exception {
assertThat(CompareCondition.Op.GT.eval(4, 3.0), is(true));
assertThat(CompareCondition.Op.GT.eval(3, 3.0), is(false));
assertThat(CompareCondition.Op.GT.eval(2, new Float(3.0)), is(false));
assertThat(CompareCondition.Op.GT.eval(2, Float.valueOf((float)3.0)), is(false));
assertThat(CompareCondition.Op.GT.eval(3, null), is(false));
assertThat(CompareCondition.Op.GT.eval(3, "2"), is(true)); // comparing as strings
assertThat(CompareCondition.Op.GT.eval(3, "4"), is(false)); // comparing as strings
@ -124,7 +124,7 @@ public class CompareConditionTests extends ESTestCase {
public void testOpEvalLTE() throws Exception {
assertThat(CompareCondition.Op.LTE.eval(4, 3.0), is(false));
assertThat(CompareCondition.Op.LTE.eval(3, 3.0), is(true));
assertThat(CompareCondition.Op.LTE.eval(2, new Float(3.0)), is(true));
assertThat(CompareCondition.Op.LTE.eval(2, Float.valueOf((float) 3.0)), is(true));
assertThat(CompareCondition.Op.LTE.eval(3, null), is(false));
assertThat(CompareCondition.Op.LTE.eval(3, "2"), is(false)); // comparing as strings
assertThat(CompareCondition.Op.LTE.eval(3, "4"), is(true)); // comparing as strings
@ -144,7 +144,7 @@ public class CompareConditionTests extends ESTestCase {
public void testOpEvalLT() throws Exception {
assertThat(CompareCondition.Op.LT.eval(4, 3.0), is(false));
assertThat(CompareCondition.Op.LT.eval(3, 3.0), is(false));
assertThat(CompareCondition.Op.LT.eval(2, new Float(3.0)), is(true));
assertThat(CompareCondition.Op.LT.eval(2, Float.valueOf((float) 3.0)), is(true));
assertThat(CompareCondition.Op.LT.eval(3, null), is(false));
assertThat(CompareCondition.Op.LT.eval(3, "2"), is(false)); // comparing as strings
assertThat(CompareCondition.Op.LT.eval(3, "4"), is(true)); // comparing as strings