use of java.lang.Exception in project hive by apache.
the class TestJdbcDriver2 method doTestSelectAll.
private void doTestSelectAll(String tableName, int maxRows, int fetchSize) throws Exception {
boolean isPartitionTable = tableName.equals(partitionedTableName);
Statement stmt = con.createStatement();
if (maxRows >= 0) {
stmt.setMaxRows(maxRows);
}
if (fetchSize > 0) {
stmt.setFetchSize(fetchSize);
assertEquals(fetchSize, stmt.getFetchSize());
}
// JDBC says that 0 means return all, which is the default
int expectedMaxRows = maxRows < 1 ? 0 : maxRows;
assertNotNull("Statement is null", stmt);
assertEquals("Statement max rows not as expected", expectedMaxRows, stmt.getMaxRows());
assertFalse("Statement should not be closed", stmt.isClosed());
ResultSet res;
// run some queries
res = stmt.executeQuery("select * from " + tableName);
assertNotNull("ResultSet is null", res);
assertTrue("getResultSet() not returning expected ResultSet", res == stmt.getResultSet());
assertEquals("get update count not as expected", -1, stmt.getUpdateCount());
int i = 0;
ResultSetMetaData meta = res.getMetaData();
int expectedColCount = isPartitionTable ? 3 : 2;
assertEquals("Unexpected column count", expectedColCount, meta.getColumnCount());
boolean moreRow = res.next();
while (moreRow) {
try {
i++;
assertEquals(res.getInt(1), res.getInt(tableName + ".under_col"));
assertEquals(res.getInt(1), res.getInt("under_col"));
assertEquals(res.getString(1), res.getString(tableName + ".under_col"));
assertEquals(res.getString(1), res.getString("under_col"));
assertEquals(res.getString(2), res.getString(tableName + ".value"));
assertEquals(res.getString(2), res.getString("value"));
if (isPartitionTable) {
assertEquals(res.getString(3), partitionedColumnValue);
assertEquals(res.getString(3), res.getString(partitionedColumnName));
assertEquals(res.getString(3), res.getString(tableName + "." + partitionedColumnName));
}
assertFalse("Last result value was not null", res.wasNull());
assertNull("No warnings should be found on ResultSet", res.getWarnings());
// verifying that method is supported
res.clearWarnings();
// System.out.println(res.getString(1) + " " + res.getString(2));
assertEquals("getInt and getString don't align for the same result value", String.valueOf(res.getInt(1)), res.getString(1));
assertEquals("Unexpected result found", "val_" + res.getString(1), res.getString(2));
moreRow = res.next();
} catch (SQLException e) {
System.out.println(e.toString());
e.printStackTrace();
throw new Exception(e.toString());
}
}
// supposed to get 500 rows if maxRows isn't set
int expectedRowCount = maxRows > 0 ? maxRows : 500;
assertEquals("Incorrect number of rows returned", expectedRowCount, i);
// should have no more rows
assertEquals(false, moreRow);
assertNull("No warnings should be found on statement", stmt.getWarnings());
// verifying that method is supported
stmt.clearWarnings();
assertNull("No warnings should be found on connection", con.getWarnings());
// verifying that method is supported
con.clearWarnings();
stmt.close();
assertTrue("Statement should be closed", stmt.isClosed());
}
use of java.lang.Exception in project hive by apache.
the class TestJdbcDriver2 method testResultSetColumnNameCaseInsensitive.
@Test
public void testResultSetColumnNameCaseInsensitive() throws SQLException {
Statement stmt = con.createStatement();
ResultSet res;
res = stmt.executeQuery("select c1 from " + dataTypeTableName + " limit 1");
try {
int count = 0;
while (res.next()) {
res.findColumn("c1");
res.findColumn("C1");
count++;
}
assertEquals(count, 1);
} catch (Exception e) {
String msg = "Unexpected exception: " + e;
LOG.info(msg, e);
fail(msg);
}
res = stmt.executeQuery("select c1 C1 from " + dataTypeTableName + " limit 1");
try {
int count = 0;
while (res.next()) {
res.findColumn("c1");
res.findColumn("C1");
count++;
}
assertEquals(count, 1);
} catch (Exception e) {
String msg = "Unexpected exception: " + e;
LOG.info(msg, e);
fail(msg);
}
stmt.close();
}
use of java.lang.Exception in project hive by apache.
the class TestJdbcDriver2 method testQueryCancel.
/**
* Test the cancellation of a query that is running.
* We spawn 2 threads - one running the query and
* the other attempting to cancel.
* We're using a dummy udf to simulate a query,
* that runs for a sufficiently long time.
* @throws Exception
*/
@Test
public void testQueryCancel() throws Exception {
String udfName = SleepMsUDF.class.getName();
Statement stmt1 = con.createStatement();
stmt1.execute("create temporary function sleepMsUDF as '" + udfName + "'");
stmt1.close();
final Statement stmt = con.createStatement();
// Thread executing the query
Thread tExecute = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("Executing query: ");
// The test table has 500 rows, so total query time should be ~ 500*500ms
stmt.executeQuery("select sleepMsUDF(t1.under_col, 1) as u0, t1.under_col as u1, " + "t2.under_col as u2 from " + tableName + " t1 join " + tableName + " t2 on t1.under_col = t2.under_col");
fail("Expecting SQLException");
} catch (SQLException e) {
// This thread should throw an exception
assertNotNull(e);
System.out.println(e.toString());
}
}
});
// Thread cancelling the query
Thread tCancel = new Thread(new Runnable() {
@Override
public void run() {
try {
// Sleep for 100ms
Thread.sleep(100);
System.out.println("Cancelling query: ");
stmt.cancel();
} catch (Exception e) {
// No-op
}
}
});
tExecute.start();
tCancel.start();
tExecute.join();
tCancel.join();
stmt.close();
}
use of java.lang.Exception in project Payara by payara.
the class MultipleSPETest method getLBIPAddress.
private String getLBIPAddress(GlassFish glassfish) {
String lbIP = null;
String IPAddressPattern = "IP-ADDRESS\\s*\n*(.*)\\s*\n(([01]?\\d*|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([0-9]?\\d\\d?|2[0-4]\\d|25[0-5]))";
try {
CommandRunner commandRunner = glassfish.getCommandRunner();
String result = commandRunner.run("list-services", "--type", "LB", "--output", "IP-ADDRESS").getOutput().toString();
if (result.contains("Nothing to list.")) {
result = commandRunner.run("list-services", "--type", "JavaEE", "--output", "IP-ADDRESS").getOutput().toString();
Pattern p = Pattern.compile(IPAddressPattern);
Matcher m = p.matcher(result);
if (m.find()) {
lbIP = m.group(2);
} else {
lbIP = "localhost";
}
} else {
Pattern p = Pattern.compile(IPAddressPattern);
Matcher m = p.matcher(result);
if (m.find()) {
lbIP = m.group(2);
} else {
lbIP = "localhost";
}
}
} catch (Exception e) {
System.out.println("Regex has thrown an exception " + e.getMessage());
return "localhost";
}
return lbIP;
}
use of java.lang.Exception in project KaellyBot by Kaysoro.
the class AlmanaxCommand method request.
@Override
public boolean request(IMessage message) {
if (super.request(message)) {
Language lg = Translator.getLanguageFrom(message.getChannel());
try {
Date date = new Date();
Matcher m = getMatcher(message);
m.find();
if (m.group(1) != null && (m.group(1).matches("\\s+true") || m.group(1).matches("\\s+0") || m.group(1).matches("\\s+on") || m.group(1).matches("\\s+false") || m.group(1).matches("\\s+1") || m.group(1).matches("\\s+off"))) {
if (!message.getChannel().isPrivate()) {
if (isUserHasEnoughRights(message)) {
if (m.group(1).matches("\\s+true") || m.group(1).matches("\\s+0") || m.group(1).matches("\\s+on"))
if (!AlmanaxCalendar.getAlmanaxCalendars().containsKey(message.getChannel().getStringID())) {
new AlmanaxCalendar(message.getGuild().getStringID(), message.getChannel().getStringID()).addToDatabase();
Message.sendText(message.getChannel(), Translator.getLabel(lg, "almanax.request.1"));
} else
Message.sendText(message.getChannel(), Translator.getLabel(lg, "almanax.request.2"));
else if (m.group(1).matches("\\s+false") || m.group(1).matches("\\s+1") || m.group(1).matches("\\s+off"))
if (AlmanaxCalendar.getAlmanaxCalendars().containsKey(message.getChannel().getStringID())) {
AlmanaxCalendar.getAlmanaxCalendars().get(message.getChannel().getStringID()).removeToDatabase();
Message.sendText(message.getChannel(), Translator.getLabel(lg, "almanax.request.3"));
} else
Message.sendText(message.getChannel(), Translator.getLabel(lg, "almanax.request.4"));
} else
noEnoughRights.throwException(message, this, lg);
} else
notUsableInMp.throwException(message, this, lg);
} else if (m.group(1) != null && m.group(1).matches("\\s+\\+\\d")) {
int number = Integer.parseInt(m.group(1).replaceAll("\\s+\\+", ""));
Message.sendEmbed(message.getChannel(), Almanax.getGroupedObject(lg, new Date(), number));
} else {
if (m.group(1) != null && m.group(1).matches("\\s+\\d{2}/\\d{2}/\\d{4}"))
date = Almanax.discordToBot.parse(m.group(1));
Almanax almanax = Almanax.get(lg, date);
Message.sendEmbed(message.getChannel(), almanax.getMoreEmbedObject(lg));
}
} catch (ParseException e) {
incorrectDateFormat.throwException(message, this, lg);
return false;
} catch (IOException e) {
ExceptionManager.manageIOException(e, message, this, lg, notFound);
} catch (Exception e) {
ExceptionManager.manageException(e, message, this, lg);
}
}
return false;
}
Aggregations