2 * This document is a part of the source code and related artifacts
\r
3 * for CollectionSpace, an open source collections management system
\r
4 * for museums and related institutions:
\r
6 * http://www.collectionspace.org
\r
7 * http://wiki.collectionspace.org
\r
9 * Copyright (c) 2009 Regents of the University of California
\r
11 * Licensed under the Educational Community License (ECL), Version 2.0.
\r
12 * You may not use this file except in compliance with this License.
\r
14 * You may obtain a copy of the ECL 2.0 License at
\r
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
\r
17 * Unless required by applicable law or agreed to in writing, software
\r
18 * distributed under the License is distributed on an "AS IS" BASIS,
\r
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
20 * See the License for the specific language governing permissions and
\r
21 * limitations under the License.
\r
24 package org.collectionspace.services.IntegrationTests.xmlreplay;
\r
26 import org.collectionspace.services.common.api.Tools;
\r
27 import org.testng.Assert;
\r
29 import java.io.File;
\r
30 import java.util.ArrayList;
\r
31 import java.util.List;
\r
33 /** Subclass this test to programmatically control XmlReplay from a surefire test. See example in IntegrationTests :: XmlReplaySelfTest
\r
35 * $LastChangedRevision: $
\r
36 * $LastChangedDate: $
\r
38 public class XmlReplayTest {
\r
40 public static final String XMLREPLAY_REL_DIR_TO_MODULE = "/src/test/resources/test-data/xmlreplay";
\r
41 public static final String REPORTS_DIRNAME = "xml-replay-reports";
\r
42 public static final String XMLREPLAY_REL_DIR_REPORTS_TO_MODULE= "/target/"+REPORTS_DIRNAME;
\r
44 /** To use this method, you should have a test repository of xml files in the path
\r
45 * defined by XMLREPLAY_REL_DIR_TO_MODULE, relative to your pom.xml file, but normally
\r
46 * you would use the central repository of tests, which live in services/IntegrationTests,
\r
47 * and which you can use by calling createXmlReplay() which calls createXmlReplayUsingIntegrationTestsModule() for you.
\r
49 public static XmlReplay createXmlReplayForModule() throws Exception {
\r
50 String pwd = (new File(".")).getCanonicalPath();
\r
51 System.out.println("createXmlReplayForModule.pwd: "+pwd);
\r
52 XmlReplay replay = new XmlReplay(pwd+XMLREPLAY_REL_DIR_TO_MODULE,
\r
53 pwd+XMLREPLAY_REL_DIR_REPORTS_TO_MODULE);
\r
54 System.out.println("XmlReplay: "+replay);
\r
58 /** Use this method if your test xml files are stored in the central repository,
\r
59 * which is "services/IntegrationTests" + XMLREPLAY_REL_DIR_TO_MODULE
\r
61 public static XmlReplay createXmlReplay() throws Exception {
\r
62 return createXmlReplayUsingIntegrationTestsModule("../..");
\r
66 * @param relToServicesRoot is a Unix-like path from the calling module to the services root,
\r
67 * so if if you are in services/dimension/client/
\r
68 * then relToServicesRoot is "../.." which is how most of the client tests are set up, or if you
\r
69 * are setting up your test repository relative to the main service, e.g. you are in
\r
70 * services/dimension/, then relToServicesRoot is ".."
\r
72 public static XmlReplay createXmlReplayUsingIntegrationTestsModule(String relToServicesRoot) throws Exception {
\r
73 String thisDir = Tools.glue(relToServicesRoot, "/", "IntegrationTests");
\r
74 String pwd = (new File(thisDir)).getCanonicalPath();
\r
75 //System.out.println("createXmlReplayUsingIntegrationTestsModule.pwd: "+pwd);
\r
76 XmlReplay replay = new XmlReplay(pwd+XMLREPLAY_REL_DIR_TO_MODULE,
\r
77 pwd+XMLREPLAY_REL_DIR_REPORTS_TO_MODULE);
\r
78 //System.out.println("XmlReplay: "+replay);
\r
82 public static void logTest(ServiceResult sresult, String testname){
\r
83 ResultSummary summary = resultSummary(sresult, HTML);
\r
84 org.testng.Reporter.log(summary.table);
\r
85 Assert.assertEquals(summary.oks, summary.total, "Expected all "+summary.total+ " XmlReplay tests to pass. See Output from test '"+testname+"'. "+summary.errorTests);
\r
88 public static void logTest(List<ServiceResult> list, String testname){
\r
89 ResultSummary summary = resultSummary(list, HTML);
\r
90 org.testng.Reporter.log(summary.table);
\r
91 Assert.assertEquals(summary.oks, summary.total, "Expected all "+summary.total+ " XmlReplay tests to pass. See Output from test '"+testname+"'. "+summary.errorTests);
\r
94 public static void logTestForGroup(List<List<ServiceResult>> list, String testname){
\r
95 ResultSummary summary = resultSummaryForGroup(list, HTML);
\r
96 org.testng.Reporter.log(summary.table);
\r
97 ResultSummary textSummary = resultSummaryForGroup(list, TEXT);
\r
98 System.out.println("SUMMARY: \r\n"+textSummary.table);
\r
99 Assert.assertEquals(summary.oks, summary.total, "Expected all "+summary.total+ " XmlReplay tests to pass. See Output from test '"+testname+"'. "+summary.errorTests);
\r
103 //============== HELPERS AND FORMATTING =====================================================
\r
104 public static class FORMAT {
\r
105 private static final String TBLSTART = "";
\r
106 private static final String ROWSTART = "\r\n ";
\r
107 private static final String ROWSTARTRED = "\r\n ** ";
\r
108 private static final String SEP = " | ";
\r
109 private static final String ROWEND = "";
\r
110 private static final String ROWENDRED = "";
\r
111 private static final String TBLEND = "";
\r
114 public static final FORMAT TEXT = new FORMAT();
\r
115 public static class HTML_FORMAT extends FORMAT {
\r
116 private static final String TBLSTART = "<table border='1'>";
\r
117 private static final String ROWSTART = "<tr><td bgcolor='white'>";
\r
118 private static final String ROWSTARTRED = "<tr><td bgcolor='red'><b>";
\r
119 private static final String SEP = "</td><td>";
\r
120 private static final String ROWEND = "</td></tr>";
\r
121 private static final String ROWENDRED = "</b></td></tr>";
\r
122 private static final String TBLEND = "</table>";
\r
124 public static final FORMAT HTML = new HTML_FORMAT();
\r
126 public static class ResultSummary {
\r
127 public long oks = 0;
\r
128 public long total = 0;
\r
129 public String table = "";
\r
130 public List<String> groups = new ArrayList<String>();
\r
131 public List<String> errorTests = new ArrayList<String>();
\r
134 public static ResultSummary resultSummaryForGroup(List<List<ServiceResult>> list, FORMAT format){
\r
135 ResultSummary summary = new ResultSummary();
\r
138 StringBuffer buff = new StringBuffer();
\r
139 buff.append(format.TBLSTART);
\r
140 for (List<ServiceResult> serviceResults : list){
\r
141 String groupID = "";
\r
142 if (serviceResults.size()>0){
\r
143 groupID = serviceResults.get(0).testGroupID;
\r
144 summary.groups.add(groupID);
\r
146 buff.append(format.ROWSTART+"XmlReplay testGroup "+groupID+format.ROWEND);
\r
147 for (ServiceResult serviceResult : serviceResults){
\r
149 if (serviceResult.gotExpectedResult()){
\r
151 buff.append(format.ROWSTART+serviceResult.minimal()+format.ROWEND);
\r
153 buff.append(format.ROWSTARTRED+serviceResult.minimal()+format.ROWENDRED);
\r
154 summary.errorTests.add(serviceResult.testGroupID+':'+serviceResult.testID+':'+serviceResult.fullURL);
\r
158 buff.append(format.TBLEND);
\r
159 summary.table = buff.toString();
\r
163 public static ResultSummary resultSummary(List<ServiceResult> serviceResults, FORMAT format){
\r
164 ResultSummary summary = new ResultSummary();
\r
167 StringBuffer buff = new StringBuffer();
\r
168 buff.append(format.TBLSTART);
\r
169 for (ServiceResult serviceResult : serviceResults){
\r
171 if (serviceResult.gotExpectedResult()){
\r
173 buff.append(format.ROWSTART+serviceResult.minimal()+format.ROWEND);
\r
175 buff.append(format.ROWSTARTRED+serviceResult.minimal()+format.ROWENDRED);
\r
178 buff.append(format.TBLEND);
\r
179 summary.table = buff.toString();
\r
183 public static ResultSummary resultSummary(ServiceResult serviceResult, FORMAT format){
\r
184 ResultSummary summary = new ResultSummary();
\r
187 StringBuffer buff = new StringBuffer();
\r
188 buff.append(format.TBLSTART);
\r
189 if (serviceResult.gotExpectedResult()){
\r
191 buff.append(format.ROWSTART+serviceResult.minimal()+format.ROWEND);
\r
193 buff.append(format.ROWSTARTRED+serviceResult.minimal()+format.ROWENDRED);
\r
195 buff.append(format.TBLEND);
\r
196 summary.table = buff.toString();
\r