### Question:
MetaDataStateChainingListener extends AbstractChainingListener { public <T> T getMetaData(String key) { T result = null; if (!this.metaDataStack.isEmpty()) { for (MetaData metaData : this.metaDataStack) { result = (T) metaData.getMetaData(key); if (result != null) { break; } } } return result; } MetaDataStateChainingListener(ListenerChain listenerChain); List<T> getAllMetaData(String key); T getMetaData(String key); @Override void beginDocument(MetaData metaData); @Override void endDocument(MetaData metaData); @Override void beginMetaData(MetaData metaData); @Override void endMetaData(MetaData metaData);  }### Answer:
@Test public void getMetaData() { MetaDataStateChainingListener listener = new MetaDataStateChainingListener(new ListenerChain()); MetaData metaData1 = new MetaData(); metaData1.addMetaData("key1", "value1"); metaData1.addMetaData("key2", "value2"); listener.beginMetaData(metaData1); assertEquals("value1", listener.getMetaData("key1")); assertEquals("value2", listener.getMetaData("key2")); assertNull(listener.getMetaData("unknown")); MetaData metaData2 = new MetaData(); metaData2.addMetaData("key3", "value3"); listener.beginMetaData(metaData2); assertEquals("value1", listener.getMetaData("key1")); assertEquals("value2", listener.getMetaData("key2")); assertEquals("value3", listener.getMetaData("key3")); assertNull(listener.getMetaData("unknown")); MetaData metaData3 = new MetaData(); metaData3.addMetaData("key1", "value4"); listener.beginMetaData(metaData3); assertEquals("value4", listener.getMetaData("key1")); assertEquals("value2", listener.getMetaData("key2")); assertEquals("value3", listener.getMetaData("key3")); assertNull(listener.getMetaData("unknown")); listener.endMetaData(metaData3); assertEquals("value1", listener.getMetaData("key1")); assertEquals("value2", listener.getMetaData("key2")); assertEquals("value3", listener.getMetaData("key3")); assertNull(listener.getMetaData("unknown")); listener.endMetaData(metaData2); assertEquals("value1", listener.getMetaData("key1")); assertEquals("value2", listener.getMetaData("key2")); assertNull(listener.getMetaData("unknown")); listener.endMetaData(metaData1); assertNull(listener.getMetaData("key1")); }