Skip to content

Commit 7a6ab1e

Browse files
committed
Added test cases for Intention action (Annotation ⇒ SQL file).
1 parent 6015d07 commit 7a6ab1e

30 files changed

+687
-1
lines changed

src/main/resources/META-INF/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
implementationClass="org.domaframework.doma.intellij.setting.SqlFileType"
8383
fieldName="INSTANCE"
8484
language="DomaSql"
85-
extensions="sql"/>
85+
extensions="sql;script"/>
8686
<lang.parserDefinition
8787
language="DomaSql"
8888
implementationClass="org.domaframework.doma.intellij.setting.SqlParserDefinition"/>
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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.DomaSqlTest
20+
21+
class ConvertSqlAnnotationToFileActionTest : DomaSqlTest() {
22+
private val sqlConversionPackage = "sqlconversion"
23+
private val convertActionName = "Convert to SQL file (set sqlFile=true)"
24+
private val convertFamilyName = "Convert @Sql annotation to SQL file"
25+
26+
fun testIntentionAvailableForSelectWithSqlAnnotation() {
27+
val daoName = "SelectWithSqlAnnotationDao"
28+
doTest(daoName, "generateSqlFile")
29+
}
30+
31+
fun testIntentionAvailableForSelectTextBlockWithSqlAnnotation() {
32+
val daoName = "SelectTextBlockWithSqlAnnotationDao"
33+
doTest(daoName, "generateSqlFileByTextBlock")
34+
}
35+
36+
fun testIntentionAvailableForSelectHasAnyOptWithSqlAnnotation() {
37+
val daoName = "SelectHasAnyOptionWithSqlAnnotationDao"
38+
doTest(daoName, "generateSqlFileHasAnyOption")
39+
}
40+
41+
fun testIntentionAvailableForInsertWithSqlAnnotation() {
42+
val daoName = "InsertWithSqlAnnotationDao"
43+
doTest(daoName, "insert")
44+
}
45+
46+
fun testIntentionAvailableForUpdateWithSqlAnnotation() {
47+
val daoName = "UpdateReturningWithSqlAnnotationDao"
48+
doTest(daoName, "updateEmployeeReturning")
49+
}
50+
51+
fun testIntentionAvailableForDeleteWithSqlAnnotation() {
52+
val daoName = "DeleteWithSqlAnnotationDao"
53+
doTest(daoName, "deleteEmployeeHasSqlFile")
54+
}
55+
56+
fun testIntentionAvailableForScriptWithSqlAnnotation() {
57+
val daoName = "ScriptWithSqlAnnotationDao"
58+
doTest(daoName, "createTable", true)
59+
}
60+
61+
fun testIntentionAvailableForBatchInsertWithSqlAnnotation() {
62+
val daoName = "BatchInsertWithSqlAnnotationDao"
63+
doTest(daoName, "batchInsert")
64+
}
65+
66+
fun testIntentionAvailableForBatchUpdateWithSqlAnnotation() {
67+
val daoName = "BatchUpdateWithSqlAnnotationDao"
68+
doTest(daoName, "batchUpdate")
69+
}
70+
71+
fun testIntentionAvailableForBatchDeleteWithSqlAnnotation() {
72+
val daoName = "BatchDeleteWithSqlAnnotationDao"
73+
doTest(daoName, "batchDelete")
74+
}
75+
76+
fun testIntentionAvailableForSqlProcessorWithSqlAnnotation() {
77+
val daoName = "SqlProcessorWithSqlAnnotationDao"
78+
doTest(daoName, "executeProcessor")
79+
}
80+
81+
fun testIntentionOverrideSqlFile() {
82+
val daoName = "SelectOverrideSqlFileDao"
83+
doTest(daoName, "overrideSqlFile")
84+
85+
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")
96+
}
97+
98+
fun testIntentionNotAvailableForMethodWithoutSqlAnnotation() {
99+
val daoName = "NoSqlAnnotationDao"
100+
addDaoJavaFile("$sqlConversionPackage/$daoName.java")
101+
val daoClass = findDaoClass("$sqlConversionPackage.$daoName")
102+
myFixture.configureFromExistingVirtualFile(daoClass.containingFile.virtualFile)
103+
104+
val intentions = myFixture.availableIntentions
105+
val convertIntention = intentions.find { it is ConvertSqlAnnotationToFileAction }
106+
107+
assertNull("$convertFamilyName intention should NOT be available without @Sql annotation", convertIntention)
108+
}
109+
110+
fun testIntentionNotAvailableForUnsupportedAnnotation() {
111+
val daoName = "UnsupportedAnnotationDao"
112+
addDaoJavaFile("$sqlConversionPackage/$daoName.java")
113+
val daoClass = findDaoClass("$sqlConversionPackage.$daoName")
114+
myFixture.configureFromExistingVirtualFile(daoClass.containingFile.virtualFile)
115+
116+
val intentions = myFixture.availableIntentions
117+
val convertIntention = intentions.find { it is ConvertSqlAnnotationToFileAction }
118+
119+
assertNull("$convertFamilyName intention should NOT be available without @Sql annotation", convertIntention)
120+
}
121+
122+
private fun doTest(
123+
daoName: String,
124+
sqlFileName: String,
125+
isScript: Boolean = false,
126+
) {
127+
addDaoJavaFile("$sqlConversionPackage/$daoName.java")
128+
129+
val daoClass = findDaoClass("$sqlConversionPackage.$daoName")
130+
myFixture.configureFromExistingVirtualFile(daoClass.containingFile.virtualFile)
131+
val intention = myFixture.findSingleIntention(convertActionName)
132+
133+
assertNotNull(
134+
"$convertActionName intention should be available",
135+
intention,
136+
)
137+
assertEquals(convertActionName, intention.text)
138+
assertEquals(convertFamilyName, intention.familyName)
139+
140+
myFixture.launchAction(intention)
141+
myFixture.checkResultByFile("java/doma/example/dao/$sqlConversionPackage/$daoName.after.java")
142+
143+
// Test SQL File Generation
144+
val sqlFile = "$sqlFileName.${if (isScript) "script" else "sql"}"
145+
val openedEditor = FileEditorManager.getInstance(project).selectedEditors
146+
assertTrue(
147+
"Open File is Not $sqlFileName",
148+
openedEditor.any { it.file.name == sqlFile.substringAfter("/") },
149+
)
150+
151+
val generatedSql = findSqlFile("$sqlConversionPackage/$daoName/$sqlFile")
152+
assertTrue("Not Found SQL File [$sqlFile]", generatedSql != null)
153+
}
154+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package doma.example.dao.sqlconversion;
2+
3+
import org.seasar.doma.BatchDelete;
4+
import org.seasar.doma.Dao;
5+
import org.seasar.doma.Sql;
6+
import java.util.List;
7+
8+
@Dao
9+
public interface BatchDeleteWithSqlAnnotationDao {
10+
@BatchDelete(sqlFile = true)
11+
int[] batchDelete(List<Integer> ids);
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package doma.example.dao.sqlconversion;
2+
3+
import org.seasar.doma.BatchDelete;
4+
import org.seasar.doma.Dao;
5+
import org.seasar.doma.Sql;
6+
import java.util.List;
7+
8+
@Dao
9+
public interface BatchDeleteWithSqlAnnotationDao {
10+
@BatchDelete
11+
@Sql("DELETE FROM users WHERE id = /* ids */1")
12+
int[] batch<caret>Delete(List<Integer> ids);
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package doma.example.dao.sqlconversion;
2+
3+
import doma.example.User;
4+
import org.seasar.doma.BatchInsert;
5+
import org.seasar.doma.Dao;
6+
import org.seasar.doma.Sql;
7+
import java.util.List;
8+
9+
@Dao
10+
public interface BatchInsertWithSqlAnnotationDao {
11+
@BatchInsert(sqlFile = true)
12+
int[] batchInsert(List<User> users);
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package doma.example.dao.sqlconversion;
2+
3+
import doma.example.User;
4+
import org.seasar.doma.BatchInsert;
5+
import org.seasar.doma.Dao;
6+
import org.seasar.doma.Sql;
7+
import java.util.List;
8+
9+
@Dao
10+
public interface BatchInsertWithSqlAnnotationDao {
11+
@BatchInsert
12+
@Sql("INSERT INTO users (name, email) VALUES (/* users.name */'test', /* users.email */'[email protected]')")
13+
int[] batchInse<caret>rt(List<User> users);
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package doma.example.dao.sqlconversion;
2+
3+
import doma.example.User;
4+
import org.seasar.doma.BatchUpdate;
5+
import org.seasar.doma.Dao;
6+
import org.seasar.doma.Sql;
7+
import java.util.List;
8+
9+
@Dao
10+
public interface BatchUpdateWithSqlAnnotationDao {
11+
@BatchUpdate(sqlFile = true)
12+
int[] batchUpdate(List<User> users);
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package doma.example.dao.sqlconversion;
2+
3+
import doma.example.User;
4+
import org.seasar.doma.BatchUpdate;
5+
import org.seasar.doma.Dao;
6+
import org.seasar.doma.Sql;
7+
import java.util.List;
8+
9+
@Dao
10+
public interface BatchUpdateWithSqlAnnotationDao {
11+
@BatchUpdate
12+
@Sql("UPDATE users SET name = /* users.name */'test' WHERE id = /* users.id */1")
13+
int[] batchUpda<caret>te(List<User> users);
14+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package doma.example.dao.sqlconversion;
2+
3+
import doma.example.User;
4+
import org.seasar.doma.Dao;
5+
import org.seasar.doma.Insert;
6+
import org.seasar.doma.Update;
7+
import org.seasar.doma.Delete;
8+
import org.seasar.doma.Returning;
9+
import org.seasar.doma.Sql;
10+
11+
@Dao
12+
public interface DeleteWithSqlAnnotationDao {
13+
@Insert
14+
@Sql("INSERT INTO users (name, email) VALUES (/* user.name */'test', /* user.email */'[email protected]')")
15+
int insert(User user);
16+
17+
@Delete(sqlFile = true)
18+
int deleteEmployeeHasSqlFile(Employee employee);
19+
20+
@Insert(returning = @Returning)
21+
@Sql("""
22+
INSERT INTO employee
23+
(id
24+
, name)
25+
VALUES ( /* employee.employeeId */0
26+
, /* employee.userName */'name' )
27+
""")
28+
int insertEmployeeReturning(Employee employee);
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package doma.example.dao.sqlconversion;
2+
3+
import doma.example.User;
4+
import org.seasar.doma.Dao;
5+
import org.seasar.doma.Insert;
6+
import org.seasar.doma.Update;
7+
import org.seasar.doma.Delete;
8+
import org.seasar.doma.Returning;
9+
import org.seasar.doma.Sql;
10+
11+
@Dao
12+
public interface DeleteWithSqlAnnotationDao {
13+
@Insert
14+
@Sql("INSERT INTO users (name, email) VALUES (/* user.name */'test', /* user.email */'[email protected]')")
15+
int insert(User user);
16+
17+
@Delete(sqlFile = false)
18+
@Sql("""
19+
DELETE FROM employee
20+
WHERE id = /* employee.employeeId */0
21+
""")
22+
int deleteEm<caret>ployeeHasSqlFile(Employee employee);
23+
24+
@Insert(returning = @Returning)
25+
@Sql("""
26+
INSERT INTO employee
27+
(id
28+
, name)
29+
VALUES ( /* employee.employeeId */0
30+
, /* employee.userName */'name' )
31+
""")
32+
int insertEmployeeReturning(Employee employee);
33+
}

0 commit comments

Comments
 (0)