org.mattressframework.test.mock.MockHttpEntityWriter Maven / Gradle / Ivy
/*
* Copyright 2007-2008, Josh Devins.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mattressframework.test.mock;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import org.mattressframework.api.HttpEntityWriter;
import org.mattressframework.api.http.Header;
import org.mattressframework.api.http.MediaType;
/**
* A mock {@link HttpEntityWriter} allowing testing of other framework elements
* easily. Allows setting of the size to be returned from
* {@link #getSize(Object)}, as well as setting the bytes that should be
* written to the {@link OutputStream} in
* {@link #writeTo(Object, MediaType, Map, OutputStream)}.
*
* @author Josh Devins ([email protected])
*/
public class MockHttpEntityWriter extends MockProvider implements
HttpEntityWriter {
private int size = -1;
private byte[] bytes;
public int getSize(final Object entity) {
return size;
}
public void setOutputStreamBytes(final byte[] bytes) {
this.bytes = bytes;
}
public void setSize(final int size) {
this.size = size;
}
public void writeTo(final Object entity, final MediaType mediaType,
final Map httpHeaders,
final OutputStream entityStream) throws IOException {
// nothig to write
if (bytes == null) {
return;
}
entityStream.write(bytes);
}
}