2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright (c) 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 package org.collectionspace.services.PerformanceTests.test;
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
28 import org.testng.Assert;
29 import org.testng.annotations.AfterClass;
30 import org.testng.annotations.Test;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import org.jboss.resteasy.client.ClientResponse;
36 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
37 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
39 import org.collectionspace.services.client.CollectionObjectClient;
40 import org.collectionspace.services.client.PayloadOutputPart;
41 import org.collectionspace.services.client.PoxPayloadOut;
42 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
45 * A test related to Issue CSPACE-1591, which creates a large number of
46 * records in a variation where:
47 * - A new client object is instantiated only once, and then is re-used repeatedly
48 * for submitting each new 'create' request.
49 * - The client's HTTP connection is formally closed, and its resources released,
50 * only once, after the last 'create' request has been submitted.
52 * @version $Revision:$
54 public class I1591OneInstOneClose extends CollectionSpacePerformanceTest {
56 final Logger logger = LoggerFactory.getLogger(I1591OneInstOneClose.class);
57 private final String COLLECTION_OBJECT_COMMON_PART_NAME =
58 getCollectionObjectCommonPartName();
59 private static int MAX_RECORDS = 500;
60 String[] coList = new String[MAX_RECORDS];
62 private String getCollectionObjectCommonPartName() {
63 return new CollectionObjectClient().getCommonPartName();
67 public void testCreateWithSingleClientInstantiationAndOneClose() {
68 createCollectionObjects(MAX_RECORDS);
72 * Creates multiple CollectionObject resources.
74 * @param numberOfObjects The number of CollectionObject resources to create.
76 public void createCollectionObjects(int numberOfObjects) {
78 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
81 ClientResponse<Response> response = null;
85 for (i = 0; i <= numberOfObjects; i++) {
87 // Create a CollectionObject instance.
88 CollectionobjectsCommon co = new CollectionobjectsCommon();
89 identifier = System.currentTimeMillis();
90 fillCollectionObject(co, Long.toString(identifier));
92 // Assign it to the Common part of a multipart payload.
93 PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
94 PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
95 commonPart.setLabel(collectionObjectClient.getCommonPartName());
97 // Make a create call with that payload and check the response.
98 response = collectionObjectClient.create(multipart);
100 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
101 coList[i] = extractId(response);
103 if (logger.isDebugEnabled() == true) {
104 logger.debug("Created CollectionObject #: " + i);
109 } catch (AssertionError e) {
110 if (logger.isDebugEnabled() == true) {
111 logger.debug("FAILURE: Created " + i +
112 " of " + numberOfObjects +
115 Assert.assertTrue(false);
116 // Since failed Asserts can throw an Exception, ensure
117 // that the underlying HTTP connection is explicitly closed
118 // under all circumstances.
120 response.releaseConnection();
125 @AfterClass(alwaysRun=true)
126 public void cleanUp() {
128 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
129 String resourceId = "";
131 if (logger.isDebugEnabled() == true) {
132 logger.debug("Cleaing up CollectionObject records created during testing ...");
135 for (int i = 0; i < coList.length; i++) {
136 resourceId = coList[i];
137 ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
138 if (logger.isDebugEnabled() == true) {
139 logger.debug("Deleted CollectionObject #: " + i);
141 res.releaseConnection();
144 if (logger.isDebugEnabled()) {
145 logger.debug("Deleted " + coList.length + " CollectionObjects.");