Skip to content

Commit

Permalink
allow 'true' either as string or boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Sep 12, 2024
1 parent cc853c2 commit fc1a56d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class CSSInclude {
@RequestAttribute(injectionStrategy = InjectionStrategy.OPTIONAL)
private String rel;
@RequestAttribute(injectionStrategy = InjectionStrategy.OPTIONAL)
private String allowMultipleIncludes;
private Object allowMultipleIncludes;
@RequestAttribute(injectionStrategy = InjectionStrategy.OPTIONAL)
private Object customAttributes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class JSInclude {
@RequestAttribute(injectionStrategy = InjectionStrategy.OPTIONAL)
private String type;
@RequestAttribute(injectionStrategy = InjectionStrategy.OPTIONAL)
private String allowMultipleIncludes;
private Object allowMultipleIncludes;
@RequestAttribute(injectionStrategy = InjectionStrategy.OPTIONAL)
private Object customAttributes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class RequestIncludedLibraries {
private final boolean allowMultipleIncludes;

RequestIncludedLibraries(@NotNull SlingHttpServletRequest request,
@Nullable String allowMultipleIncludes) {
@Nullable Object allowMultipleIncludes) {
this.request = request;
this.allowMultipleIncludes = BooleanUtils.toBoolean(allowMultipleIncludes);
this.allowMultipleIncludes = toBoolean(allowMultipleIncludes);
}

/**
Expand Down Expand Up @@ -100,4 +100,14 @@ String buildMarkupIgnoringDuplicateLibraries(@NotNull List<String> libraryPaths,
return markup.toString();
}

private static boolean toBoolean(Object value) {
if (value instanceof Boolean) {
return (Boolean)value;
}
else if (value instanceof String) {
return BooleanUtils.toBoolean((String)value);
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@

import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

import org.apache.sling.xss.XSSAPI;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.provider.Arguments;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
Expand Down Expand Up @@ -99,4 +101,10 @@ public String answer(InvocationOnMock invocation) throws Throwable {
return clientlib;
}

static Stream<Arguments> booleanTrueVariants() {
return Stream.of(
Arguments.of(true),
Arguments.of("true"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
Expand Down Expand Up @@ -148,21 +149,22 @@ void testMultiIgnoreDuplicates() {
underTest.getInclude());
}

@Test
void testMultiAllowDuplicates() {
@ParameterizedTest
@MethodSource("booleanTrueVariants")
void testMultiAllowDuplicates(Object trueValue) {
final String expectedInclude = "<link href=\"/etc/clientlibs/app1/clientlib3.min.css\" rel=\"stylesheet\" type=\"text/css\">\n"
+ "<link href=\"/etc.clientlibs/app1/clientlibs/clientlib4_proxy.min.css\" rel=\"stylesheet\" type=\"text/css\">\n"
+ "<link href=\"/etc.clientlibs/app1/clientlibs/clientlib5_proxy.min.css\" rel=\"stylesheet\" type=\"text/css\">\n";

context.request().setAttribute("categories", CATEGORIES_MULTIPLE);
context.request().setAttribute("allowMultipleIncludes", "true");
context.request().setAttribute("allowMultipleIncludes", trueValue);
CSSInclude underTest = AdaptTo.notNull(context.request(), CSSInclude.class);
assertEquals(expectedInclude,
underTest.getInclude());

// include again - should be included again (no duplicates removed)
context.request().setAttribute("categories", CATEGORIES_MULTIPLE);
context.request().setAttribute("allowMultipleIncludes", "true");
context.request().setAttribute("allowMultipleIncludes", trueValue);
underTest = AdaptTo.notNull(context.request(), CSSInclude.class);
assertEquals(expectedInclude, underTest.getInclude());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.apache.sling.api.resource.PersistenceException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
Expand Down Expand Up @@ -173,20 +175,21 @@ void testMultiIgnoreDuplicates() {
underTest.getInclude());
}

@Test
void testMultiAllowDuplicates() {
@ParameterizedTest
@MethodSource("booleanTrueVariants")
void testMultiAllowDuplicates(Object trueValue) {
final String expectedInclude = "<script src=\"/etc/clientlibs/app1/clientlib3.min.js\"></script>\n"
+ "<script src=\"/etc.clientlibs/app1/clientlibs/clientlib4_proxy.min.js\"></script>\n"
+ "<script src=\"/etc.clientlibs/app1/clientlibs/clientlib5_proxy.min.js\"></script>\n";

context.request().setAttribute("categories", CATEGORIES_MULTIPLE);
context.request().setAttribute("allowMultipleIncludes", "true");
context.request().setAttribute("allowMultipleIncludes", trueValue);
JSInclude underTest = AdaptTo.notNull(context.request(), JSInclude.class);
assertEquals(expectedInclude, underTest.getInclude());

// include again - should be included again (no duplicates removed)
context.request().setAttribute("categories", CATEGORIES_MULTIPLE);
context.request().setAttribute("allowMultipleIncludes", "true");
context.request().setAttribute("allowMultipleIncludes", trueValue);
underTest = AdaptTo.notNull(context.request(), JSInclude.class);
assertEquals(expectedInclude, underTest.getInclude());
}
Expand Down

0 comments on commit fc1a56d

Please sign in to comment.