*/
public void setMax(double max) {
getState().maxValue = max;
+
+ if (getMin() > max) {
+ getState().minValue = max;
+ }
+
if (getValue() > max) {
setValue(max);
}
* Set the minimum slider value. If the current value of the slider is
* smaller than this, the value is set to the new minimum.
*
- * @param max
+ * @param min
* The new minimum slider value
*/
public void setMin(double min) {
getState().minValue = min;
+
+ if (getMax() < min) {
+ getState().maxValue = min;
+ }
+
if (getValue() < min) {
setValue(min);
}
newValue = (int) (v * Math.pow(10, resolution));
newValue = newValue / Math.pow(10, resolution);
if (getMin() > newValue || getMax() < newValue) {
- throw new ValueOutOfBoundsException(value);
+ throw new ValueOutOfBoundsException(newValue);
}
} else {
newValue = (int) v;
if (getMin() > newValue || getMax() < newValue) {
- throw new ValueOutOfBoundsException(value);
+ throw new ValueOutOfBoundsException(newValue);
}
}
* @param valueOutOfBounds
*/
public ValueOutOfBoundsException(Double valueOutOfBounds) {
+ super(String.format("Value %s is out of bounds: [%s, %s]",
+ valueOutOfBounds, getMin(), getMax()));
value = valueOutOfBounds;
}
package com.vaadin.tests.server.component.slider;
-import junit.framework.TestCase;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
import org.junit.Assert;
+import org.junit.Test;
import com.vaadin.ui.Slider;
-import com.vaadin.ui.Slider.ValueOutOfBoundsException;
-public class SliderTest extends TestCase {
+public class SliderTest {
- public void testOutOfBounds() {
+ @Test
+ public void minCannotBeLargerThanMax() {
+ Slider slider = new Slider();
+
+ slider.setMax(100);
+ slider.setMin(101);
+
+ assertThat(slider.getMin(), is(101.0));
+ assertThat(slider.getMax(), is(101.0));
+ }
+
+ @Test
+ public void maxCannotBeSmallerThanMin() {
+ Slider slider = new Slider();
+
+ slider.setMin(50);
+ slider.setMax(10);
+
+ assertThat(slider.getMax(), is(10.0));
+ assertThat(slider.getMin(), is(10.0));
+ }
+
+ @Test
+ public void valueOutOfBoundsExceptionMessageContainsBounds() {
+ Slider slider = new Slider();
+
+ try {
+
+ slider.setValue(-1.0);
+ } catch (Slider.ValueOutOfBoundsException e) {
+ assertThat(e.getMessage(),
+ containsString("Value -1.0 is out of bounds: [0.0, 100.0]"));
+ }
+ }
+
+ @Test
+ public void valueIsSet() {
+ Slider slider = new Slider();
+
+ slider.setValue(5.0);
+
+ assertThat(slider.getValue(), is(5.0));
+ }
+
+ @Test
+ public void valueCannotBeOutOfBounds() {
Slider s = new Slider(0, 10);
- s.setValue(0.0);
- Assert.assertEquals(0.0, s.getValue().doubleValue(), 0.001);
- s.setValue(10.0);
- Assert.assertEquals(10.0, s.getValue().doubleValue(), 0.001);
+
try {
s.setValue(20.0);
- fail("Should throw out of bounds exception");
- } catch (ValueOutOfBoundsException e) {
+ Assert.fail("Should throw out of bounds exception");
+ } catch (Slider.ValueOutOfBoundsException e) {
// TODO: handle exception
}