KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > setup > comptest > CmsSetupTestSimapi


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/setup/comptest/CmsSetupTestSimapi.java,v $
3  * Date : $Date: 2006/03/27 14:52:42 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.setup.comptest;
33
34 import com.alkacon.simapi.RenderSettings;
35 import com.alkacon.simapi.Simapi;
36 import com.alkacon.simapi.filter.ImageMath;
37 import com.alkacon.simapi.filter.RotateFilter;
38
39 import org.opencms.setup.CmsSetupBean;
40
41 import java.awt.image.BufferedImage JavaDoc;
42 import java.io.File JavaDoc;
43 import java.util.Arrays JavaDoc;
44 import java.util.Iterator JavaDoc;
45
46 import javax.imageio.ImageIO JavaDoc;
47
48
49 /**
50  * Tests the image processing capabilities.<p>
51  *
52  * @author Michael Moossen
53  *
54  * @version $Revision: 1.2 $
55  *
56  * @since 6.1.8
57  */

58 public class CmsSetupTestSimapi implements I_CmsSetupTest {
59
60     /** The test name. */
61     public static final String JavaDoc TEST_NAME = "Image Processing";
62
63     /**
64      * @see org.opencms.setup.comptest.I_CmsSetupTest#getName()
65      */

66     public String JavaDoc getName() {
67
68         return TEST_NAME;
69     }
70     
71     /**
72      * @see org.opencms.setup.comptest.I_CmsSetupTest#execute(org.opencms.setup.CmsSetupBean)
73      */

74     public CmsSetupTestResult execute(CmsSetupBean setupBean) {
75         
76         CmsSetupTestResult testResult = new CmsSetupTestResult(this);
77         boolean ok = true;
78         Throwable JavaDoc ex = null;
79         try {
80             RenderSettings settings = new RenderSettings(Simapi.RENDER_QUALITY);
81             settings.setCompressionQuality(0.85f);
82             Simapi simapi = new Simapi(settings);
83
84             ImageIO.scanForPlugins();
85             Iterator JavaDoc pngReaders = ImageIO.getImageReadersByFormatName(Simapi.TYPE_PNG);
86             if (!pngReaders.hasNext()) {
87                 throw (new Exception JavaDoc("No Java ImageIO readers for the PNG format are available."));
88             }
89             Iterator JavaDoc pngWriters = ImageIO.getImageWritersByFormatName(Simapi.TYPE_PNG);
90             if (!pngWriters.hasNext()) {
91                 throw (new Exception JavaDoc("No Java ImageIO writers for the PNG format are available."));
92             }
93
94             String JavaDoc basePath = setupBean.getWebAppRfsPath();
95             if (!basePath.endsWith(File.separator)) {
96                 basePath += File.separator;
97             }
98             basePath += "setup" + File.separator + "resources" + File.separator;
99
100             BufferedImage JavaDoc img1 = Simapi.read(basePath + "test1.png");
101             BufferedImage JavaDoc img3 = simapi.applyFilter(img1, new RotateFilter(ImageMath.PI));
102             simapi.write(img3, basePath + "test3.png", Simapi.TYPE_PNG);
103             BufferedImage JavaDoc img2 = Simapi.read(basePath + "test2.png");
104
105             ok = Arrays.equals(simapi.getBytes(img2, Simapi.TYPE_PNG), simapi.getBytes(img3, Simapi.TYPE_PNG));
106         } catch (Throwable JavaDoc e) {
107             ok = false;
108             ex = e;
109         }
110
111         if (ok) {
112             testResult.setResult(RESULT_PASSED);
113             testResult.setGreen();
114         } else {
115             testResult.setYellow();
116             if (ex != null) {
117                 testResult.setResult(RESULT_FAILED);
118                 testResult.setHelp(ex.toString());
119                 testResult.setInfo("<p><code>-Djava.awt.headless=true</code> JVM parameter or X-Server may be missing.<br>"
120                     + "<b>You can continue the setup, but image processing will be disabled.</b></p>");
121             } else {
122                 testResult.setResult(RESULT_WARNING);
123                 testResult.setHelp("Image processing works but result does not exactly match.");
124                 StringBuffer JavaDoc info = new StringBuffer JavaDoc();
125                 info.append("<p>Please check the following images for visible differences:</p>");
126                 info.append("<table width='100%'>");
127                 info.append("<tr><th>Expected</th><th>Result</th></tr>");
128                 info.append("<tr><td align='center' width='50%'><img SRC='resources/test2.png'></td>");
129                 info.append("<td align='center' width='50%'><img SRC='resources/test3.png'></td></table>");
130                 info.append("<p><b>You can continue the setup, but image processing may not always work as expected.</b></p>");
131                 testResult.setInfo(info.toString());
132             }
133         }
134         return testResult;
135     }
136 }
137
Popular Tags