Skip to content

Commit 7bad95b

Browse files
committed
Add test case of dao method to check parameters
1 parent d0d7cdd commit 7bad95b

File tree

10 files changed

+338
-1
lines changed

10 files changed

+338
-1
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package org.domaframework.doma.intellij.inspection.dao.processor
217

318
import com.intellij.psi.PsiAnnotation

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ open class DomaSqlTest : LightJavaCodeInsightFixtureTestCase() {
205205
}
206206

207207
fun findDaoClass(testDaoName: String): PsiClass {
208-
val dao = myFixture.findClass("doma.example.dao.$testDaoName")
208+
val daoName = testDaoName.replace("/", ".")
209+
val dao = myFixture.findClass("doma.example.dao.$daoName")
209210
assertNotNull("Not Found [$testDaoName]", dao)
210211
return dao
211212
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.inspection.dao
17+
18+
import org.domaframework.doma.intellij.DomaSqlTest
19+
import org.domaframework.doma.intellij.inspection.dao.inspector.DaoMethodParamTypeInspection
20+
21+
/**
22+
* Test class for annotation parameter type check inspection.
23+
*/
24+
class AnnotationParamTypeCheckInspectionTest : DomaSqlTest() {
25+
private val testDaoNames =
26+
listOf(
27+
"InsertUpdateDeleteParamTestDao",
28+
"BatchInsertUpdateDeleteParamTestDao",
29+
"MultiInsertParamTestDao",
30+
"ProcedureParamTestDao",
31+
"ScriptParamTestDao",
32+
"SqlProcessorParamTestDao",
33+
"SelectParamTestDao",
34+
)
35+
private val daoPackage = "inspection/paramtype"
36+
37+
override fun setUp() {
38+
super.setUp()
39+
myFixture.enableInspections(DaoMethodParamTypeInspection())
40+
addEntityJavaFile("Pckt.java")
41+
addEntityJavaFile("Packet.java")
42+
testDaoNames.forEach { daoName ->
43+
addDaoJavaFile("$daoPackage/$daoName.java")
44+
}
45+
}
46+
47+
fun testInsertUpdateDeleteParam() {
48+
val dao = findDaoClass("$daoPackage.InsertUpdateDeleteParamTestDao")
49+
myFixture.testHighlighting(false, false, false, dao.containingFile.virtualFile)
50+
}
51+
52+
fun testBatchInsertUpdateDeleteParam() {
53+
val dao = findDaoClass("$daoPackage.BatchInsertUpdateDeleteParamTestDao")
54+
myFixture.testHighlighting(false, false, false, dao.containingFile.virtualFile)
55+
}
56+
57+
fun testMultiInsertParam() {
58+
val dao = findDaoClass("$daoPackage.MultiInsertParamTestDao")
59+
myFixture.testHighlighting(false, false, false, dao.containingFile.virtualFile)
60+
}
61+
62+
fun testProcedureParam() {
63+
val dao = findDaoClass("$daoPackage.ProcedureParamTestDao")
64+
myFixture.testHighlighting(false, false, false, dao.containingFile.virtualFile)
65+
}
66+
67+
fun testScriptParam() {
68+
val dao = findDaoClass("$daoPackage.ScriptParamTestDao")
69+
myFixture.testHighlighting(false, false, false, dao.containingFile.virtualFile)
70+
}
71+
72+
fun testSqlProcessorParam() {
73+
val dao = findDaoClass("$daoPackage.SqlProcessorParamTestDao")
74+
myFixture.testHighlighting(false, false, false, dao.containingFile.virtualFile)
75+
}
76+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package doma.example.dao.inspection.paramtype;
2+
3+
import doma.example.entity.*;
4+
import org.seasar.doma.*;
5+
import org.seasar.doma.jdbc.BatchResult;
6+
7+
import java.util.List;
8+
import java.util.Optional;
9+
10+
@Dao
11+
public interface BatchInsertUpdateDeleteParamTestDao {
12+
13+
@BatchInsert
14+
int[] batchInsertWrapper(List<Integer> <error descr="The argument must be an Iterable subclass that has an Entity class as a parameter">ids</error>);
15+
16+
@BatchUpdate
17+
int[] batchInsertEmpNotList(Pckt <error descr="The argument must be an Iterable subclass that has an Entity class as a parameter">pckt</error>);
18+
19+
@BatchInsert
20+
@Sql("insert into values (/*id*/0)")
21+
int[] batchInsertWithSqlAnnotation(Integer <error descr="The argument must be an Iterable subclass that has an Entity class as a parameter">id</error>);
22+
23+
@BatchUpdate
24+
int[] <error descr="The number of parameters must be one">batchInsertEmpNotList</error>(List<Pckt> pckts,String name);
25+
26+
@BatchUpdate
27+
int[] batchInsertEmp(List<Pckt> pckts);
28+
29+
@BatchUpdate
30+
BatchResult<Pckt> batchInsertEmpResult(List<Pckt> pckts);
31+
32+
@BatchUpdate
33+
BatchResult<Packet> batchInsertResult(List<Packet> packets);
34+
35+
@BatchUpdate
36+
int[] batchInsertOptional(List<Optional<Packet>> <error descr="The argument must be an Iterable subclass that has an Entity class as a parameter">packets</error>);
37+
38+
@BatchUpdate
39+
@Sql("insert into values (/*packets.id*/0, /*packets.name*/'name')")
40+
BatchResult<List<Packet>> batchInsertResultWithSql(List<Optional<Packet>> <error descr="The argument must be an Iterable subclass that has an Entity class as a parameter">packets</error>);
41+
42+
@BatchUpdate
43+
@Sql("insert into values (/*packets.id*/0, /*packets.name*/'name')")
44+
int[] batchInsertWithSql(List<Optional<Packet>> <error descr="The argument must be an Iterable subclass that has an Entity class as a parameter">packets</error>);
45+
46+
@BatchUpdate
47+
int[] batchInsert(List<Packet> packets);
48+
}
49+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package doma.example.dao.inspection.paramtype;
2+
3+
import doma.example.entity.*;
4+
import org.seasar.doma.Dao;
5+
import org.seasar.doma.Delete;
6+
import org.seasar.doma.Insert;
7+
import org.seasar.doma.Update;
8+
import org.seasar.doma.Sql;
9+
10+
@Dao
11+
public interface InsertUpdateDeleteParamTestDao {
12+
13+
@Insert
14+
int insertEntity(Packet packet);
15+
16+
@Insert
17+
int insertPrimitive(Integer <error descr="The parameter type must be an entity class">id</error>);
18+
19+
@Update
20+
int <error descr="The number of parameters must be one">updateNoParams</error>();
21+
22+
@Delete
23+
int <error descr="The number of parameters must be one">deleteMultipleParams</error>(Packet packet, String name);
24+
25+
@Insert
26+
@Sql("insert into Packet (id, name) values (/* id */0, /* name */'test')")
27+
int insertWithSqlAnnotation(Integer id, String name);
28+
29+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package doma.example.dao.inspection.paramtype;
2+
3+
import doma.example.entity.*;
4+
import org.seasar.doma.*;
5+
6+
import java.util.List;
7+
8+
@Dao
9+
public interface MultiInsertParamTestDao {
10+
11+
@MultiInsert
12+
int multiInsertNotList(Pckt <error descr="The argument must be an Iterable subclass that has an Entity class as a parameter">Pckt</error>);
13+
14+
@MultiInsert
15+
int <error descr="The number of parameters must be one">multiInsertAnyParams</error>(List<Packet> PcktList, String name);
16+
17+
@MultiInsert
18+
int multiInsertNotEntityIterable(List<Integer> <error descr="The argument must be an Iterable subclass that has an Entity class as a parameter">PcktList</error>);
19+
20+
@MultiInsert
21+
int[] multiInsert(List<Pckt> PcktList);
22+
23+
@MultiInsert
24+
MultiResult<<error descr="Cannot resolve symbol 'Prj'">Prj</error>> multiInsertNoMatchResult(List<Pckt> PcktList);
25+
26+
@MultiInsert
27+
<error descr="Cannot resolve symbol 'MultiResult'">MultiResult</error><Pckt> multiInsertResult(List<Pckt> PcktList);
28+
29+
@MultiInsert
30+
<error descr="Cannot resolve symbol 'MultiResult'">MultiResult</error><Packet> multiInsertPacket(List<Packet> PacketList);
31+
32+
@MultiInsert(returning = @Returning)
33+
int[] multiInsertReturning(List<Pckt> Pckts);
34+
35+
@MultiInsert(returning = @Returning)
36+
List<Pckt> multiInsertReturningList(List<Pckt> Pckts);
37+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package doma.example.dao.inspection.paramtype;
2+
3+
import org.seasar.doma.*;
4+
import org.seasar.doma.jdbc.Reference;
5+
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
@Dao
10+
public interface ProcedureParamTestDao {
11+
12+
@Procedure
13+
void executeNotAnnotation(@In Integer inParam,@InOut Reference<String> inOurPatam, @Out Reference<Integer> outParam);
14+
15+
@Procedure
16+
void executeValidType(@In Reference<Integer> <error descr="Reference<Integer> is not supported as the type of the parameter annotated with @In">inParam</error>,@InOut Integer <error descr="The parameter type annotated with @InOut must be \"org.seasar.doma.jdbc.Reference\"">inOurPatam</error>, @Out String <error descr="The parameter type annotated with @Out must be \"org.seasar.doma.jdbc.Reference\"">outParam</error>,@ResultSet List<Optional<<error descr="Cannot resolve symbol 'Emp'">Emp</error>>> <error descr="\"Optional<Emp>\" is illegal as the type argument of \"java.util.List\"">optional</error>);
17+
18+
@Function
19+
int executeFuncValidType(@Out Reference<<error descr="Cannot resolve symbol 'Optional'">Optional</error><String>> <error descr="\"Optional<String>\" is illegal as the type argument of \"org.seasar.doma.jdbc.Reference\"">out</error>,@ResultSet List<Optional<<error descr="Cannot resolve symbol 'Address'">Address</error>>> <error descr="\"Optional<Address>\" is illegal as the type argument of \"java.util.List\"">optional</error>);
20+
}
21+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package doma.example.dao.inspection.paramtype;
2+
3+
import org.seasar.doma.*;
4+
5+
@Dao
6+
public interface ScriptParamTestDao {
7+
8+
@Script
9+
void <error descr="The number of parameters must be zero">scriptString</error>(String script);
10+
11+
@Script
12+
void <error descr="The number of parameters must be zero">scriptInt</error>(int value);
13+
}
14+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package doma.example.dao.inspection.paramtype;
2+
3+
import org.seasar.doma.*;
4+
import org.seasar.doma.message.Message;
5+
import doma.example.entity.*;
6+
7+
import java.math.BigDecimal;
8+
import java.util.Optional;
9+
import java.util.stream.Collector;
10+
import java.util.stream.Stream;
11+
import java.util.function.Function;
12+
13+
@Dao
14+
public interface SelectParamTestDao {
15+
16+
@Select
17+
Pckt selectPcktById(Integer id);
18+
19+
@Select
20+
@Sql("SELECT * FROM packet WHERE id = /* pckt.id */0")
21+
Pckt selectEmp(Pckt pckt);
22+
23+
@Select(strategy = SelectType.STREAM)
24+
@Sql("Select 10000 from user where name = /* name */'name' and salary = /* salary */0")
25+
Stream<Packet> selectReturnStreamWithStreamOption(String name, BigDecimal salary);
26+
27+
@Select
28+
@Sql("Select 10000 from user")
29+
Stream<Packet> selectReturnStreamWithOutStreamOption(Function<Stream<Packet>, BigDecimal> streams);
30+
31+
@Select(strategy = SelectType.STREAM)
32+
@Sql("Select 10000 from user")
33+
Integer selectReturnStream(Function<Stream<Pckt>, BigDecimal> stream);
34+
35+
@Select(strategy = SelectType.STREAM)
36+
@Sql("Select 10000 from user")
37+
BigDecimal selectStream(Function<Stream<Pckt>, BigDecimal> stream);
38+
39+
@Suppress(messages = { Message.DOMA4274 })
40+
@Select
41+
@Sql("Select 10000 from user where name = /* name */'name' and salary = /* salary */0")
42+
Stream<Package> selectReturnStream(String name, BigDecimal salary);
43+
44+
@Select
45+
@Sql("select * from packet where salary > /* salary */0")
46+
Pckt selectCollectNotOption(BigDecimal salary, Collector<Packet, BigDecimal, Pckt> collector);
47+
48+
@Select(strategy = SelectType.COLLECT)
49+
@Sql("select * from packet where salary > /* salary */0")
50+
Pckt selectCollectAccumulation(BigDecimal salary, Packet packet);
51+
52+
@Select(strategy = SelectType.COLLECT)
53+
@Sql("select * from emp where salary > /* salary */0")
54+
Optional<Packet> selectCollectInValidParamResult(BigDecimal salary,Collector<Packet, ?, Optional<Packet>> collector);
55+
56+
@Select(strategy = SelectType.COLLECT)
57+
@Sql("select * from emp where salary > /* salary */0")
58+
Pckt selectCollectAccumulation(BigDecimal salary, Collector<Packet, BigDecimal, Pckt> collector);
59+
}
60+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package doma.example.dao.inspection.paramtype;
2+
3+
import org.seasar.doma.*;
4+
import doma.example.entity.*;
5+
import java.util.function.BiFunction;
6+
import org.seasar.doma.jdbc.Config;
7+
import org.seasar.doma.jdbc.PreparedSql;
8+
9+
@Dao
10+
public interface SqlProcessorParamTestDao {
11+
12+
@SqlProcessor
13+
@Sql("")
14+
<R> R <error descr="When you annotate the method with @SqlProcessor, the BiFunction parameter is required for the method">executeNotBiFunction</error>(Integer id);
15+
16+
@SqlProcessor
17+
@Sql("")
18+
<R> R executeBiFunctionNotConfig(BiFunction<String,PreparedSql, R> <error descr="The first type argument of BiFunction must be org.seasar.doma.jdbc.Config">func</error>);
19+
20+
@SqlProcessor
21+
<R> R executeBiFunctionNotPreparedSql(BiFunction<Config,String, R> <error descr="The second type argument of BiFunction must be org.seasar.doma.jdbc.PreparedSql">func</error>);
22+
23+
@SqlProcessor
24+
<R> R executeR(Integer id,BiFunction<Config, PreparedSql, Pckt> func);
25+
26+
@SqlProcessor
27+
Integer executeInteger(Integer id,BiFunction<Config, PreparedSql, Pckt> func);
28+
29+
@SqlProcessor
30+
Pckt executePckt(BiFunction<Config, PreparedSql, Pckt> func);
31+
32+
@SqlProcessor
33+
<R> R execute(BiFunction<Config, PreparedSql, R> func);
34+
}
35+

0 commit comments

Comments
 (0)