]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
641e2cbd79e044c17ca4f6da68d42a51a9e99231
[tmp/jakarta-migration.git] /
1 /**\r
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
5  *\r
6  * http://www.collectionspace.org\r
7  * http://wiki.collectionspace.org\r
8  *\r
9  * Copyright (c) 2009 Regents of the University of California\r
10  *\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
13  *\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
16  *\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
22  */\r
23 \r
24 package org.collectionspace.services.IntegrationTests.xmlreplay;\r
25 \r
26 import org.collectionspace.services.common.api.Tools;\r
27 import org.testng.Assert;\r
28 \r
29 import java.io.File;\r
30 import java.util.ArrayList;\r
31 import java.util.List;\r
32 \r
33 /** Subclass this test to programmatically control XmlReplay from a surefire test.  See example in IntegrationTests :: XmlReplaySelfTest\r
34  * User: laramie\r
35  * $LastChangedRevision:  $\r
36  * $LastChangedDate:  $\r
37  */\r
38 public class XmlReplayTest {\r
39 \r
40     public static final String XMLREPLAY_REL_DIR_TO_MODULE = "/src/test/resources/test-data/xmlreplay";\r
41 \r
42     /** To use this method, you should have a test repository of xml files in the path\r
43      *  defined by XMLREPLAY_REL_DIR_TO_MODULE, relative to your pom.xml file, but normally\r
44      *  you would use the central repository of tests, which live in services/IntegrationTests,\r
45      *  and which you can use by calling createXmlReplay() which calls createXmlReplayUsingIntegrationTestsModule() for you.\r
46      */\r
47     public static XmlReplay createXmlReplayForModule() throws Exception {\r
48         String pwd = (new File(".")).getCanonicalPath();\r
49         System.out.println("createXmlReplayForModule.pwd: "+pwd);\r
50         XmlReplay replay = new XmlReplay(pwd+XMLREPLAY_REL_DIR_TO_MODULE);\r
51         System.out.println("XmlReplay: "+replay);\r
52         return replay;\r
53     }\r
54 \r
55     /** Use this method if your test xml files are stored in the central repository,\r
56      *   which is "services/IntegrationTests" + XMLREPLAY_REL_DIR_TO_MODULE\r
57      */\r
58     public static XmlReplay createXmlReplay() throws Exception {\r
59         return createXmlReplayUsingIntegrationTestsModule("../..");\r
60     }\r
61 \r
62     /**\r
63      * @param relToServicesRoot is a Unix-like path from the calling module to the services root,\r
64      *        so if  if you are in services/dimension/client/\r
65      *        then relToServicesRoot is "../.." which is how most of the client tests are set up, or if you\r
66      *        are setting up your test repository relative to the main service, e.g. you are in\r
67      *        services/dimension/, then relToServicesRoot is ".."\r
68      */\r
69     public static XmlReplay createXmlReplayUsingIntegrationTestsModule(String relToServicesRoot) throws Exception {\r
70         String thisDir = Tools.glue(relToServicesRoot, "/", "IntegrationTests");\r
71         String pwd = (new File(thisDir)).getCanonicalPath();\r
72         //System.out.println("createXmlReplayUsingIntegrationTestsModule.pwd: "+pwd);\r
73         XmlReplay replay = new XmlReplay(pwd+XMLREPLAY_REL_DIR_TO_MODULE);\r
74         //System.out.println("XmlReplay: "+replay);\r
75         return replay;\r
76     }\r
77 \r
78     public static void logTest(ServiceResult sresult, String testname){\r
79         ResultSummary summary = resultSummary(sresult, HTML);\r
80         org.testng.Reporter.log(summary.table);\r
81         Assert.assertEquals(summary.oks, summary.total, "Expected all "+summary.total+ " XmlReplay tests to pass.  See Output from test '"+testname+"'. "+summary.errorTests);\r
82     }\r
83 \r
84     public static void logTest(List<ServiceResult> list, String testname){\r
85         ResultSummary summary = resultSummary(list, HTML);\r
86         org.testng.Reporter.log(summary.table);\r
87         Assert.assertEquals(summary.oks, summary.total, "Expected all "+summary.total+ " XmlReplay tests to pass.  See Output from test '"+testname+"'. "+summary.errorTests);\r
88     }\r
89 \r
90     public static void logTestForGroup(List<List<ServiceResult>> list, String testname){\r
91         ResultSummary summary = resultSummaryForGroup(list, HTML);\r
92         org.testng.Reporter.log(summary.table);\r
93         ResultSummary textSummary = resultSummaryForGroup(list, TEXT);\r
94         System.out.println("SUMMARY: \r\n"+textSummary.table);\r
95         Assert.assertEquals(summary.oks, summary.total, "Expected all "+summary.total+ " XmlReplay tests to pass.  See Output from test '"+testname+"'. "+summary.errorTests);\r
96     }\r
97 \r
98 \r
99     //============== HELPERS AND FORMATTING =====================================================\r
100     public static class FORMAT {\r
101         private static final String TBLSTART = "";\r
102         private static final String ROWSTART = "\r\n       ";\r
103         private static final String ROWSTARTRED = "\r\n    ** ";\r
104         private static final String SEP = "  |  ";\r
105         private static final String ROWEND = "";\r
106         private static final String ROWENDRED = "";\r
107         private static final String TBLEND = "";\r
108 \r
109     }\r
110     public static final FORMAT TEXT = new FORMAT();\r
111     public static class HTML_FORMAT extends FORMAT {\r
112         private static final String TBLSTART = "<table border='1'>";\r
113         private static final String ROWSTART = "<tr><td bgcolor='white'>";\r
114         private static final String ROWSTARTRED = "<tr><td bgcolor='red'><b>";\r
115         private static final String SEP = "</td><td>";\r
116         private static final String ROWEND = "</td></tr>";\r
117         private static final String ROWENDRED = "</b></td></tr>";\r
118         private static final String TBLEND = "</table>";\r
119     }\r
120     public static final FORMAT HTML = new HTML_FORMAT();\r
121 \r
122     public static class ResultSummary {\r
123         public long oks = 0;\r
124         public long total = 0;\r
125         public String table = "";\r
126         public List<String> groups = new ArrayList<String>();\r
127         public List<String> errorTests = new ArrayList<String>();\r
128     }\r
129 \r
130     public static ResultSummary resultSummaryForGroup(List<List<ServiceResult>> list, FORMAT format){\r
131         ResultSummary summary = new ResultSummary();\r
132         summary.oks = 0;\r
133         summary.total = 0;\r
134         StringBuffer buff = new StringBuffer();\r
135         buff.append(format.TBLSTART);\r
136         for (List<ServiceResult> serviceResults : list){\r
137             String groupID = "";\r
138             if (serviceResults.size()>0){\r
139                 groupID = serviceResults.get(0).testGroupID;\r
140                 summary.groups.add(groupID);\r
141             }\r
142             buff.append(format.ROWSTART+"XmlReplay testGroup "+groupID+format.ROWEND);\r
143             for (ServiceResult serviceResult : serviceResults){\r
144                 summary.total++;\r
145                 if (serviceResult.gotExpectedResult()){\r
146                     summary.oks++;\r
147                     buff.append(format.ROWSTART+serviceResult.minimal()+format.ROWEND);\r
148                 } else {\r
149                     buff.append(format.ROWSTARTRED+serviceResult.minimal()+format.ROWENDRED);\r
150                     summary.errorTests.add(serviceResult.testGroupID+':'+serviceResult.testID+':'+serviceResult.fullURL);\r
151                 }\r
152             }\r
153         }\r
154         buff.append(format.TBLEND);\r
155         summary.table = buff.toString();\r
156         return summary;\r
157     }\r
158 \r
159     public static ResultSummary resultSummary(List<ServiceResult> serviceResults,  FORMAT format){\r
160         ResultSummary summary = new ResultSummary();\r
161         summary.oks = 0;\r
162         summary.total = 0;\r
163         StringBuffer buff = new StringBuffer();\r
164         buff.append(format.TBLSTART);\r
165         for (ServiceResult serviceResult : serviceResults){\r
166             summary.total++;\r
167             if (serviceResult.gotExpectedResult()){\r
168                 summary.oks++;\r
169                 buff.append(format.ROWSTART+serviceResult.minimal()+format.ROWEND);\r
170             } else {\r
171                 buff.append(format.ROWSTARTRED+serviceResult.minimal()+format.ROWENDRED);\r
172             }\r
173         }\r
174         buff.append(format.TBLEND);\r
175         summary.table = buff.toString();\r
176         return summary;\r
177     }\r
178 \r
179     public static ResultSummary resultSummary(ServiceResult serviceResult, FORMAT format){\r
180         ResultSummary summary = new ResultSummary();\r
181         summary.oks = 0;\r
182         summary.total = 1;\r
183         StringBuffer buff = new StringBuffer();\r
184         buff.append(format.TBLSTART);\r
185         if (serviceResult.gotExpectedResult()){\r
186             summary.oks = 1;\r
187             buff.append(format.ROWSTART+serviceResult.minimal()+format.ROWEND);\r
188         } else {\r
189             buff.append(format.ROWSTARTRED+serviceResult.minimal()+format.ROWENDRED);\r
190         }\r
191         buff.append(format.TBLEND);\r
192         summary.table = buff.toString();\r
193         return summary;\r
194     }\r
195 \r
196 }\r