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.assertEquals;
18  import static org.junit.Assert.assertTrue;
19  
20  import java.text.MessageFormat;
21  
22  import org.apache.commons.httpclient.HttpStatus;
23  
24  /**
25   * Helper class to run tag tests
26   *
27   * @author James Wiltshire
28   * @version $Id$
29   */
30  
31  public class JsonTestRunner
32  {
33    private String testType;
34    private int testNum;
35    
36    public JsonTestRunner(String pTestType, int pTestNum)
37    {
38      this.testType=pTestType;
39      this.testNum=pTestNum;
40    }
41    
42    public void runTest() throws Exception
43    {
44      try {
45        String msg;
46        
47        msg=MessageFormat.format("Running test {0}/{1}... ",testType,testNum);
48        System.out.println(msg);
49        
50        ResponseData data = Helper.getData(testType,testNum);
51        
52        msg=MessageFormat.format("{0}/{1} - Status {2} is expected.",
53            testType,testNum,data.expectedStatusCode);
54        
55        assertEquals(msg,data.expectedStatusCode,data.statusCode);
56        
57        if (data.statusCode==HttpStatus.SC_OK){
58          // Compare JSON objects
59          msg=MessageFormat.format("{0}/{1} - JSON Objects should match.",testType,testNum); 
60          System.out.print(msg);
61          assertEquals(msg,data.expectedJson, data.json);
62        }
63        else {  
64          String expectedMsg=Messages.getString(data.expectedMsgKey);
65          // If the expected Msg has a substitued value, just check the returned string
66          // up to that point - we can't know what value will ctually be substituted
67          if (expectedMsg.contains("{0}")){
68            expectedMsg=expectedMsg.substring(0,expectedMsg.indexOf("{0}"));
69          }
70          msg=MessageFormat.format("{0}/{1} - Exception should contain key {2} - \"{3}\"",
71              testType,testNum,data.expectedMsgKey,expectedMsg); 
72          System.out.print(msg);
73          assertTrue(msg,data.body.contains(expectedMsg));
74        }
75        System.out.println(" OK");
76      }
77      catch (Exception e){
78        String msg=MessageFormat.format("{0}/{1} - Exception occurred during test.",
79            testType,testNum);
80        System.out.println(msg);
81        System.out.println(e);
82        e.printStackTrace(System.out);
83        throw new Exception (msg,e);
84      }
85    }
86  
87  }
88