|
| 1 | +package com.ibm.opl.customdatasource; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileOutputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.sql.Connection; |
| 8 | +import java.sql.DriverManager; |
| 9 | +import java.sql.Statement; |
| 10 | + |
| 11 | +import static org.junit.Assert.*; |
| 12 | + |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import ilog.concert.IloException; |
| 16 | +import ilog.opl.IloOplDataSource; |
| 17 | +import ilog.opl.IloOplErrorHandler; |
| 18 | +import ilog.opl.IloOplException; |
| 19 | +import ilog.opl.IloOplFactory; |
| 20 | +import ilog.opl.IloOplModel; |
| 21 | +import ilog.opl.IloOplModelDefinition; |
| 22 | +import ilog.opl.IloOplRunConfiguration; |
| 23 | + |
| 24 | +public class JavaApiTest { |
| 25 | + public static String OIL_DAT="models/oil.dat"; |
| 26 | + public static String OIL_MOD="models/oil.mod"; |
| 27 | + public static String CONFIG_RESOURCE = "models/oil_sqlite.xml"; |
| 28 | + |
| 29 | + public static String CREATE_OIL_MOD="models/oil_create_db.mod"; |
| 30 | + public static String CREATE_CONFIG_RESOURCE = "models/oil_create_db.xml"; |
| 31 | + |
| 32 | + |
| 33 | + /** |
| 34 | + * Runs a model as a .mod file, using a set of .dat files, and a given jdbc configuration. |
| 35 | + * |
| 36 | + * If connectionString is specified, it will be used instead of the url in the jdbc configuration, |
| 37 | + * allowing for tests with database which url is not static (ex: temporary test databases). |
| 38 | + * @param modFilename The .mod file |
| 39 | + * @param datFilenames An array of .dat files |
| 40 | + * @param jdbcConfigurationFile The jdbc configuration file |
| 41 | + * @param connectionString An override url |
| 42 | + * @throws IOException |
| 43 | + * @throws IloException |
| 44 | + */ |
| 45 | + public final void runMod(String modFilename, String[] datFilenames, String jdbcConfigurationFile, |
| 46 | + String connectionString) throws IOException, IloException { |
| 47 | + // create OPL |
| 48 | + IloOplFactory.setDebugMode(true); |
| 49 | + IloOplFactory oplF = new IloOplFactory(); |
| 50 | + IloOplErrorHandler errHandler = oplF.createOplErrorHandler(System.out); |
| 51 | + |
| 52 | + IloOplRunConfiguration rc = null; |
| 53 | + if (datFilenames == null || datFilenames.length == 0) { |
| 54 | + rc = oplF.createOplRunConfiguration(modFilename); |
| 55 | + } |
| 56 | + else { |
| 57 | + rc = oplF.createOplRunConfiguration(modFilename, datFilenames); |
| 58 | + } |
| 59 | + |
| 60 | + rc.setErrorHandler(errHandler); |
| 61 | + IloOplModel opl = rc.getOplModel(); |
| 62 | + |
| 63 | + IloOplModelDefinition def = opl.getModelDefinition(); |
| 64 | + |
| 65 | + // |
| 66 | + // Reads the JDBC configuration, initialize a JDBC custom data source |
| 67 | + // and sets the source in OPL. |
| 68 | + // |
| 69 | + JdbcConfiguration jdbcProperties = null; |
| 70 | + if (jdbcConfigurationFile != null) { |
| 71 | + jdbcProperties = new JdbcConfiguration(); |
| 72 | + jdbcProperties.read(jdbcConfigurationFile); |
| 73 | + // we want to override connection string with conn string that has the actual temp db path |
| 74 | + if (connectionString != null) |
| 75 | + jdbcProperties.setUrl(connectionString); |
| 76 | + // Create the custom JDBC data source |
| 77 | + IloOplDataSource jdbcDataSource = new JdbcCustomDataSource(jdbcProperties, oplF, def); |
| 78 | + // Pass it to the model. |
| 79 | + opl.addDataSource(jdbcDataSource); |
| 80 | + } |
| 81 | + |
| 82 | + opl.generate(); |
| 83 | + |
| 84 | + boolean success = false; |
| 85 | + if (opl.hasCplex()) { |
| 86 | + if (opl.getCplex().solve()) { |
| 87 | + success = true; |
| 88 | + } |
| 89 | + } else { |
| 90 | + if (opl.getCP().solve()) { |
| 91 | + success = true; |
| 92 | + } |
| 93 | + } |
| 94 | + if (success == true) { |
| 95 | + opl.postProcess(); |
| 96 | + // write results |
| 97 | + if (jdbcProperties != null) { |
| 98 | + JdbcWriter writer = new JdbcWriter(jdbcProperties, def, opl); |
| 99 | + writer.customWrite(); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Tests the JdbcCustomReader/Writer by API. |
| 106 | + */ |
| 107 | + @Test |
| 108 | + public final void testApiCall() { |
| 109 | + File tempdb = null; |
| 110 | + try { |
| 111 | + // create temp db |
| 112 | + tempdb = File.createTempFile("testApiCall", ".db"); |
| 113 | + String connectionString = "jdbc:sqlite:" + tempdb.getAbsolutePath(); |
| 114 | + Connection conn = null; |
| 115 | + try { |
| 116 | + // creates the db |
| 117 | + conn = DriverManager.getConnection(connectionString); |
| 118 | + } finally { |
| 119 | + if (conn != null) |
| 120 | + conn.close(); |
| 121 | + } |
| 122 | + |
| 123 | + // use a .mod to create tmp database |
| 124 | + String createModFilename = new File(getClass().getResource(CREATE_OIL_MOD).getFile()).getAbsolutePath(); |
| 125 | + String createJdbcConfigurationFile = new File(getClass().getResource(CREATE_CONFIG_RESOURCE).getFile()).getAbsolutePath(); |
| 126 | + runMod(createModFilename, null, createJdbcConfigurationFile, connectionString); |
| 127 | + |
| 128 | + // now solve the oil model |
| 129 | + String modFilename = new File(getClass().getResource(OIL_MOD).getFile()).getAbsolutePath(); |
| 130 | + String[] datFilenames = {new File(getClass().getResource(OIL_DAT).getFile()).getAbsolutePath()}; |
| 131 | + String jdbcConfigurationFile = new File(getClass().getResource(CONFIG_RESOURCE).getFile()).getAbsolutePath(); |
| 132 | + runMod(modFilename, datFilenames, jdbcConfigurationFile, connectionString); |
| 133 | + |
| 134 | + } catch (IloOplException ex) { |
| 135 | + ex.printStackTrace(); |
| 136 | + fail("### OPL exception: " + ex.getMessage()); |
| 137 | + } catch (IloException ex) { |
| 138 | + ex.printStackTrace(); |
| 139 | + fail("### CONCERT exception: " + ex.getMessage()); |
| 140 | + } catch (Exception ex) { |
| 141 | + ex.printStackTrace(); |
| 142 | + fail("### UNEXPECTED UNKNOWN ERROR ..."); |
| 143 | + } finally { |
| 144 | + if (tempdb != null) { |
| 145 | + tempdb.delete(); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments