| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.taskdefs; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import org.apache.tools.ant.BuildException; |
| 37 | | | import org.apache.tools.ant.Location; |
| 38 | | | import org.apache.tools.ant.Project; |
| 39 | | | import org.jcoderz.commons.TestCase; |
| 40 | | | |
| 41 | | | |
| 42 | | | |
| 43 | | | |
| 44 | | | {@link } |
| 45 | | | |
| 46 | | | @author |
| 47 | | | |
| 48 | 100 | | public class LogMessageGeneratorTest |
| 49 | | | extends TestCase |
| 50 | | | { |
| 51 | | | private LogMessageGenerator mGenerator; |
| 52 | | | private File mDestDir; |
| 53 | | | |
| 54 | | | {@inheritDoc} |
| 55 | | | protected void setUp () |
| 56 | | | throws Exception |
| 57 | | | { |
| 58 | 100 | | super.setUp(); |
| 59 | 100 | | mDestDir = mkdir("build/test"); |
| 60 | 100 | | final File in = new File(getBaseDir(), "test/xml/log-message-info.xml"); |
| 61 | 100 | | final File out = new File(mDestDir, "log-message-info.out"); |
| 62 | 100 | | final LogMessageGenerator g = new LogMessageGenerator(); |
| 63 | 100 | | g.setApplication("FawkeZ"); |
| 64 | 100 | | g.setDestdir(mDestDir); |
| 65 | 100 | | g.setForce(true); |
| 66 | 100 | | g.setIn(in); |
| 67 | 100 | | g.setOut(out); |
| 68 | 100 | | g.setTaskName("test-message-generator"); |
| 69 | 100 | | g.setFailonerror(true); |
| 70 | 100 | | g.setLocation(new Location("location")); |
| 71 | | | |
| 72 | 100 | | final Project project = new Project(); |
| 73 | 100 | | project.setBaseDir(getBaseDir()); |
| 74 | 100 | | project.setName("JUnit test"); |
| 75 | 100 | | g.setProject(project); |
| 76 | 100 | | mGenerator = g; |
| 77 | 100 | | } |
| 78 | | | |
| 79 | | | |
| 80 | | | public void testExecute () |
| 81 | | | { |
| 82 | 100 | | mGenerator.execute(); |
| 83 | | | |
| 84 | 100 | | final File testFile = new File(mDestDir, |
| 85 | | | "org/jcoderz/commons/TstLogMessage.java"); |
| 86 | 100 | | assertTrue("Generated Java File " + testFile + " exists", |
| 87 | | | testFile.exists()); |
| 88 | 100 | | } |
| 89 | | | |
| 90 | | | |
| 91 | | | public void testExecuteBadInFile () |
| 92 | | | { |
| 93 | 100 | | mGenerator.setIn(null); |
| 94 | 100 | | executeAndExpectBuildException("missing input file."); |
| 95 | 100 | | mGenerator.setIn(new File("foo")); |
| 96 | 100 | | executeAndExpectBuildException("non existing input file."); |
| 97 | 100 | | mGenerator.setIn(new File(getBaseDir(), "build.xml")); |
| 98 | 100 | | executeAndExpectBuildException("bogus input file."); |
| 99 | 100 | | } |
| 100 | | | |
| 101 | | | |
| 102 | | | public void testExecuteBadDestDir () |
| 103 | | | { |
| 104 | 100 | | mGenerator.setDestdir(null); |
| 105 | 100 | | executeAndExpectBuildException("missing destination directory."); |
| 106 | 100 | | } |
| 107 | | | |
| 108 | | | |
| 109 | | | public void testExecuteBadOutFile () |
| 110 | | | { |
| 111 | 100 | | mGenerator.setOut(null); |
| 112 | 100 | | executeAndExpectBuildException("missing out file."); |
| 113 | 100 | | } |
| 114 | | | |
| 115 | | | |
| 116 | | | public void testExecuteBadApplicationName () |
| 117 | | | { |
| 118 | 100 | | mGenerator.setApplication(null); |
| 119 | 100 | | executeAndExpectBuildException("missing out file."); |
| 120 | 100 | | mGenerator.setApplication("bogus"); |
| 121 | 100 | | executeAndExpectBuildException("non-existing application name."); |
| 122 | 100 | | } |
| 123 | | | |
| 124 | | | |
| 125 | | | public void testExecuteBadXslFile () |
| 126 | | | { |
| 127 | 100 | | mGenerator.setXsl("bogus.xsl"); |
| 128 | 100 | | } |
| 129 | | | |
| 130 | | | |
| 131 | | | @param |
| 132 | | | |
| 133 | | | private void executeAndExpectBuildException (String msg) |
| 134 | | | { |
| 135 | | | try |
| 136 | | | { |
| 137 | 0 | | mGenerator.execute(); |
| 138 | 0 | | fail("Expected BuildException for " + msg); |
| 139 | | | } |
| 140 | 100 | | catch (BuildException expected) |
| 141 | | | { |
| 142 | | | |
| 143 | 100 | (1)(2) | System.err.println("" + expected); |
| 144 | 100 | (3) | expected.printStackTrace(); |
| 145 | 0 | | } |
| 146 | 100 | | } |
| 147 | | | |
| 148 | | | static File mkdir (String relativePath) |
| 149 | | | { |
| 150 | 100 | | final File dir = new File(getBaseDir(), relativePath); |
| 151 | 100 | | if (!dir.exists()) |
| 152 | | | { |
| 153 | 100 | | if (!dir.mkdirs()) |
| 154 | | | { |
| 155 | 0 | | fail("Cannot create destination directory " + dir); |
| 156 | | | } |
| 157 | | | } |
| 158 | 100 | | return dir; |
| 159 | | | } |
| 160 | | | |
| 161 | | | } |