View Javadoc

1   package org.codehaus.mojo.javascript;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Iterator;
20  
21  import junit.framework.AssertionFailedError;
22  import junit.framework.Test;
23  import junit.framework.TestResult;
24  import junit.framework.TestSuite;
25  import net.jsunit.JsUnitServer;
26  import net.jsunit.StandaloneTest;
27  import net.jsunit.TestCaseResult;
28  import net.jsunit.TestSuiteResult;
29  
30  /**
31   * Simple wrapper arround StandaloneTest to setup the jetty http server from
32   * Maven.
33   * 
34   * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
35   */
36  public class JsUnitTestCase
37      extends StandaloneTest
38  {
39      /**
40       * @param name
41       */
42      public JsUnitTestCase( String name )
43      {
44          super( name );
45      }
46  
47      private static JsUnitServer sharedServer;
48  
49      /**
50       * @param server the server to set
51       */
52      public static void setSharedServer( JsUnitServer server )
53      {
54          JsUnitTestCase.sharedServer = server;
55      }
56  
57      /**
58       * {@inheritDoc}
59       * 
60       * @see net.jsunit.StandaloneTest#setUp()
61       */
62      public void setUp()
63          throws Exception
64      {
65          setServer( sharedServer );
66          super.setUp();
67      }
68  
69      /**
70       * Override the junit run method to gather browser test result an run a fake
71       * junit test suite that replay the browser results.
72       * 
73       * @author Arnaud Bailly.
74       * @see junit.framework.TestCase#run(junit.framework.TestResult)
75       */
76      public void run( TestResult result )
77      {
78          super.run( new TestResult() );
79          TestSuiteResult suiteResult = JsUnitTestCase.sharedServer.lastResult();
80          if (suiteResult == null )
81          {
82              return;
83          }
84          for ( Iterator it = suiteResult.getTestCaseResults().iterator(); it.hasNext(); )
85          {
86              final TestCaseResult testCaseResult = (TestCaseResult) it.next();
87              Test fake = new Test()
88              {
89  
90                  public void run( TestResult result )
91                  {
92                  }
93  
94                  public int countTestCases()
95                  {
96                      return 1;
97                  }
98  
99                  public String toString()
100                 {
101                     return testCaseResult.getName();
102                 }
103             };
104             result.startTest( fake );
105             if ( testCaseResult.hadError() )
106             {
107                 result.addError( fake, new Exception( testCaseResult.getError() ) );
108             }
109             if ( testCaseResult.hadFailure() )
110             {
111                 result.addFailure( fake, new AssertionFailedError( testCaseResult.getFailure() ) );
112             }
113             result.endTest( fake );
114         }
115     }
116 
117     private static TestSuite suite;
118 
119     public static Test suite()
120     {
121         return suite;
122     }
123 
124     /**
125      * @param suite the suite to set
126      */
127     public static void setSuite( TestSuite suite )
128     {
129         JsUnitTestCase.suite = suite;
130     }
131 
132 }