001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.common.foundation.network;
022:
023: import com.db4o.*;
024: import com.db4o.config.*;
025: import com.db4o.foundation.*;
026: import com.db4o.foundation.network.*;
027:
028: import db4ounit.*;
029:
030: /**
031: * @exclude
032: */
033: public class NetworkSocketTestCase implements TestLifeCycle {
034:
035: private ServerSocket4 _server;
036:
037: private int _port;
038:
039: Socket4 client;
040:
041: Socket4 serverSide;
042:
043: private PlainSocketFactory _plainSocketFactory = new PlainSocketFactory();
044:
045: public static void main(String[] args) {
046: new TestRunner(NetworkSocketTestCase.class).run();
047: }
048:
049: public void setUp() throws Exception {
050: _server = new ServerSocket4(_plainSocketFactory, 0);
051: _port = _server.getLocalPort();
052: client = new NetworkSocket(_plainSocketFactory, "localhost",
053: _port);
054: serverSide = _server.accept();
055: }
056:
057: public void tearDown() throws Exception {
058: _server.close();
059: }
060:
061: public void testRead_Close1() throws Exception {
062: assertReadClose(client, new CodeBlock() {
063: public void run() {
064: serverSide.read();
065: }
066: });
067: }
068:
069: public void testRead_Close2() throws Exception {
070: assertReadClose(serverSide, new CodeBlock() {
071: public void run() {
072: client.read();
073: }
074: });
075: }
076:
077: public void testReadBII_Close1() throws Exception {
078: assertReadClose(client, new CodeBlock() {
079: public void run() {
080: serverSide.read(new byte[10], 0, 10);
081: }
082: });
083: }
084:
085: public void testReadBII_Close2() throws Exception {
086: assertReadClose(serverSide, new CodeBlock() {
087: public void run() {
088: client.read(new byte[10], 0, 10);
089: }
090: });
091: }
092:
093: public void testWriteB_Close1() throws Exception {
094: assertWriteClose(client, new CodeBlock() {
095: public void run() {
096: serverSide.write(new byte[10]);
097: serverSide.write(new byte[10]);
098: }
099: });
100: }
101:
102: public void testWriteB_Close2() throws Exception {
103: assertWriteClose(serverSide, new CodeBlock() {
104: public void run() {
105: client.write(new byte[10]);
106: client.write(new byte[10]);
107: }
108: });
109: }
110:
111: public void testWriteBII_Close1() throws Exception {
112: assertWriteClose(client, new CodeBlock() {
113: public void run() {
114: serverSide.write(new byte[10], 0, 10);
115: serverSide.write(new byte[10], 0, 10);
116: }
117: });
118: }
119:
120: public void testWriteBII_Close2() throws Exception {
121: assertWriteClose(serverSide, new CodeBlock() {
122: public void run() {
123: client.write(new byte[10], 0, 10);
124: client.write(new byte[10], 0, 10);
125: }
126: });
127: }
128:
129: public void testWriteI_Close1() throws Exception {
130: assertWriteClose(client, new CodeBlock() {
131: public void run() {
132: serverSide.write(0xff);
133: serverSide.write(0xff);
134: }
135: });
136: }
137:
138: public void testWriteI_Close2() throws Exception {
139: assertWriteClose(serverSide, new CodeBlock() {
140: public void run() {
141: client.write(0xff);
142: client.write(0xff);
143: }
144: });
145: }
146:
147: private void assertReadClose(final Socket4 socketToBeClosed,
148: final CodeBlock codeBlock) {
149: new Thread() {
150: public void run() {
151: Cool.sleepIgnoringInterruption(500);
152: socketToBeClosed.close();
153: }
154: }.start();
155:
156: Assert.expect(Db4oIOException.class, codeBlock);
157: }
158:
159: private void assertWriteClose(final Socket4 socketToBeClosed,
160: final CodeBlock codeBlock) {
161: socketToBeClosed.close();
162: Assert.expect(Db4oIOException.class, codeBlock);
163: }
164: }
|