/** * Will parse the attributes off an XML tag, into {@link Object} then inform the {@link ParseWatcher} * The idea is to have a callback for a number of elements to create a {@link java.util.List} * * @param attributeMarshaller The marshaller to parse the attributes into your required type * @param watcher The watcher on elements to be informed of object creation * @param attrTags The tags of the attributes you wish to parse * @param <T> The type you wish to create from the XML body * @return {@link ElementFinder} */ public <T> ElementFinder<T> getListAttributeFinder(AttributeMarshaller<T> attributeMarshaller, ParseWatcher<T> watcher, String... attrTags) { return new ListElementFinder<T>(new BasicAttributeParser<T>(attributeMarshaller, attrTags), watcher); }
@Override public void start(Attributes attributes) { String[] values = getAttributeValues(attributes); listener.onParsed(attributeMarshaller.marshal(values)); }
@Test public void delegateToMarshaller_toConvertToObjectWanted_whenAttributesParsed() throws Exception { Element element = mock(Element.class); cut.parse(element, mockParseWatcher); Attributes attributes = mock(Attributes.class); stub(attributes.getValue(ATTR_TAG)).toReturn(FOUND_ATTRIBUTE); cut.start(attributes); verify(mockMarshaller).marshal(FOUND_ATTRIBUTE); } }
@Test public void setListenerToObtainAttributes() throws Exception { Element element = mock(Element.class); cut.parse(element, mockParseWatcher); verify(element).setStartElementListener(cut); }
@Test public void informListener_whenAttributesParsed() throws Exception { Element element = mock(Element.class); cut.parse(element, mockParseWatcher); Attributes attributes = mock(Attributes.class); stub(attributes.getValue(ATTR_TAG)).toReturn(FOUND_ATTRIBUTE); Object outputObject = new Object(); stub(mockMarshaller.marshal(FOUND_ATTRIBUTE)).toReturn(outputObject); cut.start(attributes); verify(mockParseWatcher).onParsed(outputObject); }
/** * Will parse the attributes off an XML tag, these are then passed to your {@link AttributeMarshaller} * to create an object of type {@link Object}. * * @param attributeMarshaller The marshaller to parse the attributes into your required type * @param attrTags The tags of the attributes you wish to parse * @param <T> The type you wish to create from the attributes * @return {@link ElementFinder} */ public <T> ElementFinder<T> getAttributeFinder(AttributeMarshaller<T> attributeMarshaller, String... attrTags) { return getTypeFinder(new BasicAttributeParser<T>(attributeMarshaller, attrTags)); }
@Before public void setUp() { initMocks(this); cut = new BasicAttributeParser<Object>(mockMarshaller, ATTR_TAG); }