|
| 1 | +/** |
| 2 | + * Copyright (c) 2008 - 2012 10gen, Inc. <http://10gen.com> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package com.mongodb; |
| 19 | + |
| 20 | +import org.bson.types.ObjectId; |
| 21 | +import org.testng.Assert; |
| 22 | +import org.testng.annotations.BeforeTest; |
| 23 | +import org.testng.annotations.Test; |
| 24 | + |
| 25 | +import java.io.ByteArrayOutputStream; |
| 26 | +import java.io.IOException; |
| 27 | +import java.net.UnknownHostException; |
| 28 | + |
| 29 | +public class OutMessageTest { |
| 30 | + |
| 31 | + Mongo m; |
| 32 | + |
| 33 | + @BeforeTest |
| 34 | + public void setup() throws UnknownHostException { |
| 35 | + m = new Mongo(); |
| 36 | + } |
| 37 | + |
| 38 | + // Ensure defensive code is in place after doneWithMessage is called. |
| 39 | + @Test |
| 40 | + public void testDoneWithMessage() throws IOException { |
| 41 | + DBCollection collection = m.getDB("OutMessageTest").getCollection("doneWithMessage"); |
| 42 | + |
| 43 | + OutMessage om = OutMessage.insert(collection, DefaultDBEncoder.FACTORY.create(), WriteConcern.SAFE); |
| 44 | + om.putObject(new BasicDBObject("_id", new ObjectId())); |
| 45 | + |
| 46 | + // This will release the buffer and put the object in an unusable state. |
| 47 | + om.doneWithMessage(); |
| 48 | + |
| 49 | + try { |
| 50 | + om.doneWithMessage(); |
| 51 | + Assert.fail(); |
| 52 | + } catch (IllegalStateException e) { |
| 53 | + // expected |
| 54 | + } |
| 55 | + |
| 56 | + try { |
| 57 | + om.prepare(); |
| 58 | + Assert.fail(); |
| 59 | + } catch (IllegalStateException e) { |
| 60 | + // expected |
| 61 | + } |
| 62 | + |
| 63 | + try { |
| 64 | + om.putObject(new BasicDBObject("_id", new ObjectId())); |
| 65 | + Assert.fail(); |
| 66 | + } catch (IllegalStateException e) { |
| 67 | + // expected |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + om.pipe(new ByteArrayOutputStream(100)); |
| 72 | + Assert.fail(); |
| 73 | + } catch (IllegalStateException e) { |
| 74 | + // expected |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + om.size(); |
| 79 | + Assert.fail(); |
| 80 | + } catch (IllegalStateException e) { |
| 81 | + // expected |
| 82 | + } |
| 83 | + |
| 84 | + } |
| 85 | +} |
0 commit comments