CoreItemFactory implements ItemFactory { @Override public @Nullable GenericItem createItem(@Nullable String itemTypeName, String itemName) { if (itemTypeName == null) { return null; } String itemType = ItemUtil.getMainItemType(itemTypeName); switch (itemType) { case CALL: return new CallItem(itemName); case COLOR: return new ColorItem(itemName); case CONTACT: return new ContactItem(itemName); case DATETIME: return new DateTimeItem(itemName); case DIMMER: return new DimmerItem(itemName); case IMAGE: return new ImageItem(itemName); case LOCATION: return new LocationItem(itemName); case NUMBER: return new NumberItem(itemTypeName, itemName); case PLAYER: return new PlayerItem(itemName); case ROLLERSHUTTER: return new RollershutterItem(itemName); case STRING: return new StringItem(itemName); case SWITCH: return new SwitchItem(itemName); default: return null; } }  @Override @Nullable GenericItem createItem(@Nullable String itemTypeName, String itemName); @Override String[] getSupportedItemTypes(); static final String CALL; static final String COLOR; static final String CONTACT; static final String DATETIME; static final String DIMMER; static final String IMAGE; static final String LOCATION; static final String NUMBER; static final String PLAYER; static final String ROLLERSHUTTER; static final String STRING; static final String SWITCH; }
@Test public void shouldCreateItems() { for (String itemTypeName : itemTypeNames) { GenericItem item = coreItemFactory.createItem(itemTypeName, itemTypeName.toLowerCase()); assertThat(item.getType(), is(itemTypeName)); assertThat(item.getName(), is(itemTypeName.toLowerCase())); } }
@Test public void createNumberItemWithDimension() { NumberItem numberItem = (NumberItem) coreItemFactory.createItem(CoreItemFactory.NUMBER + ":Temperature", "myNumberItem"); assertThat(numberItem.getDimension(), equalTo(Temperature.class)); }
@Test public void shouldReturnNullForUnsupportedItemTypeName() { GenericItem item = coreItemFactory.createItem("NoValidItemTypeName", "IWantMyItem"); assertThat(item, is(nullValue())); }