You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DeclarativeTestBaseBase.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.design;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.UnsupportedEncodingException;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.Collections;
  23. import java.util.HashSet;
  24. import java.util.List;
  25. import org.jsoup.Jsoup;
  26. import org.jsoup.nodes.Attribute;
  27. import org.jsoup.nodes.BooleanAttribute;
  28. import org.jsoup.nodes.Element;
  29. import org.jsoup.nodes.Node;
  30. import org.jsoup.nodes.TextNode;
  31. import org.junit.Assert;
  32. import com.vaadin.ui.Component;
  33. import com.vaadin.ui.declarative.Design;
  34. import com.vaadin.ui.declarative.DesignContext;
  35. import com.vaadin.ui.declarative.ShouldWriteDataDelegate;
  36. public abstract class DeclarativeTestBaseBase<T extends Component> {
  37. private static final class AlwaysWriteDelegate implements
  38. ShouldWriteDataDelegate {
  39. private static final long serialVersionUID = -6345914431997793599L;
  40. @Override
  41. public boolean shouldWriteData(Component component) {
  42. return true;
  43. }
  44. }
  45. public static final ShouldWriteDataDelegate ALWAYS_WRITE_DATA = new AlwaysWriteDelegate();
  46. public interface EqualsAsserter<TT> {
  47. public void assertObjectEquals(TT o1, TT o2);
  48. }
  49. protected T read(String design) {
  50. try {
  51. return (T) Design.read(new ByteArrayInputStream(design
  52. .getBytes("UTF-8")));
  53. } catch (UnsupportedEncodingException e) {
  54. throw new RuntimeException(e);
  55. }
  56. }
  57. protected String write(T object, boolean writeData) {
  58. try {
  59. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  60. DesignContext dc = new DesignContext();
  61. if (writeData) {
  62. dc.setShouldWriteDataDelegate(DeclarativeTestBaseBase.ALWAYS_WRITE_DATA);
  63. }
  64. dc.setRootComponent(object);
  65. Design.write(dc, outputStream);
  66. return outputStream.toString("UTF-8");
  67. } catch (Exception e) {
  68. throw new RuntimeException(e);
  69. }
  70. }
  71. protected void assertEquals(Object o1, Object o2) {
  72. assertEquals("", o1, o2);
  73. }
  74. protected void assertEquals(String message, Object o1, Object o2) {
  75. if (o1 == null) {
  76. Assert.assertEquals(message, null, o2);
  77. return;
  78. }
  79. if (o2 == null) {
  80. Assert.assertEquals(message, null, o1);
  81. return;
  82. }
  83. if (!(o1 instanceof Collection && o2 instanceof Collection)) {
  84. Assert.assertEquals(o1.getClass(), o2.getClass());
  85. }
  86. if (o1 instanceof Object[]) {
  87. Object[] a1 = ((Object[]) o1);
  88. Object[] a2 = ((Object[]) o2);
  89. Assert.assertEquals(message + ": array length", a1.length,
  90. a2.length);
  91. for (int i = 0; i < a1.length; i++) {
  92. assertEquals(message + ": element " + i, a1[i], a2[i]);
  93. }
  94. return;
  95. }
  96. List<EqualsAsserter<Object>> comparators = getComparators(o1);
  97. if (!comparators.isEmpty()) {
  98. for (EqualsAsserter<Object> ec : comparators) {
  99. ec.assertObjectEquals(o1, o2);
  100. }
  101. } else {
  102. Assert.assertEquals(message, o1, o2);
  103. }
  104. }
  105. private List<EqualsAsserter<Object>> getComparators(Object o1) {
  106. List<EqualsAsserter<Object>> result = new ArrayList<EqualsAsserter<Object>>();
  107. getComparators(o1.getClass(), result);
  108. return result;
  109. }
  110. private void getComparators(Class<?> c, List<EqualsAsserter<Object>> result) {
  111. if (c == null || !isVaadin(c)) {
  112. return;
  113. }
  114. EqualsAsserter<Object> comparator = (EqualsAsserter<Object>) getComparator(c);
  115. if (c.getSuperclass() != Object.class) {
  116. getComparators(c.getSuperclass(), result);
  117. }
  118. for (Class<?> i : c.getInterfaces()) {
  119. getComparators(i, result);
  120. }
  121. if (!result.contains(comparator)) {
  122. result.add(comparator);
  123. }
  124. }
  125. protected abstract <TT> EqualsAsserter<TT> getComparator(Class<TT> c);
  126. private boolean isVaadin(Class<?> c) {
  127. return c.getPackage() != null
  128. && c.getPackage().getName().startsWith("com.vaadin");
  129. }
  130. public T testRead(String design, T expected) {
  131. T read = read(design);
  132. assertEquals(expected, read);
  133. return read;
  134. }
  135. public void testWrite(String design, T expected) {
  136. testWrite(design, expected, false);
  137. }
  138. public void testWrite(String design, T expected, boolean writeData) {
  139. String written = write(expected, writeData);
  140. Element producedElem = Jsoup.parse(written).body().child(0);
  141. Element comparableElem = Jsoup.parse(design).body().child(0);
  142. String produced = elementToHtml(producedElem);
  143. String comparable = elementToHtml(comparableElem);
  144. Assert.assertEquals(comparable, produced);
  145. }
  146. protected Element createElement(Component c) {
  147. return new DesignContext().createElement(c);
  148. }
  149. private String elementToHtml(Element producedElem) {
  150. StringBuilder stringBuilder = new StringBuilder();
  151. elementToHtml(producedElem, stringBuilder);
  152. return stringBuilder.toString();
  153. }
  154. /**
  155. * Produce predictable html (attributes in alphabetical order), always
  156. * include close tags
  157. */
  158. private String elementToHtml(Element producedElem, StringBuilder sb) {
  159. HashSet<String> booleanAttributes = new HashSet<String>();
  160. ArrayList<String> names = new ArrayList<String>();
  161. for (Attribute a : producedElem.attributes().asList()) {
  162. names.add(a.getKey());
  163. if (a instanceof BooleanAttribute) {
  164. booleanAttributes.add(a.getKey());
  165. }
  166. }
  167. Collections.sort(names);
  168. sb.append("<" + producedElem.tagName() + "");
  169. for (String attrName : names) {
  170. sb.append(" ").append(attrName);
  171. if (!booleanAttributes.contains(attrName)) {
  172. sb.append("=").append("\'").append(producedElem.attr(attrName))
  173. .append("\'");
  174. }
  175. }
  176. sb.append(">");
  177. for (Node child : producedElem.childNodes()) {
  178. if (child instanceof Element) {
  179. elementToHtml((Element) child, sb);
  180. } else if (child instanceof TextNode) {
  181. String text = ((TextNode) child).text();
  182. sb.append(text.trim());
  183. }
  184. }
  185. sb.append("</").append(producedElem.tagName()).append(">");
  186. return sb.toString();
  187. }
  188. protected String stripOptionTags(String design) {
  189. return design.replaceAll("[ \n]*<option(.*)</option>[ \n]*", "");
  190. }
  191. }