1   /*
2    * This file is part of AceLogger.
3    * 
4    * AceLogger is free software: you can redistribute it and/or modify it under the terms of the GNU
5    * Lesser General Public License as published by the Free Software Foundation, either version 3 of
6    * the License, or (at your option) any later version.
7    * 
8    * AceLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9    * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10   * Lesser General Public License for more details.
11   * 
12   * You should have received a copy of the GNU Lesser General Public License along with AceLogger.
13   * If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
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   * TODO: Create Doc.
30   * 
31   * @author Zardi (https://sourceforge.net/users/daniel_zardi)
32   * @version 1.0.0
33   * @since 1.0.0
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  				// TODO Auto-generated catch block
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 				// TODO Auto-generated catch block
123 				e.printStackTrace();
124 			}
125 		}
126 		return data;
127 	}
128 
129 }