fail(e1.toString());
}
net.setUnpacker(new Base64Unpacker());
ThreadProxyPipe pipe = net.getDefaultStream();
pipe.addSink(new SinkAdapter() {
public void graphAttributeAdded(String sourceId, long timeId,
String attribute, Object value) {
validate(attribute, value);
}
public void graphAttributeChanged(String sourceId, long timeId,
String attribute, Object oldValue, Object newValue) {
validate(attribute, newValue);
}
private void validate(String attribute, Object value) {
String valueType = null;
Class<?> valueClass = value.getClass();
boolean isArray = valueClass.isArray();
if (isArray) {
valueClass = ((Object[]) value)[0].getClass();
}
if (valueClass.equals(Boolean.class)) {
if (isArray) {
valueType = "booleanArray";
System.out.printf("found a %s for attribute %s=%s%n",
valueType, attribute,
Arrays.toString((Boolean[]) value));
} else {
valueType = "boolean";
System.out.printf("found a %s for attribute %s=%s%n",
valueType, attribute, value.toString());
}
} else if (valueClass.equals(Byte.class)) {
if (isArray) {
valueType = "byteArray";
System.out.printf("found a %s for attribute %s=%s%n",
valueType, attribute,
Arrays.toString((Byte[]) value));
} else {
valueType = "byte";
System.out.printf("found a %s for attribute %s=%s%n",
valueType, attribute, value.toString());
}
} else if (valueClass.equals(Short.class)) {
if (isArray) {
valueType = "shortArray";
System.out.printf("found a %s for attribute %s=%s%n",
valueType, attribute,
Arrays.toString((Short[]) value));
} else {
valueType = "short";
System.out.printf("found a %s for attribute %s=%s%n",
valueType, attribute, value.toString());
}
} else if (valueClass.equals(Integer.class)) {
if (isArray) {
valueType = "intArray";
System.out.printf("found a %s for attribute %s=%s%n",
valueType, attribute,
Arrays.toString((Integer[]) value));
} else {
valueType = "int";
System.out.printf("found a %s for attribute %s=%s%n",
valueType, attribute, value.toString());
}
} else if (valueClass.equals(Long.class)) {
if (isArray) {
valueType = "longArray";
System.out.printf("found a %s for attribute %s=%s%n",
valueType, attribute,
Arrays.toString((Long[]) value));
} else {
valueType = "long";
System.out.printf("found a %s for attribute %s=%s%n",
valueType, attribute, value.toString());
}
} else if (valueClass.equals(Float.class)) {
if (isArray) {
valueType = "floatArray";
System.out.printf("found a %s for attribute %s=%s%n",
valueType, attribute,
Arrays.toString((Float[]) value));
} else {
valueType = "float";
System.out.printf("found a %s for attribute %s=%s%n",
valueType, attribute, value.toString());
}
} else if (valueClass.equals(Double.class)) {
if (isArray) {
valueType = "doubleArray";
System.out.printf("found a %s for attribute %s=%s%n",
valueType, attribute,
Arrays.toString((Double[]) value));
} else {
valueType = "double";
System.out.printf("found a %s for attribute %s=%s%n",
valueType, attribute, value.toString());
}
} else if (valueClass.equals(String.class)) {
if (isArray) {
valueType = "typeArray";
System.out.printf("found a %s for attribute %s=%s%n",
valueType, attribute,
Arrays.toString((Boolean[]) value));
} else {
valueType = "string";
System.out.printf("found a %s for attribute %s=%s%n",
valueType, attribute, value.toString());
}
}
assertTrue(valueType.equals(attribute));
}
});
Thread t = new Thread() {
@Override
public void run() {
Graph g = new MultiGraph("G");
NetStreamSender nsc = null;
try {
nsc = new NetStreamSender("localhost", 2001);
} catch (UnknownHostException e1) {
error(e1.toString());
return;
} catch (IOException e1) {
error(e1.toString());
return;
}
nsc.setPacker(new Base64Packer());
g.addSink(nsc);
g.addAttribute("intArray", 0, Integer.MAX_VALUE,
Integer.MIN_VALUE);
g.addAttribute("floatArray", 0f, Float.MAX_VALUE,
Float.MIN_VALUE);
g.addAttribute("doubleArray", 0.0, Double.MAX_VALUE,
Double.MIN_VALUE);
g.addAttribute("shortArray", (short) 0, Short.MAX_VALUE,
Short.MIN_VALUE);
g.addAttribute("longArray", 0L, Long.MAX_VALUE, Long.MIN_VALUE);
g.addAttribute("byteArray", (byte) 0, Byte.MAX_VALUE,
Byte.MIN_VALUE);
g.addAttribute("booleanArray", true, false);
// Object[] three = {new Short((short) 3),new Long(3L),"3"};
// g.addAttribute("typeArray","one", 2 , three);
g.addAttribute("int", 1);
g.addAttribute("float", 1f);
g.addAttribute("double", 1.0);
g.addAttribute("short", (short) 0);
g.addAttribute("long", 1L);
g.addAttribute("byte", (byte) 0);
g.addAttribute("boolean", true);
g.addAttribute("string", "true");
try {
nsc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
};
t.start();
try {
t.join();
} catch (InterruptedException e) {
fail(e.getMessage());
}
while (pipe.hasPostRemaining() || net.hasActiveConnections()) {
pipe.pump();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}