|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.rest.action; |
| 21 | + |
| 22 | +import org.elasticsearch.common.bytes.BytesArray; |
| 23 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 24 | +import org.elasticsearch.rest.BytesRestResponse; |
| 25 | +import org.elasticsearch.rest.RestResponse; |
| 26 | +import org.elasticsearch.rest.RestStatus; |
| 27 | +import org.elasticsearch.test.ESTestCase; |
| 28 | +import org.elasticsearch.test.rest.FakeRestChannel; |
| 29 | +import org.elasticsearch.test.rest.FakeRestRequest; |
| 30 | +import org.elasticsearch.transport.TransportResponse; |
| 31 | +import org.elasticsearch.transport.TransportResponse.Empty; |
| 32 | + |
| 33 | +import java.util.concurrent.atomic.AtomicReference; |
| 34 | + |
| 35 | +public class RestBuilderListenerTests extends ESTestCase { |
| 36 | + |
| 37 | + public void testXContentBuilderClosedInBuildResponse() throws Exception { |
| 38 | + AtomicReference<XContentBuilder> builderAtomicReference = new AtomicReference<>(); |
| 39 | + RestBuilderListener<TransportResponse.Empty> builderListener = |
| 40 | + new RestBuilderListener<Empty>(new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)) { |
| 41 | + @Override |
| 42 | + public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception { |
| 43 | + builderAtomicReference.set(builder); |
| 44 | + builder.close(); |
| 45 | + return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + builderListener.buildResponse(Empty.INSTANCE); |
| 50 | + assertNotNull(builderAtomicReference.get()); |
| 51 | + assertTrue(builderAtomicReference.get().generator().isClosed()); |
| 52 | + } |
| 53 | + |
| 54 | + public void testXContentBuilderNotClosedInBuildResponseAssertionsDisabled() throws Exception { |
| 55 | + AtomicReference<XContentBuilder> builderAtomicReference = new AtomicReference<>(); |
| 56 | + RestBuilderListener<TransportResponse.Empty> builderListener = |
| 57 | + new RestBuilderListener<Empty>(new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)) { |
| 58 | + @Override |
| 59 | + public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception { |
| 60 | + builderAtomicReference.set(builder); |
| 61 | + return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + boolean assertBuilderClosed(XContentBuilder xContentBuilder) { |
| 66 | + // don't check the actual builder being closed so we can test auto close |
| 67 | + return true; |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + builderListener.buildResponse(Empty.INSTANCE); |
| 72 | + assertNotNull(builderAtomicReference.get()); |
| 73 | + assertTrue(builderAtomicReference.get().generator().isClosed()); |
| 74 | + } |
| 75 | + |
| 76 | + public void testXContentBuilderNotClosedInBuildResponseAssertionsEnabled() throws Exception { |
| 77 | + assumeTrue("tests are not being run with assertions", RestBuilderListener.class.desiredAssertionStatus()); |
| 78 | + |
| 79 | + RestBuilderListener<TransportResponse.Empty> builderListener = |
| 80 | + new RestBuilderListener<Empty>(new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)) { |
| 81 | + @Override |
| 82 | + public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception { |
| 83 | + return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + AssertionError error = expectThrows(AssertionError.class, () -> builderListener.buildResponse(Empty.INSTANCE)); |
| 88 | + assertEquals("callers should ensure the XContentBuilder is closed themselves", error.getMessage()); |
| 89 | + } |
| 90 | +} |
0 commit comments