ContentTransformerHandlerImpl implements ContentTransformerHandler { @Override public <T> T transform( ContentTransformerContentType<T> contentTransformerContentType, T originalContent) { List<ContentTransformer<?>> contentTransformers = _serviceTrackerMap.getService(contentTransformerContentType); if (contentTransformers == null) { return originalContent; } T transformedContent = originalContent; for (ContentTransformer<?> curContentTransformer : contentTransformers) { try { ContentTransformer<T> contentTransformer = (ContentTransformer<T>)curContentTransformer; transformedContent = contentTransformer.transform( transformedContent); } catch (Exception exception) { if (_log.isDebugEnabled()) { _log.debug(exception, exception); } } } return transformedContent; }  @Override T transform(
		ContentTransformerContentType<T> contentTransformerContentType,
		T originalContent);  }
@Test public void testIgnoresTheContentTransformersForDifferentContentTypes() throws Exception { ContentTransformerContentType<String> contentTransformerContentTypeA = new TestContentTransformerContentType<>(); String transformedContentA = RandomTestUtil.randomString(); _registerContentTransformer( contentTransformerContentTypeA, _ORIGINAL_CONTENT, transformedContentA); ContentTransformerContentType<String> contentTransformerContentTypeB = new TestContentTransformerContentType<>(); String transformedContentB = RandomTestUtil.randomString(); _registerContentTransformer( contentTransformerContentTypeB, _ORIGINAL_CONTENT, transformedContentB); Assert.assertEquals( transformedContentA, _contentTransformerHandlerImpl.transform( contentTransformerContentTypeA, _ORIGINAL_CONTENT)); Assert.assertEquals( transformedContentB, _contentTransformerHandlerImpl.transform( contentTransformerContentTypeB, _ORIGINAL_CONTENT)); }
@Test public void testReturnsTheContentTransformedByAChainOfContentTransformers() throws Exception { String intermediateTransformedContent = RandomTestUtil.randomString(); _registerContentTransformer( _contentTransformerContentType, _ORIGINAL_CONTENT, intermediateTransformedContent); String finalTransformedContent = RandomTestUtil.randomString(); _registerContentTransformer( _contentTransformerContentType, intermediateTransformedContent, finalTransformedContent); Assert.assertEquals( finalTransformedContent, _contentTransformerHandlerImpl.transform( _contentTransformerContentType, _ORIGINAL_CONTENT)); }
@Test public void testReturnsTheContentTransformedByAContentTransformerForAContentType() throws Exception { String transformedContent = RandomTestUtil.randomString(); _registerContentTransformer( _contentTransformerContentType, _ORIGINAL_CONTENT, transformedContent); Assert.assertEquals( transformedContent, _contentTransformerHandlerImpl.transform( _contentTransformerContentType, _ORIGINAL_CONTENT)); }
@Test public void testReturnsTheSameContentIfAContentTransformerThrowsAnException() throws Exception { _registerInvalidContentTransformer( _contentTransformerContentType, _ORIGINAL_CONTENT); Assert.assertSame( _ORIGINAL_CONTENT, _contentTransformerHandlerImpl.transform( _contentTransformerContentType, _ORIGINAL_CONTENT)); }
@Test public void testReturnsTheSameContentIfThereAreNoContentTransformers() { Assert.assertSame( _ORIGINAL_CONTENT, _contentTransformerHandlerImpl.transform( _contentTransformerContentType, _ORIGINAL_CONTENT)); }
@Test public void testRunsTheOtherContentTransformersEvenIfOneOfThemFails() throws Exception { _registerInvalidContentTransformer( _contentTransformerContentType, _ORIGINAL_CONTENT); String transformedContent = RandomTestUtil.randomString(); _registerContentTransformer( _contentTransformerContentType, _ORIGINAL_CONTENT, transformedContent); Assert.assertEquals( transformedContent, _contentTransformerHandlerImpl.transform( _contentTransformerContentType, _ORIGINAL_CONTENT)); }