Skip to content

Commit 1e2523a

Browse files
committed
Add more unit tests; Small refactoring
1 parent 00e4b10 commit 1e2523a

File tree

15 files changed

+433
-147
lines changed

15 files changed

+433
-147
lines changed

cdtp-java-client/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<websocket.api.version>1.1</websocket.api.version>
2020
<tyrus.version>1.13.1</tyrus.version>
2121
<logback.version>1.2.3</logback.version>
22+
<javassist.version>3.22.0-GA</javassist.version>
2223

2324
<junit.version>4.12</junit.version>
2425
<jacoco.version>0.8.0</jacoco.version>
@@ -44,6 +45,12 @@
4445
<version>${tyrus.version}</version>
4546
</dependency>
4647

48+
<dependency>
49+
<groupId>org.javassist</groupId>
50+
<artifactId>javassist</artifactId>
51+
<version>${javassist.version}</version>
52+
</dependency>
53+
4754
<dependency>
4855
<groupId>ch.qos.logback</groupId>
4956
<artifactId>logback-classic</artifactId>
Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.github.kklisura.cdtp;
22

3-
import com.github.kklisura.cdtp.protocol.ChromeDevTools;
3+
import com.github.kklisura.cdtp.services.ChromeDevToolsService;
44
import com.github.kklisura.cdtp.services.ChromeService;
5-
import com.github.kklisura.cdtp.services.exceptions.ChromeServiceException;
65
import com.github.kklisura.cdtp.services.impl.ChromeServiceImpl;
76
import com.github.kklisura.cdtp.services.model.chrome.ChromeTab;
87

@@ -11,15 +10,34 @@
1110
*
1211
*/
1312
public class App {
14-
public static void main( String[] args ) throws ChromeServiceException, InterruptedException {
13+
public static void main( String[] args ) throws Exception {
1514
final ChromeService chromeService = new ChromeServiceImpl(9222);
1615
final ChromeTab tab = chromeService.createTab();
1716

18-
final ChromeDevTools devTools = chromeService.getDevTools(tab);
17+
try (ChromeDevToolsService cdtpService = chromeService.createDevToolsService(tab)) {
1918

20-
// Network requestWillBeSent event
21-
// Page loadEventFired
19+
// Network requestWillBeSent event
20+
// Page loadEventFired
2221

23-
devTools.getPage().navigate("http://google.com");
22+
cdtpService.getPage().navigate("http://google.com");
23+
cdtpService.getPage().navigate("http://twitter.com");
24+
cdtpService.getPage().navigate("http://facebook.com");
25+
26+
cdtpService.getPage().navigate("http://atlantbh.com");
27+
}
28+
29+
try (ChromeDevToolsService cdtpService = chromeService.createDevToolsService(tab)) {
30+
31+
// Network requestWillBeSent event
32+
// Page loadEventFired
33+
34+
cdtpService.getPage().navigate("http://google.com");
35+
cdtpService.getPage().navigate("http://twitter.com");
36+
cdtpService.getPage().navigate("http://facebook.com");
37+
38+
cdtpService.getPage().navigate("http://atlantbh.com");
39+
}
40+
41+
chromeService.closeTab(tab);
2442
}
2543
}

cdtp-java-client/src/main/java/com/github/kklisura/cdtp/services/DevToolsService.java renamed to cdtp-java-client/src/main/java/com/github/kklisura/cdtp/services/ChromeDevToolsService.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.github.kklisura.cdtp.services;
22

3-
import com.github.kklisura.cdtp.services.exceptions.ChromeDevToolsException;
3+
import com.github.kklisura.cdtp.protocol.ChromeDevTools;
4+
import com.github.kklisura.cdtp.services.exceptions.ChromeDevToolsInvocationException;
45
import com.github.kklisura.cdtp.services.model.chrome.MethodInvocation;
56

67
/**
7-
* Created by Kenan Klisura on 20/01/2018.
8+
* Chrome dev tools service.
89
*
910
* @author Kenan Klisura
1011
*/
11-
public interface DevToolsService {
12+
public interface ChromeDevToolsService extends ChromeDevTools, AutoCloseable {
1213
/**
1314
* Invokes a dev tools method.
1415
*
@@ -17,6 +18,12 @@ public interface DevToolsService {
1718
* @param methodInvocation Method invocation definition.
1819
* @param <T> Type of a return class.
1920
* @return Return object.
21+
* @throws ChromeDevToolsInvocationException If invocation fails.
2022
*/
21-
<T> T invoke(String returnProperty, Class<T> clazz, MethodInvocation methodInvocation) throws ChromeDevToolsException;
23+
<T> T invoke(String returnProperty, Class<T> clazz, MethodInvocation methodInvocation);
24+
25+
/**
26+
* Closes the dev tools service.
27+
*/
28+
void close();
2229
}

cdtp-java-client/src/main/java/com/github/kklisura/cdtp/services/ChromeService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.github.kklisura.cdtp.services;
22

3-
import com.github.kklisura.cdtp.protocol.ChromeDevTools;
43
import com.github.kklisura.cdtp.services.exceptions.ChromeServiceException;
54
import com.github.kklisura.cdtp.services.model.chrome.ChromeTab;
65
import com.github.kklisura.cdtp.services.model.chrome.ChromeVersion;
@@ -63,10 +62,10 @@ public interface ChromeService {
6362
ChromeVersion getVersion() throws ChromeServiceException;;
6463

6564
/**
66-
* Gets the dev tools session to specified tab.
65+
* Creates a dev tools service to specified tab.
6766
*
6867
* @param tab Tab.
6968
* @return Dev tools.
7069
*/
71-
ChromeDevTools getDevTools(ChromeTab tab) throws ChromeServiceException;
70+
ChromeDevToolsService createDevToolsService(ChromeTab tab) throws ChromeServiceException;
7271
}

cdtp-java-client/src/main/java/com/github/kklisura/cdtp/services/exceptions/ChromeDevToolsException.java

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.github.kklisura.cdtp.services.exceptions;
2+
3+
/**
4+
* Invocation exception.
5+
*
6+
* @author Kenan Klisura
7+
*/
8+
public class ChromeDevToolsInvocationException extends RuntimeException {
9+
private Long code = -1L;
10+
11+
/**
12+
* Instantiates a new Chrome dev tools invocation exception.
13+
*
14+
* @param message Exception message.
15+
*/
16+
public ChromeDevToolsInvocationException(String message) {
17+
super(message);
18+
}
19+
20+
/**
21+
* Instantiates a new Chrome dev tools invocation exception.
22+
*
23+
* @param code Exception code.
24+
* @param message Exception message.
25+
*/
26+
public ChromeDevToolsInvocationException(Long code, String message) {
27+
super(message);
28+
this.code = code;
29+
}
30+
31+
/**
32+
* Instantiates a new Chrome dev tools invocation exception.
33+
*
34+
* @param message Exception message.
35+
* @param cause Exception cause.
36+
*/
37+
public ChromeDevToolsInvocationException(String message, Throwable cause) {
38+
super(message, cause);
39+
}
40+
41+
/**
42+
* Gets code. -1 for 'local' exceptions.
43+
*
44+
* @return the code
45+
*/
46+
public Long getCode() {
47+
return code;
48+
}
49+
}

cdtp-java-client/src/main/java/com/github/kklisura/cdtp/services/impl/DevToolsServiceImpl.java renamed to cdtp-java-client/src/main/java/com/github/kklisura/cdtp/services/impl/ChromeDevToolsServiceImpl.java

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
import com.fasterxml.jackson.annotation.JsonInclude;
44
import com.fasterxml.jackson.databind.JsonNode;
55
import com.fasterxml.jackson.databind.ObjectMapper;
6-
import com.github.kklisura.cdtp.services.DevToolsService;
6+
import com.github.kklisura.cdtp.services.ChromeDevToolsService;
77
import com.github.kklisura.cdtp.services.WebSocketService;
8-
import com.github.kklisura.cdtp.services.exceptions.ChromeDevToolsException;
8+
import com.github.kklisura.cdtp.services.exceptions.ChromeDevToolsInvocationException;
99
import com.github.kklisura.cdtp.services.exceptions.WebSocketServiceException;
10+
import com.github.kklisura.cdtp.services.model.chrome.ChromeTab;
1011
import com.github.kklisura.cdtp.services.model.chrome.MethodInvocation;
1112
import org.slf4j.Logger;
1213
import org.slf4j.LoggerFactory;
1314

1415
import java.io.IOException;
16+
import java.lang.reflect.InvocationHandler;
1517
import java.util.Map;
1618
import java.util.concurrent.ConcurrentHashMap;
1719
import java.util.concurrent.CountDownLatch;
@@ -25,8 +27,8 @@
2527
*
2628
* @author Kenan Klisura
2729
*/
28-
public class DevToolsServiceImpl implements DevToolsService, Consumer<String> {
29-
private static final Logger LOGGER = LoggerFactory.getLogger(DevToolsServiceImpl.class);
30+
public abstract class ChromeDevToolsServiceImpl implements ChromeDevToolsService, Consumer<String>, AutoCloseable {
31+
private static final Logger LOGGER = LoggerFactory.getLogger(ChromeDevToolsServiceImpl.class);
3032

3133
private static final String TIMEOUT_ENV_PROPERTY = "com.github.kklisura.cdtp.websocket.readTimeout";
3234

@@ -49,32 +51,56 @@ public class DevToolsServiceImpl implements DevToolsService, Consumer<String> {
4951

5052
private long readTimeout;
5153

54+
private ChromeTab chromeTab;
55+
private ChromeServiceImpl chromeService;
56+
5257
/**
5358
* Instantiates a new Dev tools service.
5459
*
5560
* @param webSocketService Web socket service.
5661
* @param readTimeout Read timeout in milliseconds.
5762
* @throws WebSocketServiceException Web socket service exception.
5863
*/
59-
public DevToolsServiceImpl(WebSocketService webSocketService, long readTimeout) throws WebSocketServiceException {
64+
public ChromeDevToolsServiceImpl(WebSocketService webSocketService, long readTimeout)
65+
throws WebSocketServiceException {
6066
this.webSocketService = webSocketService;
6167
this.webSocketService.addMessageHandler(this);
6268
this.readTimeout = readTimeout;
6369
}
6470

6571
/**
66-
* Instantiates a new Dev tools service.
72+
* Instantiates a new Chrome dev tools service. This is used during proxy building phase.
73+
*
74+
* See {@link ChromeServiceImpl#createDevToolsService(ChromeTab)}
75+
* See {@link com.github.kklisura.cdtp.services.utils.ProxyUtils#createProxyFromAbstract(Class, Class[], Object[], InvocationHandler)}
6776
*
6877
* @param webSocketService Web socket service.
69-
* @throws WebSocketServiceException Web socket service exception.
78+
* @throws WebSocketServiceException Web socket service exception
7079
*/
71-
public DevToolsServiceImpl(WebSocketService webSocketService) throws WebSocketServiceException {
80+
public ChromeDevToolsServiceImpl(WebSocketService webSocketService) throws WebSocketServiceException {
7281
this(webSocketService, READ_TIMEOUT);
7382
}
7483

84+
/**
85+
* Sets the chrome service container.
86+
*
87+
* @param chromeService Chrome service.
88+
*/
89+
public void setChromeService(ChromeServiceImpl chromeService) {
90+
this.chromeService = chromeService;
91+
}
92+
93+
/**
94+
* Sets the chrome tab for this service.
95+
*
96+
* @param chromeTab the tab
97+
*/
98+
public void setChromeTab(ChromeTab chromeTab) {
99+
this.chromeTab = chromeTab;
100+
}
101+
75102
@Override
76-
public <T> T invoke(String returnProperty, Class<T> clazz, MethodInvocation methodInvocation)
77-
throws ChromeDevToolsException {
103+
public <T> T invoke(String returnProperty, Class<T> clazz, MethodInvocation methodInvocation) {
78104
try {
79105
InvocationResult invocationResult = new InvocationResult(returnProperty);
80106
invocationResultMap.put(methodInvocation.getId(), invocationResult);
@@ -85,7 +111,7 @@ public <T> T invoke(String returnProperty, Class<T> clazz, MethodInvocation meth
85111
invocationResultMap.remove(methodInvocation.getId());
86112

87113
if (!hasReceivedResponse) {
88-
throw new ChromeDevToolsException("Timeout expired while waiting for server response.");
114+
throw new ChromeDevToolsInvocationException("Timeout expired while waiting for server response.");
89115
}
90116

91117
if (invocationResult.isSuccess()) {
@@ -102,14 +128,23 @@ public <T> T invoke(String returnProperty, Class<T> clazz, MethodInvocation meth
102128
errorMessageBuilder.append(error.getData());
103129
}
104130

105-
throw new ChromeDevToolsException(error.getCode(), errorMessageBuilder.toString());
131+
throw new ChromeDevToolsInvocationException(error.getCode(), errorMessageBuilder.toString());
106132
}
107-
} catch (WebSocketServiceException ex) {
108-
throw new ChromeDevToolsException("Failed sending web socket message.", ex);
109-
} catch (InterruptedException ex) {
110-
throw new ChromeDevToolsException("Interrupted while waiting response.", ex);
133+
} catch (WebSocketServiceException e) {
134+
throw new ChromeDevToolsInvocationException("Failed sending web socket message.", e);
135+
} catch (InterruptedException e) {
136+
throw new ChromeDevToolsInvocationException("Interrupted while waiting response.", e);
111137
} catch (IOException ex) {
112-
throw new ChromeDevToolsException("Failed reading response message.", ex);
138+
throw new ChromeDevToolsInvocationException("Failed reading response message.", ex);
139+
}
140+
}
141+
142+
@Override
143+
public void close() {
144+
webSocketService.close();
145+
146+
if (chromeService != null) {
147+
chromeService.clearChromeDevToolsServiceCache(chromeTab);
113148
}
114149
}
115150

@@ -158,6 +193,10 @@ public void accept(String message) {
158193
}
159194

160195
private <T> T readJsonObject(Class<T> clazz, JsonNode jsonNode) throws IOException {
196+
if (jsonNode == null) {
197+
throw new ChromeDevToolsInvocationException("Failed converting null response to clazz " + clazz.getName());
198+
}
199+
161200
return OBJECT_MAPPER.readerFor(clazz).readValue(jsonNode);
162201
}
163202

0 commit comments

Comments
 (0)