Java tutorial
/* * This file is part of AceQL. * AceQL: Remote JDBC access over HTTP. * Copyright (C) 2015, KawanSoft SAS * (http://www.kawansoft.com). All rights reserved. * * AceQL 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. * * AceQL 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 library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301 USA * * Any modifications to this file must keep this entire header * intact. */ package org.kawanfw.test.api.client; import java.sql.Connection; import org.apache.commons.lang3.StringUtils; import org.junit.Assert; import org.junit.Test; import org.kawanfw.commons.api.client.SessionParameters; import org.kawanfw.commons.api.client.RemoteException; import org.kawanfw.file.api.client.RemoteSession; import org.kawanfw.sql.api.client.RemoteConnection; import org.kawanfw.sql.util.JdbcUrlHeader; import org.kawanfw.test.parms.ConnectionLoader; import org.kawanfw.test.parms.SqlTestParms; import org.kawanfw.test.util.MessageDisplayer; /** * * Test that a re mote method is callable. * * @author Nicolas de Pomereu * */ public class CallTest { @Test public void test() throws Exception { Connection connection = null; try { connection = ConnectionLoader.getAceqlConnection(); test(connection); } finally { if (connection != null) { connection.close(); } } } public void test(Connection connection) throws Exception { MessageDisplayer.initClassDisplay(this.getClass().getSimpleName()); if (!(connection instanceof RemoteConnection)) { MessageDisplayer.display("CallTest not called because Connection is local."); return; } // Don't do it with default configurators of AceQL Web Server String url = ((RemoteConnection) connection).getUrl(); if (url.contains(":9090")) { MessageDisplayer.display("Test not done with AceQL Web Server."); return; } RemoteConnection remoteConnection = (RemoteConnection) connection; RemoteSession remoteSession = remoteConnection.getRemoteSession(); int a = 33; int b = 44; String resultStr = remoteSession.call("org.kawanfw.test.api.server.CalculatorNotAuthenticated.add", a, b); int result = Integer.parseInt(resultStr); MessageDisplayer.display("CalculatorNotAuthenticated Result: " + result); Assert.assertEquals(a + b, result); SessionParameters sessionParameters = new SessionParameters(); sessionParameters.setAcceptAllSslCertificates(true); String callUrl = SqlTestParms.ACEQL_URL; callUrl = StringUtils.substringAfter(callUrl, JdbcUrlHeader.JDBC_URL_HEADER); remoteSession = new RemoteSession(callUrl, SqlTestParms.REMOTE_USER, SqlTestParms.REMOTE_PASSWORD.toCharArray(), null, null, sessionParameters); resultStr = remoteSession.call("org.kawanfw.test.api.server.Calculator.add", a, b); result = Integer.parseInt(resultStr); MessageDisplayer.display("Calculator Result: " + result); Assert.assertEquals(a + b, result); } @Test(expected = RemoteException.class) public void test2() throws Exception { if (!SqlTestParms.isRemote()) { MessageDisplayer.display("CallTest not called because Connection is local."); return; } int a = 33; int b = 44; RemoteSession remoteSession = new RemoteSession(SqlTestParms.ACEQL_URL, SqlTestParms.REMOTE_USER, SqlTestParms.REMOTE_PASSWORD.toCharArray()); String resultStr = remoteSession.call("org.kawanfw.test.api.server.CalculatorNotCallable.add", a, b); int result = Integer.parseInt(resultStr); MessageDisplayer.display("Calculator Result: " + result); Assert.assertEquals(a + b, result); } }