Skip to content

Commit 643da87

Browse files
committed
Refactor DAO return type tests and add new test cases for Function and Select annotations
1 parent 36bc84e commit 643da87

File tree

12 files changed

+292
-24
lines changed

12 files changed

+292
-24
lines changed

src/main/kotlin/org/domaframework/doma/intellij/inspection/dao/processor/returntype/FunctionAnnotationReturnTypeCheckProcessor.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ class FunctionAnnotationReturnTypeCheckProcessor(
7979
}
8080

8181
val checkTypeClass = project.getJavaClazz(checkType.canonicalText)
82-
// TODO: 基本クラス、ドメイン、エンティティ、Map<String, Object>、Optional 以外の型チェックを実装
8382
if (checkTypeClass != null &&
8483
(
8584
checkTypeClass.isDomain() ||

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ open class DomaSqlTest : LightJavaCodeInsightFixtureTestCase() {
169169
)
170170
}
171171

172+
protected fun addOtherJavaFile(
173+
packageName: String,
174+
fileName: String,
175+
) {
176+
val file = File("$testDataPath/$sourceRoot/$packagePath/$packageName/$fileName")
177+
myFixture.addFileToProject(
178+
"main/$sourceRoot/$packagePath/$packageName/$fileName",
179+
file.readText(),
180+
)
181+
}
182+
172183
fun addResourceEmptySqlFile(vararg sqlFileNames: String) {
173184
for (sqlFileName in sqlFileNames) {
174185
myFixture.addFileToProject(

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

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,47 +24,60 @@ import org.domaframework.doma.intellij.inspection.dao.inspector.DaoMethodReturnT
2424
class AnnotationReturnTypeCheckInspectionTest : DomaSqlTest() {
2525
private val testDaoNames =
2626
listOf(
27-
"UpdateReturnTypeDao",
28-
"BatchReturnTypeDao",
29-
"MultiInsertReturnTypeDao",
30-
"SqlProcessorReturnTypeDao",
31-
"ProcedureReturnTypeDao",
27+
"SelectReturnTypeTestDao",
28+
"UpdateReturnTypeTestDao",
29+
"BatchReturnTypeTestDao",
30+
"MultiInsertReturnTypeTestDao",
31+
"SqlProcessorReturnTypeTestDao",
32+
"ProcedureReturnTypeTestDao",
33+
"FunctionReturnTypeTestDao",
3234
)
33-
private val daoPackage = "inspection"
35+
private val daoPackage = "inspection/returntype"
3436

3537
override fun setUp() {
3638
super.setUp()
3739
testDaoNames.forEach { daoName ->
38-
addDaoJavaFile("inspection/$daoName.java")
40+
addDaoJavaFile("$daoPackage/$daoName.java")
3941
}
40-
// Entity classes
4142
addEntityJavaFile("Packet.java")
4243
addEntityJavaFile("Pckt.java")
44+
addOtherJavaFile("domain", "Hiredate.java")
45+
addOtherJavaFile("collector", "HogeCollector.java")
4346
myFixture.enableInspections(DaoMethodReturnTypeInspection())
4447
}
4548

49+
fun testSelectReturnTypeCheckProcessor() {
50+
val dao = findDaoClass("$daoPackage.SelectReturnTypeTestDao")
51+
myFixture.testHighlighting(false, false, false, dao.containingFile.virtualFile)
52+
}
53+
4654
fun testUpdateAnnotationReturnTypeCheckProcessor() {
47-
val dao = findDaoClass("$daoPackage.UpdateReturnTypeDao")
55+
val dao = findDaoClass("$daoPackage.UpdateReturnTypeTestDao")
4856
myFixture.testHighlighting(false, false, false, dao.containingFile.virtualFile)
4957
}
5058

5159
fun testBatchAnnotationReturnTypeCheckProcessor() {
52-
val dao = findDaoClass("$daoPackage.BatchReturnTypeDao")
60+
val dao = findDaoClass("$daoPackage.BatchReturnTypeTestDao")
5361
myFixture.testHighlighting(false, false, false, dao.containingFile.virtualFile)
5462
}
5563

5664
fun testMultiInsertAnnotationReturnTypeCheckProcessor() {
57-
val dao = findDaoClass("$daoPackage.MultiInsertReturnTypeDao")
65+
val dao = findDaoClass("$daoPackage.MultiInsertReturnTypeTestDao")
5866
myFixture.testHighlighting(false, false, false, dao.containingFile.virtualFile)
5967
}
6068

6169
fun testSqlProcessorAnnotationReturnTypeCheckProcessor() {
62-
val dao = findDaoClass("$daoPackage.SqlProcessorReturnTypeDao")
70+
val dao = findDaoClass("$daoPackage.SqlProcessorReturnTypeTestDao")
6371
myFixture.testHighlighting(false, false, false, dao.containingFile.virtualFile)
6472
}
6573

6674
fun testProcedureAnnotationReturnTypeCheckProcessor() {
67-
val dao = findDaoClass("$daoPackage.ProcedureReturnTypeDao")
75+
val dao = findDaoClass("$daoPackage.ProcedureReturnTypeTestDao")
76+
myFixture.testHighlighting(false, false, false, dao.containingFile.virtualFile)
77+
}
78+
79+
fun testFunctionAnnotationReturnTypeCheckProcessor() {
80+
val dao = findDaoClass("$daoPackage.FunctionReturnTypeTestDao")
6881
myFixture.testHighlighting(false, false, false, dao.containingFile.virtualFile)
6982
}
7083
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package doma.example.collector;
2+
3+
import java.util.stream.Collector;
4+
5+
public class HogeCollector implements Collector<String, String, String> {
6+
7+
@Override
8+
public Supplier<String> supplier() {
9+
return null;
10+
}
11+
12+
@Override
13+
public BiConsumer<String, String> accumulator() {
14+
return null;
15+
}
16+
17+
@Override
18+
public BinaryOperator<String> combiner() {
19+
return null;
20+
}
21+
22+
@Override
23+
public Function<String, String> finisher() {
24+
return null;
25+
}
26+
27+
@Override
28+
public Set<Characteristics> characteristics() {
29+
return null;
30+
}
31+
}

src/test/testData/src/main/java/doma/example/dao/inspection/BatchReturnTypeDao.java renamed to src/test/testData/src/main/java/doma/example/dao/inspection/returntype/BatchReturnTypeTestDao.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package doma.example.dao.inspection;
1+
package doma.example.dao.inspection.returntype;
22

33
import org.seasar.doma.*;
44
import org.seasar.doma.jdbc.BatchResult;
55
import doma.example.entity.*;
66
import java.util.List;
77

88
@Dao
9-
public interface BatchReturnTypeDao {
9+
public interface BatchReturnTypeTestDao {
1010
@BatchInsert
1111
int[] batchInsertReturnsIntArray(List<Packet> e);
1212

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package doma.example.dao.inspection.returntype;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.Optional;
6+
import org.seasar.doma.Dao;
7+
import org.seasar.doma.Function;
8+
import org.seasar.doma.In;
9+
import org.seasar.doma.InOut;
10+
import org.seasar.doma.Out;
11+
import org.seasar.doma.ResultSet;
12+
import doma.example.entity.*;
13+
import org.seasar.doma.jdbc.Reference;
14+
15+
@Dao
16+
public interface FunctionReturnTypeTestDao {
17+
18+
@Function
19+
int <error descr="The return type int is invalid">primitiveFunction</error>(@In int id);
20+
21+
@Function
22+
void voidFunction(@In int id);
23+
24+
@Function
25+
String executeFunction(
26+
@In Integer arg1, @InOut Reference<Integer> arg2, @Out Reference<Integer> arg3);
27+
28+
@Function
29+
Packet entityFunction(
30+
@In Packet arg1, @InOut Reference<Packet> arg2, @Out Reference<Packet> arg3);
31+
32+
@Function
33+
List<String> listFunction(@ResultSet List<String> arg1);
34+
35+
@Function
36+
List<Packet> immutableEntityListFunction(@ResultSet List<Packet> arg1);
37+
38+
@Function
39+
List<Pckt> entityListFunction(@ResultSet List<Pckt> arg1);
40+
41+
@Function
42+
Optional<String> optionalFunction(
43+
@In Optional<Integer> arg1,
44+
@InOut Reference<Optional<Integer>> arg2,
45+
@Out Reference<Optional<Integer>> arg3);
46+
47+
@Function
48+
List<Optional<String>> listOptionalFunction(@ResultSet List<Optional<String>> arg1);
49+
50+
@Function
51+
List<Optional<Packet>> entityOptionalListFunction(@ResultSet List<Optional<Packet>> arg1);
52+
53+
@Function
54+
Map<String, Object> mapFunction(@ResultSet List<Optional<Packet>> arg1);
55+
56+
@Function
57+
Optional<Map<String, Object>> mapOptionalFunction(@ResultSet List<Optional<Packet>> arg1);
58+
59+
@Function
60+
List<Map<String, Object>> mapListFunction(@ResultSet List<Optional<Packet>> arg1);
61+
62+
@Function
63+
List<Optional<Map<String, Object>>> mapOptionalListFunction(@ResultSet List<Optional<Packet>> arg1);
64+
65+
@Function
66+
Optional<MyEnum> enumFunction(
67+
@In Optional<MyEnum> arg1,
68+
@InOut Reference<Optional<MyEnum>> arg2,
69+
@Out Reference<Optional<MyEnum>> arg3);
70+
71+
@Function
72+
List<Optional<MyEnum>> listEnumFunction(@ResultSet List<Optional<MyEnum>> arg1);
73+
74+
enum MyEnum {}
75+
76+
}

src/test/testData/src/main/java/doma/example/dao/inspection/MultiInsertReturnTypeDao.java renamed to src/test/testData/src/main/java/doma/example/dao/inspection/returntype/MultiInsertReturnTypeTestDao.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package doma.example.dao.inspection;
1+
package doma.example.dao.inspection.returntype;
22

33
import org.seasar.doma.*;
44
import org.seasar.doma.jdbc.MultiResult;
55
import doma.example.entity.*;
66
import java.util.List;
77

88
@Dao
9-
public interface MultiInsertReturnTypeDao {
9+
public interface MultiInsertReturnTypeTestDao {
1010
@MultiInsert
1111
int multiInsertReturnsInt(List<Packet> e);
1212

src/test/testData/src/main/java/doma/example/dao/inspection/ProcedureReturnTypeDao.java renamed to src/test/testData/src/main/java/doma/example/dao/inspection/returntype/ProcedureReturnTypeTestDao.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package doma.example.dao.inspection;
1+
package doma.example.dao.inspection.returntype;
22

33
import org.seasar.doma.*;
44

55
@Dao
6-
public interface ProcedureReturnTypeDao {
6+
public interface ProcedureReturnTypeTestDao {
77
@Procedure
88
void callProcedureReturnsVoid();
99
@Procedure
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package doma.example.dao.inspection.returntype;
2+
3+
import java.math.BigDecimal;
4+
import org.seasar.doma.*;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.function.Function;
8+
import java.util.stream.Collector;
9+
import java.util.stream.Stream;
10+
import org.seasar.doma.*;
11+
import org.seasar.doma.jdbc.SelectOptions;
12+
import org.seasar.doma.message.Message;
13+
import doma.example.entity.*;
14+
import doma.example.domain.*;
15+
import doma.example.collector.*;
16+
17+
@Dao
18+
public interface SelectReturnTypeTestDao {
19+
20+
@Select
21+
void <error descr="The return type void is invalid">selectVoid</error>(Employee e);
22+
23+
@Select
24+
List<Employee> selectByExample(Employee e);
25+
26+
@Select
27+
List<Employee> selectWithOptionalOrderBy(String employeeName, String orderBy);
28+
29+
@Select
30+
Employee selectById(Integer employeeId);
31+
32+
@Select
33+
Employee selectById(Integer employeeId,SelectOptions options);
34+
35+
@Sql("select * from EMPLOYEE where EMPLOYEE_NAME in /*names*/('aaa', 'bbb')")
36+
@Select
37+
List<Employee> selectByNames(List<String> names);
38+
39+
@Select(mapKeyNaming = MapKeyNamingType.CAMEL_CASE)
40+
Map<String, Object> selectByIdAsMap(Integer employeeId);
41+
42+
@Select(mapKeyNaming = MapKeyNamingType.CAMEL_CASE)
43+
List<Map<String, Object>> selectAllAsMapList();
44+
45+
@Select(strategy = SelectType.STREAM, mapKeyNaming = MapKeyNamingType.CAMEL_CASE)
46+
<R> R selectAllAsMapList(Function<Stream<Map<String, Object>>, R> mapper);
47+
48+
@Select(strategy = SelectType.STREAM, mapKeyNaming = MapKeyNamingType.CAMEL_CASE)
49+
<R> R selectAllAsMapList(Function<Stream<Map<String, Object>>, R> mapper,SelectOptions options);
50+
51+
@Select(strategy = SelectType.STREAM, mapKeyNaming = MapKeyNamingType.CAMEL_CASE)
52+
String <error descr="The return type must match RESULT of the \"java.util.function.Function\" type parameter">selectStreamInvalid</error>(Function<Stream<Map<String, Object>>, Integer> mapper,SelectOptions options);
53+
54+
@Select
55+
List<Employee> selectByNamePrefix(String employeeName);
56+
57+
@Select
58+
List<Employee> selectByNameInfix(String employeeName);
59+
60+
@Select
61+
List<Employee> selectByNameSuffix(String employeeName);
62+
63+
@Select
64+
List<Employee> selectAll();
65+
66+
@Select
67+
List<Employee> selectAll(SelectOptions options);
68+
69+
@Select
70+
List<Employee> selectDistinctAll(SelectOptions options);
71+
72+
@Select(ensureResultMapping = true)
73+
List<Employee> selectOnlyNameWithMappingCheck();
74+
75+
@Select(ensureResultMapping = false)
76+
List<Employee> selectOnlyNameWithoutMappingCheck();
77+
78+
@Select
79+
List<Employee> selectByInterface(Hiredate hiredate);
80+
81+
@Select(strategy = SelectType.STREAM)
82+
<R> R streamAll(Function<Stream<Employee>, R> mapper);
83+
84+
@Select(strategy = SelectType.STREAM)
85+
<R> R streamAll(Function<Stream<Employee>, R> mapper,SelectOptions options);
86+
87+
@Select(strategy = SelectType.STREAM)
88+
<R> R streamAllSalary(Function<Stream<BigDecimal>, R> mapper);
89+
90+
@Select(strategy = SelectType.STREAM)
91+
<R> R streamAllSalary(Function<Stream<BigDecimal>, R> mapper,SelectOptions options);
92+
93+
@Select(strategy = SelectType.STREAM)
94+
<R> R streamBySalary(BigDecimal salary, Function<Stream<Employee>, R> mapper);
95+
96+
@Select(strategy = SelectType.COLLECT)
97+
<R> R collectAll(Collector<Employee, ?, R> collector);
98+
99+
@Select
100+
@Suppress(messages = {Message.DOMA4274})
101+
Stream<Employee> streamAll();
102+
103+
@Select
104+
@Suppress(messages = {Message.DOMA4274})
105+
Stream<Employee> streamAll(SelectOptions options);
106+
107+
@Select
108+
@Suppress(messages = {Message.DOMA4274})
109+
Stream<Employee> streamBySalary(BigDecimal salary);
110+
111+
@Select(strategy = SelectType.COLLECT)
112+
Integer selectCollector(Integer id, String name, Collector<Pckt, ?, Integer> collector);
113+
114+
@Select(strategy = SelectType.COLLECT)
115+
<R> R selectCollectorR(Integer id, Collector<Packet, ?, R> collector);
116+
117+
@Select(strategy = SelectType.COLLECT)
118+
Pckt selectWithOutCollector(BigDecimal salary, Packet packet);
119+
120+
@Select(strategy = SelectType.COLLECT)
121+
<R extends Number> R select(Collector<String, ?, R> collector);
122+
123+
@Select(strategy = SelectType.COLLECT)
124+
String selectWithHogeCollector(HogeCollector collector);
125+
126+
@Select(strategy = SelectType.COLLECT, mapKeyNaming = MapKeyNamingType.CAMEL_CASE)
127+
<R> R selectByIdAsMap(Integer id, Collector<Map<String, Object>, ?, R> collector);
128+
129+
130+
}

src/test/testData/src/main/java/doma/example/dao/inspection/SqlProcessorReturnTypeDao.java renamed to src/test/testData/src/main/java/doma/example/dao/inspection/returntype/SqlProcessorReturnTypeTestDao.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package doma.example.dao.inspection;
1+
package doma.example.dao.inspection.returntype;
22

33
import org.seasar.doma.Dao;
44
import org.seasar.doma.SqlProcessor;
@@ -9,7 +9,7 @@
99
import doma.example.entity.*;
1010

1111
@Dao
12-
public interface SqlProcessorReturnTypeDao {
12+
public interface SqlProcessorReturnTypeTestDao {
1313
@SqlProcessor
1414
<R> R processSqlReturnsR(BiFunction<Config, PreparedSql, R> handler);
1515

0 commit comments

Comments
 (0)