rmannibucau commented on code in PR #8787:
URL: https://github.com/apache/hop/pull/8787#discussion_r4212242178


##########
plugins/transforms/tableinput/src/test/java/org/apache/hop/pipeline/transforms/tableinput/TableInputTest.java:
##########
@@ -0,0 +1,262 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hop.pipeline.transforms.tableinput;
+
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.ArgumentMatchers.notNull;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyInt;
+import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.UUID;
+import org.apache.hop.core.HopClientEnvironment;
+import org.apache.hop.core.database.Database;
+import org.apache.hop.core.database.DatabaseMeta;
+import org.apache.hop.core.row.IRowMeta;
+import org.apache.hop.core.row.RowMeta;
+import org.apache.hop.core.row.value.ValueMetaString;
+import org.apache.hop.core.variables.Variables;
+import org.apache.hop.databases.h2.H2DatabaseMeta;
+import org.apache.hop.metadata.serializer.memory.MemoryMetadataProvider;
+import org.apache.hop.pipeline.PipelineMeta;
+import org.apache.hop.pipeline.engines.local.LocalPipelineEngine;
+import org.apache.hop.pipeline.transform.TransformMeta;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit tests for the Table Input query/parameter path.
+ *
+ * <p>These tests reproduce the regression where a hop into a Table Input 
whose SQL has no
+ * placeholders was still consumed as parameter rows in 2.20 (Issue #2722 / PR 
#8034): the incoming
+ * row was bound to a PreparedStatement without parameters, which fails on 
Oracle with ORA-17003 and
+ * on H2 with "Parameter index out of range".
+ *
+ * <p>The fix keeps the 2.20 "optional lookup" feature (collect parameter rows 
from any incoming
+ * hop, named parameters) while restoring the legacy behavior for SQL without 
placeholders: no
+ * collection, no bind, and the incoming hops are still drained so the 
sequence (header transform
+ * then extraction) keeps working.
+ */
+class TableInputTest {
+
+  private static final String CONNECTION_NAME = "table-input-test";
+
+  private Variables variables;
+  private TableInput tableInput;
+  private TableInputMeta meta;
+  private TableInputData data;
+  private Database db;
+
+  @BeforeAll
+  static void initHop() throws Exception {
+    HopClientEnvironment.init();
+    org.apache.hop.core.database.DatabasePluginType.getInstance()
+        .registerClassPathPlugin(H2DatabaseMeta.class);
+  }
+
+  @BeforeEach
+  void setUp() throws Exception {
+    variables = new Variables();
+
+    DatabaseMeta databaseMeta = connectionTo("mem:" + UUID.randomUUID());
+    // Force the H2 driver to load and validate the connection name before the 
transform uses it.
+    try (Database ignored =
+        new Database(
+            new org.apache.hop.core.logging.LoggingObject("TableInputTest"),
+            variables,
+            databaseMeta)) {
+      // connecting is enough
+    }
+
+    meta = new TableInputMeta();
+    meta.setConnection(CONNECTION_NAME);
+    meta.setUseNamedParameters(false);
+    meta.setSql("SELECT 1");
+
+    data = new TableInputData();
+    db = mock(Database.class);
+    data.db = db;
+    doReturn(new RowMeta()).when(db).getReturnRowMeta();
+    doReturn(null).when(db).getRow(any(ResultSet.class));
+    doReturn(databaseMeta).when(db).getDatabaseMeta();
+    doReturn(mock(ResultSet.class))
+        .when(db)
+        .openQuery(anyString(), any(IRowMeta.class), any(Object[].class), 
anyInt(), anyBoolean());
+
+    PipelineMeta pipelineMeta = new PipelineMeta();
+    MemoryMetadataProvider metadataProvider = new MemoryMetadataProvider();
+    metadataProvider.getSerializer(DatabaseMeta.class).save(databaseMeta);
+    pipelineMeta.setMetadataProvider(metadataProvider);
+    TransformMeta transformMeta = new TransformMeta("table input", meta);
+    pipelineMeta.addTransform(transformMeta);
+    // dispatch() in the BaseTransform constructor walks previous/next 
transforms; none exist here.
+
+    tableInput =
+        spy(
+            new TableInput(
+                transformMeta, meta, data, 1, pipelineMeta, spy(new 
LocalPipelineEngine())));
+    doReturn(transformMeta).when(tableInput).getTransformMeta();
+    doReturn(false).when(tableInput).isRowLevel();
+    doReturn(false).when(tableInput).isDebug();
+    doNothing().when(tableInput).logDetailed(any());
+    doNothing().when(tableInput).logBasic(any());
+    doNothing().when(tableInput).logDebug(any());
+    doNothing().when(tableInput).logError(any());
+    // doQuery() reads through data.db; make the query "open" successfully 
with no rows.
+    doReturn(mock(ResultSet.class))
+        .when(db)
+        .openQuery(anyString(), any(), any(), anyInt(), anyBoolean());

Review Comment:
   updated the test but small side note: `any()` *does* match `null` (not the 
other signature)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to