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