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