1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package net.sourceforge.acelogger.test;
16
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.FileWriter;
20
21 import net.sourceforge.acelogger.LogEvent;
22 import net.sourceforge.acelogger.level.LogLevel;
23 import net.sourceforge.acelogger.location.LogEventLocation;
24 import net.sourceforge.acelogger.location.resolver.InternalCodeFrameResolver;
25
26 import com.thoughtworks.xstream.XStream;
27
28
29
30
31
32
33
34
35 public final class DataProviderHelper {
36
37 private static final String BASE_NAME = "src" + File.separator + "test" + File.separator
38 + "resources";
39
40 private static final File BASE_FILE = new File(BASE_NAME);
41
42 private DataProviderHelper() {
43 }
44
45 private static void doAlias(XStream stream) {
46 stream.alias("level", LogLevel.class);
47 stream.alias("string", String.class);
48 stream.alias("call", LogEvent.class);
49 stream.alias("location", LogEventLocation.class);
50 stream.alias("throwable", Throwable.class);
51 }
52
53 private static String getFileName(String identifier) {
54 String packageName = "";
55 String className = "";
56 String methodName = "";
57 String fileName = "";
58 Integer packageEndIndex = identifier.lastIndexOf('.');
59 if (packageEndIndex > 0) {
60 packageName = identifier.substring(0, packageEndIndex);
61 } else {
62 packageEndIndex = 0;
63 }
64 Integer classEndIndex = identifier.lastIndexOf('#');
65 if (classEndIndex > 0) {
66 className = identifier.substring(packageEndIndex + 1, classEndIndex);
67 methodName = identifier.substring(classEndIndex + 1);
68 StringBuilder location = new StringBuilder();
69 location.append(packageName);
70 location.append(File.separator);
71 location.append(className);
72 location.append(File.separator);
73 location.append(methodName);
74 location.append(".xml");
75 fileName = location.toString();
76 }
77 return fileName;
78 }
79
80 public static void saveDataFile(String identifier, Object[][] data) {
81 String fileName = getFileName(identifier);
82 if (fileName.length() > 0) {
83 try {
84 File dataFile = new File(BASE_FILE, fileName);
85 if (!dataFile.exists()) {
86 if (!dataFile.getParentFile().exists()) {
87 dataFile.getParentFile().mkdirs();
88 }
89 dataFile.createNewFile();
90 }
91 XStream serializer = new XStream();
92 doAlias(serializer);
93 FileWriter writer = new FileWriter(dataFile);
94 serializer.toXML(data, writer);
95 writer.flush();
96 writer.close();
97 } catch (Exception e) {
98
99 e.printStackTrace();
100 }
101 }
102 }
103
104 public static Object[][] loadData() {
105 InternalCodeFrameResolver resolver = new InternalCodeFrameResolver(1);
106 StringBuilder identifier = new StringBuilder();
107 StackTraceElement frame = resolver.getCodeFrame();
108 identifier.append(frame.getClassName());
109 identifier.append('#');
110 identifier.append(frame.getMethodName());
111 Object[][] data = new Object[][] {};
112 String fileName = getFileName(identifier.toString());
113 if (fileName.length() > 0) {
114 try {
115 XStream reader = new XStream();
116 doAlias(reader);
117 File dataFile = new File(BASE_FILE, fileName);
118 FileInputStream stream = new FileInputStream(dataFile);
119 data = (Object[][]) reader.fromXML(stream);
120 stream.close();
121 } catch (Exception e) {
122
123 e.printStackTrace();
124 }
125 }
126 return data;
127 }
128
129 }