From 8ac1ef8733955a38200937e3c85b2c1adfb6086e Mon Sep 17 00:00:00 2001 From: krihy Date: Mon, 4 Sep 2023 19:18:27 +0800 Subject: [PATCH] 1.format code --- .../OnlineSchemaChangeValidatorTest.java | 8 +- .../ddl/TableNameReplacerTest.java | 73 +++++++++++++++++++ 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/server/integration-test/src/test/java/com/oceanbase/odc/service/onlineschemachange/OnlineSchemaChangeValidatorTest.java b/server/integration-test/src/test/java/com/oceanbase/odc/service/onlineschemachange/OnlineSchemaChangeValidatorTest.java index a2c6f28adf..8dd9d0f181 100644 --- a/server/integration-test/src/test/java/com/oceanbase/odc/service/onlineschemachange/OnlineSchemaChangeValidatorTest.java +++ b/server/integration-test/src/test/java/com/oceanbase/odc/service/onlineschemachange/OnlineSchemaChangeValidatorTest.java @@ -16,12 +16,12 @@ package com.oceanbase.odc.service.onlineschemachange; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.util.Assert; import com.oceanbase.odc.ServiceTestEnv; import com.oceanbase.odc.TestConnectionUtil; @@ -31,7 +31,6 @@ import com.oceanbase.odc.core.shared.constant.ConnectType; import com.oceanbase.odc.core.shared.constant.ErrorCodes; import com.oceanbase.odc.core.shared.exception.BadArgumentException; -import com.oceanbase.odc.core.shared.exception.HttpException; import com.oceanbase.odc.service.connection.ConnectionService; import com.oceanbase.odc.service.connection.model.ConnectionConfig; import com.oceanbase.odc.service.flow.model.CreateFlowInstanceReq; @@ -110,9 +109,8 @@ public void test_Validate_Invalid_Sql() { validService.validate(getCreateRequest( sql, OnlineSchemaChangeSqlType.CREATE)); - } catch (Exception ex) { - Assert.isTrue(ex instanceof BadArgumentException); - Assert.isTrue(((HttpException) ex).getErrorCode() == ErrorCodes.ObPreCheckDdlFailed); + } catch (BadArgumentException ex) { + Assert.assertSame(ex.getErrorCode(), ErrorCodes.ObPreCheckDdlFailed); throw ex; } } diff --git a/server/integration-test/src/test/java/com/oceanbase/odc/service/onlineschemachange/ddl/TableNameReplacerTest.java b/server/integration-test/src/test/java/com/oceanbase/odc/service/onlineschemachange/ddl/TableNameReplacerTest.java index 293394fed8..51ab35d4a2 100644 --- a/server/integration-test/src/test/java/com/oceanbase/odc/service/onlineschemachange/ddl/TableNameReplacerTest.java +++ b/server/integration-test/src/test/java/com/oceanbase/odc/service/onlineschemachange/ddl/TableNameReplacerTest.java @@ -15,9 +15,20 @@ */ package com.oceanbase.odc.service.onlineschemachange.ddl; +import java.io.StringReader; +import java.util.List; +import java.util.Optional; + import org.junit.Assert; import org.junit.Test; +import com.oceanbase.tools.sqlparser.OBMySQLParser; +import com.oceanbase.tools.sqlparser.OBOracleSQLParser; +import com.oceanbase.tools.sqlparser.statement.Statement; +import com.oceanbase.tools.sqlparser.statement.createtable.CreateTable; +import com.oceanbase.tools.sqlparser.statement.createtable.OutOfLineConstraint; +import com.oceanbase.tools.sqlparser.statement.createtable.OutOfLineForeignConstraint; + public class TableNameReplacerTest { private static final String CREATE_STMT = "create table t1 (id int);"; private static final String CREATE_QUOTE_STMT = "create table \"t1\" (id int);"; @@ -44,6 +55,68 @@ public void test_RewriteCreateStmt_Oracle() { Assert.assertEquals("create table t1_osc_new_ (id int);", newSql); } + @Test + public void test_RewriteCreateStmtWittConstraint_Oracle() { + String createSql = "CREATE TABLE CHILD_TABLE1 (\n" + + "COL NUMBER NOT NULL,\n" + + "COL1 NUMBER NOT NULL,\n" + + "CONSTRAINT P1 PRIMARY KEY (COL),\n" + + "CONSTRAINT U1 UNIQUE (COL1),\n" + + "CONSTRAINT F1 FOREIGN KEY (COL) REFERENCES PARENT_TABLE1 (COL) ON DELETE CASCADE \n" + + ")"; + String newSql = new OBOracleTableNameReplacer().replaceCreateStmt(createSql, "CHILD_TABLE_NEW"); + Statement statement = new OBOracleSQLParser().parse(new StringReader(newSql)); + Assert.assertTrue(statement instanceof CreateTable); + CreateTable createTable = (CreateTable) statement; + List constraints = createTable.getConstraints(); + Optional pk = constraints.stream().filter(OutOfLineConstraint::isPrimaryKey).findFirst(); + Assert.assertTrue(pk.isPresent()); + Assert.assertNotEquals("P1", pk.get().getConstraintName()); + + Optional uk = constraints.stream().filter(OutOfLineConstraint::isUniqueKey).findFirst(); + Assert.assertTrue(uk.isPresent()); + Assert.assertNotEquals("U1", uk.get().getConstraintName()); + + Optional fk = + constraints.stream().filter(c -> (c instanceof OutOfLineForeignConstraint)) + .map(c -> (OutOfLineForeignConstraint) c).findFirst(); + Assert.assertTrue(fk.isPresent()); + Assert.assertNotEquals("F1", pk.get().getConstraintName()); + } + + + + @Test + public void test_RewriteCreateStmtWittConstraint_MySql() { + String createSql = "CREATE TABLE `child_table1` (\n" + + "`col` int NOT NULL,\n" + + "`col1` int NOT NULL,\n" + + "CONSTRAINT `p1` PRIMARY KEY (`col`),\n" + + "CONSTRAINT `u1` UNIQUE (`col`),\n" + + "UNIQUE (`col`),\n" + + "CONSTRAINT `f1` FOREIGN KEY (`col`) REFERENCES `parent_table1` (`col`) ON DELETE CASCADE ON " + + "UPDATE NO ACTION\n" + + ")\n"; + String newSql = new OBMysqlTableNameReplacer().replaceCreateStmt(createSql, "`child_table1_new`"); + Statement statement = new OBMySQLParser().parse(new StringReader(newSql)); + Assert.assertTrue(statement instanceof CreateTable); + CreateTable createTable = (CreateTable) statement; + List constraints = createTable.getConstraints(); + Optional pk = constraints.stream().filter(OutOfLineConstraint::isPrimaryKey).findFirst(); + Assert.assertTrue(pk.isPresent()); + Assert.assertEquals("`p1`", pk.get().getConstraintName()); + + Optional uk = constraints.stream().filter(OutOfLineConstraint::isUniqueKey).findFirst(); + Assert.assertTrue(uk.isPresent()); + Assert.assertEquals("`u1`", uk.get().getConstraintName()); + + Optional fk = + constraints.stream().filter(c -> (c instanceof OutOfLineForeignConstraint)) + .map(c -> (OutOfLineForeignConstraint) c).findFirst(); + Assert.assertTrue(fk.isPresent()); + Assert.assertNotEquals("`f1`", pk.get().getConstraintName()); + } + @Test public void test_RewriteCreateStmtWithQuote_Oracle() { String newSql = new OBOracleTableNameReplacer().replaceCreateStmt(CREATE_QUOTE_STMT,