File: TestChart2DHeadless.java

package info (click to toggle)
libjchart2d-java 3.2.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,808 kB
  • ctags: 3,689
  • sloc: java: 24,854; xml: 831; makefile: 13
file content (227 lines) | stat: -rw-r--r-- 6,971 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 *  TestChart2DHeadless.java of project jchart2d, junit tests for Chart2D
 *  in headless mode.
 *  Copyright (c) 2007 Achim Westermann, created on 14:32:20.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA*
 *  If you modify or optimize the code in a useful way please let me know.
 *  Achim.Westermann@gmx.de
 *
 */
package info.monitorenter.gui.chart;

import info.monitorenter.gui.chart.axis.AAxis;
import info.monitorenter.gui.chart.axis.AxisLinear;
import info.monitorenter.gui.chart.events.Chart2DActionSaveImageSingleton;
import info.monitorenter.gui.chart.labelformatters.LabelFormatterDate;
import info.monitorenter.gui.chart.traces.Trace2DLtd;
import info.monitorenter.gui.chart.traces.Trace2DSimple;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.WeakHashMap;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import junit.framework.Assert;
import junit.framework.TestCase;

/**
 * Junit testcase for <code>{@link info.monitorenter.gui.chart.Chart2D}</code>
 * in headless (non-ui) mode.
 * <p>
 * 
 * @author <a href="mailto:Achim.Westermann@gmx.de">Achim Westermann</a>
 * 
 * @version $Revision: 1.10 $
 */
public class TestChart2DHeadless extends TestCase {

  /**
   * Junit test ui runner.
   * <p>
   * 
   * @param args
   *          ignored.
   */
  public static void main(final String[] args) {
//    TestRunner.run(TestChart2DHeadless.class);
    TestChart2DHeadless test = new TestChart2DHeadless("blabla");
    test.testAddRemoveTraceAfterChangingZIndex();
  }

  /**
   * Constructor with test name.
   * <p>
   * 
   * @param testName
   *          the name of the test.
   */
  public TestChart2DHeadless(final String testName) {
    super(testName);
  }

  /**
   * Creates several charts, adds a trace to each of them, destroys the chart
   * and checks, if a memory leak occurs.
   * <p>
   * 
   * @org.junit.Test
   */
  public void testMemoryLeak() {
    Chart2D chart;
    ITrace2D trace;
    WeakHashMap<Chart2D, ? > chartMap = new WeakHashMap<Chart2D, Object>();
    for (int i = 0; i < 50; i++) {
      chart = new Chart2D();
      System.out.print("Creating really big trace (100000)...");
      trace = new Trace2DLtd(100000);
      System.out.println("    done!");
      if (i % 5 == 0) {
        System.out.println(i * 100 / 50 + " % done.");
      }
      chart.addTrace(trace);
      chartMap.put(chart, null);
      chart.destroy();
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    chart = null;
    trace = null;
    System.runFinalization();
    System.gc();
    try {
      System.out.println("Please wait 10 seconds...");
      Thread.sleep(10000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.runFinalization();
    System.gc();
    try {
      System.out.println("Please wait another 10 seconds...");
      Thread.sleep(10000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    Assert.assertEquals(0, chartMap.size());
  }

  /**
   * Tests the method {@link Chart2D#snapShot()} method in non-UI mode by
   * creating an image of a chart that has not been painted (in UI) before.
   * <p>
   * 
   * @throws IOException
   *           if sth goes wrong in I/O (saving chart, deleting test file,...).
   * 
   * @org.junit.Test
   * 
   */
  public void testSnapshot() throws IOException {
    Chart2D chart;
    ITrace2D trace;
    chart = new Chart2D();
    trace = new Trace2DSimple();
    chart.addTrace(trace);
    for (int i = 0; i < 100; i++) {
      trace.addPoint(i, Math.random() + 1 * i);
    }
    Chart2DActionSaveImageSingleton saver = Chart2DActionSaveImageSingleton.getInstance(chart,
        "BLUE");
    saver.actionPerformed(null);

    final BufferedImage img = chart.snapShot();

    JFrame frame = new JFrame("testShanpshot()");
    JPanel imgPanel = new JPanel() {
      /** serialVersionUID */
      private static final long serialVersionUID = -1171046385909150778L;

      /**
       * @see javax.swing.JComponent#paint(java.awt.Graphics)
       */
      @Override
      public void paint(final Graphics g) {
        super.paint(g);
        g.drawImage(img, 0, 0, null);
      }
    };
    frame.getContentPane().add(imgPanel);
    frame.setSize(img.getWidth(), img.getHeight());
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    frame.setVisible(true);
    while (frame.isVisible()) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }

  /**
   * Tests the policy of adding axis to charts.
   * <p>
   * 
   * Checks the old formatter of the x axis and adds a new x axis with a
   * different formatter: after the call the new axis should have the formatter
   * of the previous axis due to the replace semantics of the
   * {@link Chart2D#setAxisXBottom(AAxis, int)}.
   * <p>
   * 
   */
  @org.junit.Test
  public void testSetAxis() {
    Chart2D chart = new Chart2D();
    IAxisLabelFormatter oldFormatter = chart.getAxisX().getFormatter();
    AAxis<?> axis = new AxisLinear<IAxisScalePolicy>();
    IAxisLabelFormatter formatter = new LabelFormatterDate((SimpleDateFormat) DateFormat
        .getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT));
    axis.setFormatter(formatter);
    chart.setAxisXBottom(axis, 0);
    Assert.assertSame(oldFormatter, chart.getAxisX().getFormatter());
  }

  /**
   * Test for bug 3352480: <br/>
   * Removing trace after z-index has changed does not work.
   * <p>
   * @org.junit.Test
   */
  public void testAddRemoveTraceAfterChangingZIndex() {
    Chart2D chart = new Chart2D();
    ITrace2D trace = new Trace2DSimple();
    chart.addTrace(trace);
    // add some more dummy traces to test the finding of trace within a set:
    for (int i = 0; i < 100; i++) {
      chart.addTrace(new Trace2DSimple());
    }
    trace.setZIndex(new Integer(33));
    boolean removed = chart.removeTrace(trace);
    assertTrue("The trace was not removed after changing z-index!", removed);

  }
  
}