1   /**
2    * Copyright 2007 Art Technology Group, Inc (ATG)
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    *
7    * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and limitations under the License.
13   */
14  
15  package atg.taglib.json;
16  
17  import static org.junit.Assert.assertTrue;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.io.PrintStream;
21  
22  import org.junit.Test;
23  import org.mortbay.jetty.Server;
24  import org.mortbay.jetty.webapp.WebAppContext;
25  
26  
27  /**
28   * Main test class for running all tag tests. 
29   *
30   * @author James Wiltshire
31   * @version $Id$
32   */
33  
34  public class JsonTagsTest
35  {
36    private Server mContainer;
37    private PrintStream mSystemError;
38    
39    
40    /**
41     * Run tests for each type of tag. This method will invoke the JSP in a Jetty container
42     * for each test, and compare the results to the expected results.
43     *
44     * @throws Exception
45     */
46    @Test
47    public void testTags() throws Exception
48    { 
49      try{
50        startContainer();
51        runTests("property",5);
52        runTests("object",6);
53        runTests("array",15); 
54        runTests("misc",7);
55        runPrettyPrintingTests(11);
56      }
57      catch (Exception e){
58        // Just rethrow - will be handled by test container
59        System.out.println(e);
60        e.printStackTrace(System.out);
61        throw e;
62      }
63      finally{
64        stopContainer();
65      }
66    }
67    
68    private void runTests(String pType, int pNumTests) throws Exception{
69      for (int i=1; i<=pNumTests; i++){
70        JsonTestRunner runner = new JsonTestRunner(pType,i);
71        runner.runTest();    
72      }
73    }
74    
75    private void runPrettyPrintingTests(int pNumTests) throws Exception
76    {
77      for (int i=1; i<=pNumTests; i++){     
78        ResponseData data = Helper.getData("prettyPrint",i);
79        // Tests 1-3 should contain no spaces, 4-5 should contain spaces,
80        // 6,7,8 should NOT contain spaces, 9-10 should contain spaces
81        boolean spacesExpected=(i==4||i==5||i==9||i==10);
82        boolean result=(data.body.contains(" ")==spacesExpected);
83        String msg="prettyPrint/"+i+" - should "+(spacesExpected?"":"NOT ")+"contain spaces";
84        System.out.print(msg);
85        assertTrue(msg, result );
86        System.out.println(" ...OK");
87      }
88    }
89    
90    public void startContainer() throws Exception
91    {
92      String webappDir=System.getProperty("webappDir");
93      String suppressContainerErrors=System.getProperty("suppressContainerErrors");
94      int containerPort=Integer.parseInt(System.getProperty("containerPort"));
95      
96      System.out.println("Starting Container - using webapp dir ["+webappDir+"]");
97      mContainer = new Server(containerPort);
98      WebAppContext context = new WebAppContext(webappDir,"/json-taglib");
99      mContainer.setHandler(context);
100     mContainer.start();
101     
102     // Swallow all the errors that will be pumped out by the container. These are
103     // expected by the tests
104     mSystemError = System.err; // Hold on to the original value
105     if (suppressContainerErrors!=null && 
106         (suppressContainerErrors.equals("true")||suppressContainerErrors.equals("yes"))){
107       System.setErr(new PrintStream(new ByteArrayOutputStream()));
108     }
109   }
110   
111   public void stopContainer() throws Exception
112   {
113     System.out.println("Stopping container");
114     //  restore sys err
115     System.setErr(mSystemError);
116     mContainer.stop();
117     // Give container a moment to close sockets before exiting (prevents occassional SocketAccept
118     // interrupted exception in build log)
119     Thread.sleep(500);
120   }
121 
122 }
123