From 5d08dd6efc1d9d955ec0444204c8730438ec8923 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 3 Feb 2015 21:48:35 +0200 Subject: Base classes for declarative tests Change-Id: Ia3a421b84eac0252a6e1b8238756eeaac2a35b6e --- .../vaadin/tests/design/DeclarativeTestBase.java | 104 +++++++++++++ .../tests/design/DeclarativeTestBaseBase.java | 163 +++++++++++++++++++++ .../AbsoluteLayoutDeclarativeTest.java | 68 +++++++++ .../AbstractFieldDeclarativeTest.java | 63 ++++++++ .../component/button/ButtonDeclarativeTest.java | 72 +++++++++ .../checkbox/CheckboxDeclarativeTest.java | 56 +++++++ .../component/label/LabelDeclarativeTest.java | 95 ++++++++++++ .../textarea/TextAreaDeclarativeTest.java | 53 +++++++ .../textfield/TextFieldDeclarativeTest.java | 50 +++++++ 9 files changed, 724 insertions(+) create mode 100644 server/tests/src/com/vaadin/tests/design/DeclarativeTestBase.java create mode 100644 server/tests/src/com/vaadin/tests/design/DeclarativeTestBaseBase.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/absolutelayout/AbsoluteLayoutDeclarativeTest.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractfield/AbstractFieldDeclarativeTest.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/button/ButtonDeclarativeTest.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/checkbox/CheckboxDeclarativeTest.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/label/LabelDeclarativeTest.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/textarea/TextAreaDeclarativeTest.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/textfield/TextFieldDeclarativeTest.java diff --git a/server/tests/src/com/vaadin/tests/design/DeclarativeTestBase.java b/server/tests/src/com/vaadin/tests/design/DeclarativeTestBase.java new file mode 100644 index 0000000000..5bdca6e06a --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/DeclarativeTestBase.java @@ -0,0 +1,104 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.design; + +import java.beans.BeanInfo; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; + +import com.vaadin.ui.Component; + +public abstract class DeclarativeTestBase extends + DeclarativeTestBaseBase { + + private static boolean debug = false; + + private final Map, EqualsAsserter> comparators = new HashMap, EqualsAsserter>(); + private static EqualsAsserter standardEqualsComparator = new EqualsAsserter() { + + @Override + public void assertObjectEquals(Object o1, Object o2) { + Assert.assertEquals(o1, o2); + } + }; + + public class IntrospectorEqualsAsserter implements EqualsAsserter { + + private Class c; + + public IntrospectorEqualsAsserter(Class c) { + this.c = c; + } + + @Override + public void assertObjectEquals(T o1, T o2) { + try { + BeanInfo bi = Introspector.getBeanInfo(c); + for (PropertyDescriptor pd : bi.getPropertyDescriptors()) { + Method readMethod = pd.getReadMethod(); + Method writeMethod = pd.getWriteMethod(); + if (readMethod == null || writeMethod == null) { + continue; + } + try { + c.getDeclaredMethod(readMethod.getName()); + } catch (Exception e) { + // Not declared in this class, will be tested by parent + // class tester + if (debug) { + System.out.println("Skipped " + c.getSimpleName() + + "." + readMethod.getName()); + } + continue; + } + + if (debug) { + System.out.println("Testing " + c.getSimpleName() + "." + + readMethod.getName()); + } + Object v1 = readMethod.invoke(o1); + Object v2 = readMethod.invoke(o2); + assertEquals(pd.getDisplayName(), v1, v2); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } + + @Override + protected EqualsAsserter getComparator(Class c) { + com.vaadin.tests.design.DeclarativeTestBaseBase.EqualsAsserter comp = comparators + .get(c); + if (comp == null) { + if (c.isEnum()) { + return standardEqualsComparator; + } + if (debug) { + System.out.println("No comparator found for " + c.getName() + + ". Using introspector."); + } + return new IntrospectorEqualsAsserter(c); + } + return comp; + } + +} diff --git a/server/tests/src/com/vaadin/tests/design/DeclarativeTestBaseBase.java b/server/tests/src/com/vaadin/tests/design/DeclarativeTestBaseBase.java new file mode 100644 index 0000000000..653e1b1cb9 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/DeclarativeTestBaseBase.java @@ -0,0 +1,163 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.design; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Attribute; +import org.jsoup.nodes.Element; +import org.junit.Assert; + +import com.vaadin.ui.Component; +import com.vaadin.ui.declarative.Design; + +public abstract class DeclarativeTestBaseBase { + public interface EqualsAsserter { + public void assertObjectEquals(TT o1, TT o2); + } + + protected T read(String design) { + try { + return (T) Design.read(new ByteArrayInputStream(design + .getBytes("UTF-8"))); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + + protected String write(T object) { + try { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + Design.write(object, outputStream); + return outputStream.toString("UTF-8"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + protected void assertEquals(Object o1, Object o2) { + assertEquals("", o1, o2); + } + + protected void assertEquals(String message, Object o1, Object o2) { + if (o1 == null) { + Assert.assertEquals(message, null, o2); + return; + } + if (o2 == null) { + Assert.assertEquals(message, null, o1); + return; + } + + if (o1 instanceof Collection && o2 instanceof Collection) { + + } else { + Assert.assertEquals(o1.getClass(), o2.getClass()); + } + + List> comparators = getComparators(o1); + if (!comparators.isEmpty()) { + for (EqualsAsserter ec : comparators) { + ec.assertObjectEquals(o1, o2); + } + } else { + Assert.assertEquals(message, o1, o2); + } + } + + private List> getComparators(Object o1) { + List> result = new ArrayList>(); + getComparators(o1.getClass(), result); + return result; + } + + private void getComparators(Class c, List> result) { + if (c == null || !isVaadin(c)) { + return; + } + EqualsAsserter comparator = (EqualsAsserter) getComparator(c); + if (c.getSuperclass() != Object.class) { + getComparators(c.getSuperclass(), result); + } + for (Class i : c.getInterfaces()) { + getComparators(i, result); + } + + if (!result.contains(comparator)) { + result.add(comparator); + } + } + + protected abstract EqualsAsserter getComparator(Class c); + + private boolean isVaadin(Class c) { + return c.getPackage().getName().startsWith("com.vaadin"); + } + + public void testRead(String design, T expected) { + assertEquals(expected, read(design)); + } + + public void testWrite(String design, T expected) { + String written = write(expected); + + Element producedElem = Jsoup.parse(written).body().child(0); + Element comparableElem = Jsoup.parse(design).body().child(0); + + String produced = elementToHtml(producedElem); + String comparable = elementToHtml(comparableElem); + + Assert.assertEquals(comparable, produced); + } + + private String elementToHtml(Element producedElem) { + StringBuilder stringBuilder = new StringBuilder(); + elementToHtml(producedElem, stringBuilder); + return stringBuilder.toString(); + } + + /** + * Produce predictable html (attributes in alphabetical order), always + * include close tags + */ + private String elementToHtml(Element producedElem, StringBuilder sb) { + ArrayList names = new ArrayList(); + for (Attribute a : producedElem.attributes().asList()) { + names.add(a.getKey()); + } + Collections.sort(names); + + sb.append("<" + producedElem.tagName() + ""); + for (String attrName : names) { + sb.append(" ").append(attrName).append("=").append("\'") + .append(producedElem.attr(attrName)).append("\'"); + } + sb.append(">"); + for (Element child : producedElem.children()) { + elementToHtml(child, sb); + } + sb.append(""); + return sb.toString(); + } + +} diff --git a/server/tests/src/com/vaadin/tests/server/component/absolutelayout/AbsoluteLayoutDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/absolutelayout/AbsoluteLayoutDeclarativeTest.java new file mode 100644 index 0000000000..14629827fb --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/absolutelayout/AbsoluteLayoutDeclarativeTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.server.component.absolutelayout; + +import org.junit.Test; + +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.AbsoluteLayout; +import com.vaadin.ui.Button; + +/** + * Tests declarative support for implementations of {@link AbsoluteLayout}. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class AbsoluteLayoutDeclarativeTest extends + DeclarativeTestBase { + + @Test + public void testPlainTextRead() { + testRead(getDesign(), getExpected()); + } + + @Test + public void testPlainTextWrite() { + testWrite(getDesign(), getExpected()); + } + + protected String getDesign() { + return "" + + "OK" + + "Cancel" + + ""; + } + + protected AbsoluteLayout getExpected() { + AbsoluteLayout c = new AbsoluteLayout(); + Button b1 = new Button("OK"); + b1.setCaptionAsHtml(true); + Button b2 = new Button("Cancel"); + b2.setCaptionAsHtml(true); + + c.addComponent(b1, "top: 100px; left: 0px; z-index: 21"); + c.addComponent(b2, "bottom: 0px; right: 0px;"); + System.out.println(c.getComponentCount()); + return c; + }; + + @Test + public void testEmpty() { + testRead("", new AbsoluteLayout()); + } + +} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbstractFieldDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbstractFieldDeclarativeTest.java new file mode 100644 index 0000000000..f415a5fd18 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbstractFieldDeclarativeTest.java @@ -0,0 +1,63 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.server.component.abstractfield; + +import org.junit.Test; + +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.AbstractField; +import com.vaadin.ui.TextField; + +/** + * Tests declarative support for implementations of {@link AbstractField}. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class AbstractFieldDeclarativeTest extends + DeclarativeTestBase> { + + @Test + public void testPlainTextRead() { + testRead(getDesign(), getExpected()); + } + + @Test + public void testPlainTextWrite() { + testWrite(getDesign(), getExpected()); + } + + protected String getDesign() { + return ""; + } + + protected AbstractField getExpected() { + TextField tf = new TextField(); + tf.setBuffered(true); + tf.setValidationVisible(false); + tf.setInvalidCommitted(true); + tf.setInvalidAllowed(false); + tf.setRequired(true); + tf.setRequiredError("This is a required field"); + tf.setConversionError("Input {0} cannot be parsed"); + tf.setTabIndex(3); + tf.setReadOnly(true); + return tf; + }; + +} diff --git a/server/tests/src/com/vaadin/tests/server/component/button/ButtonDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/button/ButtonDeclarativeTest.java new file mode 100644 index 0000000000..2962620052 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/button/ButtonDeclarativeTest.java @@ -0,0 +1,72 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.server.component.button; + +import org.junit.Test; + +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.Button; + +/** + * Tests declarative support for implementations of {@link Button}. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class ButtonDeclarativeTest extends DeclarativeTestBase