Code cleanup. Adding more tests.
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/HEXValueConverter_toValue_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/HEXValueConverter_toValue_Test.java
index 992040f..50b4256 100644
--- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/HEXValueConverter_toValue_Test.java
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/HEXValueConverter_toValue_Test.java
@@ -10,9 +10,10 @@
import static java.util.Arrays.asList;
import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
-import com.google.eclipse.protobuf.junit.core.XtextRule;
+import java.util.Collection;
import org.eclipse.xtext.nodemodel.INode;
import org.junit.*;
@@ -20,11 +21,11 @@
import org.junit.runners.*;
import org.junit.runners.Parameterized.Parameters;
-import java.util.*;
+import com.google.eclipse.protobuf.junit.core.XtextRule;
/**
* Tests for <code>{@link HEXValueConverter#toValue(String, INode)}</code>.
- *
+ *
* @author alruiz@google.com (Alex Ruiz)
*/
@RunWith(Parameterized.class)
@@ -34,7 +35,7 @@
private final String hexadecimal;
private final Integer expected;
-
+
@Parameters
public static Collection<Object[]> parameters() {
return asList(new Object[][] {
@@ -43,22 +44,22 @@
{ "0xFF", 255 }
});
}
-
+
public HEXValueConverter_toValue_Test(String hexadecimal, Integer expected) {
this.hexadecimal = hexadecimal;
this.expected = expected;
}
-
+
private HEXValueConverter converter;
private INode node;
-
+
@Before public void setUp() {
node = mock(INode.class);
converter = xtext.injector().getInstance(HEXValueConverter.class);
}
-
+
@Test public void should_parse_hexadecimal_number() {
Integer value = converter.toValue(hexadecimal, node);
- Assert.assertThat(value, equalTo(expected));
+ assertThat(value, equalTo(expected));
}
}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/HEXValueConverter_toValue_withInvalidInput_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/HEXValueConverter_toValue_withInvalidInput_Test.java
new file mode 100644
index 0000000..9a01e03
--- /dev/null
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/HEXValueConverter_toValue_withInvalidInput_Test.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2011 Google Inc.
+ *
+ * All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ *
+ * http://www.eclipse.org/legal/epl-v10.html
+ */
+package com.google.eclipse.protobuf.conversion;
+
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
+import static org.junit.Assert.*;
+import static org.junit.rules.ExpectedException.none;
+import static org.mockito.Mockito.mock;
+
+import org.eclipse.xtext.conversion.ValueConverterException;
+import org.eclipse.xtext.nodemodel.INode;
+import org.junit.*;
+import org.junit.rules.ExpectedException;
+
+import com.google.eclipse.protobuf.junit.core.XtextRule;
+
+/**
+ * Tests for <code>{@link HEXValueConverter#toValue(String, INode)}</code>.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class HEXValueConverter_toValue_withInvalidInput_Test {
+
+ @Rule public XtextRule xtext = new XtextRule();
+ @Rule public ExpectedException thrown = none();
+
+ private HEXValueConverter converter;
+ private INode node;
+
+ @Before public void setUp() {
+ node = mock(INode.class);
+ converter = xtext.injector().getInstance(HEXValueConverter.class);
+ }
+
+ @Test public void should_throw_error_if_input_is_null() {
+ thrown.expect(ValueConverterException.class);
+ thrown.expectMessage("Couldn't convert empty string to int.");
+ converter.toValue(null, node);
+ }
+
+ @Test public void should_throw_error_if_input_is_empty() {
+ thrown.expect(ValueConverterException.class);
+ thrown.expectMessage("Couldn't convert empty string to int.");
+ converter.toValue("", node);
+ }
+
+ @Test public void should_throw_error_if_input_has_less_than_three_characters() {
+ thrown.expect(ValueConverterException.class);
+ thrown.expectMessage("Couldn't convert '0x' to int.");
+ converter.toValue("0x", node);
+ }
+
+ @Test public void should_throw_error_if_input_does_not_start_with_0x() {
+ thrown.expect(ValueConverterException.class);
+ thrown.expectMessage("Couldn't convert '65' to int.");
+ converter.toValue("65", node);
+ }
+
+ @Test public void should_throw_error_if_conversion_throws_NumberFormatException() {
+ try {
+ converter.toValue("0xZ", node);
+ fail("Expecting a " + ValueConverterException.class.getName());
+ } catch (ValueConverterException e) {
+ assertThat(e.getMessage(), equalTo("Couldn't convert '0xZ' to int."));
+ assertThat(e.getCause(), instanceOf(NumberFormatException.class));
+ }
+ }
+}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/HEXValueConverter.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/HEXValueConverter.java
index 92c2cbd..d731b46 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/HEXValueConverter.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/HEXValueConverter.java
@@ -15,18 +15,31 @@
import org.eclipse.xtext.nodemodel.INode;
/**
+ * Converts hexadecimal numbers to {@code int}s.
+ *
* @author alruiz@google.com (Alex Ruiz)
*/
public class HEXValueConverter extends AbstractLexerBasedConverter<Integer> {
+ /**
+ * Creates am {@code int} from the given input, if the given input represents an hexadecimal number.
+ * @param string the given input.
+ * @param node the parsed node including hidden parts.
+ * @return the new integer.
+ * @throws ValueConverterException if the given input is {@code null}, empty or does not represent an hexadecimal
+ * number.
+ */
public Integer toValue(String string, INode node) throws ValueConverterException {
if (isEmpty(string)) throw new ValueConverterException("Couldn't convert empty string to int.", node, null);
int length = string.length();
if (length < 3) throw parsingError(string, node);
if (!string.substring(0, 2).equalsIgnoreCase("0x")) throw parsingError(string, node);
String val = string.substring(2, length);
- int parsed = Integer.parseInt(val, 16);
- return parsed;
+ try {
+ return Integer.parseInt(val, 16);
+ } catch (NumberFormatException e) {
+ throw parsingError(string, node, e);
+ }
}
private ValueConverterException parsingError(String string, INode node) {