]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
021dfe66ed666eb2216e6e2f2973e5c4fdb85209
[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 \r
28 import java.util.ArrayList;\r
29 import java.util.Arrays;\r
30 import java.util.List;\r
31 \r
32 /**\r
33  * User: laramie\r
34  * $LastChangedRevision:  $\r
35  * $LastChangedDate:  $\r
36  */\r
37 public class TreeWalkResults extends ArrayList<TreeWalkResults.TreeWalkEntry> {\r
38       public String toString(String LEAD){\r
39           StringBuffer res = new StringBuffer();\r
40           for (TreeWalkResults.TreeWalkEntry entry: this) {\r
41               res.append(entry.toString(LEAD));\r
42           }\r
43           return  res.toString();\r
44       }\r
45 \r
46     /** This cllass has two public Lists: you can construct your own to set the acceptable and unacceptable STATUS codes.\r
47      *   They are defaulted to R_ADDED being acceptable. */\r
48     public static class MatchSpec {\r
49         public static final TreeWalkEntry.STATUS[]  defaultAcceptableStatiArray = {TreeWalkEntry.STATUS.INFO,\r
50                                                                                          TreeWalkEntry.STATUS.MATCHED,\r
51                                                                                          TreeWalkEntry.STATUS.R_ADDED};\r
52 \r
53         public static final TreeWalkEntry.STATUS[] defaultErrorStatiArray =           {TreeWalkEntry.STATUS.R_MISSING,\r
54                                                                                           TreeWalkEntry.STATUS.NESTED_ERROR,\r
55                                                                                           TreeWalkEntry.STATUS.TEXT_DIFFERENT,\r
56                                                                                           TreeWalkEntry.STATUS.DOC_ERROR};\r
57         public List<TreeWalkEntry.STATUS> errorStati;\r
58 \r
59         public static MatchSpec createDefault(){\r
60             MatchSpec result = new MatchSpec();\r
61             result.errorStati = Arrays.asList(defaultErrorStatiArray);\r
62             return result;\r
63         }\r
64         public static MatchSpec create(TreeWalkEntry.STATUS[] statiArray){\r
65             MatchSpec result = new MatchSpec();\r
66             result.errorStati = Arrays.asList(statiArray);\r
67             return result;\r
68         }\r
69         public void removeErrorFromSpec(TreeWalkEntry.STATUS status){\r
70             ArrayList arrayList = new ArrayList(errorStati);\r
71             arrayList.remove(status);\r
72             errorStati = arrayList;\r
73         }\r
74         public String toString(){\r
75             StringBuffer buff = new StringBuffer("{");\r
76             int i = 0;\r
77             for (TreeWalkEntry.STATUS status : errorStati){\r
78                  if (i>0) buff.append(",");\r
79                 String foo = status.toString();\r
80                 buff.append(foo);\r
81                 i++;\r
82             }\r
83             buff.append("}");\r
84             return buff.toString();\r
85         }\r
86 \r
87     }\r
88 \r
89     public static class TreeWalkEntry {\r
90         public String lpath = "";\r
91         public String rpath = "";\r
92         public String ltextTrimmed = "";\r
93         public String rtextTrimmed = "";\r
94         public String expected = "";\r
95         public String actual = "";\r
96         public String message = "";\r
97         public String errmessage = "";\r
98         public TreeWalkResults nested;\r
99         public static enum STATUS {INFO, MATCHED, R_MISSING, R_ADDED, DOC_ERROR, TEXT_DIFFERENT, NESTED_ERROR};\r
100         public STATUS status;\r
101         public String toString(){\r
102             return toString("\r\n");\r
103         }\r
104         public String toString(String LEAD){\r
105             String INDENT = "    ";\r
106             return\r
107                  LEAD + "{"\r
108                  +status.name()\r
109                  +(Tools.notEmpty(lpath) ? ", L.path:"+lpath : "")\r
110                  +(Tools.notEmpty(rpath) ? ", R.path:"+rpath : "")\r
111                  +(Tools.notEmpty(message) ? ", message:"+message : "")\r
112                  +(Tools.notEmpty(errmessage) ? ", errmessage:"+errmessage : "")\r
113                  +", status:"+status\r
114                  +((status != STATUS.MATCHED) && Tools.notEmpty(ltextTrimmed) ? ","+LEAD+"    L.trimmed:"+ltextTrimmed : "")\r
115                  +((status != STATUS.MATCHED) && Tools.notEmpty(rtextTrimmed) ? ","+LEAD+"    R.trimmed:"+rtextTrimmed : "")\r
116                  +((status != STATUS.MATCHED) && Tools.notEmpty(expected) ? LEAD+"EXPECTED:"+LEAD+"------------------"+LEAD+expected.trim()+LEAD+"------------------" : "")\r
117                  +((status != STATUS.MATCHED) && Tools.notEmpty(actual) ? LEAD+"ACTUAL:"+LEAD+"------------------"+LEAD+actual.trim()+LEAD+"------------------"+LEAD : "")\r
118                  +((status != STATUS.MATCHED) && (nested != null) ? LEAD+"NESTED:"+LEAD+"------------------"+LEAD+nested.toString(LEAD+INDENT)+LEAD+"------------------"+LEAD : "")\r
119                  +"}";\r
120         }\r
121     }\r
122 \r
123     public boolean hasDocErrors(){\r
124         for (TreeWalkEntry entry : this){\r
125             if (entry.status == TreeWalkEntry.STATUS.DOC_ERROR){\r
126                 return true;\r
127             }\r
128         }\r
129         return false;\r
130     }\r
131 \r
132     public String getErrorMessages(){\r
133         StringBuffer buf = new StringBuffer();\r
134         boolean first = true;\r
135         for (TreeWalkEntry entry : this){\r
136             if ( Tools.notEmpty(entry.errmessage)){\r
137                 if (first) {\r
138                     buf.append(",errors:");\r
139                 } else {\r
140                     buf.append(',');\r
141                 }\r
142                 buf.append('\''+entry.errmessage+"\'");\r
143                 first = false;\r
144             }\r
145         }\r
146         return buf.toString();\r
147     }\r
148 \r
149 \r
150 \r
151     public boolean isStrictMatch(){\r
152         for (TreeWalkEntry entry : this){\r
153             if (entry.status == TreeWalkEntry.STATUS.DOC_ERROR){\r
154                 return false;\r
155             }\r
156             if ( !(   entry.status == TreeWalkEntry.STATUS.MATCHED\r
157                    || entry.status == TreeWalkEntry.STATUS.INFO)){\r
158                 return false;\r
159             }\r
160         }\r
161         return true;\r
162     }\r
163     public int getMismatchCount(){\r
164         int c = 0;\r
165         for (TreeWalkEntry entry : this){\r
166             if ( entry.status == TreeWalkEntry.STATUS.DOC_ERROR\r
167                 || entry.status != TreeWalkEntry.STATUS.MATCHED\r
168                 || entry.status != TreeWalkEntry.STATUS.INFO){\r
169                 c++;\r
170             }\r
171         }\r
172         return c;\r
173     }\r
174     /** For our purposes, trees match if they have the same element tree structure - no checking is done for text node changes. */\r
175     public boolean treesMatch(){\r
176         for (TreeWalkEntry entry : this){\r
177             if (entry.status == TreeWalkEntry.STATUS.DOC_ERROR\r
178                 || entry.status == TreeWalkEntry.STATUS.R_MISSING\r
179                 || entry.status == TreeWalkEntry.STATUS.R_ADDED  ){\r
180                 return false;\r
181             }\r
182         }\r
183         return true;\r
184     }\r
185 \r
186     public boolean treesMatch(MatchSpec matchSpec) {\r
187         for (TreeWalkEntry entry : this) {\r
188             if (matchSpec.errorStati.contains(entry.status)) {\r
189                 return false;\r
190             }\r
191         }\r
192         return true;\r
193     }\r
194 \r
195     public int countFor(TreeWalkEntry.STATUS status){\r
196         int count = 0;\r
197         for (TreeWalkEntry entry : this){\r
198             if (entry.status.equals(status)){\r
199                 count++;\r
200             }\r
201         }\r
202         return count;\r
203     }\r
204 \r
205     public String miniSummary(){\r
206         //MATCHED, INFO, R_MISSING, R_ADDED, TEXT_DIFFERENT};\r
207         StringBuffer buf = new StringBuffer();\r
208         buf.append("{");\r
209         boolean nextline = false;\r
210         for (TreeWalkEntry.STATUS st : TreeWalkEntry.STATUS.values()){\r
211             if (nextline) buf.append(',');\r
212             buf.append(st.name()+':'+countFor(st));\r
213             nextline = true;\r
214         }\r
215         buf.append(getErrorMessages());\r
216         buf.append("}");\r
217         return buf.toString();\r
218     }\r
219 \r
220     public String fullSummary(){\r
221         StringBuffer buf = new StringBuffer();\r
222         for (TreeWalkResults.TreeWalkEntry entry : this){\r
223             buf.append(entry.toString()).append("\r\n");\r
224         }\r
225         return buf.toString();\r
226     }\r
227 \r
228 \r
229     public String leftID;\r
230     public String rightID;\r
231 }