001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Denis M. Kishenko
019: * @version $Revision$
020: */package java.awt;
021:
022: import junit.framework.TestCase;
023:
024: import java.awt.BasicStroke;
025: import java.awt.Shape;
026: import java.awt.Tools;
027: import java.awt.image.BufferedImage;
028: import java.io.File;
029: import java.io.IOException;
030: import java.io.StreamTokenizer;
031: import java.io.StringReader;
032: import java.util.Arrays;
033: import java.net.URL;
034:
035: public class BasicStrokeTest extends TestCase {
036:
037: boolean OUTPUT = System.getProperty("TEST_OUTPUT") != null;
038: final double SHAPE_DELTA = 0.01;
039:
040: String shapePath, outputPath;
041: BasicStroke stroke;
042: Shape srcShape, dstShape;
043:
044: public BasicStrokeTest(String name) {
045: super (name);
046:
047: String classPath = "../resources/shapes/"
048: + Tools.getClasstPath(this .getClass());
049: URL url = ClassLoader.getSystemClassLoader().getResource(
050: classPath);
051:
052: assertNotNull("Path not found " + classPath, url);
053: shapePath = url.getPath();
054: outputPath = shapePath + "output/";
055: }
056:
057: public void testCreate() {
058: BasicStroke bs = new BasicStroke();
059: assertNotNull(bs);
060: assertEquals(bs, new BasicStroke(1.0f));
061: assertEquals(bs, new BasicStroke(1.0f, BasicStroke.CAP_SQUARE,
062: BasicStroke.JOIN_MITER));
063: assertEquals(bs, new BasicStroke(1.0f, BasicStroke.CAP_SQUARE,
064: BasicStroke.JOIN_MITER, 10.0f));
065: assertEquals(bs, new BasicStroke(1.0f, BasicStroke.CAP_SQUARE,
066: BasicStroke.JOIN_MITER, 10.0f, null, 0.0f));
067:
068: assertEquals(1.0f, bs.getLineWidth(), 0.0f);
069: assertEquals(BasicStroke.CAP_SQUARE, bs.getEndCap());
070: assertEquals(BasicStroke.JOIN_MITER, bs.getLineJoin());
071: assertEquals(10.0f, bs.getMiterLimit(), 0.0f);
072: assertNull(bs.getDashArray());
073: assertEquals(0.0f, bs.getDashPhase(), 0.0f);
074: }
075:
076: public final void testGetLineWidth() {
077: BasicStroke bs = new BasicStroke(10.0f);
078: assertEquals(10.0f, bs.getLineWidth(), 0.0f);
079: }
080:
081: public final void testGetEndCap() {
082: BasicStroke bs = new BasicStroke(10.0f, BasicStroke.CAP_ROUND,
083: BasicStroke.JOIN_ROUND);
084: assertEquals(BasicStroke.CAP_ROUND, bs.getEndCap());
085: }
086:
087: public final void testGetLineJoin() {
088: BasicStroke bs = new BasicStroke(10.0f, BasicStroke.CAP_ROUND,
089: BasicStroke.JOIN_ROUND);
090: assertEquals(BasicStroke.JOIN_ROUND, bs.getLineJoin());
091: }
092:
093: public final void testGetMiterLimit() {
094: BasicStroke bs = new BasicStroke(10.0f, BasicStroke.CAP_ROUND,
095: BasicStroke.JOIN_ROUND, 20.0f);
096: assertEquals(20.0f, bs.getMiterLimit(), 0.0f);
097: }
098:
099: public final void testGetDashArray() {
100: float dash[] = new float[] { 10.0f, 20.f, 30.f };
101: BasicStroke bs = new BasicStroke(10.0f, BasicStroke.CAP_ROUND,
102: BasicStroke.JOIN_ROUND, 20.0f, dash, 0.0f);
103: assertTrue(Arrays.equals(bs.getDashArray(), dash));
104: }
105:
106: public final void testGetDashPhase() {
107: float dash[] = new float[] { 10.0f, 20.f, 30.f };
108: BasicStroke bs = new BasicStroke(10.0f, BasicStroke.CAP_ROUND,
109: BasicStroke.JOIN_ROUND, 20.0f, dash, 5.0f);
110: assertEquals(5.0f, bs.getDashPhase(), 0.0f);
111: }
112:
113: BasicStroke createSampleStroke() {
114: return new BasicStroke(10.0f, BasicStroke.CAP_ROUND,
115: BasicStroke.JOIN_ROUND, 20.0f, new float[] { 10.0f,
116: 20.f, 30.f }, 5.0f);
117: }
118:
119: BasicStroke[] createStrokeArray() {
120: float dash[] = createSampleStroke().getDashArray();
121: float dash2[] = new float[] { 10.0f, 15.f, 30.f };
122: return new BasicStroke[] {
123: new BasicStroke(9.0f, BasicStroke.CAP_ROUND,
124: BasicStroke.JOIN_ROUND, 20.0f, dash, 5.0f),
125: new BasicStroke(10.0f, BasicStroke.CAP_BUTT,
126: BasicStroke.JOIN_ROUND, 20.0f, dash, 5.0f),
127: new BasicStroke(10.0f, BasicStroke.CAP_ROUND,
128: BasicStroke.JOIN_BEVEL, 20.0f, dash, 5.0f),
129: new BasicStroke(10.0f, BasicStroke.CAP_ROUND,
130: BasicStroke.JOIN_ROUND, 30.0f, dash, 5.0f),
131: new BasicStroke(10.0f, BasicStroke.CAP_ROUND,
132: BasicStroke.JOIN_ROUND, 20.0f, dash2, 5.0f),
133: new BasicStroke(10.0f, BasicStroke.CAP_ROUND,
134: BasicStroke.JOIN_ROUND, 20.0f, dash, 15.0f) };
135: }
136:
137: public final void testEquals() {
138: BasicStroke bs = createSampleStroke();
139: assertTrue("Object isn't equal itself", bs.equals(bs));
140: BasicStroke bsa[] = createStrokeArray();
141: for (BasicStroke element : bsa) {
142: assertTrue("Different objects are equal", !bs
143: .equals(element));
144: }
145: }
146:
147: public final void testHashCode() {
148: BasicStroke bs = createSampleStroke();
149: assertTrue("Hash code isn't equal for the same object", bs
150: .hashCode() == bs.hashCode());
151: BasicStroke bsa[] = createStrokeArray();
152: for (BasicStroke element : bsa) {
153: assertTrue("Different objects have the same hash code", bs
154: .hashCode() != element.hashCode());
155: }
156: }
157:
158: public void testCreateStrokedShape() {
159: File path = new File(shapePath);
160: String test[] = path.list();
161:
162: if (test == null) {
163: fail("Golden files folder is empty "
164: + path.getAbsolutePath());
165: } else {
166: System.out.println("Golden files folder "
167: + path.getAbsolutePath());
168: }
169:
170: for (String element : test) {
171: if (element.indexOf("_d") != -1
172: && element.indexOf("#JAVA") == -1) {
173: check(path.getAbsolutePath() + File.separator + element);
174: }
175: }
176: }
177:
178: void check(String fileName) {
179: int a = fileName.indexOf("_d");
180: int b = fileName.indexOf(".shape");
181: String strokeDesc = fileName.substring(a + 2, b);
182: String srcName = fileName.substring(0, a)
183: + fileName.substring(b);
184: // System.out.println(SHAPE_PATH + Tools.File.extractFileName(fileName));
185:
186: BasicStroke bs = createStroke(strokeDesc);
187: Shape src = Tools.Shape.load(srcName);
188: Shape dstExpected = Tools.Shape.load(fileName);
189:
190: Shape dstActual = bs.createStrokedShape(src);
191:
192: String srcFile = Tools.File.extractFileName(srcName);
193: String testFile = Tools.File.extractFileName(fileName);
194:
195: if (OUTPUT) {
196: Tools.Shape.save(dstActual, outputPath
197: + Tools.File.changeExt(testFile, " #ACTUAL.shape"));
198: createImage(src, outputPath
199: + Tools.File.changeExt(srcFile, " #SOURCE.jpeg"),
200: "SOURCE");
201: createImage(dstActual, outputPath
202: + Tools.File.changeExt(testFile, " #ACTUAL.jpeg"),
203: "ACTUAL");
204: createImage(dstExpected,
205: outputPath
206: + Tools.File.changeExt(testFile,
207: " #EXPECTED.jpeg"), "EXPECTED");
208: }
209:
210: assertTrue("Non equal shape " + fileName, Tools.PathIterator
211: .equals(dstExpected.getPathIterator(null), dstActual
212: .getPathIterator(null), SHAPE_DELTA));
213: }
214:
215: BasicStroke createStroke(String text) {
216: BasicStroke bs = new BasicStroke();
217: float width = bs.getLineWidth();
218: int cap = bs.getEndCap();
219: int join = bs.getLineJoin();
220: float miter = bs.getMiterLimit();
221: float dash[] = bs.getDashArray();
222: float dashPhase = bs.getDashPhase();
223:
224: int dashCount = 0;
225: int state = 0;
226: // 0 - width
227: // 1 - cap
228: // 2 - join
229: // 3 - miter
230: // 4 - dash
231: // 5 - phase
232: // 6 - completed
233:
234: try {
235: StreamTokenizer t = new StreamTokenizer(new StringReader(
236: text));
237: t.ordinaryChar('R');
238: t.ordinaryChar('S');
239: t.ordinaryChar('B');
240: t.ordinaryChar('M');
241: while (t.nextToken() != StreamTokenizer.TT_EOF) {
242: switch (t.ttype) {
243: case StreamTokenizer.TT_NUMBER:
244: switch (state) {
245: case 0: // width
246: width = (float) t.nval;
247: state++;
248: break;
249: case 3:
250: miter = (float) t.nval;
251: state++;
252: break;
253: case 4: // dash
254: dash[dashCount++] = (float) t.nval;
255: break;
256: case 5: // phase
257: dashPhase = (float) t.nval;
258: state++;
259: break;
260: }
261: break;
262: case '(':
263: dash = new float[10];
264: break;
265: case ')':
266: float tmp[] = new float[dashCount];
267: System.arraycopy(dash, 0, tmp, 0, dashCount);
268: dash = tmp;
269: state++;
270: break;
271: default:
272: switch (state) {
273: case 1: // cap
274: switch (t.ttype) {
275: case 'B':
276: cap = BasicStroke.CAP_BUTT;
277: break;
278: case 'R':
279: cap = BasicStroke.CAP_ROUND;
280: break;
281: case 'S':
282: cap = BasicStroke.CAP_SQUARE;
283: break;
284: }
285: state++;
286: break;
287: case 2: // join
288: switch (t.ttype) {
289: case 'B':
290: join = BasicStroke.JOIN_BEVEL;
291: state += 2;
292: break;
293: case 'M':
294: join = BasicStroke.JOIN_MITER;
295: state++;
296: break;
297: case 'R':
298: join = BasicStroke.JOIN_ROUND;
299: state += 2;
300: break;
301: }
302: break;
303: }
304: break;
305: }
306: }
307: } catch (IOException e) {
308: fail("Can't extract stroke description " + text);
309: }
310:
311: return new BasicStroke(width, cap, join, miter, dash, dashPhase);
312: }
313:
314: void createImage(Shape s, String fileName, String caption) {
315: BufferedImage img = Tools.Shape.createImage(s, null,
316: Color.gray, Color.lightGray);
317: Graphics g = img.getGraphics();
318: g.setColor(Color.green);
319: g.drawString(caption, 10, 20);
320: Tools.BufferedImage.save(img, fileName);
321: }
322:
323: public static void main(String[] args) {
324: junit.textui.TestRunner.run(BasicStrokeTest.class);
325: }
326:
327: }
|