Skip to content

Commit 155d63d

Browse files
committed
Add tests for JAX-RS
1 parent f63fd68 commit 155d63d

File tree

5 files changed

+407
-1
lines changed

5 files changed

+407
-1
lines changed

java/ql/test/library-tests/frameworks/JaxWs/JaxRs.expected

Whitespace-only changes.
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import java
2+
import semmle.code.java.frameworks.JaxWS
3+
import TestUtilities.InlineExpectationsTest
4+
5+
class JaxRsTest extends InlineExpectationsTest {
6+
JaxRsTest() { this = "JaxRsTest" }
7+
8+
override string getARelevantTag() {
9+
result =
10+
[
11+
"ResourceMethod", "RootResourceClass", "NonRootResourceClass",
12+
"ResourceMethodOnResourceClass", "InjectableConstructor", "InjectableField",
13+
"InjectionAnnotation", "ResponseDeclaration", "ResponseBuilderDeclaration",
14+
"ClientDeclaration", "BeanParamConstructor", "MessageBodyReaderDeclaration",
15+
"MessageBodyReaderReadFromCall", "MessageBodyReaderReadCall", "ProducesAnnotation",
16+
"ConsumesAnnotation"
17+
]
18+
}
19+
20+
override predicate hasActualResult(Location location, string element, string tag, string value) {
21+
tag = "ResourceMethod" and
22+
exists(JaxRsResourceMethod resourceMethod |
23+
resourceMethod.getLocation() = location and
24+
element = resourceMethod.toString() and
25+
if exists(resourceMethod.getProducesAnnotation())
26+
then value = resourceMethod.getProducesAnnotation().getADeclaredContentType()
27+
else value = ""
28+
)
29+
or
30+
tag = "RootResourceClass" and
31+
exists(JaxRsResourceClass resourceClass |
32+
resourceClass.isRootResource() and
33+
resourceClass.getLocation() = location and
34+
element = resourceClass.toString() and
35+
value = ""
36+
)
37+
or
38+
tag = "NonRootResourceClass" and
39+
exists(JaxRsResourceClass resourceClass |
40+
not resourceClass.isRootResource() and
41+
resourceClass.getLocation() = location and
42+
element = resourceClass.toString() and
43+
value = ""
44+
)
45+
or
46+
tag = "ResourceMethodOnResourceClass" and
47+
exists(JaxRsResourceMethod resourceMethod |
48+
resourceMethod = any(JaxRsResourceClass ResourceClass).getAResourceMethod()
49+
|
50+
resourceMethod.getLocation() = location and
51+
element = resourceMethod.toString() and
52+
value = ""
53+
)
54+
or
55+
tag = "InjectableConstructor" and
56+
exists(Constructor cons |
57+
cons = any(JaxRsResourceClass resourceClass).getAnInjectableConstructor()
58+
|
59+
cons.getLocation() = location and
60+
element = cons.toString() and
61+
value = ""
62+
)
63+
or
64+
tag = "InjectableField" and
65+
exists(Field field | field = any(JaxRsResourceClass resourceClass).getAnInjectableField() |
66+
field.getLocation() = location and
67+
element = field.toString() and
68+
value = ""
69+
)
70+
or
71+
tag = "InjectionAnnotation" and
72+
exists(JaxRsInjectionAnnotation injectionAnnotation |
73+
injectionAnnotation.getLocation() = location and
74+
element = injectionAnnotation.toString() and
75+
value = ""
76+
)
77+
or
78+
tag = "ResponseDeclaration" and
79+
exists(LocalVariableDecl decl |
80+
decl.getType() instanceof JaxRsResponse and
81+
decl.getLocation() = location and
82+
element = decl.toString() and
83+
value = ""
84+
)
85+
or
86+
tag = "ResponseBuilderDeclaration" and
87+
exists(LocalVariableDecl decl |
88+
decl.getType() instanceof JaxRsResponseBuilder and
89+
decl.getLocation() = location and
90+
element = decl.toString() and
91+
value = ""
92+
)
93+
or
94+
tag = "ClientDeclaration" and
95+
exists(LocalVariableDecl decl |
96+
decl.getType() instanceof JaxRsClient and
97+
decl.getLocation() = location and
98+
element = decl.toString() and
99+
value = ""
100+
)
101+
or
102+
tag = "BeanParamConstructor" and
103+
exists(JaxRsBeanParamConstructor cons |
104+
cons.getLocation() = location and
105+
element = cons.toString() and
106+
value = ""
107+
)
108+
or
109+
tag = "MessageBodyReaderDeclaration" and
110+
exists(LocalVariableDecl decl |
111+
decl.getType().(RefType).getSourceDeclaration() instanceof MessageBodyReader and
112+
decl.getLocation() = location and
113+
element = decl.toString() and
114+
value = ""
115+
)
116+
or
117+
tag = "MessageBodyReaderReadFromCall" and
118+
exists(MethodAccess ma |
119+
ma.getMethod() instanceof MessageBodyReaderReadFrom and
120+
ma.getLocation() = location and
121+
element = ma.toString() and
122+
value = ""
123+
)
124+
or
125+
tag = "MessageBodyReaderReadCall" and
126+
exists(MethodAccess ma |
127+
ma.getMethod() instanceof MessageBodyReaderRead and
128+
ma.getLocation() = location and
129+
element = ma.toString() and
130+
value = ""
131+
)
132+
or
133+
tag = "ProducesAnnotation" and
134+
exists(JaxRSProducesAnnotation producesAnnotation |
135+
producesAnnotation.getLocation() = location and
136+
element = producesAnnotation.toString() and
137+
value = producesAnnotation.getADeclaredContentType()
138+
)
139+
or
140+
tag = "ConsumesAnnotation" and
141+
exists(JaxRSConsumesAnnotation consumesAnnotation |
142+
consumesAnnotation.getLocation() = location and
143+
element = consumesAnnotation.toString() and
144+
value = ""
145+
)
146+
}
147+
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import java.io.InputStream;
2+
import java.io.IOException;
3+
import java.lang.annotation.Annotation;
4+
import java.lang.reflect.Type;
5+
import javax.ws.rs.GET;
6+
import javax.ws.rs.POST;
7+
import javax.ws.rs.DELETE;
8+
import javax.ws.rs.PUT;
9+
import javax.ws.rs.OPTIONS;
10+
import javax.ws.rs.HEAD;
11+
import javax.ws.rs.Path;
12+
import javax.ws.rs.BeanParam;
13+
import javax.ws.rs.CookieParam;
14+
import javax.ws.rs.FormParam;
15+
import javax.ws.rs.HeaderParam;
16+
import javax.ws.rs.MatrixParam;
17+
import javax.ws.rs.PathParam;
18+
import javax.ws.rs.Produces;
19+
import javax.ws.rs.QueryParam;
20+
import javax.ws.rs.client.Client;
21+
import javax.ws.rs.core.Context;
22+
import javax.ws.rs.core.MediaType;
23+
import javax.ws.rs.core.MultivaluedMap;
24+
import javax.ws.rs.core.Response;
25+
import javax.ws.rs.ext.MessageBodyReader;
26+
27+
@Path("")
28+
public class JaxRs1 { // $RootResourceClass
29+
public JaxRs1() { // $InjectableConstructor
30+
}
31+
32+
@GET
33+
void Get() { // $ResourceMethod $ResourceMethodOnResourceClass
34+
}
35+
36+
@POST
37+
void Post() { // $ResourceMethod $ResourceMethodOnResourceClass
38+
}
39+
40+
@Produces("text/plain") // $ProducesAnnotation=text/plain
41+
@DELETE
42+
void Delete() { // $ResourceMethod=text/plain $ResourceMethodOnResourceClass
43+
}
44+
45+
@Produces(MediaType.TEXT_HTML) // $ProducesAnnotation=text/html
46+
@PUT
47+
void Put() { // $ResourceMethod=text/html $ResourceMethodOnResourceClass
48+
}
49+
50+
@OPTIONS
51+
void Options() { // $ResourceMethod $ResourceMethodOnResourceClass
52+
}
53+
54+
@HEAD
55+
void Head() { // $ResourceMethod $ResourceMethodOnResourceClass
56+
}
57+
58+
@Path("")
59+
NonRootResourceClass subResourceLocator() { // $SubResourceLocator
60+
return null;
61+
}
62+
}
63+
64+
class NonRootResourceClass { // $NonRootResourceClass
65+
@Path("")
66+
AnotherNonRootResourceClass subResourceLocator1() { // $SubResourceLocator
67+
return null;
68+
}
69+
70+
@GET
71+
@Path("")
72+
NotAResourceClass1 NotASubResourceLocator1() { // $ResourceMethod
73+
return null;
74+
}
75+
76+
@GET
77+
NotAResourceClass2 NotASubResourceLocator2() { // $ResourceMethod
78+
return null;
79+
}
80+
81+
NotAResourceClass2 NotASubResourceLocator3() {
82+
return null;
83+
}
84+
}
85+
86+
class AnotherNonRootResourceClass { // $NonRootResourceClass
87+
public AnotherNonRootResourceClass() {
88+
}
89+
90+
public AnotherNonRootResourceClass(@BeanParam int beanParam, @CookieParam("") int cookieParam, @FormParam("") int formParam, // $InjectionAnnotation
91+
@HeaderParam("") int headerParam, @MatrixParam("") int matrixParam, @PathParam("") int pathParam, @QueryParam("") int queryParam, // $InjectionAnnotation
92+
@Context int context) { // $InjectionAnnotation
93+
}
94+
95+
@Path("")
96+
public void resourceMethodWithBeanParamParameter(@BeanParam Foo foo) { // $SubResourceLocator $InjectionAnnotation
97+
}
98+
}
99+
100+
class Foo {
101+
Foo() { // $BeanParamConstructor
102+
}
103+
104+
public Foo(@BeanParam int beanParam, @CookieParam("") int cookieParam, @FormParam("") int formParam, // $InjectionAnnotation $BeanParamConstructor
105+
@HeaderParam("") int headerParam, @MatrixParam("") int matrixParam, @PathParam("") int pathParam, @QueryParam("") int queryParam, // $InjectionAnnotation
106+
@Context int context) { // $InjectionAnnotation
107+
}
108+
109+
public Foo(@BeanParam int beanParam, @CookieParam("") int cookieParam, @FormParam("") int formParam, // $InjectionAnnotation
110+
@HeaderParam("") int headerParam, @MatrixParam("") int matrixParam, @PathParam("") int pathParam, @QueryParam("") int queryParam, // $InjectionAnnotation
111+
@Context int context, int paramWithoutAnnotation) { // $InjectionAnnotation
112+
}
113+
}
114+
115+
class NotAResourceClass1 {
116+
}
117+
118+
class NotAResourceClass2 {
119+
}
120+
121+
class ExtendsJaxRs1 extends JaxRs1 {
122+
@Override
123+
void Get() { // $ResourceMethod
124+
}
125+
126+
@Override
127+
@QueryParam("") // $InjectionAnnotation
128+
void Post() {
129+
}
130+
131+
@Override
132+
void Delete() { // $ResourceMethod=text/plain
133+
}
134+
135+
@Override
136+
void Put() { // $ResourceMethod=text/html
137+
}
138+
139+
@Produces("application/json") // $ProducesAnnotation=application/json
140+
@Override
141+
void Options() {
142+
}
143+
144+
@Produces(MediaType.TEXT_XML) // $ProducesAnnotation=text/xml
145+
@Override
146+
void Head() {
147+
}
148+
149+
}
150+
151+
@Produces(MediaType.TEXT_XML) // $ProducesAnnotation=text/xml
152+
class ExtendsJaxRs1WithProducesAnnotation extends JaxRs1 {
153+
@Override
154+
void Get() { // $ResourceMethod=text/xml
155+
}
156+
157+
@Override
158+
@QueryParam("") // $InjectionAnnotation
159+
void Post() {
160+
}
161+
162+
@Override
163+
void Delete() { // $ResourceMethod=text/plain
164+
}
165+
166+
@Override
167+
void Put() { // $ResourceMethod=text/html
168+
}
169+
170+
@Override
171+
void Options() { // $ResourceMethod=text/xml
172+
}
173+
}

0 commit comments

Comments
 (0)