1 | /* |
2 | * Created on Sep 12, 2004 |
3 | * |
4 | * TODO To change the template for this generated file go to |
5 | * Window - Preferences - Java - Code Style - Code Templates |
6 | */ |
7 | package biz.xsoftware.buildtemplate.test; |
8 | |
9 | import java.io.File; |
10 | import java.io.FileInputStream; |
11 | import java.io.FileOutputStream; |
12 | import java.io.IOException; |
13 | import java.net.URL; |
14 | import java.util.logging.Level; |
15 | import java.util.logging.Logger; |
16 | |
17 | import junit.framework.TestCase; |
18 | import junit.framework.TestSuite; |
19 | import biz.xsoftware.buildtemplate.AntSocketLogger; |
20 | import biz.xsoftware.buildtemplate.Exit; |
21 | import biz.xsoftware.buildtemplate.FileLocator; |
22 | import biz.xsoftware.buildtemplate.Main; |
23 | import biz.xsoftware.buildtemplate.Version; |
24 | import biz.xsoftware.mock.CalledMethod; |
25 | import biz.xsoftware.mock.MockObject; |
26 | import biz.xsoftware.mock.MockObjectFactory; |
27 | |
28 | /** |
29 | * @author Dean Hiller |
30 | * |
31 | * TODO To change the template for this generated type comment go to |
32 | * Window - Preferences - Java - Code Style - Code Templates |
33 | */ |
34 | public class TestBasicInstall extends TestCase { |
35 | |
36 | private final static Logger log = Logger.getLogger(TestBasicInstall.class.getName()); |
37 | public final static String EXIT_MTHD = "exit"; |
38 | public final static String GETFILE = "getFile"; |
39 | public final static String GETVERSION = "getVersion"; |
40 | |
41 | public final static String f = File.separator; |
42 | public final static String OUTPUT = "output"; |
43 | public final static File JAR = new File(OUTPUT+f+"jardist"+f+"buildtemplate.jar"); |
44 | public final static File TESTDIR = new File(OUTPUT+f+"tests"); |
45 | |
46 | private MockObject mock; |
47 | //the area used for each test...different for each test. |
48 | private String area; |
49 | private String testName; |
50 | private String realUserDir; |
51 | |
52 | /** |
53 | * @param arg0 |
54 | */ |
55 | public TestBasicInstall(String name) { |
56 | super(name); |
57 | this.testName = name; |
58 | } |
59 | |
60 | public void setUp() throws Exception { |
61 | assertTrue("Run ./build.sh first so jar is built so we can use in testing", JAR.exists()); |
62 | Main.setTesting(); |
63 | |
64 | TESTDIR.mkdirs(); |
65 | mock = MockObjectFactory.createMock(new Class[] {Version.class, Exit.class, FileLocator.class}); |
66 | |
67 | area = Util.getAvailableFileName(TESTDIR, testName); |
68 | realUserDir = System.getProperty("user.dir"); |
69 | System.setProperty("user.dir", area); |
70 | log.info("*****"+testName+"******area="+area); |
71 | } |
72 | |
73 | public void tearDown() { |
74 | //erase the tests directory!!! |
75 | boolean ableToDelete = Util.removeDir(TESTDIR); |
76 | if(!ableToDelete) |
77 | throw new RuntimeException("Someone is forgetting to close an file, so we couldn't delete the tests directory. Fix the code to close all files"); |
78 | |
79 | mock.expectCall(MockObject.NONE); |
80 | System.setProperty("user.dir", realUserDir); |
81 | log.info("realUserDir="+realUserDir); |
82 | } |
83 | |
84 | public void testBasicMisuse() throws Throwable { |
85 | Main m = new Main(); |
86 | m.setExit((Exit)mock); |
87 | m.start(new String[] {}); |
88 | |
89 | Integer code = (Integer)mock.expectCall(EXIT_MTHD).getParameter(0); |
90 | assertEquals("Code should be equal", Main.USAGE_INCORRECT, code.intValue()); |
91 | } |
92 | |
93 | public void testVersionNotWorkOnMain() throws Exception { |
94 | Main m = new Main(); |
95 | m.setExit((Exit)mock); |
96 | m.start(new String[] {"-version"}); |
97 | Integer code = (Integer)mock.expectCall(EXIT_MTHD).getParameter(0); |
98 | assertEquals("Code should be equal", Main.USAGE_INCORRECT, code.intValue()); |
99 | } |
100 | |
101 | /** |
102 | * To run this test in eclipse, you must have run ./build first!!! |
103 | * @throws Exception |
104 | */ |
105 | public void testBasicInstall() throws Exception { |
106 | log.info("******ntestBasicInstall***********area="+area); |
107 | |
108 | mock.addReturnValue(GETFILE, JAR); |
109 | mock.addReturnValue(GETVERSION, "r1-0-0"); //pretend it is release r1-0-0 |
110 | |
111 | Main m = new Main(); |
112 | m.setVersion((Version)mock); |
113 | m.setExit((Exit)mock); |
114 | m.setFileLocator((FileLocator)mock); |
115 | m.start(new String[] {"-directory", area}); |
116 | m.shutdown(); |
117 | |
118 | String[] methods = new String[] { GETFILE, GETVERSION }; |
119 | CalledMethod[] params = mock.expectUnorderedCalls(methods); |
120 | |
121 | //check over some of the permanent files that are supposed to exist |
122 | File build = new File(area, "build"); |
123 | File buildbat = new File(area, "build.bat"); |
124 | File distxml = new File(area, "bldfiles"+f+"createdist.xml"); |
125 | assertTrue("file must exist", build.exists()); |
126 | assertTrue("file must exist", buildbat.exists()); |
127 | assertTrue("file must exist", distxml.exists()); |
128 | |
129 | //check over some of the files that are not supposed to exist |
130 | File antDir = new File(area, "tools"+f+"ant"); |
131 | File emmaDir = new File(area, "tools"+f+"emma"); |
132 | assertTrue("file must not exist", !antDir.exists()); |
133 | assertTrue("file must not exist", !emmaDir.exists()); |
134 | |
135 | //check over some user files that are supposed to exist |
136 | File antProps = new File(area, "bldfiles"+f+"ant.properties"); |
137 | assertTrue("file must exist", antProps.exists()); |
138 | } |
139 | |
140 | /** |
141 | * Test when we upgrade buildtemplate.jar, 'build.bat' is not written |
142 | * to disk again. Also test modifying 'build' file in between builds |
143 | * and the second build should not write 'build' to disk either. |
144 | * |
145 | * @throws Exception |
146 | */ |
147 | public void testUpgradeWithNoChanges() throws Exception { |
148 | log.info("*******testUpgradeWithNoChanges**********area="+area); |
149 | |
150 | mock.addReturnValue(GETFILE, JAR); |
151 | mock.addReturnValue(GETVERSION, "r1-0-0"); //pretend it is release r1-0-0 |
152 | |
153 | Main m = new Main(); |
154 | m.setVersion((Version)mock); |
155 | m.setExit((Exit)mock); |
156 | m.setFileLocator((FileLocator)mock); |
157 | m.start(new String[] {"-directory", area}); |
158 | m.shutdown(); |
159 | String[] methods = new String[] { GETVERSION, GETFILE }; |
160 | CalledMethod[] params = mock.expectUnorderedCalls(methods); |
161 | |
162 | File build = new File(area, "build"); |
163 | File buildbat = new File(area, "build.bat"); |
164 | long buildMod1 = build.lastModified(); |
165 | long buildbatMod1 = buildbat.lastModified(); |
166 | |
167 | //modify 'build' file to be the same as it was before...this is just to change |
168 | //the timestamp and see if the buildtemplate is smart enough not to write it |
169 | //to disk again so it plays nicely with version control systems. |
170 | modifyFilesTimeStamp(build); |
171 | long buildMod2 = build.lastModified(); |
172 | assertTrue("buildModifications should be different now", buildMod1 != buildMod2); |
173 | |
174 | mock.addReturnValue(GETVERSION, "r1-0-1"); //pretend it is release r1-0-0 |
175 | //NOTE: must run dist target otherwise could get caught in an infinite loop |
176 | //where ant is forked and testall is run again which leads to running this |
177 | //test case again forking ant again, etc. |
178 | m.start(new String[] {"-installed", "clean"}); |
179 | m.shutdown(); |
180 | CalledMethod method = mock.expectCall(GETVERSION); |
181 | |
182 | long buildMod3 = build.lastModified(); |
183 | assertEquals("build modification should be the same as 2nd modification time", buildMod2, buildMod3); |
184 | long buildbatMod2 = buildbat.lastModified(); |
185 | assertEquals("build.bat should not have been modified", buildbatMod1, buildbatMod2); |
186 | } |
187 | |
188 | private void modifyFilesTimeStamp(File build) throws IOException { |
189 | FileInputStream in = new FileInputStream(build); |
190 | int fileSize = (int)build.length(); |
191 | byte[] buffer = new byte[fileSize]; |
192 | int bytesRead = 0; |
193 | while(bytesRead >= 0) { |
194 | bytesRead = in.read(buffer, bytesRead, fileSize-bytesRead); |
195 | } |
196 | in.close(); |
197 | FileOutputStream out = new FileOutputStream(build); |
198 | out.write(buffer); |
199 | out.close(); |
200 | } |
201 | |
202 | /** |
203 | * Tests the parsing of exceptions on LogFormatter.java |
204 | * |
205 | * @throws Exception |
206 | */ |
207 | public void testExceptionToLogFormatter() throws Exception { |
208 | Main m = new Main(); |
209 | m.setExit((Exit)mock); |
210 | m.start(new String[] {}); //sets the formatter on everyone!!!!! |
211 | //turn on highest level otherwise it is filtered and the code is not |
212 | //covered/tested...see coverage results of LogFormatter.java... |
213 | log.log(Level.SEVERE, "test formatting on custom formatter", new Throwable().fillInStackTrace()); |
214 | mock.expectCall(EXIT_MTHD); |
215 | } |
216 | |
217 | public void testGetFileMethod() throws Exception { |
218 | System.setProperty("user.dir", realUserDir); |
219 | |
220 | File f = new File("tools"+File.separator+"buildtemplate.jar"); |
221 | assertTrue("file should exist before we test this", f.exists()); |
222 | URL url = f.toURL(); |
223 | URL urlToClassFile = new URL("file", null, -1, url+"!/biz/xsoftware/buildtemplate/Util.class"); |
224 | File tmp = new biz.xsoftware.buildtemplate.Util().getFile(urlToClassFile); |
225 | |
226 | assertNotNull("Should return a real file", tmp); |
227 | assertTrue("file should still exist", tmp.exists()); |
228 | } |
229 | |
230 | public void testDummyRunClass() throws Exception { |
231 | //install template then run dummy class on it.... |
232 | mock.addReturnValue(GETFILE, JAR); |
233 | mock.addReturnValue(GETVERSION, "r1-0-0"); //pretend it is release r1-0-0 |
234 | Main m = new Main(); |
235 | m.setVersion((Version)mock); |
236 | m.setExit((Exit)mock); |
237 | m.setFileLocator((FileLocator)mock); |
238 | m.start(new String[] {"-directory", area}); |
239 | m.shutdown(); |
240 | String[] methods = new String[] { GETVERSION, GETFILE }; |
241 | CalledMethod[] params = mock.expectUnorderedCalls(methods); |
242 | |
243 | DummyRun.main(new String[] {"testing"}); |
244 | |
245 | //gain some code coverage for free... |
246 | new DummyRun(); |
247 | new Util(); |
248 | } |
249 | |
250 | /** |
251 | * test not finished.... |
252 | */ |
253 | public void testAntSocketLogger() throws Exception { |
254 | AntSocketLogger logger = new AntSocketLogger(); |
255 | } |
256 | |
257 | public static void main(String[] args) { |
258 | TestSuite suite = new TestSuite(); |
259 | suite.addTest(new TestBasicInstall("testBasicMisuse")); |
260 | |
261 | junit.textui.TestRunner.run(suite); |
262 | System.exit(0); |
263 | } |
264 | } |