Skip to content

Commit

Permalink
refactor: pass the complete package name as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-godoy authored and paodb committed Oct 8, 2024
1 parent 3d9077b commit 462b74d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 39 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ static void setImpl(AllowedPackages impl) {
* <p>
* 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.
* <p>
* 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 <T extends Component> void check(T component, Class<? super T> type) {
check(component, type, 0);
String componentPackageName = type.getPackage().getName();
check(component, type, componentPackageName);
}

/**
Expand All @@ -69,46 +68,30 @@ public static <T extends Component> void check(T component, Class<? super T> 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.
* <p>
* 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 <T extends Component> void check(T component, Class<? super T> type, int removePackages) {
if (removePackages > 0) {
public static <T extends Component> void check(T component, Class<? super T> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

}

0 comments on commit 462b74d

Please sign in to comment.