| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 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 | * JUnit test for the Ant task |
|---|
| 44 | * {@link org.jcoderz.commons.taskdefs.LogMessageGenerator}. |
|---|
| 45 | * |
|---|
| 46 | * @author Michael Griffel |
|---|
| 47 | */ |
|---|
| 48 | 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 | super.setUp(); |
|---|
| 59 | mDestDir = mkdir("build/test"); |
|---|
| 60 | final File in = new File(getBaseDir(), "test/xml/log-message-info.xml"); |
|---|
| 61 | final File out = new File(mDestDir, "log-message-info.out"); |
|---|
| 62 | final LogMessageGenerator g = new LogMessageGenerator(); |
|---|
| 63 | g.setApplication("FawkeZ"); |
|---|
| 64 | g.setDestdir(mDestDir); |
|---|
| 65 | g.setForce(true); |
|---|
| 66 | g.setIn(in); |
|---|
| 67 | g.setOut(out); |
|---|
| 68 | g.setTaskName("test-message-generator"); |
|---|
| 69 | g.setFailonerror(true); |
|---|
| 70 | g.setLocation(new Location("location")); |
|---|
| 71 | |
|---|
| 72 | final Project project = new Project(); |
|---|
| 73 | project.setBaseDir(getBaseDir()); |
|---|
| 74 | project.setName("JUnit test"); |
|---|
| 75 | g.setProject(project); |
|---|
| 76 | mGenerator = g; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | /** Tests the Ant task. */ |
|---|
| 80 | public void testExecute () |
|---|
| 81 | { |
|---|
| 82 | mGenerator.execute(); |
|---|
| 83 | |
|---|
| 84 | final File testFile = new File(mDestDir, |
|---|
| 85 | "org/jcoderz/commons/TstLogMessage.java"); |
|---|
| 86 | assertTrue("Generated Java File " + testFile + " exists", |
|---|
| 87 | testFile.exists()); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /** Tests the Ant task. */ |
|---|
| 91 | public void testExecuteBadInFile () |
|---|
| 92 | { |
|---|
| 93 | mGenerator.setIn(null); |
|---|
| 94 | executeAndExpectBuildException("missing input file."); |
|---|
| 95 | mGenerator.setIn(new File("foo")); |
|---|
| 96 | executeAndExpectBuildException("non existing input file."); |
|---|
| 97 | mGenerator.setIn(new File(getBaseDir(), "build.xml")); |
|---|
| 98 | executeAndExpectBuildException("bogus input file."); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | /** Tests the Ant task. */ |
|---|
| 102 | public void testExecuteBadDestDir () |
|---|
| 103 | { |
|---|
| 104 | mGenerator.setDestdir(null); |
|---|
| 105 | executeAndExpectBuildException("missing destination directory."); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | /** Tests the Ant task. */ |
|---|
| 109 | public void testExecuteBadOutFile () |
|---|
| 110 | { |
|---|
| 111 | mGenerator.setOut(null); |
|---|
| 112 | executeAndExpectBuildException("missing out file."); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /** Tests the Ant task. */ |
|---|
| 116 | public void testExecuteBadApplicationName () |
|---|
| 117 | { |
|---|
| 118 | mGenerator.setApplication(null); |
|---|
| 119 | executeAndExpectBuildException("missing out file."); |
|---|
| 120 | mGenerator.setApplication("bogus"); |
|---|
| 121 | executeAndExpectBuildException("non-existing application name."); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | /** Tests the Ant task. */ |
|---|
| 125 | public void testExecuteBadXslFile () |
|---|
| 126 | { |
|---|
| 127 | mGenerator.setXsl("bogus.xsl"); // -> use default stylesheet |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | /** |
|---|
| 131 | * @param g |
|---|
| 132 | */ |
|---|
| 133 | private void executeAndExpectBuildException (String msg) |
|---|
| 134 | { |
|---|
| 135 | try |
|---|
| 136 | { |
|---|
| 137 | mGenerator.execute(); |
|---|
| 138 | fail("Expected BuildException for " + msg); |
|---|
| 139 | } |
|---|
| 140 | catch (BuildException expected) |
|---|
| 141 | { |
|---|
| 142 | // expected |
|---|
| 143 | System.err.println("" + expected); |
|---|
| 144 | expected.printStackTrace(); |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | static File mkdir (String relativePath) |
|---|
| 149 | { |
|---|
| 150 | final File dir = new File(getBaseDir(), relativePath); |
|---|
| 151 | if (!dir.exists()) |
|---|
| 152 | { |
|---|
| 153 | if (!dir.mkdirs()) |
|---|
| 154 | { |
|---|
| 155 | fail("Cannot create destination directory " + dir); |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | return dir; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | } |
|---|