001: package org.openi.chart;
002:
003: import com.tonbeller.jpivot.olap.model.OlapException;
004: import com.tonbeller.jpivot.olap.model.OlapModel;
005:
006: import junit.framework.TestCase;
007:
008: import org.apache.log4j.Logger;
009:
010: import org.jfree.chart.ChartUtilities;
011: import org.jfree.chart.JFreeChart;
012: import org.jfree.chart.plot.Plot;
013:
014: import org.jfree.data.category.DefaultCategoryDataset;
015:
016: import org.openi.analysis.Analysis;
017: import org.openi.analysis.Datasource;
018:
019: import org.openi.test.NullOutputStream;
020: import org.openi.test.Util;
021:
022: import org.openi.xml.BeanStorage;
023:
024: import org.openi.xmla.DatasetAdapter;
025: import org.openi.xmla.XmlaConnector;
026:
027: import org.xml.sax.SAXException;
028:
029: import java.io.FileNotFoundException;
030: import java.io.FileOutputStream;
031: import java.io.IOException;
032: import java.io.OutputStream;
033: import java.util.Locale;
034:
035: public class ChartTester extends TestCase {
036: private static Logger logger = Logger.getLogger(ChartTester.class);
037: private Analysis analysis;
038:
039: private String xmlaUri = "http://localhost:8181/xmla/msxisapi.dll";
040: private String catalog = "Foodmart 2000";
041:
042: protected void setUp() throws Exception {
043: super .setUp();
044: Util.setupLog4j();
045: this .analysis = restoreAnalysis("Sales_by_Store_and_Time.analysis");
046: }
047:
048: protected void tearDown() throws Exception {
049: super .tearDown();
050: }
051:
052: private OlapModel getDataset(String xmlaUri, String catalog,
053: Analysis analysis) throws IOException, OlapException,
054: SAXException {
055: XmlaConnector connector = new XmlaConnector();
056: String mdxQuery = analysis.getMdxQuery();
057:
058: OlapModel model = connector.query(new Datasource(xmlaUri,
059: catalog), mdxQuery);
060:
061: // return new DatasetAdapter().buildCategoryDataset(model);
062: return model;
063: }
064:
065: private Analysis restoreAnalysis(String analysisName)
066: throws IOException {
067: return (Analysis) new BeanStorage().restoreBeanFromFile(Util
068: .findTestDirectory()
069: + "/projects/standard/public/" + analysisName);
070: }
071:
072: public void testCreateThumbnail() throws Exception {
073: // DefaultCategoryDataset dataset = getDataset(xmlaUri, catalog, analysis);
074: analysis.setChartType(1);
075: analysis.setBgColorR(255);
076: analysis.setBgColorG(255);
077: analysis.setBgColorB(255);
078:
079: OlapModel olapModel = getDataset(xmlaUri, catalog, analysis);
080: OutputStream out = new FileOutputStream(Util
081: .findTestDirectory()
082: + "/test.jpg");
083: // NullOutputStream out = new NullOutputStream();
084: // analysis.setChartType(19);
085: try {
086: EnhancedChartFactory.createChart(out, analysis, olapModel,
087: 950, 600, Locale.getDefault());
088: } catch (Throwable e) {
089: logger.error(e);
090: }
091: // assertTrue(out.length()>0);
092: }
093:
094: public void AllChartTypes() throws Exception {
095: long start = System.currentTimeMillis();
096:
097: // DefaultCategoryDataset dataset = getDataset(xmlaUri, catalog, analysis);
098: OlapModel olapModel = getDataset(xmlaUri, catalog, analysis);
099:
100: // NullOutputStream out = new NullOutputStream();
101: // try 1 - 18
102: for (int i = 1; i < 19; i++) {
103: // OutputStream out = new FileOutputStream(Util.findTestDirectory() +
104: // "/test" + i + ".jpg");
105: NullOutputStream out = new NullOutputStream();
106: analysis.setChartType(i);
107: try {
108: EnhancedChartFactory.createChart(out, analysis,
109: olapModel, 320, 240, Locale.getDefault());
110: } catch (Throwable e) {
111: logger.error(e);
112: }
113: assertTrue(out.length() > 0);
114: }
115:
116: long elapsed = System.currentTimeMillis() - start;
117: logger.debug("test complete in: " + elapsed + " ms");
118: }
119:
120: /**
121: * [2006].[Q1]
122: */
123: public void TimeSeriesChartUsingYearQuarter() throws Exception {
124: XmlaConnector connector = new XmlaConnector();
125: String mdxQuery = analysis.getMdxQuery();
126:
127: OlapModel model = connector.query(new Datasource(xmlaUri,
128: catalog), mdxQuery);
129:
130: OutputStream out = new FileOutputStream(Util
131: .findTestDirectory()
132: + "/time-series-quarter.jpg");
133: JFreeChart chart = EnhancedChartFactory.createTimeChart(
134: analysis.getAnalysisTitle(), analysis
135: .getHorizAxisLabel(), analysis
136: .getVertAxisLabel(), new DatasetAdapter(Locale
137: .getDefault()).buildXYDataset(model));
138: ChartUtilities.writeChartAsJPEG(out, 1.0f, chart, 640, 480);
139: out.flush();
140: out.close();
141: }
142:
143: /**
144: * [2006].[January]
145: */
146: public void TimeSeriesChartYYYYMMM() throws Exception {
147: XmlaConnector connector = new XmlaConnector();
148: String mdxQuery = analysis.getMdxQuery();
149:
150: OlapModel model = connector.query(
151: new Datasource("http://hostname/xmla/msxisapi.dll",
152: "Customer Weekly"), mdxQuery);
153:
154: OutputStream out = new FileOutputStream(Util
155: .findTestDirectory()
156: + "/time-series-YYYY-MMM.jpg");
157: JFreeChart chart = EnhancedChartFactory.createTimeChart(
158: analysis.getAnalysisTitle(), analysis
159: .getHorizAxisLabel(), analysis
160: .getVertAxisLabel(), new DatasetAdapter(Locale
161: .getDefault()).buildXYDataset(model));
162: ChartUtilities.writeChartAsJPEG(out, 1.0f, chart, 640, 480);
163: out.flush();
164: out.close();
165: }
166:
167: /**
168: * [2006].[January].[1]
169: * @throws SAXException
170: * @throws OlapException
171: * @throws IOException
172: */
173: public void TimeSeriesChartYYYYMMMdd() throws IOException,
174: OlapException, SAXException {
175: XmlaConnector connector = new XmlaConnector();
176: String mdxQuery = analysis.getMdxQuery();
177:
178: OlapModel model = connector.query(new Datasource(
179: "http://server/xmla/msxisapi.dll", "Customer Weekly"),
180: mdxQuery);
181:
182: OutputStream out = new FileOutputStream(Util
183: .findTestDirectory()
184: + "/time-series-YYYY-MMM-dd.jpg");
185: try {
186: JFreeChart chart = EnhancedChartFactory.createTimeChart(
187: analysis.getAnalysisTitle(), analysis
188: .getHorizAxisLabel(), analysis
189: .getVertAxisLabel(), new DatasetAdapter(
190: Locale.getDefault()).buildXYDataset(model));
191: ChartUtilities.writeChartAsJPEG(out, 1.0f, chart, 640, 480);
192: } finally {
193: out.flush();
194: out.close();
195: }
196: }
197: }
|