ChainingMediaPackageSerializer implements MediaPackageSerializer { @Override public URI encodeURI(URI uri) throws URISyntaxException { return $(serializers).reverse().foldl(uri, new Fn2<URI, MediaPackageSerializer, URI>() { @Override public URI apply(URI uri, MediaPackageSerializer serializer) { try { return serializer.encodeURI(uri); } catch (URISyntaxException e) { logger.warn("Error while encoding URI with serializer '{}':", serializer, e); return chuck(e); } } }); }  @Override URI encodeURI(URI uri); @Override URI decodeURI(URI uri); @Override int getRanking(); static final int RANKING; }
@Test public void testEncodeURI() throws Exception { final URI uri1 = new URI("http: MediaPackageSerializer serializer1 = createNiceMock(MediaPackageSerializer.class); expect(serializer1.getRanking()).andStubReturn(10); expect(serializer1.encodeURI(anyObject(URI.class))).andStubReturn(uri1); expect(serializer1.decodeURI(anyObject(URI.class))).andStubReturn(uri1); replay(serializer1); final URI uri2 = new URI("https: MediaPackageSerializer serializer2 = createNiceMock(MediaPackageSerializer.class); expect(serializer2.getRanking()).andStubReturn(5); expect(serializer2.encodeURI(anyObject(URI.class))).andStubReturn(uri2); expect(serializer2.decodeURI(anyObject(URI.class))).andStubReturn(uri2); replay(serializer2); ChainingMediaPackageSerializer chain = new ChainingMediaPackageSerializer(); chain.addMediaPackageSerializer(serializer1); chain.addMediaPackageSerializer(serializer2); final URI uri = new URI("file: assertEquals(uri2, chain.encodeURI(uri)); assertEquals(uri1, chain.decodeURI(uri)); }