01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: TestRawFormatter.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.cmf.format;
09:
10: import com.uwyn.rife.cmf.Content;
11: import com.uwyn.rife.cmf.MimeType;
12: import com.uwyn.rife.cmf.dam.exceptions.ContentManagerException;
13: import com.uwyn.rife.cmf.format.exceptions.InvalidContentDataTypeException;
14: import com.uwyn.rife.cmf.transform.RawContentTransformer;
15: import com.uwyn.rife.resources.ResourceFinderClasspath;
16: import com.uwyn.rife.tools.FileUtils;
17: import com.uwyn.rife.tools.exceptions.FileUtilsErrorException;
18: import java.io.ByteArrayInputStream;
19: import java.io.InputStream;
20: import java.net.URL;
21: import java.util.Arrays;
22: import java.util.Map;
23: import junit.framework.TestCase;
24:
25: public class TestRawFormatter extends TestCase {
26: public TestRawFormatter(String name) {
27: super (name);
28: }
29:
30: public void testFormatBasic() throws Exception {
31: URL image_resource_gif = ResourceFinderClasspath.getInstance()
32: .getResource("uwyn.gif");
33: byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
34:
35: Content content = new Content(MimeType.RAW,
36: new ByteArrayInputStream(data_image_gif));
37: RawFormatter formatter = new RawFormatter();
38: InputStream result = (InputStream) formatter.format(content,
39: null);
40:
41: assertNotNull(result);
42:
43: assertTrue(Arrays.equals(data_image_gif, FileUtils
44: .readBytes(result)));
45: }
46:
47: public void testFormatInvalidDataType() throws Exception {
48: Content content = new Content(MimeType.RAW, new byte[1]);
49: RawFormatter formatter = new RawFormatter();
50: try {
51: formatter.format(content, null);
52: fail();
53: } catch (InvalidContentDataTypeException e) {
54: assertSame(InputStream.class, e.getExpectedType());
55: assertSame(formatter, e.getFormatter());
56: assertSame(MimeType.RAW, e.getMimeType());
57: assertSame(byte[].class, e.getReceivedType());
58: }
59: }
60:
61: public void testFormatTransformer() throws Exception {
62: URL image_resource_gif = ResourceFinderClasspath.getInstance()
63: .getResource("uwyn.gif");
64: byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
65:
66: Content content = new Content(MimeType.RAW,
67: new ByteArrayInputStream(data_image_gif));
68: RawFormatter formatter = new RawFormatter();
69: InputStream result = (InputStream) formatter.format(content,
70: new TransparentRawTransformer());
71:
72: assertNotNull(result);
73:
74: URL image_resource_png = ResourceFinderClasspath.getInstance()
75: .getResource("uwyn.png");
76: byte[] data_image_png = FileUtils.readBytes(image_resource_png);
77:
78: assertTrue(Arrays.equals(data_image_png, FileUtils
79: .readBytes(result)));
80: }
81:
82: static class TransparentRawTransformer implements
83: RawContentTransformer {
84: public InputStream transform(InputStream data,
85: Map<String, String> attributes)
86: throws ContentManagerException {
87: try {
88: URL image_resource_png = ResourceFinderClasspath
89: .getInstance().getResource("uwyn.png");
90: byte[] data_image_png = FileUtils
91: .readBytes(image_resource_png);
92:
93: return new ByteArrayInputStream(data_image_png);
94: } catch (FileUtilsErrorException e) {
95: return null;
96: }
97: }
98: }
99: }
|