Debian Codegraph @

test/src/net/sf/antcontrib/BuildFileTestBase.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.antcontrib;
17
18 import java.io.File;
19 import java.io.PrintWriter;
20 import java.io.StringWriter;
21 import java.net.URL;
22
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.BuildFileTest;
25
26
27 /**
28 * More methods for BuildFileTest.
29 *
30 * @author Dale Anson
31 */
32 public abstract class BuildFileTestBase extends BuildFileTest {
33
34 /**
35 * Constructor for the BuildFileTestBase object
36 *
37 * @param name string to pass up to TestCase constructor
38 */
39 public BuildFileTestBase( String name ) {
40 super( name );
41 }
42
43 /**
44 * run a target, expect a build exception
45 *
46 * @param target target to run
47 */
48 protected void expectBuildException( String target ) {
49 expectSpecificBuildException( target, "no specific reason", null );
50 }
51
52 /**
53 * Assert that the given message has NOT been logged with a priority >= INFO
54 * when running the given target.
55 *
56 * @param target Description of the Parameter
57 * @param log Description of the Parameter
58 */
59 protected void expectLogNotContaining( String target, String log ) {
60 executeTarget( target );
61 String realLog = getLog();
62 assertTrue( "expecting log to NOT contain \"" + log + "\" log was \""
63 + realLog + "\"",
64 realLog.indexOf( log ) < 0 );
65 }
66
67 /**
68 * set up to run the named project
69 * <p>
70 * Overrides BuildFileTest.configureProject to first
71 * attempt to make a File out of the filename parameter, if the resulting
72 * file does not exists, then attempt to locate the file in the classpath.
73 * This way, test xml files can be placed alongside of their corresponding
74 * class file and can be easily found.
75 *
76 * @param filename name of project file to run
77 * @exception BuildException Description of the Exception
78 */
79 protected void configureProject( String filename ) throws BuildException {
80 // find the build file
81 File f = new File( filename );
82 if ( !f.exists() ) {
83 URL url = getClass().getClassLoader().getResource( filename );
84 if ( url == null )
85 throw new BuildException( "Can't find " + filename );
86 f = new File( url.getPath() );
87 if ( !f.exists() )
88 throw new BuildException( "Can't find " + filename );
89 }
90 super.configureProject(f.getAbsolutePath());
91 }
92
93 /**
94 * run a target, expect an exception string containing the substring we look
95 * for (case sensitive match)
96 *
97 * @param target target to run
98 * @param cause information string to reader of report
99 * @param contains substring of the build exception to look for
100 */
101 protected void expectBuildExceptionStackTraceContaining( String target, String cause, String contains ) {
102 try {
103 executeTarget( target );
104 }
105 catch ( org.apache.tools.ant.BuildException ex ) {
106 //buildException = ex; // buildException has private access in super
107 StringWriter stacktrace = new StringWriter();
108 PrintWriter writer = new PrintWriter( stacktrace, true );
109 ex.printStackTrace( writer );
110 String trace = stacktrace.toString();
111 if ( ( null != contains ) && ( trace.indexOf( contains ) == -1 ) ) {
112 fail( "Should throw BuildException because '" + cause + "' with message containing '" + contains + "' (actual message '" + trace + "' instead)" );
113 }
114 return;
115 }
116 fail( "Should throw BuildException because: " + cause );
117 }
118 }
119