1 /* 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.io.File; 25 import java.net.InetAddress; 26 27 28 public class IoTraceBase implements IoTraceListener { 29 30 protected static final Object my_context = new Object() { 31 }; 32 33 private String path; 34 private long bytesRead; 35 private long bytesWritten; 36 private Object context; 37 private InetAddress address; 38 private int port; 39 private int timeout; 40 41 protected void clear() { 42 context = null; 43 bytesRead = 0; 44 bytesWritten = 0; 45 address = null; 46 port = 0; 47 timeout = 0; 48 path = null; 49 } 50 51 @Override 52 public Object fileWriteBegin(String p) { 53 path = p; 54 return my_context; 55 } 56 57 @Override 58 public void fileWriteEnd(Object ctx, long bw) { 59 context = ctx; 60 bytesWritten = bw; 61 } 62 63 @Override 64 public Object fileReadBegin(String p) { 65 path = p; 66 return my_context; 67 } 68 69 @Override 70 public void fileReadEnd(Object ctx, long br) { 71 context = ctx; 72 bytesRead = br; 73 } 74 75 @Override 76 public Object socketReadBegin(InetAddress address, int port, 77 int timeout) { 78 this.address = address; 79 this.port = port; 80 this.timeout = timeout; 81 return my_context; 82 } 83 84 @Override 85 public void socketReadEnd(Object context, long bytesRead) { 86 this.context = context; 87 this.bytesRead = bytesRead; 88 } 89 90 @Override 91 public Object socketWriteBegin(InetAddress address, int port) { 92 this.address = address; 93 this.port = port; 94 return my_context; 95 } 96 97 @Override 98 public void socketWriteEnd(Object context, long bytesWritten) { 99 this.context = context; 100 this.bytesWritten = bytesWritten; 101 } 102 103 protected void expectFileRead(long br, File f) throws Exception { 104 expectFile(0, br, f); 105 } 106 107 protected void expectFileWrite(long bw, File f) throws Exception { 108 expectFile(bw, 0, f); 109 } 110 111 protected void expectFile(long bw, long br, File f) throws Exception { 112 if (context != my_context) { 113 throw new Exception("Wrong context: " + context); 114 } 115 if (bytesWritten != bw) { 116 throw new Exception("Expected " + bw + " byte to be read, got: " 117 + bytesWritten); 118 } 119 if (bytesRead != br) { 120 throw new Exception("Expected " + br + " byte to be read, got: " 121 + bytesWritten); 122 } 123 if (!path.equals(f.getPath())) { 124 throw new Exception("Incorrect path: " + path + ". Expected: " 125 + f.getPath()); 126 } 127 } 128 129 protected void expectSocket(int br, int bw, InetAddress ia, int p, int t) 130 throws Exception { 131 if (context != my_context) { 132 throw new Exception("Wrong context: " + context); 133 } 134 if (bytesWritten != bw) { 135 throw new Exception("Expected " + bw + " byte to be written, got: " 136 + bytesWritten); 137 } 138 if (bytesRead != br) { 139 throw new Exception("Expected " + br + " byte to be read, got: " 140 + bytesWritten); 141 } 142 if (!address.equals(ia)) { 143 throw new Exception("Incorrect address: " + address 144 + ". Expected: " + ia); 145 } 146 if (port != p) { 147 throw new Exception("Expected " + p + " port, got: " + port); 148 } 149 if (timeout != t) { 150 throw new Exception("Expected " + t + " timeout, got: " + timeout); 151 } 152 } 153 }