/************************************************************************* * * * EJBCA: The OpenSource Certificate Authority * * * * This software is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or any later version. * * * * See terms of license at gnu.org. * * * *************************************************************************/ package org.cesecore.util.ui; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.Arrays; import java.util.HashSet; import java.util.List; import org.cesecore.roles.Role; import org.cesecore.roles.RoleData; import org.junit.Test; /** * @version $Id: DynamicUiPropertyTest.java 30189 2018-10-25 13:51:44Z samuellb $ * */ public class DynamicUiPropertyTest { @Test public void testEncodingAndDecodingOfComplexType() throws PropertyValidationException { RoleData anybody = new RoleData(new Role(null, "anybody")); DynamicUiProperty roleProperty = new DynamicUiProperty<>("test", anybody, new HashSet<>(Arrays.asList(anybody))); roleProperty.setHasMultipleValues(true); List encodedValues = roleProperty.getEncodedValues(); DynamicUiProperty rolePropertyCopy = new DynamicUiProperty<>("test", anybody, new HashSet()); rolePropertyCopy.setHasMultipleValues(true); rolePropertyCopy.setEncodedValues(encodedValues); assertTrue("RoleData object didn't survive encodement/decodement", rolePropertyCopy.getValues().contains(anybody)); } @Test public void testConstructors() { final RoleData anybody = new RoleData(new Role(null, "anybody")); DynamicUiProperty property = new DynamicUiProperty<>("someproperty", anybody); checkPropertyState(property, "constructor with default value"); property = new DynamicUiProperty<>(property); checkPropertyState(property, "copy constructor"); } @Test public void testSetValue() throws PropertyValidationException { final RoleData anybody = new RoleData(new Role(null, "anybody")); DynamicUiProperty property = new DynamicUiProperty<>(); property.setValue(anybody); checkPropertyState(property, "setValue(something)"); assertEquals(anybody, property.getValue()); property.setValue(null); checkPropertyState(property, "setValue(null)"); property.setEncodedValue(new DynamicUiProperty().getAsEncodedValue(anybody)); checkPropertyState(property, "setEncodedValue(something)"); assertEquals(anybody, property.getValue()); } private void checkPropertyState(final DynamicUiProperty property, final String step) { property.getValue(); // Make sure we can get the value try { property.getValues(); fail("getValues() call should not be allowed (at step \"" + step + "\")."); } catch (IllegalStateException e) { // NOPMD expected } } }