ElementUtil { public static void setComponent(Element element, Component component) { if (element == null) { throw new IllegalArgumentException("Element must not be null"); } if (component == null) { throw new IllegalArgumentException("Component must not be null"); } Optional<Component> currentComponent = element.getComponent(); if (currentComponent.isPresent()) { boolean isCompositeReplacingItsContent = component instanceof Composite && component.getChildren().findFirst() .get() == currentComponent.get(); if (!isCompositeReplacingItsContent) { throw new IllegalStateException("A component of type " + currentComponent.get().getClass().getName() + " is already attached to this element"); } } element.getStateProvider().setComponent(element.getNode(), component); } private  ElementUtil(); static boolean isValidTagName(String tag); static boolean isValidAttributeName(String attribute); static void validateStylePropertyName(String name); static boolean isValidStylePropertyName(String name); static boolean isValidStylePropertyValue(String value); static void validateStylePropertyValue(String value); static void setComponent(Element element, Component component); static Node toJsoup(Document document, Element element); static Optional<Element> fromJsoup(Node node); static boolean isCustomElement(Element element); static boolean isScript(Element element);  }
@Test public void attachToComponent() { Element e = ElementFactory.createDiv(); Component c = Mockito.mock(Component.class); ElementUtil.setComponent(e, c); Assert.assertEquals(c, e.getComponent().get()); }
@Test public void attachComponentToTextElement() { Element e = Element.createText("Text text"); Component c = Mockito.mock(Component.class); ElementUtil.setComponent(e, c); Assert.assertEquals(c, e.getComponent().get()); }
@Test(expected = IllegalStateException.class) public void attachTwiceToComponent() { Element e = ElementFactory.createDiv(); Component c = Mockito.mock(Component.class); ElementUtil.setComponent(e, c); ElementUtil.setComponent(e, c); }
@Test(expected = IllegalArgumentException.class) public void attachToNull() { Element e = ElementFactory.createDiv(); ElementUtil.setComponent(e, null); }
@Test(expected = IllegalStateException.class) public void attachTwoComponents() { Element e = ElementFactory.createDiv(); Component c = Mockito.mock(Component.class); Component c2 = Mockito.mock(Component.class); ElementUtil.setComponent(e, c); ElementUtil.setComponent(e, c2); }