summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatti Hosio <mhosio@vaadin.com>2014-12-17 14:08:59 +0200
committerArtur Signell <artur@vaadin.com>2014-12-17 12:51:14 +0000
commit6d75a89e8d16e0fd1370fe080a5b5b78d1f4679d (patch)
tree0a7224c3008b3dca20b4de453cb346554be1735c
parent7818595589e73b3da119d3c51e3950515f900bb6 (diff)
downloadvaadin-framework-6d75a89e8d16e0fd1370fe080a5b5b78d1f4679d.tar.gz
vaadin-framework-6d75a89e8d16e0fd1370fe080a5b5b78d1f4679d.zip
Do not throw exception when trying to bind a preinitialized instance field (#7749)
Change-Id: I8b40e667d03b63e05f006e7cedf108345591d118
-rw-r--r--server/src/com/vaadin/ui/declarative/DesignContext.java9
-rw-r--r--server/src/com/vaadin/ui/declarative/FieldBinder.java15
-rw-r--r--server/tests/src/com/vaadin/tests/design/DesignTest.java12
-rw-r--r--server/tests/src/com/vaadin/tests/design/designroot/DesignRootTest.java4
-rw-r--r--server/tests/src/com/vaadin/tests/design/designroot/DesignWithAnnotation.java2
-rw-r--r--server/tests/src/com/vaadin/tests/design/designroot/DesignWithEmptyAnnotation.html1
-rw-r--r--server/tests/src/com/vaadin/tests/design/designroot/DesignWithEmptyAnnotation.java2
-rw-r--r--server/tests/src/com/vaadin/tests/design/duplicate-ids.html4
-rw-r--r--server/tests/src/com/vaadin/tests/design/duplicate-local-ids.html4
9 files changed, 42 insertions, 11 deletions
diff --git a/server/src/com/vaadin/ui/declarative/DesignContext.java b/server/src/com/vaadin/ui/declarative/DesignContext.java
index 25765611aa..0ce269261b 100644
--- a/server/src/com/vaadin/ui/declarative/DesignContext.java
+++ b/server/src/com/vaadin/ui/declarative/DesignContext.java
@@ -437,11 +437,12 @@ public class DesignContext implements Serializable {
* at the given component. Also registers the componentid, localId and
* caption of the given component and all its children to the context
*
- *
* @param component
* The component to be synchronized from design
* @param componentDesign
* The html tree node containing the description of the component
+ * @throws DesignException
+ * if the design contains duplicate local or global ids
*/
public void synchronizeAndRegister(Component component,
Element componentDesign) {
@@ -463,7 +464,11 @@ public class DesignContext implements Serializable {
// from the attributes of componentDesign
if (attributes.hasKey(LOCAL_ID_ATTRIBUTE)) {
String localId = attributes.get(LOCAL_ID_ATTRIBUTE);
- mapLocalId(localId, component); // two-way map
+ boolean mappingExists = mapLocalId(localId, component);
+ if (mappingExists) {
+ throw new DesignException(
+ "the following local id is not unique: " + localId);
+ }
}
// caption: a property of a component, possibly not unique
String caption = component.getCaption();
diff --git a/server/src/com/vaadin/ui/declarative/FieldBinder.java b/server/src/com/vaadin/ui/declarative/FieldBinder.java
index af82d9c58c..fcc78db414 100644
--- a/server/src/com/vaadin/ui/declarative/FieldBinder.java
+++ b/server/src/com/vaadin/ui/declarative/FieldBinder.java
@@ -135,8 +135,6 @@ public class FieldBinder implements Serializable {
/**
* Tries to bind the given {@link Component} instance to a member field of
* the bind target. The fields are matched based on localId, id and caption.
- * If a field is already bound (not null), {@link FieldBindingException} is
- * thrown.
*
* @param instance
* the instance to be bound to a field
@@ -197,14 +195,13 @@ public class FieldBinder implements Serializable {
Object fieldValue = ReflectTools.getJavaFieldValue(bindTarget,
field);
if (fieldValue != null) {
- getLogger().severe(
- "The field with identifier \"" + identifier
- + "\" already mapped");
- throw new FieldBindingException(
- "Duplicate identifier found for a field: " + fieldName);
+ getLogger().fine(
+ "The field \"" + fieldName
+ + "\" was already mapped. Ignoring.");
+ } else {
+ // set the field value
+ ReflectTools.setJavaFieldValue(bindTarget, field, instance);
}
- // set the field value
- ReflectTools.setJavaFieldValue(bindTarget, field, instance);
return true;
} catch (IllegalAccessException e) {
throw new FieldBindingException("Field binding failed", e);
diff --git a/server/tests/src/com/vaadin/tests/design/DesignTest.java b/server/tests/src/com/vaadin/tests/design/DesignTest.java
index 10bc2e0079..a5ccef0d2c 100644
--- a/server/tests/src/com/vaadin/tests/design/DesignTest.java
+++ b/server/tests/src/com/vaadin/tests/design/DesignTest.java
@@ -101,6 +101,18 @@ public class DesignTest {
assertHierarchyEquals(dc.getRootComponent(), newRoot);
}
+ @Test(expected = DesignException.class)
+ public void testDuplicateIds() throws FileNotFoundException {
+ Design.read(new FileInputStream(
+ "server/tests/src/com/vaadin/tests/design/duplicate-ids.html"));
+ }
+
+ @Test(expected = DesignException.class)
+ public void testDuplicateLocalIds() throws FileNotFoundException {
+ Design.read(new FileInputStream(
+ "server/tests/src/com/vaadin/tests/design/duplicate-local-ids.html"));
+ }
+
private void assertHierarchyEquals(Component expected, Component actual) {
if (expected.getClass() != actual.getClass()) {
throw new AssertionError(
diff --git a/server/tests/src/com/vaadin/tests/design/designroot/DesignRootTest.java b/server/tests/src/com/vaadin/tests/design/designroot/DesignRootTest.java
index 300d293e66..682da30344 100644
--- a/server/tests/src/com/vaadin/tests/design/designroot/DesignRootTest.java
+++ b/server/tests/src/com/vaadin/tests/design/designroot/DesignRootTest.java
@@ -24,6 +24,7 @@ public class DesignRootTest {
DesignWithEmptyAnnotation d = new DesignWithEmptyAnnotation();
Assert.assertNotNull(d.ok);
Assert.assertNotNull(d.CaNCEL);
+ Assert.assertEquals("original", d.preInitializedField.getValue());
}
@Test
@@ -31,6 +32,7 @@ public class DesignRootTest {
DesignWithAnnotation d = new DesignWithAnnotation();
Assert.assertNotNull(d.ok);
Assert.assertNotNull(d.cancel);
+ Assert.assertEquals("original", d.preInitializedField.getValue());
}
@Test
@@ -38,6 +40,7 @@ public class DesignRootTest {
DesignWithEmptyAnnotation d = new ExtendedDesignWithEmptyAnnotation();
Assert.assertNotNull(d.ok);
Assert.assertNotNull(d.CaNCEL);
+ Assert.assertEquals("original", d.preInitializedField.getValue());
}
@Test
@@ -45,6 +48,7 @@ public class DesignRootTest {
DesignWithAnnotation d = new ExtendedDesignWithAnnotation();
Assert.assertNotNull(d.ok);
Assert.assertNotNull(d.cancel);
+ Assert.assertEquals("original", d.preInitializedField.getValue());
}
}
diff --git a/server/tests/src/com/vaadin/tests/design/designroot/DesignWithAnnotation.java b/server/tests/src/com/vaadin/tests/design/designroot/DesignWithAnnotation.java
index 1107d8c00c..70809cb694 100644
--- a/server/tests/src/com/vaadin/tests/design/designroot/DesignWithAnnotation.java
+++ b/server/tests/src/com/vaadin/tests/design/designroot/DesignWithAnnotation.java
@@ -19,6 +19,7 @@ import org.junit.Ignore;
import com.vaadin.annotations.DesignRoot;
import com.vaadin.ui.Button;
+import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.declarative.Design;
@@ -28,6 +29,7 @@ public class DesignWithAnnotation extends VerticalLayout {
public Button ok;
public Button cancel;
+ public Label preInitializedField = new Label("original");
public DesignWithAnnotation() {
Design.read(this);
diff --git a/server/tests/src/com/vaadin/tests/design/designroot/DesignWithEmptyAnnotation.html b/server/tests/src/com/vaadin/tests/design/designroot/DesignWithEmptyAnnotation.html
index 27c323dfec..66e9202ca7 100644
--- a/server/tests/src/com/vaadin/tests/design/designroot/DesignWithEmptyAnnotation.html
+++ b/server/tests/src/com/vaadin/tests/design/designroot/DesignWithEmptyAnnotation.html
@@ -1,4 +1,5 @@
<v-vertical-layout>
<v-button>OK</v-button>
<v-button>Cancel</v-button>
+ <v-label caption="preInitializedField">a Label that should not override pre initalized field<v-label/>
</v-vertical-layout> \ No newline at end of file
diff --git a/server/tests/src/com/vaadin/tests/design/designroot/DesignWithEmptyAnnotation.java b/server/tests/src/com/vaadin/tests/design/designroot/DesignWithEmptyAnnotation.java
index 9723df9afa..f6fb928e75 100644
--- a/server/tests/src/com/vaadin/tests/design/designroot/DesignWithEmptyAnnotation.java
+++ b/server/tests/src/com/vaadin/tests/design/designroot/DesignWithEmptyAnnotation.java
@@ -19,6 +19,7 @@ import org.junit.Ignore;
import com.vaadin.annotations.DesignRoot;
import com.vaadin.ui.Button;
+import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.declarative.Design;
@@ -28,6 +29,7 @@ public class DesignWithEmptyAnnotation extends VerticalLayout {
protected Button ok;
protected Button CaNCEL;
+ protected Label preInitializedField = new Label("original");
public DesignWithEmptyAnnotation() {
Design.read(this);
diff --git a/server/tests/src/com/vaadin/tests/design/duplicate-ids.html b/server/tests/src/com/vaadin/tests/design/duplicate-ids.html
new file mode 100644
index 0000000000..ef845d46d5
--- /dev/null
+++ b/server/tests/src/com/vaadin/tests/design/duplicate-ids.html
@@ -0,0 +1,4 @@
+<v-vertical-layout>
+ <v-label id="foo"/>
+ <v-label id="foo"/>
+</v-vertical-layout> \ No newline at end of file
diff --git a/server/tests/src/com/vaadin/tests/design/duplicate-local-ids.html b/server/tests/src/com/vaadin/tests/design/duplicate-local-ids.html
new file mode 100644
index 0000000000..5a506ad00c
--- /dev/null
+++ b/server/tests/src/com/vaadin/tests/design/duplicate-local-ids.html
@@ -0,0 +1,4 @@
+<v-vertical-layout>
+ <v-label _id="foo"/>
+ <v-label _id="foo"/>
+</v-vertical-layout> \ No newline at end of file