]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b887f20ff95b5ee961c04027676bb9f00831673e
[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.Random;\r
31 \r
32 import javax.ws.rs.core.MultivaluedMap;\r
33 import javax.ws.rs.core.Response;\r
34 import javax.xml.bind.JAXBContext;\r
35 import javax.xml.bind.Marshaller;\r
36 \r
37 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;\r
38 import org.collectionspace.services.intake.IntakesCommon;\r
39 import org.collectionspace.services.relation.RelationsCommon;\r
40 import org.collectionspace.services.relation.RelationshipType;\r
41 import org.jboss.resteasy.client.ClientResponse;\r
42 import org.jboss.resteasy.plugins.providers.multipart.InputPart;\r
43 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;\r
44 \r
45 /**\r
46  * The Class CollectionSpacePerformanceTests.\r
47  */\r
48 public abstract class CollectionSpacePerformanceTest {\r
49 \r
50         protected final static String OBJECT_NUMBER = "objectNumber_";\r
51         protected final static String OBJECT_NAME = "objectName_";\r
52         \r
53         /*\r
54          * Package scoped methods.\r
55          */\r
56 \r
57         /**\r
58          * Fill collection object.\r
59          * \r
60          * @param co the co\r
61          * @param identifier the identifier\r
62          */\r
63         void fillCollectionObject(CollectionobjectsCommon co, String identifier) {\r
64                 fillCollectionObject(co, OBJECT_NUMBER + identifier, OBJECT_NAME + identifier);\r
65         }\r
66 \r
67         /**\r
68          * Fill collection object.\r
69          * \r
70          * @param co the co\r
71          * @param objectNumber the object number\r
72          * @param objectName the object name\r
73          */\r
74         void fillCollectionObject(CollectionobjectsCommon co, String objectNumber,\r
75                         String objectName) {\r
76                 co.setObjectNumber(objectNumber);\r
77                 co.setObjectName(objectName);\r
78         }\r
79 \r
80         /**\r
81          * Fill intake.\r
82          * \r
83          * @param theIntake the the intake\r
84          * @param identifier the identifier\r
85          */\r
86         void fillIntake(IntakesCommon theIntake, String identifier) {\r
87                 fillIntake(theIntake, "entryNumber-" + identifier, "entryDate-"\r
88                                 + identifier);\r
89         }\r
90 \r
91         /**\r
92          * Fill intake.\r
93          * \r
94          * @param theIntake the the intake\r
95          * @param entryNumber the entry number\r
96          * @param entryDate the entry date\r
97          */\r
98         void fillIntake(IntakesCommon theIntake, String entryNumber, String entryDate) {\r
99                 theIntake.setEntryNumber(entryNumber);\r
100                 theIntake.setEntryDate(entryDate);\r
101         }\r
102 \r
103     /**\r
104      * Fill relation.\r
105      * \r
106      * @param relation the relation\r
107      * @param documentId1 the document id1\r
108      * @param documentType1 the document type1\r
109      * @param documentId2 the document id2\r
110      * @param documentType2 the document type2\r
111      * @param rt the rt\r
112      */\r
113     void fillRelation(RelationsCommon relation, String documentId1, String documentType1,\r
114                 String documentId2, String documentType2, RelationshipType rt)\r
115     {\r
116         relation.setDocumentId1(documentId1);\r
117         relation.setDocumentType1(documentType1);\r
118         relation.setDocumentId2(documentId2);\r
119         relation.setDocumentType2(documentType2);\r
120         \r
121         relation.setRelationshipType(rt);\r
122     }\r
123         \r
124         /**\r
125          * Creates the identifier.\r
126          * \r
127          * @return the string\r
128          */\r
129         String createIdentifier() {\r
130                 long identifier = System.currentTimeMillis();\r
131                 return Long.toString(identifier);\r
132         }\r
133 \r
134         /**\r
135          * Extract id.\r
136          * \r
137          * @param res the res\r
138          * \r
139          * @return the string\r
140          */\r
141         String extractId(ClientResponse<Response> res) {\r
142                 String result = null;\r
143                 \r
144                 MultivaluedMap mvm = res.getMetadata();\r
145                 String uri = (String) ((ArrayList) mvm.get("Location")).get(0);\r
146                 verbose("extractId:uri=" + uri);\r
147                 String[] segments = uri.split("/");\r
148                 result = segments[segments.length - 1];\r
149                 verbose("id=" + result);\r
150                 \r
151                 return result;\r
152         }\r
153 \r
154         /**\r
155          * Extract part.\r
156          * \r
157          * @param input\r
158          *            the input\r
159          * @param label\r
160          *            the label\r
161          * @param clazz\r
162          *            the clazz\r
163          * \r
164          * @return the object\r
165          * \r
166          * @throws Exception\r
167          *             the exception\r
168          */\r
169         static Object extractPart(MultipartInput input, String label, Class clazz) {\r
170                 Object obj = null;\r
171                 \r
172                 try {\r
173                         for (InputPart part : input.getParts()) {\r
174                                 String partLabel = part.getHeaders().getFirst("label");\r
175                                 if (label.equalsIgnoreCase(partLabel)) {\r
176                                         String partStr = part.getBodyAsString();\r
177                                         obj = part.getBody(clazz, null);\r
178                                         break;\r
179                                 }\r
180                         }\r
181                 } catch (Exception e) {\r
182                         e.printStackTrace();\r
183                 }\r
184 \r
185                 return obj;\r
186         }\r
187         \r
188         /**\r
189          * Verbose.\r
190          * \r
191          * @param msg the msg\r
192          */\r
193         void verbose(String msg) {\r
194 //              System.out.println(msg);\r
195         }\r
196 \r
197         /**\r
198          * Verbose.\r
199          * \r
200          * @param msg the msg\r
201          * @param o the o\r
202          * @param clazz the clazz\r
203          */\r
204         void verbose(String msg, Object o, Class clazz) {\r
205                 try {\r
206                         verbose(msg);\r
207                         JAXBContext jc = JAXBContext.newInstance(clazz);\r
208                         Marshaller m = jc.createMarshaller();\r
209                         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);\r
210                         m.marshal(o, System.out);\r
211                 } catch (Exception e) {\r
212                         e.printStackTrace();\r
213                 }\r
214         }\r
215 \r
216         /**\r
217          * Verbose map.\r
218          * \r
219          * @param map the map\r
220          */\r
221         void verboseMap(MultivaluedMap map) {\r
222                 for (Object entry : map.entrySet()) {\r
223                         MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;\r
224                         verbose("  name=" + mentry.getKey() + " value=" + mentry.getValue());\r
225                 }\r
226         }\r
227 \r
228         boolean isEnabled() {\r
229             return Boolean.getBoolean("cspace.perf");\r
230         }\r
231 \r
232 }\r