1 package org.kite9.framework.common;
2
3 import java.awt.image.BufferedImage;
4 import java.io.File;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.net.URL;
8 import java.util.SortedSet;
9 import java.util.TreeSet;
10
11 import javax.imageio.ImageIO;
12
13 import org.kite9.diagram.adl.Arrow;
14 import org.kite9.diagram.adl.Context;
15 import org.kite9.diagram.adl.Diagram;
16 import org.kite9.diagram.adl.Glyph;
17 import org.kite9.diagram.adl.Link;
18 import org.kite9.diagram.adl.TextLine;
19 import org.kite9.diagram.primitives.DiagramElement;
20 import org.kite9.diagram.primitives.IdentifiableDiagramElement;
21 import org.kite9.diagram.visitors.DiagramElementVisitor;
22 import org.kite9.diagram.visitors.VisitorAction;
23 import org.kite9.framework.logging.LogicException;
24 import org.kite9.framework.logging.Table;
25
26 /***
27 * Helps with comparing the results of tests
28 *
29 * @author robmoffat
30 *
31 */
32 public class TestingHelp {
33
34 private static final String TARGET_DIR = "target/functional-test/outputs";
35
36 protected static File prepareFileName(Class<?> theTest, String subtest, String item) {
37 String directory = getFullFileName(theTest, subtest);
38 File f = new File(TARGET_DIR);
39 f.mkdirs();
40 File f2 = new File(f, directory);
41 f2.mkdirs();
42
43 File f3 = new File(f2, item);
44 return f3;
45 }
46
47 public static void writeOutput(Class<?> theTest, String subtest, String item, String contents) {
48 File f = prepareFileName(theTest, subtest, item);
49 try {
50 FileWriter fw = new FileWriter(f);
51 fw.write(contents);
52 fw.close();
53 } catch (IOException e) {
54 throw new LogicException("Could not save output: " + f.toString(), e);
55 }
56 }
57
58 public static void renderToFile(Class<?> theTest, String subtest, String item, BufferedImage bi) {
59 File f = prepareFileName(theTest, subtest, item);
60 try {
61 ImageIO.write(bi, "PNG", f);
62 } catch (IOException e) {
63 throw new LogicException("Could not save output: " + f.toString(), e);
64 }
65 }
66
67
68 /***
69 * Produces a report containing all the elements of the diagram
70 */
71 public String getPositionalInformationADL(Diagram d) {
72 StringBuffer details = new StringBuffer();
73 Rowify r = getRegularRowify();
74
75 getPositions(d, details, Arrow.class, r);
76 getPositions(d, details, Context.class, r);
77 getPositions(d, details, Glyph.class, r);
78 getPositions(d, details, Diagram.class, r);
79 getPositions(d, details, Link.class, r);
80 getPositions(d, details, TextLine.class, r);
81 return details.toString();
82 }
83
84
85
86 interface Rowify {
87
88 String[] rowify(Object o);
89
90 String[] getHeaders();
91
92 }
93
94 private Rowify getRegularRowify() {
95 return new Rowify() {
96
97 public String[] rowify(Object o) {
98 String id = "";
99 if (o instanceof IdentifiableDiagramElement ) {
100 id = ((IdentifiableDiagramElement)o).getID();
101 }
102 DiagramElement de = (DiagramElement) o;
103
104 return new String[] { id, de.toString(), de.getRenderingInformation().toString() };
105 }
106
107 public String[] getHeaders() {
108 return new String[] { "ID", "ToString", "Rendering information" };
109 }
110 };
111 }
112
113 private <X extends DiagramElement> void getPositions(Diagram d, StringBuffer details, final Class<X> class1,
114 Rowify r) {
115 final SortedSet<X> items = new TreeSet<X>();
116 new DiagramElementVisitor().visit(d, new VisitorAction() {
117
118 @SuppressWarnings("unchecked")
119 public void visit(DiagramElement de) {
120 if (class1.isInstance(de)) {
121 items.add((X) de);
122 }
123 }
124 });
125
126 if (items.size() > 0) {
127 details.append("\n\n");
128 details.append(class1.getSimpleName());
129 details.append("\n-------------------------------------\n");
130
131 Table t = new Table();
132 t.addRow((Object[]) r.getHeaders());
133
134 for (X x : items) {
135 t.addRow((Object[]) r.rowify(x));
136 }
137
138 t.display(details);
139 }
140 }
141
142 public static File getHandleToFileInClasspath(Class<?> testClass, String name) {
143 String fullName = getFullFileName(testClass, name);
144 URL u = testClass.getClassLoader().getResource(fullName);
145 File file = new File(u.getFile());
146
147 return file;
148 }
149
150 public static String getFullFileName(Class<?> testClass, String name) {
151 String packageName = testClass.getPackage().getName().replace(".", "/");
152 String className = testClass.getSimpleName();
153 String directory = packageName + "/" + className;
154 directory = directory.toLowerCase();
155 if (name!=null) {
156 return directory + "/" + name;
157 } else {
158 return directory;
159 }
160 }
161 }