Skip to content

Commit 042585a

Browse files
committed
Add bulk conversion tests for SQL annotations and files
1 parent 54af869 commit 042585a

29 files changed

+554
-12
lines changed

src/main/kotlin/org/domaframework/doma/intellij/action/dao/SqlAnnotationConverter.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import com.intellij.psi.PsiMethod
3030
import com.intellij.psi.PsiNameValuePair
3131
import com.intellij.psi.codeStyle.CodeStyleManager
3232
import com.intellij.psi.codeStyle.JavaCodeStyleManager
33+
import com.intellij.testFramework.LightVirtualFile
3334
import org.domaframework.doma.intellij.common.dao.jumpToDaoMethod
3435
import org.domaframework.doma.intellij.common.psi.PsiDaoMethod
3536
import org.domaframework.doma.intellij.common.util.StringUtil
@@ -88,6 +89,7 @@ class SqlAnnotationConverter(
8889
setSqlFileOption(targetAnnotation, false)
8990
addSqlAnnotation(sqlContent)
9091

92+
if(sqlFile is LightVirtualFile) return
9193
deleteSqlFile(sqlFile)
9294
}
9395

@@ -292,7 +294,6 @@ class SqlAnnotationConverter(
292294
if (editorManager.isFileOpen(virtualFile)) {
293295
editorManager.closeFile(virtualFile)
294296
}
295-
296297
virtualFile.delete(null)
297298
}
298299

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright Doma Tools Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.domaframework.doma.intellij.action.dao
17+
18+
import com.intellij.openapi.fileEditor.FileEditorManager
19+
import org.domaframework.doma.intellij.bundle.MessageBundle
20+
21+
class BulkConvertSqlAnnotationToFileActionTest : ConvertSqlActionTest() {
22+
private val sqlConversionPackage = "sqltofile/bulk"
23+
private val convertActionName = MessageBundle.message("bulk.convert.sql.annotation.to.file.text")
24+
private val convertFamilyName = MessageBundle.message("bulk.convert.sql.annotation.to.file.family")
25+
26+
fun testBulkConvertAnnotationToSqlFile() {
27+
val daoName = "BulkConvertToSqlFileDao"
28+
val targetSqlFileNames =
29+
listOf(
30+
"selectEmployee.sql",
31+
"insertEmployee.sql",
32+
"updateEmployee.sql",
33+
"deleteEmployee.sql",
34+
"batchInsertEmployees.sql",
35+
"batchUpdateEmployees.sql",
36+
"batchDeleteEmployees.sql",
37+
"createTables.script",
38+
"processData.sql",
39+
)
40+
doTest(daoName, targetSqlFileNames)
41+
}
42+
43+
private fun doTest(
44+
daoName: String,
45+
targetMethods: List<String>,
46+
) {
47+
doConvertAction(
48+
daoName,
49+
convertFamilyName,
50+
sqlConversionPackage,
51+
convertActionName,
52+
)
53+
54+
targetMethods.forEach { file ->
55+
val generatedSql = findSqlFile("$sqlConversionPackage/$daoName/$file")
56+
if (generatedSql == null) {
57+
fail("Not Found SQL File [$file]")
58+
return
59+
}
60+
val extension = generatedSql.extension == "script"
61+
val fileNameWithoutExtension = generatedSql.nameWithoutExtension
62+
doTestSqlFormat(daoName, fileNameWithoutExtension, sqlConversionPackage, extension)
63+
}
64+
65+
// Test SQL File Generation
66+
val openedEditor = FileEditorManager.getInstance(project).selectedEditors
67+
val openSqlFile = openedEditor.find { it.file.extension in listOf("sql", "script") }
68+
assertFalse(
69+
"Open File is SQL File [${openSqlFile?.file?.name}]",
70+
openSqlFile == null,
71+
)
72+
}
73+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright Doma Tools Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.domaframework.doma.intellij.action.dao
17+
18+
import org.domaframework.doma.intellij.bundle.MessageBundle
19+
20+
class BulkConvertSqlFileToAnnotationActionTest : ConvertSqlActionTest() {
21+
private val sqlConversionPackage = "sqltoannotation/bulk"
22+
private val convertFamilyName = MessageBundle.message("bulk.convert.sql.file.to.annotation.family")
23+
24+
fun testBulkConvertToSqlAnnotation() {
25+
val daoName = "BulkConvertToSqlAnnotationDao"
26+
val targetSqlFileNames =
27+
listOf(
28+
"generateSqlFile.sql",
29+
"insertEmployee.sql",
30+
"updateEmployee.sql",
31+
"deleteEmployee.sql",
32+
"batchInsertEmployees.sql",
33+
"batchUpdateEmployees.sql",
34+
"batchDeleteEmployees.sql",
35+
"createTables.script",
36+
"processData.sql",
37+
)
38+
doTest(daoName, targetSqlFileNames)
39+
}
40+
41+
private fun doTest(
42+
daoName: String,
43+
targetSqlFileNames: List<String>,
44+
) {
45+
targetSqlFileNames.forEach { file ->
46+
addSqlFile( "$sqlConversionPackage/$daoName/$file")
47+
}
48+
doAvailableConvertActionTest(
49+
daoName,
50+
sqlConversionPackage,
51+
convertFamilyName,
52+
)
53+
// Test SQL File Removed
54+
targetSqlFileNames.forEach { file ->
55+
val generatedSql = findSqlFile("$sqlConversionPackage/$daoName/$file")
56+
assertNull("SQL File [$file] should exists ", generatedSql)
57+
}
58+
}
59+
}

src/test/kotlin/org/domaframework/doma/intellij/action/dao/ConvertSqlActionTest.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import com.intellij.openapi.fileEditor.FileDocumentManager
1919
import com.intellij.openapi.fileEditor.FileEditorManager
2020
import com.intellij.psi.PsiDocumentManager
2121
import org.domaframework.doma.intellij.DomaSqlTest
22-
import kotlin.reflect.KClass
2322

2423
abstract class ConvertSqlActionTest : DomaSqlTest() {
2524
protected fun doConvertAction(
@@ -45,20 +44,25 @@ abstract class ConvertSqlActionTest : DomaSqlTest() {
4544
myFixture.checkResultByFile("java/doma/example/dao/$sqlConversionPackage/$daoName.after.java")
4645
}
4746

48-
protected fun <T : AbstractConvertSqlFileToAnnotationAction> doConvertActionTest(
47+
/**
48+
* Execute the intention action with the specified family name.
49+
*/
50+
protected fun doAvailableConvertActionTest(
4951
daoName: String,
5052
sqlToAnnotationPackage: String,
5153
convertFamilyName: String,
52-
convertActionClass: KClass<T>,
5354
) {
5455
addDaoJavaFile("$sqlToAnnotationPackage/$daoName.java")
5556
val daoClass = findDaoClass("$sqlToAnnotationPackage.$daoName")
5657
myFixture.configureFromExistingVirtualFile(daoClass.containingFile.virtualFile)
5758

5859
val intentions = myFixture.availableIntentions
59-
val convertIntention = intentions.find { convertActionClass.java.isInstance(it) }
60-
61-
assertNull("$convertFamilyName intention should NOT be available without @Sql annotation", convertIntention)
60+
val convertIntention = intentions.find { it.familyName == convertFamilyName }
61+
if (convertIntention == null) {
62+
fail("[$convertFamilyName] intention should NOT be available without @Sql annotation")
63+
return
64+
}
65+
convertIntention.invoke(myFixture.project, myFixture.editor, myFixture.file)
6266
}
6367

6468
protected fun doTestSqlFormat(

src/test/kotlin/org/domaframework/doma/intellij/action/dao/ConvertSqlAnnotationToFileActionTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ class ConvertSqlAnnotationToFileActionTest : ConvertSqlActionTest() {
110110

111111
fun testIntentionNotAvailableForMethodWithoutSqlAnnotation() {
112112
val daoName = "NoSqlAnnotationDao"
113-
doConvertActionTest(daoName, sqlConversionPackage, convertFamilyName, ConvertSqlFileToAnnotationFromDaoAction::class)
113+
doAvailableConvertActionTest(daoName, sqlConversionPackage, convertFamilyName)
114114
}
115115

116116
fun testIntentionNotAvailableForUnsupportedAnnotation() {
117117
val daoName = "UnsupportedAnnotationDao"
118-
doConvertActionTest(daoName, sqlConversionPackage, convertFamilyName, ConvertSqlFileToAnnotationFromDaoAction::class)
118+
doAvailableConvertActionTest(daoName, sqlConversionPackage, convertFamilyName)
119119
}
120120

121121
private fun doTest(

src/test/kotlin/org/domaframework/doma/intellij/action/dao/ConvertSqlFileToAnnotationActionTest.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,16 @@ class ConvertSqlFileToAnnotationActionTest : ConvertSqlActionTest() {
118118
fun testSelectWithSqlFileConvertAnnotation() {
119119
val daoName = "SelectWithSqlFileConvertAnnotationDao"
120120
val sqlFileName = "selectEmployee.sql"
121-
doConvertActionTest(
121+
addSqlFile("$sqlToAnnotationPackage/$daoName/$sqlFileName")
122+
doAvailableConvertActionTest(
122123
daoName,
123124
sqlToAnnotationPackage,
124125
convertFamilyName,
125-
ConvertSqlFileToAnnotationFromDaoAction::class,
126126
)
127+
127128
// Test SQL File Removed
128129
val generatedSql = findSqlFile("$sqlToAnnotationPackage/$daoName/$sqlFileName")
129-
assertTrue("SQL File [$sqlFileName] should exists ", generatedSql == null)
130+
assertNull("SQL File [$sqlFileName] should exists ", generatedSql)
130131
}
131132

132133
private fun doTest(
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package doma.example.dao.sqltoannotation.bulk;
2+
3+
import doma.example.entity.Employee;
4+
import doma.example.entity.Project;
5+
import java.util.List;
6+
import java.util.function.BiFunction;
7+
import org.seasar.doma.BatchDelete;
8+
import org.seasar.doma.BatchInsert;
9+
import org.seasar.doma.BatchUpdate;
10+
import org.seasar.doma.Dao;
11+
import org.seasar.doma.Delete;
12+
import org.seasar.doma.Function;
13+
import org.seasar.doma.Insert;
14+
import org.seasar.doma.Script;
15+
import org.seasar.doma.Select;
16+
import org.seasar.doma.Sql;
17+
import org.seasar.doma.SqlProcessor;
18+
import org.seasar.doma.Update;
19+
import org.seasar.doma.jdbc.Config;
20+
import org.seasar.doma.jdbc.PreparedSql;
21+
22+
/**
23+
* Bulk annotation type conversion test (SQL file ⇒ Sql annotation)
24+
*/
25+
@Dao
26+
public interface BulkConvertToSqlAnnotationDao {
27+
28+
@Select
29+
@Sql("select * from employee where id = /* id */1")
30+
String generateSqlFile(Employee employee, Project project);
31+
32+
/**
33+
* Confirm that formatting is performed after the action
34+
*
35+
* @param id
36+
* @return
37+
*/
38+
@Select
39+
@Sql("""
40+
SELECT *
41+
FROM employee WHERE id = /* id */1
42+
""")
43+
Employee selectEmployee(int id);
44+
45+
@Insert
46+
@Sql("""
47+
INSERT INTO employee(id, name)
48+
VALUES ( /* employee.employeeId */0, /* employee.userName */'name' )""")
49+
int insertEmployee(Employee employee);
50+
51+
@Update
52+
@Sql("""
53+
UPDATE employee
54+
SET name = /* employee.employeeName */'John', rank = /* employee.rank */30
55+
WHERE id = /* employee.managerId */1
56+
""")
57+
int updateEmployee(Employee employee);
58+
59+
@Delete
60+
@Sql("""
61+
DELETE
62+
FROM employee
63+
WHERE id = /* employee.managerId */1
64+
""")
65+
int deleteEmployee(Employee employee);
66+
67+
@BatchInsert
68+
@Sql("""
69+
INSERT INTO employee
70+
(id, name, age)
71+
VALUES ( /* employees.employeeId */1, /* employees.employeeName */'John', /* employees.rank */30 )
72+
""")
73+
int[] batchInsertEmployees(List<Employee> employees);
74+
75+
@BatchUpdate
76+
@Sql("""
77+
UPDATE employee
78+
SET name = /* employees.employeeName */'John', rank = /* employees.rank */30
79+
WHERE id = /* employees.employeeId */1
80+
""")
81+
int[] batchUpdateEmployees(List<Employee> employees);
82+
83+
@BatchDelete
84+
@Sql("""
85+
DELETE FROM employee WHERE id = /* employees.employeeId */1
86+
""")
87+
int[] batchDeleteEmployees(List<Employee> employees);
88+
89+
@Script
90+
@Sql("""
91+
CREATE TABLE employee
92+
( id INTEGER PRIMARY KEY, name VARCHAR(100), age INTEGER) ;
93+
CREATE INDEX idx_employee_name ON employee(name) ;
94+
""")
95+
void createTables();
96+
97+
@SqlProcessor
98+
@Sql("""
99+
SELECT count(*) FROM employee WHERE age > 25
100+
""")
101+
<R> R processData(BiFunction<Config, PreparedSql, R> processor);
102+
103+
// Annotation not supported
104+
@Function
105+
void executeFunc();
106+
}

0 commit comments

Comments
 (0)