11package 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 ;
36import com .tngtech .archunit .core .importer .ImportOption ;
47import com .tngtech .archunit .junit .AnalyzeClasses ;
58import com .tngtech .archunit .junit .ArchTest ;
912import javax .annotation .security .PermitAll ;
1013import javax .annotation .security .RolesAllowed ;
1114
15+ import java .util .List ;
16+
1217import 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