Skip to content

Commit a933ae1

Browse files
committed
Add tests for SourceNameDao SQL file generation and refactor existing tests
1 parent 371ebff commit a933ae1

File tree

4 files changed

+106
-33
lines changed

4 files changed

+106
-33
lines changed

src/test/kotlin/org/domaframework/doma/intellij/DomaSqlTest.kt

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,17 @@ open class DomaSqlTest : LightJavaCodeInsightFixtureTestCase() {
173173
packageName: String,
174174
fileName: String,
175175
) {
176-
val file = File("$testDataPath/$sourceRoot/$packagePath/$packageName/$fileName")
176+
val originalPackageName = "$packagePath/$packageName"
177+
addOtherPackageJavaFile(originalPackageName, fileName)
178+
}
179+
180+
protected fun addOtherPackageJavaFile(
181+
packageName: String,
182+
fileName: String,
183+
) {
184+
val file = File("$testDataPath/$sourceRoot/$packageName/$fileName")
177185
myFixture.addFileToProject(
178-
"main/$sourceRoot/$packagePath/$packageName/$fileName",
186+
"main/$sourceRoot/$packageName/$fileName",
179187
file.readText(),
180188
)
181189
}
@@ -207,18 +215,29 @@ open class DomaSqlTest : LightJavaCodeInsightFixtureTestCase() {
207215
}
208216
}
209217

210-
fun findSqlFile(sqlName: String): VirtualFile? {
218+
fun findSqlFile(sqlName: String): VirtualFile? = findSqlFile(packagePath, sqlName)
219+
220+
fun findSqlFile(
221+
packageName: String,
222+
sqlName: String,
223+
): VirtualFile? {
211224
val module = myFixture.module
225+
val sqlFileName = "$RESOURCES_META_INF_PATH/$packageName/dao/$sqlName"
212226
return module?.getResourcesSQLFile(
213-
"$RESOURCES_META_INF_PATH/$packagePath/dao/$sqlName",
227+
sqlFileName,
214228
false,
215229
)
216230
}
217231

218-
fun findDaoClass(testDaoName: String): PsiClass {
219-
val daoName = testDaoName.replace("/", ".")
220-
val dao = myFixture.findClass("doma.example.dao.$daoName")
221-
assertNotNull("Not Found [$testDaoName]", dao)
232+
fun findDaoClass(testDaoName: String): PsiClass = findDaoClass("doma.example.dao", testDaoName)
233+
234+
fun findDaoClass(
235+
packageName: String,
236+
testDaoName: String,
237+
): PsiClass {
238+
val daoName = "$packageName.$testDaoName".replace("/", ".")
239+
val dao = myFixture.findClass(daoName)
240+
assertNotNull("Not Found [$daoName]", dao)
222241
return dao
223242
}
224243

src/test/kotlin/org/domaframework/doma/intellij/inspection/dao/DomaSqlQuickFixTest.kt

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.domaframework.doma.intellij.inspection.dao
1717

18+
import com.intellij.openapi.vfs.VirtualFile
1819
import org.domaframework.doma.intellij.DomaSqlTest
1920
import org.domaframework.doma.intellij.bundle.MessageBundle
2021
import org.domaframework.doma.intellij.inspection.dao.inspector.SqlFileExistInspection
@@ -39,6 +40,8 @@ class DomaSqlQuickFixTest : DomaSqlTest() {
3940
"$packagename/SqlProcessorQuickFixTestDao.java",
4041
)
4142

43+
addOtherPackageJavaFile("doma/java/dao", "SourceNameDao.java")
44+
4245
addResourceEmptySqlFile(
4346
"$packagename/SelectQuickFixTestDao/existsSQLFile.sql",
4447
"$packagename/InsertQuickFixTestDao/existsSQLFile.sql",
@@ -55,78 +58,123 @@ class DomaSqlQuickFixTest : DomaSqlTest() {
5558

5659
fun testSelectGenerateSQLFileQuickFix() {
5760
val testDaoName = "SelectQuickFixTestDao"
58-
daoQuickFixTest(testDaoName, false) {
59-
highlightingDao(testDaoName)
61+
daoQuickFixTest(testDaoName) { virtual ->
62+
highlightingDao(virtual)
6063
}
6164
}
6265

6366
fun testInsertGenerateSQLFileQuickFix() {
6467
val testDaoName = "InsertQuickFixTestDao"
65-
daoQuickFixTest(testDaoName, false) { highlightingDao(testDaoName) }
68+
daoQuickFixTest(testDaoName) { virtual ->
69+
highlightingDao(virtual)
70+
}
6671
}
6772

6873
fun testUpdateGenerateSQLFileQuickFix() {
6974
val testDaoName = "UpdateQuickFixTestDao"
70-
daoQuickFixTest(testDaoName, false) { highlightingDao(testDaoName) }
75+
daoQuickFixTest(testDaoName) { virtual ->
76+
highlightingDao(virtual)
77+
}
7178
}
7279

7380
fun testDeleteGenerateSQLFileQuickFix() {
7481
val testDaoName = "DeleteQuickFixTestDao"
75-
daoQuickFixTest(testDaoName, false) { highlightingDao(testDaoName) }
82+
daoQuickFixTest(testDaoName) { virtual ->
83+
highlightingDao(virtual)
84+
}
7685
}
7786

7887
fun testBatchInsertGenerateSQLFileQuickFix() {
7988
val testDaoName = "BatchInsertQuickFixTestDao"
80-
daoQuickFixTest(testDaoName, false) { highlightingDao(testDaoName) }
89+
daoQuickFixTest(testDaoName) { virtual ->
90+
highlightingDao(virtual)
91+
}
8192
}
8293

8394
fun testBatchUpdateGenerateSQLFileQuickFix() {
8495
val testDaoName = "BatchUpdateQuickFixTestDao"
85-
daoQuickFixTest(testDaoName, false) { highlightingDao(testDaoName) }
96+
daoQuickFixTest(testDaoName) { virtual ->
97+
highlightingDao(virtual)
98+
}
8699
}
87100

88101
fun testBatchDeleteGenerateSQLFileQuickFix() {
89102
val testDaoName = "BatchDeleteQuickFixTestDao"
90-
daoQuickFixTest(testDaoName, false) { highlightingDao(testDaoName) }
103+
daoQuickFixTest(testDaoName) { virtual ->
104+
highlightingDao(virtual)
105+
}
91106
}
92107

93108
fun testScriptGenerateSQLFileQuickFix() {
94109
val testDaoName = "ScriptQuickFixTestDao"
95-
daoQuickFixTest(testDaoName, true) { highlightingDao(testDaoName) }
110+
daoQuickFixTest(testDaoName, true) { virtual ->
111+
highlightingDao(virtual)
112+
}
96113
}
97114

98115
fun testSqlProcessorGenerateSQLFileQuickFix() {
99116
val testDaoName = "SqlProcessorQuickFixTestDao"
100-
daoQuickFixTest(testDaoName, false) { highlightingDao(testDaoName) }
117+
daoQuickFixTest(testDaoName) { virtual ->
118+
highlightingDao(virtual)
119+
}
101120
}
102121

103-
private fun highlightingDao(testDaoName: String) {
104-
val dao = findDaoClass(getQuickFixTestDaoName(testDaoName).replace("/", "."))
122+
fun testSourceNameDaoGenerateSQLFileQuickFix() {
123+
val originalPackageName = "doma/java"
124+
val testDaoName = "SourceNameDao"
125+
daoQuickFixTestOtherPackage(originalPackageName, testDaoName) { virtual ->
126+
highlightingDao(virtual)
127+
}
128+
}
129+
130+
private fun highlightingDao(virtual: VirtualFile) {
105131
myFixture.testHighlighting(
106132
false,
107133
false,
108134
false,
109-
dao.containingFile.virtualFile,
135+
virtual,
110136
)
111137
}
112138

113139
private fun daoQuickFixTest(
114140
testDaoName: String,
115-
isScript: Boolean,
116-
afterCheck: () -> Unit,
141+
isScript: Boolean = false,
142+
afterCheck: (VirtualFile) -> Unit,
117143
) {
118144
val quickFixDaoName = getQuickFixTestDaoName(testDaoName)
119-
val dao = findDaoClass(quickFixDaoName.replace("/", "."))
145+
val dao = findDaoClass(quickFixDaoName)
120146

121147
myFixture.configureFromExistingVirtualFile(dao.containingFile.virtualFile)
122-
val intention = myFixture.findSingleIntention(MessageBundle.message("generate.sql.quickfix.title"))
148+
val intention =
149+
myFixture.findSingleIntention(MessageBundle.message("generate.sql.quickfix.title"))
123150
myFixture.launchAction(intention)
124151

125-
val generatedSql =
126-
findSqlFile("$quickFixDaoName/generateSQLFile.${if (isScript) "script" else "sql"}")
127-
assertTrue("Not Found SQL File", generatedSql != null)
152+
val extension = if (isScript) "script" else "sql"
153+
findSqlFile("$quickFixDaoName/generateSQLFile.$extension")?.let { generatedSql ->
154+
afterCheck(generatedSql)
155+
}
156+
}
157+
158+
private fun daoQuickFixTestOtherPackage(
159+
packageName: String,
160+
testDaoName: String,
161+
isScript: Boolean = false,
162+
afterCheck: (VirtualFile) -> Unit,
163+
) {
164+
val daoPackage = packageName.plus("/dao")
165+
val dao = findDaoClass(daoPackage.replace("/", "."), testDaoName)
166+
167+
myFixture.configureFromExistingVirtualFile(dao.containingFile.virtualFile)
168+
val intention =
169+
myFixture.findSingleIntention(MessageBundle.message("generate.sql.quickfix.title"))
170+
myFixture.launchAction(intention)
128171

129-
afterCheck()
172+
val extension = if (isScript) "script" else "sql"
173+
findSqlFile(packageName, "$testDaoName/generateSQLFile.$extension")?.let { generatedSql ->
174+
afterCheck(generatedSql)
175+
} ?: {
176+
fail("Not Found SQL File: $packageName/$testDaoName/generateSQLFile.$extension")
177+
}
130178
}
131179

132180
private fun getQuickFixTestDaoName(daoName: String): String = "$packagename/$daoName"

src/test/testData/src/main/java/doma/example/dao/quickfix/BatchDeleteQuickFixTestDao.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@
88
@Dao
99
public interface BatchDeleteQuickFixTestDao {
1010

11-
12-
1311
@BatchDelete
1412
int[] nonExistSQLFile(List<Employee> employees);
1513

16-
1714
@BatchDelete(sqlFile = true)
1815
int[] generateSQLFile<caret>(List<Employee> employees);
1916

20-
2117
@BatchDelete(sqlFile = true)
2218
int[] existsSQLFile(List<Employee> employees);
2319

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package doma.java.dao;
2+
3+
import doma.example.entity.*;
4+
import org.seasar.doma.*;
5+
6+
@Dao
7+
interface SourceNameDao {
8+
@Select
9+
Employee generateSQLFile<caret>(Integer id,String name);
10+
}

0 commit comments

Comments
 (0)