From 462b74dc82d3f312b82a1f1ce1dc0ea54a6cba28 Mon Sep 17 00:00:00 2001 From: Javier Godoy <11554739+javier-godoy@users.noreply.github.com> Date: Tue, 8 Oct 2024 16:09:01 -0300 Subject: [PATCH] refactor: pass the complete package name as argument --- README.md | 4 +- .../utils/checker/AllowedPackageChecker.java | 45 ++++++------------- .../checker/TestAllowedPackagesChecker.java | 23 +++++++--- 3 files changed, 33 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 3ab2553..b65dcf9 100644 --- a/README.md +++ b/README.md @@ -73,14 +73,14 @@ public class MyComponent extends Div { } ``` -Check that `com.flowingcode.vaadin.addons.example` is included in the allowed-packages list. If missing, show an alert in the browser (the alert will reference `com.flowingcode.vaadin.addons`, i.e. the package name with one subpackage removed). +Check that `com.flowingcode.vaadin.addons.example` is included in the allowed-packages list. If missing, show an alert in the browser (the alert will reference `com.flowingcode`). ``` package com.flowingcode.vaadin.addons.example; public class MyComponent extends Div { public MyComponent() { - AllowedPackageChecker.check(this, MyComponent.class, -1); + AllowedPackageChecker.check(this, MyComponent.class, "com.flowingcode"); } } diff --git a/addon-utils/src/main/java/com/flowingcode/vaadin/addons/utils/checker/AllowedPackageChecker.java b/addon-utils/src/main/java/com/flowingcode/vaadin/addons/utils/checker/AllowedPackageChecker.java index b942dca..eebbe5a 100644 --- a/addon-utils/src/main/java/com/flowingcode/vaadin/addons/utils/checker/AllowedPackageChecker.java +++ b/addon-utils/src/main/java/com/flowingcode/vaadin/addons/utils/checker/AllowedPackageChecker.java @@ -52,14 +52,13 @@ static void setImpl(AllowedPackages impl) { *

* The check is performed only in development mode for Spring Boot applications that have an * allowed packages list. No check is done in production mode. - *

- * This method is equivalent to {@link #check(Component, String, int) check(component,type,0)}. * * @param component the component instance to be checked. * @param type the type of the component to be checked. */ public static void check(T component, Class type) { - check(component, type, 0); + String componentPackageName = type.getPackage().getName(); + check(component, type, componentPackageName); } /** @@ -69,46 +68,30 @@ public static void check(T component, Class typ * The check is performed only in development mode for Spring Boot applications that have an * allowed packages list. No check is done in production mode. *

- * When displaying the error message, {@code -removePackages} subpackages are removed from the - * package of {@code type}. For instance, if {@code type} is - * {@code com.flowingcode.vaadin.addons.utils} and {@code removePackages} is {@code -1}, the error - * will reference {@code com.flowingcode.vaadin.addons}. + * The error message will reference {@code packageName}. * * @param component the component instance to be checked. * @param type the type of the component to be checked. - * @param removePackages a negative number specifying how many subpackage levels to discard from - * the type package when displaying the error message. - * @throws IllegalArgumentException if {@code removePackages} is positive, or if the package name - * does not contain enough subpackages to remove the specified number of levels. + * @param packageName the package to report in the error message. + * @throws NullPointerException if any argument is {@code null}. + * @throws IllegalArgumentException if the component's package does not match or is not a + * subpackage of {@code packageName}. */ - public static void check(T component, Class type, int removePackages) { - if (removePackages > 0) { + public static void check(T component, Class type, String packageName) { + String componentPackageName = type.getPackage().getName(); + if (!componentPackageName.equals(packageName) && !componentPackageName.startsWith(packageName + ".")) { throw new IllegalArgumentException(); } - String packageName = type.getPackage().getName(); - check(component, packageName, removePackages); + check(component, componentPackageName, packageName); } - private static void check(Component component, String packageName, int removePackages) { + private static void check(Component component, String componentPackageName, + String messagePackageName) { if (impl != null) { - // call removePackages before if in order to validate attribute - String messagePackageName = removePackages(packageName, removePackages); - if (!impl.isPackageAllowed(packageName)) { + if (!impl.isPackageAllowed(componentPackageName)) { component.getElement().executeJs(SCRIPT, messagePackageName); } } } - private static String removePackages(String packageName, int removePackages) { - while (removePackages != 0) { - int pos = packageName.lastIndexOf('.'); - if (pos < 0) { - throw new IllegalArgumentException(); - } - packageName = packageName.substring(0, pos); - removePackages++; - } - return packageName; - } - } diff --git a/addon-utils/src/test/java/com/flowingcode/vaadin/addons/utils/checker/TestAllowedPackagesChecker.java b/addon-utils/src/test/java/com/flowingcode/vaadin/addons/utils/checker/TestAllowedPackagesChecker.java index eaf2b6e..973fd0e 100644 --- a/addon-utils/src/test/java/com/flowingcode/vaadin/addons/utils/checker/TestAllowedPackagesChecker.java +++ b/addon-utils/src/test/java/com/flowingcode/vaadin/addons/utils/checker/TestAllowedPackagesChecker.java @@ -50,27 +50,38 @@ public void testCheck() { } @Test - public void testCheckMinus1() { + public void testCheckPacakge() { var impl = Mockito.mock(AllowedPackages.class); var component = mockComponent(); AllowedPackageChecker.setImpl(impl); - AllowedPackageChecker.check(component, Component.class, -1); + AllowedPackageChecker.check(component, Component.class); + verify(impl, times(1)).isPackageAllowed("com.vaadin.flow.component"); + verify(component.getElement(), times(1)).executeJs(any(), eq("com.vaadin.flow.component")); + } + + @Test + public void testCheckContainingPackage() { + var impl = Mockito.mock(AllowedPackages.class); + var component = mockComponent(); + AllowedPackageChecker.setImpl(impl); + + AllowedPackageChecker.check(component, Component.class, "com.vaadin.flow"); verify(impl, times(1)).isPackageAllowed("com.vaadin.flow.component"); verify(component.getElement(), times(1)).executeJs(any(), eq("com.vaadin.flow")); } @Test(expected = IllegalArgumentException.class) - public void testCheckMinus4() { + public void testIllegalArgument1() { var component = mockComponent(); - AllowedPackageChecker.check(component, Component.class, -4); + AllowedPackageChecker.check(component, Component.class, "com.vaadin.flow.component.grid"); } @Test(expected = IllegalArgumentException.class) - public void testCheckPlus1() { + public void testIllegalArgument2() { var component = mockComponent(); - AllowedPackageChecker.check(component, Component.class, +1); + AllowedPackageChecker.check(component, Component.class, "org.foo"); } }