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.collectionobject.CollectionobjectsCommon;
43 * A test related to Issue CSPACE-1591, which creates a large number of
44 * records in a variation where:
45 * - A new client object is instantiated only once, and then is re-used repeatedly
46 * for submitting each new 'create' request.
47 * - The client's HTTP connection is formally closed, and its resources released,
48 * only once, after the last 'create' request has been submitted.
50 * @version $Revision:$
52 public class I1591OneInstOneClose extends CollectionSpacePerformanceTest {
54 final Logger logger = LoggerFactory.getLogger(I1591OneInstOneClose.class);
55 private final String COLLECTION_OBJECT_COMMON_PART_NAME =
56 getCollectionObjectCommonPartName();
57 private static int MAX_RECORDS = 500;
58 String[] coList = new String[MAX_RECORDS];
60 private String getCollectionObjectCommonPartName() {
61 return new CollectionObjectClient().getCommonPartName();
65 public void testCreateWithSingleClientInstantiationAndOneClose() {
66 createCollectionObjects(MAX_RECORDS);
70 * Creates multiple CollectionObject resources.
72 * @param numberOfObjects The number of CollectionObject resources to create.
74 public void createCollectionObjects(int numberOfObjects) {
76 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
79 ClientResponse<Response> response = null;
83 for (i = 0; i <= numberOfObjects; i++) {
85 // Create a CollectionObject instance.
86 CollectionobjectsCommon co = new CollectionobjectsCommon();
87 identifier = System.currentTimeMillis();
88 fillCollectionObject(co, Long.toString(identifier));
90 // Assign it to the Common part of a multipart payload.
91 MultipartOutput multipart = new MultipartOutput();
92 OutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
93 commonPart.getHeaders().add("label", COLLECTION_OBJECT_COMMON_PART_NAME);
95 // Make a create call with that payload and check the response.
96 response = collectionObjectClient.create(multipart);
98 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
99 coList[i] = extractId(response);
101 if (logger.isDebugEnabled() == true) {
102 logger.debug("Created CollectionObject #: " + i);
107 } catch (AssertionError e) {
108 if (logger.isDebugEnabled() == true) {
109 logger.debug("FAILURE: Created " + i +
110 " of " + numberOfObjects +
113 Assert.assertTrue(false);
114 // Since failed Asserts can throw an Exception, ensure
115 // that the underlying HTTP connection is explicitly closed
116 // under all circumstances.
118 response.releaseConnection();
123 @AfterClass(alwaysRun=true)
124 public void cleanUp() {
126 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
127 String resourceId = "";
129 if (logger.isDebugEnabled() == true) {
130 logger.debug("Cleaing up CollectionObject records created during testing ...");
133 for (int i = 0; i < coList.length; i++) {
134 resourceId = coList[i];
135 ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
136 if (logger.isDebugEnabled() == true) {
137 logger.debug("Deleted CollectionObject #: " + i);
139 res.releaseConnection();
142 if (logger.isDebugEnabled()) {
143 logger.debug("Deleted " + coList.length + " CollectionObjects.");