|
| 1 | +import org.cytoscape.model.CyNetworkFactory; |
| 2 | +import org.cytoscape.model.CyNetworkManager; |
| 3 | +import org.cytoscape.work.TaskMonitor; |
| 4 | +import org.junit.Assert; |
| 5 | +import org.junit.Test; |
| 6 | +import org.sap.cytoscape.internal.hdb.HanaConnectionManager; |
| 7 | +import org.sap.cytoscape.internal.tasks.CyLoadTask; |
| 8 | + |
| 9 | +import java.lang.reflect.Field; |
| 10 | +import java.lang.reflect.Proxy; |
| 11 | +import java.sql.Connection; |
| 12 | +import java.sql.PreparedStatement; |
| 13 | +import java.sql.ResultSet; |
| 14 | +import java.sql.ResultSetMetaData; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +/** |
| 19 | + * Unit tests for CyLoadTask error handling. |
| 20 | + */ |
| 21 | +public class CyLoadTaskTest { |
| 22 | + |
| 23 | + // ----------------------------------------------------------------------- |
| 24 | + // Stubs |
| 25 | + // ----------------------------------------------------------------------- |
| 26 | + |
| 27 | + /** A TaskMonitor stub that records all showMessage calls. */ |
| 28 | + private static class RecordingTaskMonitor implements TaskMonitor { |
| 29 | + final List<String> errors = new ArrayList<>(); |
| 30 | + |
| 31 | + @Override public void showMessage(Level level, String message) { |
| 32 | + if (level == Level.ERROR) errors.add(message); |
| 33 | + } |
| 34 | + @Override public void setTitle(String title) {} |
| 35 | + @Override public void setProgress(double progress) {} |
| 36 | + @Override public void setStatusMessage(String statusMessage) {} |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Builds a HanaConnectionManager stub that reports connected=true and returns |
| 41 | + * an empty ResultSet for any query (simulating no graph workspaces). |
| 42 | + */ |
| 43 | + private static HanaConnectionManager connectedManagerWithNoWorkspaces() throws Exception { |
| 44 | + // Empty ResultSet stub |
| 45 | + ResultSetMetaData emptyMeta = (ResultSetMetaData) Proxy.newProxyInstance( |
| 46 | + ClassLoader.getSystemClassLoader(), |
| 47 | + new Class[]{ResultSetMetaData.class}, |
| 48 | + (proxy, method, args) -> { |
| 49 | + if (method.getName().equals("getColumnCount")) return 0; |
| 50 | + return null; |
| 51 | + } |
| 52 | + ); |
| 53 | + ResultSet emptyRs = (ResultSet) Proxy.newProxyInstance( |
| 54 | + ClassLoader.getSystemClassLoader(), |
| 55 | + new Class[]{ResultSet.class}, |
| 56 | + (proxy, method, args) -> { |
| 57 | + if (method.getName().equals("next")) return false; |
| 58 | + if (method.getName().equals("getMetaData")) return emptyMeta; |
| 59 | + return null; |
| 60 | + } |
| 61 | + ); |
| 62 | + PreparedStatement emptyStmt = (PreparedStatement) Proxy.newProxyInstance( |
| 63 | + ClassLoader.getSystemClassLoader(), |
| 64 | + new Class[]{PreparedStatement.class}, |
| 65 | + (proxy, method, args) -> { |
| 66 | + if (method.getName().equals("executeQuery")) return emptyRs; |
| 67 | + if (method.getName().equals("getResultSet")) return emptyRs; |
| 68 | + return null; |
| 69 | + } |
| 70 | + ); |
| 71 | + Connection conn = (Connection) Proxy.newProxyInstance( |
| 72 | + ClassLoader.getSystemClassLoader(), |
| 73 | + new Class[]{Connection.class}, |
| 74 | + (proxy, method, args) -> { |
| 75 | + switch (method.getName()) { |
| 76 | + case "isValid": return true; |
| 77 | + case "isClosed": return false; |
| 78 | + case "prepareStatement": return emptyStmt; |
| 79 | + default: return null; |
| 80 | + } |
| 81 | + } |
| 82 | + ); |
| 83 | + |
| 84 | + HanaConnectionManager manager = new HanaConnectionManager(); |
| 85 | + Field f = HanaConnectionManager.class.getDeclaredField("connection"); |
| 86 | + f.setAccessible(true); |
| 87 | + f.set(manager, conn); |
| 88 | + return manager; |
| 89 | + } |
| 90 | + |
| 91 | + private static CyNetworkFactory dummyNetworkFactory() { |
| 92 | + return (CyNetworkFactory) Proxy.newProxyInstance( |
| 93 | + ClassLoader.getSystemClassLoader(), |
| 94 | + new Class[]{CyNetworkFactory.class}, |
| 95 | + (proxy, method, args) -> null |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + private static CyNetworkManager dummyNetworkManager() { |
| 100 | + return (CyNetworkManager) Proxy.newProxyInstance( |
| 101 | + ClassLoader.getSystemClassLoader(), |
| 102 | + new Class[]{CyNetworkManager.class}, |
| 103 | + (proxy, method, args) -> null |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + // ----------------------------------------------------------------------- |
| 108 | + // Tests |
| 109 | + // ----------------------------------------------------------------------- |
| 110 | + |
| 111 | + /** |
| 112 | + * Regression test: when the connected HANA instance has no valid graph workspaces, |
| 113 | + * run() must show a friendly error message and return — not throw a NullPointerException |
| 114 | + * or a cryptic HanaConnectionManagerException. |
| 115 | + */ |
| 116 | + @Test |
| 117 | + public void testRunShowsErrorWhenNoWorkspacesExist() throws Exception { |
| 118 | + HanaConnectionManager manager = connectedManagerWithNoWorkspaces(); |
| 119 | + CyLoadTask task = new CyLoadTask(dummyNetworkFactory(), dummyNetworkManager(), manager); |
| 120 | + |
| 121 | + RecordingTaskMonitor monitor = new RecordingTaskMonitor(); |
| 122 | + task.run(monitor); |
| 123 | + |
| 124 | + Assert.assertFalse("Expected at least one ERROR message", monitor.errors.isEmpty()); |
| 125 | + String errorMsg = monitor.errors.get(0).toLowerCase(); |
| 126 | + Assert.assertTrue( |
| 127 | + "Error message should mention 'graph workspace'", |
| 128 | + errorMsg.contains("graph workspace") |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
0 commit comments