Clara { public static Component findComponentById(Component root, String componentId) { if (componentId == null) { throw new IllegalArgumentException("Component id must not be null."); } if (root == null) { throw new IllegalArgumentException( "Root component must not be null."); } if (componentId.equals(root.getId())) { return root; } else if (root instanceof HasComponents) { for (Component c : (HasComponents) root) { Component result = findComponentById(c, componentId); if (result != null) { return result; } } } return null; }  static ClaraBuilder build(); static Component create(InputStream xml); static Component create(InputStream xml,
            ComponentProvider... componentProviders); static Component create(InputStream xml, Object controller,
            AttributeFilter... attributeFilters); static Component create(InputStream xml, Object controller,
            List<ComponentProvider> componentProviders,
            AttributeFilter... attributeFilters); static Component create(String xmlClassResourceFileName,
            Object controller, AttributeFilter... attributeFilters); static Component create(String xmlClassResourceFileName,
            Object controller, List<ComponentProvider> componentProviders,
            AttributeFilter... attributeFilters); static Component findComponentById(Component root, String componentId); static Component findComponentById(Component root, String idPrefix,
            String componentId);  }
@Test(expected = IllegalArgumentException.class) public void testFindComponentById_nullComponent_exceptionThrown() { Clara.findComponentById(null, "foobar"); }
@Test(expected = IllegalArgumentException.class) public void testFindComponentById_nullComponentId_exceptionThrown() { Clara.findComponentById(new VerticalLayout(), null); }
@Test public void testFindComponentById_componentExistInTree_componentFound() { @SuppressWarnings("serial") Layout layout = new VerticalLayout() { { Button b = new Button(); b.setId("foobar"); addComponent(b); } }; Component c = Clara.findComponentById(layout, "foobar"); assertTrue(c instanceof Button); assertEquals("foobar", c.getId()); }
@Test public void testFindComponentById_componentDoesntExistInTree_nullReturned() { @SuppressWarnings("serial") Layout layout = new VerticalLayout() { { Button b = new Button(); b.setId("button"); addComponent(b); } }; Component c = Clara.findComponentById(layout, "foobar"); assertNull(c); }