Skip to content

Commit

Permalink
Fixing SwitchComponent on myfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
cuioss committed Jul 22, 2024
1 parent 6a71e28 commit 37abbe0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import de.cuioss.jsf.bootstrap.BootstrapFamily;
import de.cuioss.jsf.bootstrap.common.partial.ColumnProvider;
import de.cuioss.tools.collect.MapBuilder;
import de.cuioss.tools.logging.CuiLogger;
import jakarta.faces.component.FacesComponent;
import jakarta.faces.component.behavior.ClientBehavior;
import jakarta.faces.event.ComponentSystemEvent;
Expand All @@ -44,6 +45,8 @@
@SuppressWarnings("squid:MaximumInheritanceDepth") // Artifact of Jsf-structure
public class SwitchComponent extends BaseCuiHtmlSelectBooleanCheckboxComponent {

private static final CuiLogger LOGGER = new CuiLogger(SwitchComponent.class);

private static final String OFF_TEXT_VALUE = "offTextValue";
private static final String OFF_TEXT_KEY = "offTextKey";
private static final String OFF_TEXT_CONVERTER = "offTextConverter";
Expand Down Expand Up @@ -254,6 +257,31 @@ private String replaceId(final String id) {
return id;
}

/**
* For some reason on myfaces this method is necessary. It used to work with mojarra:
* Exception:
* Portal-112: An unspecified exception has been caught and handled by fallback strategy: java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Boolean (java.lang.String and java.lang.Boolean are in module java.base of loader 'bootstrap')
* at jakarta.faces.component.UISelectBoolean.isSelected(UISelectBoolean.java:65)
* at de.cuioss.jsf.bootstrap.checkbox.SwitchRenderer.renderText(SwitchRenderer.java:139)
* at de.cuioss.jsf.bootstrap.checkbox.SwitchRenderer.doEncodeEnd(SwitchRenderer.java:85)
* at de.cuioss.jsf.bootstrap.checkbox.SwitchRenderer.doEncodeEnd(SwitchRenderer.java:52)
*/
@Override
public boolean isSelected() {
var submittedValue = getSubmittedValue();
LOGGER.debug("submittedValue={}", submittedValue);
if (null == submittedValue) {
LOGGER.debug("Nothing submitted");
return false;
}
if (submittedValue instanceof Boolean selected) {
LOGGER.debug("submittedBoolean={}", selected);
return selected;
}
LOGGER.debug("Using Boolean.parseBoolean()");
return Boolean.parseBoolean(submittedValue.toString());
}

@Override
public String getFamily() {
return BootstrapFamily.COMPONENT_FAMILY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package de.cuioss.jsf.bootstrap.checkbox;

import static de.cuioss.tools.collect.CollectionLiterals.immutableMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.*;

import de.cuioss.test.generator.domain.UUIDGenerator;
import org.junit.jupiter.api.Test;

import de.cuioss.jsf.bootstrap.BootstrapFamily;
Expand Down Expand Up @@ -84,4 +84,36 @@ void shouldResolvePassThroughAttributes() {
underTest.setDisabled(true);
assertEquals(immutableMap("data-switch-disabled", "true"), underTest.resolvePassThroughAttributes());
}

@Test
void shouldResolveSelectedWithEmpty() {
var component = anyComponent();
component.setSubmittedValue(null);
assertFalse(component.isSelected());
}

@Test
void shouldResolveSelectedWithBoolean() {
var component = anyComponent();
component.setSubmittedValue(Boolean.TRUE);
assertTrue(component.isSelected());
component.setSubmittedValue(Boolean.FALSE);
assertFalse(component.isSelected());
}

@Test
void shouldResolveSelectedWithString() {
var component = anyComponent();
component.setSubmittedValue(Boolean.TRUE.toString());
assertTrue(component.isSelected());
component.setSubmittedValue(Boolean.FALSE.toString());
assertFalse(component.isSelected());
}

@Test
void shouldResolveSelectedWithOther() {
var component = anyComponent();
component.setSubmittedValue(Generators.runtimeExceptions().next());
assertFalse(component.isSelected());
}
}

0 comments on commit 37abbe0

Please sign in to comment.