Skip to content

Commit

Permalink
fix: Fix NumberField allow null value. Fix number conversion tests to…
Browse files Browse the repository at this point in the history
… be system independent
  • Loading branch information
TatuJLund committed Sep 24, 2024
1 parent e34a1ed commit 782ba76
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ public void mergeDraft(Product draft) {
updateDirtyIndicators();
}

@Override
public void focus() {
productName.focus();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public NumberField(String string) {
});
fireEvent(event);
});
textField.setWidth("100%");
}

private void setTypeNumber() {
Expand All @@ -81,6 +80,14 @@ protected TextField initContent() {
return textField;
}

@Override
public void setValue(Integer value) {
if (value == null) {
value = 0;
}
super.setValue(value);
}

@Override
protected void doSetValue(Integer value) {
textField.setValue(stockCountConverter.convertToPresentation(value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,7 @@ $fake-grid-background-color: white;
-webkit-appearance: none;
margin: 0;
}
input[type=number] {
appearance: textfield;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,34 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.vaadin.tatu.vaadincreate.i18n.DefaultI18NProvider;

import com.vaadin.data.ValueContext;
import com.vaadin.server.ServiceException;
import com.vaadin.testbench.uiunittest.UIUnitTest;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;

public class NumberFieldTest extends UIUnitTest {

private final static char MINUS = '\u002D';
private static final String ERROR_MESSAGE = "Cannot convert value to a number.";
UI ui;
private UI ui;
private NumberField field;

@Before
public void setup() throws ServiceException {
// Vaadin mocks
Locale.setDefault(DefaultI18NProvider.LOCALE_EN);

ui = mockVaadin();
ui.getSession().setLocale(DefaultI18NProvider.LOCALE_EN);
field = new NumberField("Field");
ui.setContent(field);
}
Expand All @@ -44,7 +52,7 @@ public void setValueByText() {
test(field.textField).setValue("20");
assertEquals(Integer.valueOf(20), field.getValue());

test(field.textField).setValue("-20");
test(field.textField).setValue(MINUS + "20");
assertEquals(Integer.valueOf(-20), field.getValue());
}

Expand Down Expand Up @@ -85,4 +93,10 @@ public void valueChangeEvent() {
assertEquals(3, count.get());
assertEquals(1, user.get());
}

@Test
public void setValueNull() {
field.setValue(null);
assertEquals(Integer.valueOf(0), field.getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Locale;

import org.junit.Before;
import org.junit.Test;
import org.vaadin.tatu.vaadincreate.crud.form.StockCountConverter;

Expand All @@ -15,42 +17,70 @@

public class StockCountConverterTest {

private static final String ERROR_MESSAGE = "Invalid stock count";
private final static char MINUS = '\u002D';
private StockCountConverter converter;
private TextField field;
private ValueContext context;

@Before
public void setup() {
Locale.setDefault(Locale.ENGLISH);
converter = new StockCountConverter(ERROR_MESSAGE);

field = new TextField();
context = new ValueContext(null, field, Locale.ENGLISH);
}

@Test
public void testConvertToModel() {
var converter = new StockCountConverter("Invalid stock count");

// Test with valid stock count
var result = converter.convertToModel("10", new ValueContext());
var result = converter.convertToModel("10", context);
assertNotNull(result);
assertFalse(result.isError());
result.ifOk(value -> assertEquals(Integer.valueOf(10), value));

// Test with negative stock count
result = converter.convertToModel(MINUS + "10", context);
assertNotNull(result);
assertFalse(result.isError());
result.ifOk(value -> assertEquals(Integer.valueOf(-10), value));

// Test with null stock count
result = converter.convertToModel(null, new ValueContext());
result = converter.convertToModel(null, context);
assertNotNull(result);
assertFalse(result.isError());
result.ifOk(value -> assertEquals(Integer.valueOf(0), value));

// Test with invalid stock count
result = converter.convertToModel("abc", new ValueContext());
result = converter.convertToModel("2.2", context);
assertNotNull(result);
assertTrue(result.isError());
result.ifError(error -> assertEquals("Invalid stock count", error));
result.ifError(error -> assertEquals(ERROR_MESSAGE, error));

// Test with invalid stock count
result = converter.convertToModel("10 000", new ValueContext());
result = converter.convertToModel("abc", context);
assertNotNull(result);
assertTrue(result.isError());
result.ifError(error -> assertEquals("Invalid stock count", error));
result.ifError(error -> assertEquals(ERROR_MESSAGE, error));

// Test with invalid stock count
result = converter.convertToModel("10 000", context);
assertNotNull(result);
assertTrue(result.isError());
result.ifError(error -> assertEquals(ERROR_MESSAGE, error));
}

@Test
public void testConvertToPresentation() {
var converter = new StockCountConverter("Invalid stock count");
var field = new TextField();
var context = new ValueContext(null, field, new Locale("fi"));

var result = converter.convertToPresentation(1000, context);
assertEquals("1000", result);

result = converter.convertToPresentation(-1000, context);
assertEquals(MINUS + "1000", result);

result = converter.convertToPresentation(null, context);
assertNull(result);
}
}

0 comments on commit 782ba76

Please sign in to comment.