View Javadoc
1   package io.guixer.logs;
2   
3   import static com.google.common.base.Preconditions.checkNotNull;
4   import static net.avcompris.commons3.databeans.DataBeans.instantiate;
5   
6   import java.io.File;
7   import java.io.FileNotFoundException;
8   import java.io.IOException;
9   import java.util.Arrays;
10  import java.util.Comparator;
11  
12  import io.guixer.logs.GuixerOut.Run;
13  import io.guixer.logs.GuixerOut.TestClass;
14  import io.guixer.logs.GuixerOut.TestMethod;
15  
16  public class GuixerOutLoader {
17  
18  	private final GuixerOut guixerOut;
19  
20  	public GuixerOutLoader(
21  		final File guixerOutDir
22  	) throws IOException {
23  
24  		checkNotNull(guixerOutDir, "guixerOutDir");
25  
26  		if (!guixerOutDir.isDirectory()) {
27  
28  			throw new FileNotFoundException(
29  					"guixerOutDir should be a directory, but was: " + guixerOutDir.getCanonicalPath());
30  		}
31  
32  		final MutableGuixerOut guixerOut = instantiate(MutableGuixerOut.class) //
33  				.setDir(guixerOutDir);
34  
35  		this.guixerOut = guixerOut;
36  
37  		final File[] testClassDirs = sortFiles(guixerOutDir.listFiles(file -> file.isDirectory()));
38  
39  		for (final File testClassDir : testClassDirs) {
40  
41  			final String testClassSimpleName = testClassDir.getName();
42  
43  			final MutableTestClass testClass = instantiate(MutableTestClass.class) //
44  					.setSimpleName(testClassSimpleName);
45  
46  			guixerOut.addToTestClasses(testClass);
47  
48  			final File[] testMethodDirs = sortFiles(testClassDir.listFiles(file -> file.isDirectory()));
49  
50  			for (final File testMethodDir : testMethodDirs) {
51  
52  				final String testMethodName = testMethodDir.getName();
53  
54  				final MutableTestMethod testMethod = instantiate(MutableTestMethod.class) //
55  						.setName(testMethodName);
56  
57  				testClass.addToTestMethods(testMethod);
58  
59  				final File[] runDirs = sortFiles(testMethodDir.listFiles(file -> file.isDirectory()));
60  
61  				for (final File runDir : runDirs) {
62  
63  					final File testLogFile = new File(runDir, "test.log");
64  
65  					if (!testLogFile.isFile()) {
66  
67  						throw new FileNotFoundException("Cannot find test.log: " + testLogFile.getCanonicalPath());
68  					}
69  
70  					final String runDirName = runDir.getName();
71  
72  					final MutableRun run = instantiate(MutableRun.class) //
73  							.setDirName(runDirName);
74  
75  					testMethod.addToRuns(run);
76  
77  					final long timeMillis;
78  
79  					try {
80  
81  						timeMillis = Long.parseLong(runDirName);
82  
83  					} catch (final NumberFormatException e) {
84  
85  						throw new RuntimeException("Unable to parse timeMillis: " + runDirName);
86  					}
87  
88  					run.setTimeMillis(timeMillis);
89  
90  					run.setLog(new LogLoader(runDir).getLog());
91  				}
92  			}
93  		}
94  	}
95  
96  	public GuixerOut getGuixerOut() {
97  
98  		return guixerOut;
99  	}
100 
101 	private interface MutableGuixerOut extends GuixerOut {
102 
103 		MutableGuixerOut setDir(
104 			File guixerOutDir
105 		);
106 
107 		MutableGuixerOut addToTestClasses(
108 			TestClass testClass
109 		);
110 	}
111 
112 	private interface MutableTestClass extends TestClass {
113 
114 		MutableTestClass setSimpleName(
115 			String testClassSimpleName
116 		);
117 
118 		MutableTestClass addToTestMethods(
119 			TestMethod testMethod
120 		);
121 	}
122 
123 	private interface MutableTestMethod extends TestMethod {
124 
125 		MutableTestMethod setName(
126 			String testMethodName
127 		);
128 
129 		MutableTestMethod addToRuns(
130 			Run run
131 		);
132 	}
133 
134 	private interface MutableRun extends Run {
135 
136 		MutableRun setDirName(
137 			String dirName
138 		);
139 
140 		MutableRun setTimeMillis(
141 			long timeMillis
142 		);
143 
144 		MutableRun setLog(
145 			Log log
146 		);
147 	}
148 
149 	private static File[] sortFiles(
150 		final File[] files
151 	) {
152 
153 		Arrays.sort(files, new Comparator<File>() {
154 
155 			@Override
156 			public int compare(
157 				final File f1,
158 				final File f2
159 			) {
160 
161 				return f1.getName().compareTo(f2.getName());
162 			}
163 		});
164 
165 		return files;
166 	}
167 }