Skip to content

Commit a0c0527

Browse files
Merge pull request appium#266 from TikhomirovSergey/appium#264-fix
appium#264 fix
2 parents b103b0a + bd6f6c8 commit a0c0527

3 files changed

Lines changed: 128 additions & 1 deletion

File tree

src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@
2525

2626
import java.io.File;
2727
import java.io.IOException;
28+
import java.io.OutputStream;
2829
import java.net.MalformedURLException;
2930
import java.net.URL;
31+
import java.util.List;
3032
import java.util.concurrent.TimeUnit;
3133
import java.util.concurrent.locks.ReentrantLock;
3234

35+
import static com.google.common.base.Preconditions.checkNotNull;
36+
3337
public final class AppiumDriverLocalService extends DriverService {
3438

3539
private static final String URL_MASK = "http://%s:%d/wd/hub";
@@ -41,6 +45,7 @@ public final class AppiumDriverLocalService extends DriverService {
4145
private final long startupTimeout;
4246
private final TimeUnit timeUnit;
4347
private final ReentrantLock lock = new ReentrantLock();
48+
private final ListOutputStream stream = new ListOutputStream().add(System.out);
4449

4550

4651

@@ -114,7 +119,7 @@ public void start() throws AppiumServerHasNotBeenStartedLocallyException {
114119
try {
115120
process = new CommandLine(this.nodeJSExec.getCanonicalPath(), nodeJSArgs.toArray(new String[] {}));
116121
process.setEnvironmentVariables(nodeJSEnvironment);
117-
process.copyOutputTo(System.err);
122+
process.copyOutputTo(stream);
118123
process.executeAsync();
119124
ping(startupTimeout, timeUnit);
120125
} catch (Throwable e) {
@@ -162,6 +167,18 @@ public String getStdOut() {
162167
return null;
163168
}
164169

170+
public void addOutPutStream(OutputStream outputStream){
171+
checkNotNull(outputStream, "outputStream parameter is NULL!");
172+
stream.add(outputStream);
173+
}
174+
175+
public void addOutPutStreams(List<OutputStream> outputStreams){
176+
checkNotNull(outputStreams, "outputStreams parameter is NULL!");
177+
for (OutputStream stream: outputStreams){
178+
addOutPutStream(stream);
179+
}
180+
}
181+
165182
public static AppiumDriverLocalService buildDefaultService(){
166183
return buildService(new AppiumServiceBuilder());
167184
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.service.local;
18+
19+
20+
import java.io.IOException;
21+
import java.io.OutputStream;
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
25+
class ListOutputStream extends OutputStream {
26+
27+
private final List<OutputStream> streams = new ArrayList<>();
28+
29+
ListOutputStream add(OutputStream stream) {
30+
streams.add(stream);
31+
return this;
32+
}
33+
34+
@Override
35+
public void write(int i) throws IOException {
36+
for (OutputStream stream: streams) {
37+
stream.write(i);
38+
}
39+
}
40+
41+
@Override
42+
public void write(byte[] var1) throws IOException {
43+
for (OutputStream stream: streams) {
44+
stream.write(var1, 0, var1.length);
45+
}
46+
}
47+
48+
@Override
49+
public void write(byte[] var1, int var2, int var3) throws IOException {
50+
for (OutputStream stream: streams) {
51+
stream.write(var1, var2, var3);
52+
}
53+
}
54+
55+
public void flush() throws IOException {
56+
for (OutputStream stream: streams) {
57+
stream.flush();
58+
}
59+
}
60+
61+
public void close() throws IOException {
62+
for (OutputStream stream: streams) {
63+
stream.close();
64+
}
65+
}
66+
}

src/test/java/io/appium/java_client/localserver/ServerBuilderTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.*;
2727
import java.util.Properties;
2828

29+
import static junit.framework.Assert.assertTrue;
2930
import static org.junit.Assert.assertEquals;
3031

3132
public class ServerBuilderTest {
@@ -135,4 +136,47 @@ public void checkStartingOfTheServiceDefinedExternally(){
135136
assertEquals(true, service.isRunning());
136137
service.stop();
137138
}
139+
140+
@Test
141+
public void checkAbilityToChangeOutputStream() throws Exception{
142+
File file = new File("target/test");
143+
file.createNewFile();
144+
OutputStream stream = new FileOutputStream(file);
145+
AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService();
146+
service.addOutPutStream(stream);
147+
try {
148+
service.start();
149+
assertTrue(file.length() > 0);
150+
}
151+
finally {
152+
service.stop();
153+
if (stream != null)
154+
stream.close();
155+
156+
if (file.exists())
157+
file.delete();
158+
}
159+
}
160+
161+
@Test
162+
public void checkAbilityToChangeOutputStreamAfterTheServiceIsStarted() throws Exception{
163+
File file = new File("target/test");
164+
file.createNewFile();
165+
OutputStream stream = new FileOutputStream(file);
166+
AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService();
167+
try {
168+
service.start();
169+
service.addOutPutStream(stream);
170+
service.isRunning();
171+
assertTrue(file.length() > 0);
172+
}
173+
finally {
174+
service.stop();
175+
if (stream != null)
176+
stream.close();
177+
178+
if (file.exists())
179+
file.delete();
180+
}
181+
}
138182
}

0 commit comments

Comments
 (0)