add unit test

This commit is contained in:
XudongXie 2024-09-20 12:57:18 +08:00
parent 9cd1066f07
commit acaf7f1214
7 changed files with 204 additions and 48 deletions

View File

@ -5,10 +5,10 @@
</component>
<component name="ChangeListManager">
<list default="true" id="d8d12963-4777-48e3-9669-309cf5e41d60" name="更改" comment="add unit test">
<change afterPath="$PROJECT_DIR$/src/test/java/testCreateTable.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/testDelete.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/testDropTable.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/testException.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/parser/OracleParser.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/parser/OracleParser.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/testCommitRollbackExec.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/testCommitRollbackExec.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -48,6 +48,9 @@
"JUnit.testCommitRollbackExec.test.executor": "Run",
"JUnit.testCreateTable.executor": "Run",
"JUnit.testCreateTable.test.executor": "Run",
"JUnit.testDelete.test.executor": "Run",
"JUnit.testDropTable.test.executor": "Run",
"JUnit.testException.executor": "Debug",
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"git-widget-placeholder": "master",
@ -59,34 +62,7 @@
"settings.editor.selected.configurable": "preferences.pluginManager"
}
}]]></component>
<component name="RunManager" selected="JUnit.testCreateTable">
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Main" />
<module name="sqlTranslate" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="testAlterTable.testAlterTable" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="sqlTranslate" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="testAlterTable" />
<option name="METHOD_NAME" value="testAlterTable" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="testCommitRollbackExec.test" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="sqlTranslate" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="testCommitRollbackExec" />
<option name="METHOD_NAME" value="test" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<component name="RunManager" selected="JUnit.testException.test">
<configuration name="testCreateTable" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="sqlTranslate" />
<option name="PACKAGE_NAME" value="" />
@ -96,10 +72,39 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="testCreateTable.test" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<configuration name="testDelete.test" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="sqlTranslate" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="testCreateTable" />
<option name="MAIN_CLASS_NAME" value="testDelete" />
<option name="METHOD_NAME" value="test" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="testDropTable.test" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="sqlTranslate" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="testDropTable" />
<option name="METHOD_NAME" value="test" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="testException" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="sqlTranslate" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="testException" />
<option name="TEST_OBJECT" value="class" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="testException.test" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="sqlTranslate" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="testException" />
<option name="METHOD_NAME" value="test" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
@ -107,19 +112,19 @@
</method>
</configuration>
<list>
<item itemvalue="Application.Main" />
<item itemvalue="JUnit.testAlterTable.testAlterTable" />
<item itemvalue="JUnit.testCommitRollbackExec.test" />
<item itemvalue="JUnit.testCreateTable" />
<item itemvalue="JUnit.testCreateTable.test" />
<item itemvalue="JUnit.testDelete.test" />
<item itemvalue="JUnit.testDropTable.test" />
<item itemvalue="JUnit.testException" />
<item itemvalue="JUnit.testException.test" />
</list>
<recent_temporary>
<list>
<item itemvalue="JUnit.testException.test" />
<item itemvalue="JUnit.testException" />
<item itemvalue="JUnit.testDropTable.test" />
<item itemvalue="JUnit.testDelete.test" />
<item itemvalue="JUnit.testCreateTable" />
<item itemvalue="JUnit.testCreateTable.test" />
<item itemvalue="JUnit.testCommitRollbackExec.test" />
<item itemvalue="JUnit.testAlterTable.testAlterTable" />
<item itemvalue="Application.Main" />
</list>
</recent_temporary>
</component>
@ -475,7 +480,7 @@
<option name="project" value="LOCAL" />
<updated>1726633703475</updated>
</task>
<option name="localTasksCounter" value="98" />
<option name="localTasksCounter" value="99" />
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">

View File

@ -23,7 +23,7 @@ public class testAlterTable {
testSQL.add("ALTER TABLE employees ADD CONSTRAINT emp_pk PRIMARY KEY (employee_id);");
testSQL.add("ALTER TABLE employees DROP CONSTRAINT emp_pk;");
testSQL.add("ALTER TABLE employees RENAME TO staff;");
System.out.println("===== test of the alter table =====");
System.out.println("===== test of alter table =====");
System.out.println("The source DBMS is: " + CommonConfig.getSourceDB());
System.out.println("The target DBMS is: " + CommonConfig.getTargetDB());
System.out.println();
@ -34,7 +34,7 @@ public class testAlterTable {
{
int num = 1;
for (String sql : testSQL) {
System.out.println("===== test of the SQL" + num++ + " =====");
System.out.println("===== test the SQL" + num++ + " =====");
System.out.println("Input SQL: " + sql);
OracleLexer lexer = new OracleLexer(sql);
lexer.printTokens();

View File

@ -24,7 +24,7 @@ public class testCommitRollbackExec {
" INSERT INTO my_table (id, value) VALUES (1, 'test');\n" +
" COMMIT;\n" +
"END;");
System.out.println("===== test of the alter table =====");
System.out.println("===== test of Commit, RollbackExec =====");
System.out.println("The source DBMS is: " + CommonConfig.getSourceDB());
System.out.println("The target DBMS is: " + CommonConfig.getTargetDB());
System.out.println();
@ -35,7 +35,7 @@ public class testCommitRollbackExec {
{
int num = 1;
for (String sql : testSQL) {
System.out.println("===== test of the SQL" + num++ + " =====");
System.out.println("===== test the SQL" + num++ + " =====");
System.out.println("Input SQL: " + sql);
OracleLexer lexer = new OracleLexer(sql);
lexer.printTokens();

View File

@ -22,7 +22,7 @@ public class testCreateTable {
" hire_date DATE,\n" +
" CONSTRAINT chk_example CHECK (employee_id > 0)" +
");");
System.out.println("===== test of the alter table =====");
System.out.println("===== test of Create table =====");
System.out.println("The source DBMS is: " + CommonConfig.getSourceDB());
System.out.println("The target DBMS is: " + CommonConfig.getTargetDB());
System.out.println();
@ -33,7 +33,7 @@ public class testCreateTable {
{
int num = 1;
for (String sql : testSQL) {
System.out.println("===== test of the SQL" + num++ + " =====");
System.out.println("===== test the SQL" + num++ + " =====");
System.out.println("Input SQL: " + sql);
OracleLexer lexer = new OracleLexer(sql);
lexer.printTokens();

View File

@ -0,0 +1,51 @@
import config.CommonConfig;
import generator.OpenGaussGenerator;
import lexer.OracleLexer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import parser.OracleParser;
import parser.ast.ASTNode;
import java.util.ArrayList;
import java.util.List;
public class testDelete {
List<String> testSQL = new ArrayList<>();
@BeforeEach
public void loadData()
{
testSQL.add("DELETE FROM employees e\n" +
"WHERE e.department_id IN (\n" +
" SELECT d.department_id\n" +
" FROM departments d\n" +
" WHERE d.department_name = 'Sales'\n" +
");");
System.out.println("===== test of Delete =====");
System.out.println("The source DBMS is: " + CommonConfig.getSourceDB());
System.out.println("The target DBMS is: " + CommonConfig.getTargetDB());
System.out.println();
}
@Test
public void test()
{
int num = 1;
for (String sql : testSQL) {
System.out.println("===== test the SQL" + num++ + " =====");
System.out.println("Input SQL: " + sql);
OracleLexer lexer = new OracleLexer(sql);
lexer.printTokens();
OracleParser parser = new OracleParser(lexer);
ASTNode root = parser.parse();
System.out.println("The AST of the input SQL: ");
System.out.println(root.getASTString());
System.out.println("The query String of the AST parsed from the input SQL: ");
System.out.println(root.toQueryString());
OpenGaussGenerator generator = new OpenGaussGenerator(root);
System.out.println("The converted query String: ");
System.out.println(generator.generate());
System.out.println();
}
}
}

View File

@ -0,0 +1,47 @@
import config.CommonConfig;
import generator.OpenGaussGenerator;
import lexer.OracleLexer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import parser.OracleParser;
import parser.ast.ASTNode;
import java.util.ArrayList;
import java.util.List;
public class testDropTable {
List<String> testSQL = new ArrayList<>();
@BeforeEach
public void loadData()
{
testSQL.add("DROP TABLE employees CASCADE CONSTRAINTS;");
testSQL.add("DROP TABLE employees;");
System.out.println("===== test of Drop table =====");
System.out.println("The source DBMS is: " + CommonConfig.getSourceDB());
System.out.println("The target DBMS is: " + CommonConfig.getTargetDB());
System.out.println();
}
@Test
public void test()
{
int num = 1;
for (String sql : testSQL) {
System.out.println("===== test the SQL" + num++ + " =====");
System.out.println("Input SQL: " + sql);
OracleLexer lexer = new OracleLexer(sql);
lexer.printTokens();
OracleParser parser = new OracleParser(lexer);
ASTNode root = parser.parse();
System.out.println("The AST of the input SQL: ");
System.out.println(root.getASTString());
System.out.println("The query String of the AST parsed from the input SQL: ");
System.out.println(root.toQueryString());
OpenGaussGenerator generator = new OpenGaussGenerator(root);
System.out.println("The converted query String: ");
System.out.println(generator.generate());
System.out.println();
}
}
}

View File

@ -0,0 +1,53 @@
import config.CommonConfig;
import generator.OpenGaussGenerator;
import lexer.OracleLexer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import parser.OracleParser;
import parser.ast.ASTNode;
import java.util.ArrayList;
import java.util.List;
public class testException {
List<String> testSQL = new ArrayList<>();
@BeforeEach
public void loadData()
{
testSQL.add("EXCEPTION\n" +
" WHEN e_custom_exception THEN\n" +
" DBMS_OUTPUT.PUT_LINE('Caught an exception: Custom exception raised');\n" +
" WHEN ZERO_DIVIDE THEN\n" +
" DBMS_OUTPUT.PUT_LINE('Caught an exception: Division by zero');\n" +
" WHEN INVALID_NUMBER THEN\n" +
" DBMS_OUTPUT.PUT_LINE('Caught an exception: Invalid number');\n" +
" WHEN OTHERS THEN\n" +
" DBMS_OUTPUT.PUT_LINE('Caught an exception: ' || SQLERRM);");
System.out.println("===== test of EXCEPTION =====");
System.out.println("The source DBMS is: " + CommonConfig.getSourceDB());
System.out.println("The target DBMS is: " + CommonConfig.getTargetDB());
System.out.println();
}
@Test
public void test()
{
int num = 1;
for (String sql : testSQL) {
System.out.println("===== test the SQL" + num++ + " =====");
System.out.println("Input SQL: " + sql);
OracleLexer lexer = new OracleLexer(sql);
lexer.printTokens();
ASTNode root = OracleParser.parseException(lexer.getTokens());
System.out.println("The AST of the input SQL: ");
System.out.println(root.getASTString());
System.out.println("The query String of the AST parsed from the input SQL: ");
System.out.println(root.toQueryString());
OpenGaussGenerator generator = new OpenGaussGenerator(root);
System.out.println("The converted query String: ");
System.out.println(generator.generate());
System.out.println();
}
}
}