Skip to content

Commit dc11870

Browse files
authored
#20: Implemented Security Rule Y2 (#35)
1 parent f699ca9 commit dc11870

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/test/java/com/devonfw/sample/archunit/SecurityTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.devonfw.sample.archunit;
22

3+
import com.tngtech.archunit.base.DescribedPredicate;
4+
import com.tngtech.archunit.core.domain.JavaMethodCall;
5+
import com.tngtech.archunit.core.domain.JavaType;
36
import com.tngtech.archunit.core.importer.ImportOption;
47
import com.tngtech.archunit.junit.AnalyzeClasses;
58
import com.tngtech.archunit.junit.ArchTest;
@@ -9,7 +12,10 @@
912
import javax.annotation.security.PermitAll;
1013
import javax.annotation.security.RolesAllowed;
1114

15+
import java.util.List;
16+
1217
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods;
18+
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
1319

1420
/**
1521
* JUnit test that validates the security rules of this application.
@@ -31,4 +37,28 @@ public class SecurityTest {
3137
.orShould().beAnnotatedWith(DenyAll.class)
3238
.because("All Use-Case implementation methods must be annotated with a security " +
3339
"constraint from javax.annotation.security");
40+
41+
/**
42+
* Checks if these methods are being used
43+
* Query createQuery(String qlString)
44+
* <T> TypedQuery<T> createQuery(String qlString, Class<T> resultClass)
45+
* Query createNativeQuery(String sqlString)
46+
* Query createNativeQuery(String sqlString, Class resultClass)
47+
* Query createNativeQuery(String sqlString, String resultSetMapping)
48+
*/
49+
@ArchTest
50+
private static final ArchRule shouldnTUseCreateQuery = noClasses().should().callMethodWhere(new DescribedPredicate<JavaMethodCall>("test whether CreateQuery or CreateNativQuery is used") {
51+
@Override
52+
public boolean test(JavaMethodCall javaMethod) {
53+
if(javaMethod.getName().equals("createQuery")){
54+
return parameterCheck(javaMethod.getTarget().getParameterTypes());
55+
}else if(javaMethod.getName().equals("createNativeQuery")){
56+
return parameterCheck(javaMethod.getTarget().getParameterTypes());
57+
}
58+
return false;
59+
}
60+
public boolean parameterCheck(List<JavaType> parameters){
61+
return(!parameters.isEmpty() &&
62+
parameters.get(0).getName().equals(String.class.getName()));}
63+
});
3464
}

0 commit comments

Comments
 (0)