KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > TestEscherGraphics2d


1 /* ====================================================================
2    Copyright 2003-2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17 package org.apache.poi.hssf.usermodel;
18
19 import junit.framework.TestCase;
20
21 import java.awt.*;
22 import java.io.FileOutputStream JavaDoc;
23
24 /**
25  * Tests the Graphics2d drawing capability.
26  *
27  * @author Glen Stampoultzis (glens at apache.org)
28  */

29 public class TestEscherGraphics2d extends TestCase
30 {
31     private HSSFShapeGroup escherGroup;
32     private EscherGraphics2d graphics;
33
34     protected void setUp() throws Exception JavaDoc
35     {
36         super.setUp();
37
38         HSSFWorkbook workbook = new HSSFWorkbook();
39         HSSFSheet sheet = workbook.createSheet("test");
40         escherGroup = sheet.createDrawingPatriarch().createGroup(new HSSFClientAnchor(0,0,1023,255,(short)0,0,(short) 0,0));
41         escherGroup = new HSSFShapeGroup(null, new HSSFChildAnchor());
42         EscherGraphics g = new EscherGraphics(this.escherGroup, workbook, Color.black, 1.0f);
43         graphics = new EscherGraphics2d(g);
44
45     }
46
47     public void testDrawString() throws Exception JavaDoc
48     {
49         graphics.drawString("This is a test", 10, 10);
50         HSSFTextbox t = (HSSFTextbox) escherGroup.getChildren().get(0);
51         assertEquals("This is a test", t.getString().toString());
52     }
53
54     public void testFillRect() throws Exception JavaDoc
55     {
56         graphics.fillRect( 10, 10, 20, 20 );
57         HSSFSimpleShape s = (HSSFSimpleShape) escherGroup.getChildren().get(0);
58         assertEquals(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE, s.getShapeType());
59         assertEquals(10, s.getAnchor().getDx1());
60         assertEquals(10, s.getAnchor().getDy1());
61         assertEquals(30, s.getAnchor().getDy2());
62         assertEquals(30, s.getAnchor().getDx2());
63     }
64
65     public void testGetFontMetrics() throws Exception JavaDoc
66     {
67         FontMetrics fontMetrics = graphics.getFontMetrics(graphics.getFont());
68     if (graphics.getFont().toString().indexOf("dialog") != -1) // if dialog is returned we can't run the test properly.
69
return;
70         assertEquals(7, fontMetrics.charWidth('X'));
71         assertEquals("java.awt.Font[family=Arial,name=Arial,style=plain,size=10]", fontMetrics.getFont().toString());
72     }
73
74     public void testSetFont() throws Exception JavaDoc
75     {
76         Font f = new Font("Helvetica", 0, 12);
77         graphics.setFont(f);
78         assertEquals(f, graphics.getFont());
79     }
80
81     public void testSetColor() throws Exception JavaDoc
82     {
83         graphics.setColor(Color.red);
84         assertEquals(Color.red, graphics.getColor());
85     }
86
87     public void testGetFont() throws Exception JavaDoc
88     {
89         Font f = graphics.getFont();
90         if (graphics.getFont().toString().indexOf("dialog") != -1) // if dialog is returned we can't run the test properly.
91
return;
92
93         assertEquals("java.awt.Font[family=Arial,name=Arial,style=plain,size=10]", f.toString());
94     }
95 }
96
Popular Tags