### Question:
PerThreadSAXParserProvider implements SAXParserProvider { public SAXParser getSAXParser(boolean validating) throws ParserConfigurationException, SAXException { try { return validating ? validatingParserCache.getInstanceForCurrentThread() : nonValidatingParserCache.getInstanceForCurrentThread(); } catch (SAXException ex) { if (ex.getCause() instanceof ParserConfigurationException) { throw (ParserConfigurationException) ex.getCause(); } throw ex; } } PerThreadSAXParserProvider(SAXParserProvider delegate, int maxSize);   PerThreadSAXParserProvider(SAXParserProvider delegate); SAXParser getSAXParser(boolean validating);  }### Answer:
@Test public void shouldReturnSameInstanceForSameThread() throws Exception { boolean validating = false; SAXParser parser1 = testProvider.getSAXParser(validating); SAXParser parser2 = testProvider.getSAXParser(validating); assertThat(parser1).isSameAs(parser2); }
@Test public void shouldReturnDifferentInstancesForDifferentThreads() throws Exception { boolean validating = false; ExecutorService otherThread = Executors.newSingleThreadExecutor(); SAXParser parser1 = testProvider.getSAXParser(validating); SAXParser parser2 = otherThread.submit(getParserAction(validating)).get(); assertThat(parser1).isNotSameAs(parser2); }
@Test public void shouldEvictLeastRecentlyAccessedElements() throws Exception { int maxSize = 1; boolean validating = false; ExecutorService otherThread = Executors.newSingleThreadExecutor(); testProvider = new PerThreadSAXParserProvider(new MockSAXParserProvider(), maxSize); SAXParser parser1 = testProvider.getSAXParser(validating); otherThread.submit(getParserAction(validating)).get(); SAXParser parser2 = testProvider.getSAXParser(validating); assertThat(parser1).isNotSameAs(parser2); }
@Test public void shouldCacheValidatingAndNonValidatingInstancesSeparately() throws Exception { SAXParser validatingParser = testProvider.getSAXParser(true); SAXParser nonValidatingParser = testProvider.getSAXParser(false); assertThat(validatingParser).isNotSameAs(nonValidatingParser); }