1 package org.jmage.mapper;
2
3 import junit.framework.TestCase;
4 import org.apache.log4j.Logger;
5 import org.jmage.ApplicationContext;
6 import org.jmage.ConfigurationException;
7 import org.jmage.ImageRequest;
8 import org.jmage.JmageException;
9 import org.jmage.dispatcher.RequestDispatcher;
10
11 import java.io.File;
12 import java.io.FileOutputStream;
13 import java.io.IOException;
14 import java.net.URI;
15 import java.net.URISyntaxException;
16 import java.util.Properties;
17
18 /***
19 * Maps Requests for JUNIT
20 */
21 public class JunitMapper extends TestCase {
22 protected ApplicationContext context;
23 protected RequestDispatcher dispatcher;
24 protected ImageRequest request;
25 protected File outputDir;
26
27 protected static Logger log = Logger.getLogger(JunitMapper.class.getName());
28
29 /***
30 * Set up a TestCase
31 *
32 * @throws URISyntaxException
33 */
34 protected void setUp() throws URISyntaxException, ConfigurationException {
35 context = ApplicationContext.getContext();
36 dispatcher = context.obtainRequestDispatcher();
37 request = new ImageRequest();
38 String dir = System.getProperty("JMAGE_OUTPUT_DIR", "images");
39
40 outputDir = new File(dir);
41 if (!outputDir.isDirectory()) {
42 outputDir.mkdirs();
43 }
44 log.info("initialized JunitMapper with resourcedir: " + context.getProperty("resourcedir"));
45 log.info("initialized JunitMapper with JMAGE_OUTPUT_DIR: " + outputDir.getAbsolutePath());
46 this.fillRequest();
47 }
48
49 /***
50 * Fill ImageRequest with params.
51 *
52 * @throws URISyntaxException
53 */
54 public void fillRequest() throws URISyntaxException {
55 this.setImageURI();
56 this.setFilterChainURI();
57 this.setProperties();
58 this.setEncodingFormat();
59 }
60
61 /***
62 * Sets the imageURI
63 *
64 * @throws URISyntaxException
65 */
66 public void setImageURI() throws URISyntaxException {
67 request.setImageURI(new URI("file:///sample/june_vase.jpg"));
68 }
69
70 /***
71 * Sets the filterChainURI
72 *
73 * @throws URISyntaxException
74 */
75 public void setFilterChainURI() throws URISyntaxException {
76 request.setFilterChainURI(new URI[] {new URI("chain:org.jmage.filter.NoOpFilter") });
77 }
78
79 /***
80 * Sets the properties
81 */
82 public void setProperties() {
83 Properties props = request.getFilterChainProperties();
84 assert(props != null) : "filterProperties should not be null";
85 request.setFilterChainProperties(props);
86 }
87
88 public void setEncodingFormat() {
89 request.setEncodingFormat("jpg");
90 }
91
92 /***
93 * Run the test by giving the ImageRequest to the ApplicationContext's dispatcher.
94 *
95 * @throws JmageException
96 */
97 public void testFilter() throws JmageException, IOException {
98 long before = System.currentTimeMillis();
99 dispatcher.dispatch(request);
100 log.debug("processed image in seconds: " + (System.currentTimeMillis() - before) / 1000f);
101 this.writeResults(request);
102 }
103
104 /***
105 * Write the results to disk.
106 *
107 * @param request
108 * @throws IOException
109 */
110 public void writeResults(ImageRequest request) throws IOException {
111 String fileName = null;
112 fileName = this.getClass().getName() + "@" + String.valueOf(request.hashCode());
113 fileName += "." + request.getEncodingFormat();
114 File file = new File(outputDir, fileName);
115 FileOutputStream fos = new FileOutputStream(file);
116 fos.write(request.getEncoded());
117 fos.flush();
118 fos.close();
119 assertTrue("did not write results to disk", file.isFile() && file.exists());
120 log.info("wrote imagerequest: " + request + " test results to file: " + file.getAbsolutePath());
121 }
122
123 /***
124 * Tear down the test.
125 *
126 * @throws Exception
127 */
128 protected void tearDown() throws Exception {
129 super.tearDown();
130 context.releaseRequestDispatcher(dispatcher);
131 outputDir = null;
132 }
133
134 public boolean dispatchFails() {
135 boolean failed = false;
136 try {
137 dispatcher.dispatch(request);
138 this.writeResults(request);
139 } catch (Throwable t) {
140 failed = true;
141 }
142 return failed;
143 }
144 }