您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DesignTest.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2000-2016 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.FileNotFoundException;
  20. import java.io.IOException;
  21. import java.util.Iterator;
  22. import org.junit.Assert;
  23. import org.junit.Ignore;
  24. import org.junit.Test;
  25. import com.vaadin.ui.Button;
  26. import com.vaadin.ui.Component;
  27. import com.vaadin.ui.HasComponents;
  28. import com.vaadin.ui.HorizontalLayout;
  29. import com.vaadin.ui.Panel;
  30. import com.vaadin.ui.TextField;
  31. import com.vaadin.ui.VerticalLayout;
  32. import com.vaadin.ui.declarative.Design;
  33. import com.vaadin.ui.declarative.DesignContext;
  34. import com.vaadin.ui.declarative.DesignException;
  35. public class DesignTest {
  36. @Test
  37. public void readStream() throws FileNotFoundException {
  38. Component root = Design.read(getClass()
  39. .getResourceAsStream("verticallayout-two-children.html"));
  40. VerticalLayout rootLayout = (VerticalLayout) root;
  41. Assert.assertEquals(VerticalLayout.class, root.getClass());
  42. Assert.assertEquals(2, rootLayout.getComponentCount());
  43. Assert.assertEquals(TextField.class,
  44. rootLayout.getComponent(0).getClass());
  45. Assert.assertEquals(Button.class,
  46. rootLayout.getComponent(1).getClass());
  47. }
  48. @Test(expected = DesignException.class)
  49. @Ignore("Feature needs to be fixed")
  50. public void readWithIncorrectRoot() throws FileNotFoundException {
  51. Design.read(
  52. getClass().getResourceAsStream("verticallayout-one-child.html"),
  53. new Panel());
  54. }
  55. public static class MyVerticalLayout extends VerticalLayout {
  56. }
  57. @Test
  58. public void readWithSubClassRoot() throws FileNotFoundException {
  59. Design.read(
  60. getClass().getResourceAsStream("verticallayout-one-child.html"),
  61. new MyVerticalLayout());
  62. }
  63. @Test
  64. public void writeComponentToStream() throws IOException {
  65. HorizontalLayout root = new HorizontalLayout(new Button("OK"),
  66. new Button("Cancel"));
  67. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  68. Design.write(root, baos);
  69. Component newRoot = Design
  70. .read(new ByteArrayInputStream(baos.toByteArray()));
  71. assertHierarchyEquals(root, newRoot);
  72. }
  73. @Test
  74. public void writeDesignContextToStream() throws IOException {
  75. DesignContext dc = Design.read(getClass()
  76. .getResourceAsStream("verticallayout-two-children.html"), null);
  77. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  78. Design.write(dc, baos);
  79. Component newRoot = Design
  80. .read(new ByteArrayInputStream(baos.toByteArray()));
  81. assertHierarchyEquals(dc.getRootComponent(), newRoot);
  82. }
  83. @Test(expected = DesignException.class)
  84. public void testDuplicateIds() throws FileNotFoundException {
  85. Design.read(getClass().getResourceAsStream("duplicate-ids.html"));
  86. }
  87. @Test(expected = DesignException.class)
  88. public void testDuplicateLocalIds() throws FileNotFoundException {
  89. Design.read(getClass().getResourceAsStream("duplicate-local-ids.html"));
  90. }
  91. private void assertHierarchyEquals(Component expected, Component actual) {
  92. if (expected.getClass() != actual.getClass()) {
  93. throw new AssertionError(
  94. "Component classes do not match. Expected: "
  95. + expected.getClass().getName() + ", was: "
  96. + actual.getClass().getName());
  97. }
  98. if (expected instanceof HasComponents) {
  99. HasComponents expectedHC = (HasComponents) expected;
  100. HasComponents actualHC = (HasComponents) actual;
  101. Iterator<Component> eI = expectedHC.iterator();
  102. Iterator<Component> aI = actualHC.iterator();
  103. while (eI.hasNext()) {
  104. assertHierarchyEquals(eI.next(), aI.next());
  105. }
  106. }
  107. }
  108. }