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