Source Code Cross Referenced for TestImageFormatter.java in  » Web-Framework » rife-1.6.1 » com » uwyn » rife » cmf » format » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » rife 1.6.1 » com.uwyn.rife.cmf.format 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003:         * Distributed under the terms of either:
004:         * - the common development and distribution license (CDDL), v1.0; or
005:         * - the GNU Lesser General Public License, v2.1 or later
006:         * $Id: TestImageFormatter.java 3634 2007-01-08 21:42:24Z gbevin $
007:         */
008:        package com.uwyn.rife.cmf.format;
009:
010:        import com.uwyn.rife.cmf.Content;
011:        import com.uwyn.rife.cmf.MimeType;
012:        import com.uwyn.rife.cmf.dam.exceptions.ContentManagerException;
013:        import com.uwyn.rife.cmf.format.exceptions.FormatException;
014:        import com.uwyn.rife.cmf.format.exceptions.InvalidContentDataTypeException;
015:        import com.uwyn.rife.cmf.format.exceptions.UnreadableDataFormatException;
016:        import com.uwyn.rife.cmf.format.exceptions.UnsupportedTargetMimeTypeException;
017:        import com.uwyn.rife.cmf.loader.ImageContentLoader;
018:        import com.uwyn.rife.cmf.transform.ImageContentTransformer;
019:        import com.uwyn.rife.resources.ResourceFinderClasspath;
020:        import com.uwyn.rife.tools.FileUtils;
021:        import java.awt.AlphaComposite;
022:        import java.awt.Color;
023:        import java.awt.Graphics2D;
024:        import java.awt.Image;
025:        import java.awt.image.BufferedImage;
026:        import java.net.URL;
027:        import java.util.Arrays;
028:        import java.util.Map;
029:        import javax.swing.ImageIcon;
030:        import junit.framework.TestCase;
031:
032:        public class TestImageFormatter extends TestCase {
033:            public TestImageFormatter(String name) {
034:                super (name);
035:            }
036:
037:            public void testFormatBasic() throws Exception {
038:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
039:                        .getResource("uwyn.gif");
040:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
041:
042:                Content content = new Content(MimeType.IMAGE_PNG,
043:                        data_image_gif);
044:                ImageFormatter formatter = new ImageFormatter();
045:                byte[] result = formatter.format(content, null);
046:
047:                assertNotNull(result);
048:
049:                URL image_resource_png = ResourceFinderClasspath.getInstance()
050:                        .getResource("uwyn.png");
051:                byte[] data_image_png = FileUtils.readBytes(image_resource_png);
052:
053:                assertTrue(Arrays.equals(data_image_png, result));
054:            }
055:
056:            public void testFormatInvalidDataType() throws Exception {
057:                Content content = new Content(MimeType.IMAGE_PNG, new Object());
058:                ImageFormatter formatter = new ImageFormatter();
059:                try {
060:                    formatter.format(content, null);
061:                    fail();
062:                } catch (InvalidContentDataTypeException e) {
063:                    assertSame(byte[].class, e.getExpectedType());
064:                    assertSame(formatter, e.getFormatter());
065:                    assertSame(MimeType.IMAGE_PNG, e.getMimeType());
066:                    assertSame(Object.class, e.getReceivedType());
067:                }
068:            }
069:
070:            public void testFormatCachedLoadedData() throws Exception {
071:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
072:                        .getResource("uwyn.gif");
073:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
074:
075:                Content content = new Content(MimeType.IMAGE_PNG,
076:                        data_image_gif);
077:                Image image = new ImageContentLoader().load(data_image_gif,
078:                        false, null);
079:                content.setCachedLoadedData(image);
080:
081:                ImageFormatter formatter = new ImageFormatter();
082:                byte[] result = formatter.format(content, null);
083:
084:                assertNotNull(result);
085:
086:                URL image_resource_png = ResourceFinderClasspath.getInstance()
087:                        .getResource("uwyn.png");
088:                byte[] data_image_png = FileUtils.readBytes(image_resource_png);
089:
090:                assertTrue(Arrays.equals(data_image_png, result));
091:            }
092:
093:            public void testFormatUnreadableData() throws Exception {
094:                Content content = new Content(MimeType.IMAGE_PNG, new byte[] {
095:                        34, 9, 12, 5, 92 }); // random invalid bytes
096:                ImageFormatter formatter = new ImageFormatter();
097:                try {
098:                    formatter.format(content, null);
099:                    fail();
100:                } catch (UnreadableDataFormatException e) {
101:                    assertSame(MimeType.IMAGE_PNG, e.getMimeType());
102:                    assertTrue(e.getErrors().size() > 0);
103:                }
104:            }
105:
106:            public void testFormatUnsupportedMimetype() throws Exception {
107:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
108:                        .getResource("uwyn.gif");
109:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
110:
111:                Content content = new Content(MimeType.APPLICATION_XHTML,
112:                        data_image_gif);
113:                ImageFormatter formatter = new ImageFormatter();
114:                try {
115:                    formatter.format(content, null);
116:                    fail();
117:                } catch (UnsupportedTargetMimeTypeException e) {
118:                    assertSame(MimeType.APPLICATION_XHTML, e.getMimeType());
119:                }
120:            }
121:
122:            public void testFormatAttributeWidth() throws Exception {
123:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
124:                        .getResource("uwyn.gif");
125:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
126:
127:                Content content = new Content(MimeType.IMAGE_PNG,
128:                        data_image_gif);
129:                content.attribute("width", 20);
130:                ImageFormatter formatter = new ImageFormatter();
131:                byte[] result = formatter.format(content, null);
132:
133:                assertNotNull(result);
134:
135:                URL image_resource_png = ResourceFinderClasspath.getInstance()
136:                        .getResource("uwyn_resized-width_20.png");
137:                byte[] data_image_png = FileUtils.readBytes(image_resource_png);
138:
139:                assertTrue(Arrays.equals(data_image_png, result));
140:            }
141:
142:            public void testFormatInvalidAttributeWidth() throws Exception {
143:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
144:                        .getResource("uwyn.gif");
145:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
146:
147:                Content content = new Content(MimeType.IMAGE_PNG,
148:                        data_image_gif);
149:                content.attribute("width", "notanumber");
150:                ImageFormatter formatter = new ImageFormatter();
151:                try {
152:                    formatter.format(content, null);
153:                    fail();
154:                } catch (FormatException e) {
155:                    assertTrue(e.getCause() instanceof  NumberFormatException);
156:                }
157:            }
158:
159:            public void testFormatAttributeHeight() throws Exception {
160:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
161:                        .getResource("uwyn.gif");
162:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
163:
164:                Content content = new Content(MimeType.IMAGE_PNG,
165:                        data_image_gif);
166:                content.attribute("height", 15);
167:                ImageFormatter formatter = new ImageFormatter();
168:                byte[] result = formatter.format(content, null);
169:
170:                assertNotNull(result);
171:
172:                URL image_resource_png = ResourceFinderClasspath.getInstance()
173:                        .getResource("uwyn_resized-height_15.png");
174:                byte[] data_image_png = FileUtils.readBytes(image_resource_png);
175:
176:                assertTrue(Arrays.equals(data_image_png, result));
177:            }
178:
179:            public void testFormatInvalidAttributeHeight() throws Exception {
180:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
181:                        .getResource("uwyn.gif");
182:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
183:
184:                Content content = new Content(MimeType.IMAGE_PNG,
185:                        data_image_gif);
186:                content.attribute("height", "notanumber");
187:                ImageFormatter formatter = new ImageFormatter();
188:                try {
189:                    formatter.format(content, null);
190:                    fail();
191:                } catch (FormatException e) {
192:                    assertTrue(e.getCause() instanceof  NumberFormatException);
193:                }
194:            }
195:
196:            public void testFormatAttributeWidthHeight() throws Exception {
197:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
198:                        .getResource("uwyn.gif");
199:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
200:
201:                Content content = new Content(MimeType.IMAGE_JPEG,
202:                        data_image_gif);
203:                content.attribute("width", 30).attribute("height", 70);
204:                ImageFormatter formatter = new ImageFormatter();
205:                byte[] result = formatter.format(content, null);
206:
207:                assertNotNull(result);
208:
209:                URL image_resource_png = ResourceFinderClasspath.getInstance()
210:                        .getResource("uwyn_resized-width_30-height_70.jpg");
211:                byte[] data_image_png = FileUtils.readBytes(image_resource_png);
212:
213:                assertTrue(Arrays.equals(data_image_png, result));
214:            }
215:
216:            public void testFormatAttributeWidthHeight2() throws Exception {
217:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
218:                        .getResource("uwyn.gif");
219:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
220:
221:                Content content = new Content(MimeType.IMAGE_JPEG,
222:                        data_image_gif);
223:                content.attribute("width", 300).attribute("height", 70);
224:                ImageFormatter formatter = new ImageFormatter();
225:                byte[] result = formatter.format(content, null);
226:
227:                assertNotNull(result);
228:
229:                URL image_resource_png = ResourceFinderClasspath.getInstance()
230:                        .getResource("uwyn_resized-width_300-height_70.jpg");
231:                byte[] data_image_png = FileUtils.readBytes(image_resource_png);
232:
233:                assertTrue(Arrays.equals(data_image_png, result));
234:            }
235:
236:            public void testFormatNegativeAttributeWidthHeight()
237:                    throws Exception {
238:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
239:                        .getResource("uwyn.gif");
240:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
241:
242:                Content content = new Content(MimeType.IMAGE_PNG,
243:                        data_image_gif);
244:                content.attribute("width", -12).attribute("height", -5);
245:                ImageFormatter formatter = new ImageFormatter();
246:                byte[] result = formatter.format(content, null);
247:
248:                assertNotNull(result);
249:
250:                URL image_resource_png = ResourceFinderClasspath.getInstance()
251:                        .getResource("uwyn.png");
252:                byte[] data_image_png = FileUtils.readBytes(image_resource_png);
253:
254:                assertTrue(Arrays.equals(data_image_png, result));
255:            }
256:
257:            public void testFormatUnsupportedAttributes() throws Exception {
258:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
259:                        .getResource("uwyn.gif");
260:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
261:
262:                Content content = new Content(MimeType.IMAGE_PNG,
263:                        data_image_gif);
264:                content.attribute("unsupported", "blah");
265:                ImageFormatter formatter = new ImageFormatter();
266:                byte[] result = formatter.format(content, null);
267:
268:                assertNotNull(result);
269:
270:                URL image_resource_png = ResourceFinderClasspath.getInstance()
271:                        .getResource("uwyn.png");
272:                byte[] data_image_png = FileUtils.readBytes(image_resource_png);
273:
274:                assertTrue(Arrays.equals(data_image_png, result));
275:            }
276:
277:            public void testFormatTransformer() throws Exception {
278:                URL image_resource_gif = ResourceFinderClasspath.getInstance()
279:                        .getResource("uwyn.gif");
280:                byte[] data_image_gif = FileUtils.readBytes(image_resource_gif);
281:
282:                Content content = new Content(MimeType.IMAGE_PNG,
283:                        data_image_gif);
284:                ImageFormatter formatter = new ImageFormatter();
285:                byte[] result = formatter.format(content,
286:                        new TransparentImageTransformer());
287:
288:                assertNotNull(result);
289:
290:                URL image_resource_png = ResourceFinderClasspath.getInstance()
291:                        .getResource("uwyn-transparent.png");
292:                byte[] data_image_png = FileUtils.readBytes(image_resource_png);
293:
294:                assertTrue(Arrays.equals(data_image_png, result));
295:            }
296:
297:            static class TransparentImageTransformer implements 
298:                    ImageContentTransformer {
299:                public Image transform(Image data,
300:                        Map<String, String> attributes)
301:                        throws ContentManagerException {
302:                    // retreive the rife logo to stamp on top of it
303:                    URL rife_url = ResourceFinderClasspath.getInstance()
304:                            .getResource("rife-logo_small.png");
305:                    Image rife_image = new ImageIcon(rife_url).getImage();
306:
307:                    // create a new drawing buffer
308:                    int width = data.getWidth(null);
309:                    int height = data.getHeight(null);
310:                    BufferedImage buffer = new BufferedImage(width, height,
311:                            BufferedImage.TYPE_INT_RGB);
312:                    Graphics2D g2 = buffer.createGraphics();
313:
314:                    // make the background white
315:                    g2.setColor(Color.WHITE);
316:                    g2.fillRect(0, 0, width, height);
317:
318:                    // draw a transparent image on it
319:                    g2.setComposite(AlphaComposite.getInstance(
320:                            AlphaComposite.SRC_OVER, 0.5f));
321:                    g2.drawImage(rife_image, 0, 0, null);
322:
323:                    // draw a transparent image on it
324:                    g2.setComposite(AlphaComposite.getInstance(
325:                            AlphaComposite.SRC_OVER, 0.5f));
326:                    g2.drawImage(data, 0, 0, null);
327:
328:                    // clean up
329:                    g2.dispose();
330:
331:                    return buffer;
332:                }
333:            }
334:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.