CatTestCase.java
1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.dianping.cat.message;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import org.junit.After;
import org.junit.Before;
import org.unidal.helper.Files;
import org.unidal.lookup.ComponentTestCase;
import com.dianping.cat.Cat;
import com.dianping.cat.configuration.client.entity.ClientConfig;
import com.dianping.cat.configuration.client.entity.Domain;
import com.dianping.cat.configuration.client.entity.Server;
public abstract class CatTestCase extends ComponentTestCase {
protected File getConfigurationFile() {
if (isCatServerAlive()) {
try {
ClientConfig config = new ClientConfig();
config.setMode("client");
config.addDomain(new Domain("cat"));
config.addServer(new Server("localhost").setPort(2280));
File file = new File("target/cat-config.xml");
Files.forIO().writeTo(file, config.toString());
return file;
} catch (IOException e) {
return null;
}
}
return null;
}
protected boolean isCatServerAlive() {
// detect if a CAT server listens on localhost:2280
try {
SocketChannel channel = SocketChannel.open(new InetSocketAddress("localhost", 2280));
channel.close();
return true;
} catch (Exception e) {
// ignore it
}
return false;
}
@Before
public void setup() throws Exception {
Cat.initialize(getContainer(), getConfigurationFile());
}
@After
public void teardown() throws Exception {
}
}