KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > tests > ConvertImageTest


1 package org.mmbase.bridge.tests;
2
3 import org.mmbase.bridge.*;
4 import java.io.*;
5
6 import org.mmbase.util.functions.Function;
7 import org.mmbase.util.functions.Parameters;
8
9 /**
10  * JUnit tests for convertimage-interface implementation.
11  *
12  * @author Michiel Meeuwissen
13  * @version $Id: ConvertImageTest.java,v 1.5 2005/05/29 11:20:04 nico Exp $
14  */

15 public class ConvertImageTest extends org.mmbase.tests.BridgeTest {
16
17     private final static String JavaDoc JPG_IMAGE_NAME = "testimage.jpg";
18
19     public void testImportedJpegImage() {
20         Cloud cloud = getCloud();
21         Node node = cloud.getNode("jpeg.test.image");
22         node.getByteValue("handle");
23         assertTrue("MMBase failed to determine mime-type properly (magicfile problem?)", node.getStringValue("itype").equals("jpeg"));
24         //node.delete();
25
}
26     
27     /**
28      * test if an image can be converted using the getIntValue
29      */

30     public void testGetInvalueCachedImage() {
31         Cloud cloud = getCloud();
32         Node node = cloud.getNode("jpeg.test.image");
33         node.getIntValue("cache(s(30x30))");
34     }
35
36     /**
37      * test if an image can be converted using getFunctionValue
38      *
39      */

40     public void testFunctionValueCachedImage() {
41         Cloud cloud = getCloud();
42         Node node = cloud.getNode("jpeg.test.image");
43         Function f = node.getFunction("cache");
44         Parameters p = f.createParameters();
45         p.set("template","s(30x30)");
46         f.getFunctionValue(p);
47     }
48
49     /**
50      * Sets up before each test.
51      */

52     public void setUp() throws Exception JavaDoc {
53         startMMBase();
54         startLogging();
55         Cloud cloud = getCloud();
56         NodeManager nodeManager = cloud.getNodeManager("images");
57         Node jpegNode = nodeManager.createNode();
58         jpegNode.setStringValue("title", JPG_IMAGE_NAME);
59         byte[] bytes = getTextImageBytes(JPG_IMAGE_NAME);
60         jpegNode.setByteValue("handle", bytes);
61         jpegNode.commit();
62         jpegNode.createAlias("jpeg.test.image");
63     }
64
65
66     /* (non-Javadoc)
67      * @see junit.framework.TestCase#tearDown()
68      */

69     protected void tearDown() throws Exception JavaDoc {
70         Cloud cloud = getCloud();
71         Node node = cloud.getNode("jpeg.test.image");
72         node.delete();
73     }
74     /**
75      * read the test image from the file system and return it a byte[]
76      *
77      * @return the byte[] containing the image
78      */

79     private byte[] getTextImageBytes(String JavaDoc name) {
80         try {
81             InputStream in = this.getClass().getResourceAsStream(name);
82
83             ByteArrayOutputStream baos = new ByteArrayOutputStream();
84
85             byte[] buffer = new byte[200];
86             int readSize = 0;
87             while ((readSize = in.read(buffer)) > 0) {
88                 baos.write(buffer, 0, readSize);
89             }
90             return baos.toByteArray();
91         } catch (IOException ioe) {
92             fail("did not find image to load " + this.getClass().getResource(name));
93         }
94         return null;
95     }
96 }
97
Popular Tags