1515 */
1616package org.domaframework.doma.intellij.action.dao
1717
18+ import com.intellij.openapi.fileEditor.FileDocumentManager
1819import com.intellij.openapi.fileEditor.FileEditorManager
20+ import com.intellij.psi.PsiDocumentManager
1921import org.domaframework.doma.intellij.DomaSqlTest
2022
2123class ConvertSqlAnnotationToFileActionTest : DomaSqlTest () {
@@ -25,74 +27,87 @@ class ConvertSqlAnnotationToFileActionTest : DomaSqlTest() {
2527
2628 fun testIntentionAvailableForSelectWithSqlAnnotation () {
2729 val daoName = " SelectWithSqlAnnotationDao"
28- doTest(daoName, " generateSqlFile" )
30+ val sqlFileName = " generateSqlFile"
31+ doTest(daoName, sqlFileName)
32+ doTestSqlFormat(daoName, sqlFileName)
2933 }
3034
3135 fun testIntentionAvailableForSelectTextBlockWithSqlAnnotation () {
3236 val daoName = " SelectTextBlockWithSqlAnnotationDao"
33- doTest(daoName, " generateSqlFileByTextBlock" )
37+ val sqlFileName = " generateSqlFileByTextBlock"
38+ doTest(daoName, sqlFileName)
39+ doTestSqlFormat(daoName, sqlFileName)
3440 }
3541
3642 fun testIntentionAvailableForSelectHasAnyOptWithSqlAnnotation () {
3743 val daoName = " SelectHasAnyOptionWithSqlAnnotationDao"
38- doTest(daoName, " generateSqlFileHasAnyOption" )
44+ val sqlFileName = " generateSqlFileHasAnyOption"
45+ doTest(daoName, sqlFileName)
46+ doTestSqlFormat(daoName, sqlFileName)
3947 }
4048
4149 fun testIntentionAvailableForInsertWithSqlAnnotation () {
4250 val daoName = " InsertWithSqlAnnotationDao"
43- doTest(daoName, " insert" )
51+ val sqlFileName = " insert"
52+ doTest(daoName, sqlFileName)
53+ doTestSqlFormat(daoName, sqlFileName)
4454 }
4555
4656 fun testIntentionAvailableForUpdateWithSqlAnnotation () {
4757 val daoName = " UpdateReturningWithSqlAnnotationDao"
48- doTest(daoName, " updateEmployeeReturning" )
58+ val sqlFileName = " updateEmployeeReturning"
59+ doTest(daoName, sqlFileName)
60+ doTestSqlFormat(daoName, sqlFileName)
4961 }
5062
5163 fun testIntentionAvailableForDeleteWithSqlAnnotation () {
5264 val daoName = " DeleteWithSqlAnnotationDao"
53- doTest(daoName, " deleteEmployeeHasSqlFile" )
65+ val sqlFileName = " deleteEmployeeHasSqlFile"
66+ doTest(daoName, sqlFileName)
67+ doTestSqlFormat(daoName, sqlFileName)
5468 }
5569
5670 fun testIntentionAvailableForScriptWithSqlAnnotation () {
5771 val daoName = " ScriptWithSqlAnnotationDao"
58- doTest(daoName, " createTable" , true )
72+ val sqlFileName = " createTable"
73+ doTest(daoName, sqlFileName, true )
74+ doTestSqlFormat(daoName, sqlFileName, true )
5975 }
6076
6177 fun testIntentionAvailableForBatchInsertWithSqlAnnotation () {
6278 val daoName = " BatchInsertWithSqlAnnotationDao"
63- doTest(daoName, " batchInsert" )
79+ val sqlFileName = " batchInsert"
80+ doTest(daoName, sqlFileName)
81+ doTestSqlFormat(daoName, sqlFileName)
6482 }
6583
6684 fun testIntentionAvailableForBatchUpdateWithSqlAnnotation () {
6785 val daoName = " BatchUpdateWithSqlAnnotationDao"
68- doTest(daoName, " batchUpdate" )
86+ val sqlFileName = " batchUpdate"
87+ doTest(daoName, sqlFileName)
88+ doTestSqlFormat(daoName, sqlFileName)
6989 }
7090
7191 fun testIntentionAvailableForBatchDeleteWithSqlAnnotation () {
7292 val daoName = " BatchDeleteWithSqlAnnotationDao"
73- doTest(daoName, " batchDelete" )
93+ val sqlFileName = " batchDelete"
94+ doTest(daoName, sqlFileName)
95+ doTestSqlFormat(daoName, sqlFileName)
7496 }
7597
7698 fun testIntentionAvailableForSqlProcessorWithSqlAnnotation () {
7799 val daoName = " SqlProcessorWithSqlAnnotationDao"
78- doTest(daoName, " executeProcessor" )
100+ val sqlFileName = " executeProcessor"
101+ doTest(daoName, sqlFileName)
102+ doTestSqlFormat(daoName, sqlFileName)
79103 }
80104
81105 fun testIntentionOverrideSqlFile () {
82106 val daoName = " SelectOverrideSqlFileDao"
83- doTest(daoName, " overrideSqlFile" )
84-
85107 val sqlFileName = " overrideSqlFile"
86- val openedEditor = FileEditorManager .getInstance(project).selectedEditors
87- val sqlFile = openedEditor.find { it.file.name == sqlFileName.substringAfter(" /" ).plus(" .sql" ) }
88-
89- if (sqlFile == null ) {
90- fail(" SQL file $sqlFileName should be opened after conversion" )
91- return
92- }
93-
94- myFixture.configureFromExistingVirtualFile(sqlFile.file)
95- myFixture.checkResultByFile(" resources/META-INF/doma/example/dao/$sqlConversionPackage /$daoName /$sqlFileName .after.sql" )
108+ addResourceEmptySqlFile(" $sqlConversionPackage /$daoName /$sqlFileName .sql" )
109+ doTest(daoName, sqlFileName)
110+ doTestSqlFormat(daoName, sqlFileName)
96111 }
97112
98113 fun testIntentionNotAvailableForMethodWithoutSqlAnnotation () {
@@ -151,4 +166,28 @@ class ConvertSqlAnnotationToFileActionTest : DomaSqlTest() {
151166 val generatedSql = findSqlFile(" $sqlConversionPackage /$daoName /$sqlFile " )
152167 assertTrue(" Not Found SQL File [$sqlFile ]" , generatedSql != null )
153168 }
169+
170+ private fun doTestSqlFormat (
171+ daoName : String ,
172+ sqlFileName : String ,
173+ isScript : Boolean = false,
174+ ) {
175+ val openedEditor = FileEditorManager .getInstance(project).selectedEditors
176+ val extension = if (isScript) " script" else " sql"
177+ val sqlFile = openedEditor.find { it.file.name == sqlFileName.substringAfter(" /" ).plus(" .$extension " ) }
178+
179+ if (sqlFile == null ) {
180+ fail(" SQL file $sqlFileName .$extension should be opened after conversion" )
181+ return
182+ }
183+ // If the generated `PsiFile` has an associated `Document`, explicitly reload it to ensure memory–disk consistency.
184+ // If not reloaded, the test may produce: *Unexpected memory–disk conflict in tests for*.
185+ val fdm = FileDocumentManager .getInstance()
186+ fdm.saveAllDocuments()
187+ PsiDocumentManager .getInstance(project).commitAllDocuments()
188+ fdm.getDocument(sqlFile.file)?.let { fdm.reloadFromDisk(it) }
189+
190+ myFixture.configureFromExistingVirtualFile(sqlFile.file)
191+ myFixture.checkResultByFile(" resources/META-INF/doma/example/dao/$sqlConversionPackage /$daoName /$sqlFileName .after.$extension " )
192+ }
154193}
0 commit comments