]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ae9cf211501ef6b80e9086cdb79416f473b385b6
[tmp/jakarta-migration.git] /
1 /**     \r
2  * CollectionSpacePerformanceTest.java\r
3  *\r
4  * {Purpose of This Class}\r
5  *\r
6  * {Other Notes Relating to This Class (Optional)}\r
7  *\r
8  * $LastChangedBy: $\r
9  * $LastChangedRevision: $\r
10  * $LastChangedDate: $\r
11  *\r
12  * This document is a part of the source code and related artifacts\r
13  * for CollectionSpace, an open source collections management system\r
14  * for museums and related institutions:\r
15  *\r
16  * http://www.collectionspace.org\r
17  * http://wiki.collectionspace.org\r
18  *\r
19  * Copyright © 2009 {Contributing Institution}\r
20  *\r
21  * Licensed under the Educational Community License (ECL), Version 2.0.\r
22  * You may not use this file except in compliance with this License.\r
23  *\r
24  * You may obtain a copy of the ECL 2.0 License at\r
25  * https://source.collectionspace.org/collection-space/LICENSE.txt\r
26  */\r
27 package org.collectionspace.services.PerformanceTests.test;\r
28 \r
29 import java.util.ArrayList;\r
30 import java.util.List;\r
31 import java.util.Random;\r
32 \r
33 import javax.ws.rs.core.MultivaluedMap;\r
34 import javax.ws.rs.core.Response;\r
35 import javax.xml.bind.JAXBContext;\r
36 import javax.xml.bind.Marshaller;\r
37 \r
38 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;\r
39 import org.collectionspace.services.collectionobject.TitleGroup;\r
40 import org.collectionspace.services.collectionobject.TitleGroupList;\r
41 import org.collectionspace.services.intake.IntakesCommon;\r
42 import org.collectionspace.services.relation.RelationsCommon;\r
43 import org.collectionspace.services.relation.RelationshipType;\r
44 import org.jboss.resteasy.client.ClientResponse;\r
45 import org.jboss.resteasy.plugins.providers.multipart.InputPart;\r
46 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;\r
47 \r
48 /**\r
49  * The Class CollectionSpacePerformanceTests.\r
50  */\r
51 public abstract class CollectionSpacePerformanceTest {\r
52 \r
53         protected final static String OBJECT_NUMBER = "objectNumber_";\r
54         protected final static String OBJECT_TITLE = "objectTitle_";\r
55         \r
56         /*\r
57          * Package scoped methods.\r
58          */\r
59 \r
60         /**\r
61          * Fill collection object.\r
62          * \r
63          * @param co the co\r
64          * @param identifier the identifier\r
65          */\r
66         void fillCollectionObject(CollectionobjectsCommon co, String identifier) {\r
67                 fillCollectionObject(co, OBJECT_NUMBER + identifier, OBJECT_TITLE + identifier);\r
68         }\r
69 \r
70         /**\r
71          * Fill collection object.\r
72          * \r
73          * @param co the co\r
74          * @param objectNumber the object number\r
75          * @param title the object title\r
76          */\r
77         void fillCollectionObject(CollectionobjectsCommon co, String objectNumber,\r
78                         String title) {\r
79                 co.setObjectNumber(objectNumber);\r
80                 TitleGroupList titleGroupList = new TitleGroupList();\r
81                 List<TitleGroup> titleGroups = titleGroupList.getTitleGroup();\r
82                 TitleGroup titleGroup = new TitleGroup();\r
83                 titleGroup.setTitle(title);\r
84                 titleGroups.add(titleGroup);\r
85                 co.setTitleGroupList(titleGroupList);\r
86         }\r
87 \r
88         /**\r
89          * Fill intake.\r
90          * \r
91          * @param theIntake the the intake\r
92          * @param identifier the identifier\r
93          */\r
94         void fillIntake(IntakesCommon theIntake, String identifier) {\r
95                 fillIntake(theIntake, "entryNumber-" + identifier, "entryDate-"\r
96                                 + identifier);\r
97         }\r
98 \r
99         /**\r
100          * Fill intake.\r
101          * \r
102          * @param theIntake the the intake\r
103          * @param entryNumber the entry number\r
104          * @param entryDate the entry date\r
105          */\r
106         void fillIntake(IntakesCommon theIntake, String entryNumber, String entryDate) {\r
107                 theIntake.setEntryNumber(entryNumber);\r
108                 theIntake.setEntryDate(entryDate);\r
109         }\r
110 \r
111     /**\r
112      * Fill relation.\r
113      * \r
114      * @param relation the relation\r
115      * @param subjectCsid the document id1\r
116      * @param subjectDocumentType the document type1\r
117      * @param objectCsid the document id2\r
118      * @param objectDocumentType the document type2\r
119      * @param rt the rt\r
120      */\r
121     void fillRelation(RelationsCommon relation, String subjectCsid, String subjectDocumentType,\r
122                 String objectCsid, String objectDocumentType, RelationshipType rt)\r
123     {\r
124         relation.setSubjectCsid(subjectCsid);\r
125         relation.setSubjectDocumentType(subjectDocumentType);\r
126         relation.setSubjectCsid(objectCsid);\r
127         relation.setObjectDocumentType(objectDocumentType);\r
128         \r
129         relation.setRelationshipType(rt.toString());\r
130     }\r
131         \r
132         /**\r
133          * Creates the identifier.\r
134          * \r
135          * @return the string\r
136          */\r
137         String createIdentifier() {\r
138                 long identifier = System.currentTimeMillis();\r
139                 return Long.toString(identifier);\r
140         }\r
141 \r
142         /**\r
143          * Extract id.\r
144          * \r
145          * @param res the res\r
146          * \r
147          * @return the string\r
148          */\r
149         String extractId(ClientResponse<Response> res) {\r
150                 String result = null;\r
151                 \r
152                 MultivaluedMap mvm = res.getMetadata();\r
153                 String uri = (String) ((ArrayList) mvm.get("Location")).get(0);\r
154                 verbose("extractId:uri=" + uri);\r
155                 String[] segments = uri.split("/");\r
156                 result = segments[segments.length - 1];\r
157                 verbose("id=" + result);\r
158                 \r
159                 return result;\r
160         }\r
161 \r
162         /**\r
163          * Extract part.\r
164          * \r
165          * @param input\r
166          *            the input\r
167          * @param label\r
168          *            the label\r
169          * @param clazz\r
170          *            the clazz\r
171          * \r
172          * @return the object\r
173          * \r
174          * @throws Exception\r
175          *             the exception\r
176          */\r
177         static Object extractPart(MultipartInput input, String label, Class clazz) {\r
178                 Object obj = null;\r
179                 \r
180                 try {\r
181                         for (InputPart part : input.getParts()) {\r
182                                 String partLabel = part.getHeaders().getFirst("label");\r
183                                 if (label.equalsIgnoreCase(partLabel)) {\r
184                                         String partStr = part.getBodyAsString();\r
185                                         obj = part.getBody(clazz, null);\r
186                                         break;\r
187                                 }\r
188                         }\r
189                 } catch (Exception e) {\r
190                         e.printStackTrace();\r
191                 }\r
192 \r
193                 return obj;\r
194         }\r
195         \r
196         /**\r
197          * Verbose.\r
198          * \r
199          * @param msg the msg\r
200          */\r
201         void verbose(String msg) {\r
202 //              System.out.println(msg);\r
203         }\r
204 \r
205         /**\r
206          * Verbose.\r
207          * \r
208          * @param msg the msg\r
209          * @param o the o\r
210          * @param clazz the clazz\r
211          */\r
212         void verbose(String msg, Object o, Class clazz) {\r
213                 try {\r
214                         verbose(msg);\r
215                         JAXBContext jc = JAXBContext.newInstance(clazz);\r
216                         Marshaller m = jc.createMarshaller();\r
217                         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);\r
218                         m.marshal(o, System.out);\r
219                 } catch (Exception e) {\r
220                         e.printStackTrace();\r
221                 }\r
222         }\r
223 \r
224         /**\r
225          * Verbose map.\r
226          * \r
227          * @param map the map\r
228          */\r
229         void verboseMap(MultivaluedMap map) {\r
230                 for (Object entry : map.entrySet()) {\r
231                         MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;\r
232                         verbose("  name=" + mentry.getKey() + " value=" + mentry.getValue());\r
233                 }\r
234         }\r
235 \r
236         boolean isEnabled() {\r
237             return Boolean.getBoolean("cspace.perf");\r
238         }\r
239 \r
240 }\r