Skip to content

Commit

Permalink
Remove duplicated code by introducing Executor#tryExec
Browse files Browse the repository at this point in the history
  • Loading branch information
jcgay committed Aug 15, 2017
1 parent 92a4b92 commit 6b1faaf
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

public interface Executor {
Process exec(String[] command);
boolean tryExec(String[] command);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ public Process exec(String[] command) {
throw Throwables.propagate(e);
}
}

@Override
public boolean tryExec(String[] command) {
try {
return exec(command).waitFor() == 0;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
} catch (RuntimeException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,7 @@ public boolean tryInit() {
commands.add(configuration.bin());
commands.add("-v");

try {
return executor.exec(commands.toArray(new String[commands.size()])).waitFor() == 0;
} catch (RuntimeException e) {
return false;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
return executor.tryExec(commands.toArray(new String[commands.size()]));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,7 @@ public boolean tryInit() {
commands.add(configuration.bin());
commands.add("-help");

try {
return executor.exec(commands.toArray(new String[commands.size()])).waitFor() == 0;
} catch (RuntimeException e) {
return false;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
return executor.tryExec(commands.toArray(new String[commands.size()]));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,7 @@ public boolean tryInit() {
commands.add(configuration.bin());
commands.add("/v");

try {
return executor.exec(commands.toArray(new String[commands.size()])).waitFor() == 0;
} catch (RuntimeException e) {
return false;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
return executor.tryExec(commands.toArray(new String[commands.size()]));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,7 @@ public boolean tryInit() {
commands.add(configuration.bin());
commands.add("-v");

try {
return executor.exec(commands.toArray(new String[commands.size()])).waitFor() == 0;
} catch (RuntimeException e) {
return false;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
return executor.tryExec(commands.toArray(new String[commands.size()]));
}

private static String toUrgency(Notification.Level level) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,7 @@ public boolean tryInit() {
List<String> commands = new ArrayList<String>();
commands.add(configuration.bin());

try {
return executor.exec(commands.toArray(new String[commands.size()])).waitFor() == 0;
} catch (RuntimeException e) {
return false;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
return executor.tryExec(commands.toArray(new String[commands.size()]));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@ class KdialogNotifierSpec extends Specification {
Application application = Application.builder('id', 'name', TestIcon.ok()).build()

List<String> executedCommand
Executor executor = { String[] command -> executedCommand = command; Stub(Process) }
Executor executor = new Executor() {
@Override
Process exec(String[] command) {
executedCommand = command
null
}

@Override
boolean tryExec(String[] command) {
throw new IllegalStateException("This method should not be called!")
}
}

def "should build command line to call kdialog"() {
given:
Expand Down Expand Up @@ -61,7 +72,7 @@ class KdialogNotifierSpec extends Specification {

then:
result
1 * executor.exec([KdialogConfiguration.byDefault().bin(), '-v']) >> Stub(Process) { waitFor() >> 0 }
1 * executor.tryExec([KdialogConfiguration.byDefault().bin(), '-v']) >> true
}

def "should return false if binary is not available"() {
Expand All @@ -76,6 +87,6 @@ class KdialogNotifierSpec extends Specification {

then:
!result
1 * executor.exec([KdialogConfiguration.byDefault().bin(), '-v']) >> Stub(Process) { waitFor() >> 127 }
1 * executor.tryExec([KdialogConfiguration.byDefault().bin(), '-v']) >> false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@ import spock.lang.Subject
class SimpleNotificationCenterSpec extends Specification {

List<String> executedCommand
Executor executor = { String[] command -> executedCommand = command; Stub(Process) }
Executor executor = new Executor() {
@Override
Process exec(String[] command) {
executedCommand = command
null
}

@Override
boolean tryExec(String[] command) {
throw new IllegalStateException("This method should not be called!")
}
}

@Subject
SimpleNotificationCenterNotifier notifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ class TerminalNotifierSpec extends Specification {
String temp = System.getProperty('java.io.tmpdir')

List<String> executedCommand
Executor executor = { String[] command -> executedCommand = command; Stub(Process) }
Executor executor = new Executor() {
@Override
Process exec(String[] command) {
executedCommand = command
null
}

@Override
boolean tryExec(String[] command) {
throw new IllegalStateException("This method should not be called!")
}
}

@Subject
TerminalNotifier notifier
Expand Down Expand Up @@ -85,7 +96,7 @@ class TerminalNotifierSpec extends Specification {

then:
result
1 * executor.exec([TerminalNotifierConfiguration.byDefault().bin(), '-help']) >> Stub(Process) { waitFor() >> 0 }
1 * executor.tryExec([TerminalNotifierConfiguration.byDefault().bin(), '-help']) >> true
}

def "should return false when binary is not available"() {
Expand All @@ -100,6 +111,6 @@ class TerminalNotifierSpec extends Specification {

then:
!result
1 * executor.exec([TerminalNotifierConfiguration.byDefault().bin(), '-help']) >> Stub(Process) { waitFor() >> 127 }
1 * executor.tryExec([TerminalNotifierConfiguration.byDefault().bin(), '-help']) >> false
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package fr.jcgay.notification.notifier.notifu

import fr.jcgay.notification.Application
import fr.jcgay.notification.Notification
import fr.jcgay.notification.TestIcon
import fr.jcgay.notification.notifier.executor.Executor
import fr.jcgay.notification.notifier.executor.RuntimeExecutor
import spock.lang.Specification
import spock.lang.Subject
Expand All @@ -20,7 +22,18 @@ class NotifuNotifierSpec extends Specification {
NotifuNotifier notifier

def setup() {
notifier = new NotifuNotifier(application, NotifuConfiguration.byDefault(), { String[] command -> executedCommand = command; Stub(Process) })
notifier = new NotifuNotifier(application, NotifuConfiguration.byDefault(), new Executor() {
@Override
Process exec(String[] command) {
executedCommand = command
null
}

@Override
boolean tryExec(String[] command) {
throw new IllegalStateException("This method should not be called!")
}
})
}

def "should build command line to call notifu"() {
Expand Down Expand Up @@ -84,7 +97,7 @@ class NotifuNotifierSpec extends Specification {

then:
result
1 * executor.exec([NotifuConfiguration.byDefault().bin(), "/v"]) >> Stub(Process) { waitFor() >> 0 }
1 * executor.tryExec([NotifuConfiguration.byDefault().bin(), "/v"]) >> true
}

def "should return false when binary is available"() {
Expand All @@ -99,7 +112,7 @@ class NotifuNotifierSpec extends Specification {

then:
!result
1 * executor.exec([NotifuConfiguration.byDefault().bin(), "/v"]) >> Stub(Process) { waitFor() >> 127 }
1 * executor.tryExec([NotifuConfiguration.byDefault().bin(), "/v"]) >> false
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@ class NotifySendNotifierSpec extends Specification {
Application application
Notification notification

Executor executor = { String[] command -> executedCommand = command; Stub(Process) }
List<String> executedCommand
Executor executor = new Executor() {
@Override
Process exec(String[] command) {
executedCommand = command
null
}

@Override
boolean tryExec(String[] command) {
throw new IllegalStateException("This method should not be called!")
}
}

@Subject
NotifySendNotifier notifier
Expand Down Expand Up @@ -82,7 +93,7 @@ class NotifySendNotifierSpec extends Specification {

then:
!result
1 * executor.exec([NotifySendConfiguration.byDefault().bin(), '-v']) >> Stub(Process) { waitFor() >> 127 }
1 * executor.tryExec([NotifySendConfiguration.byDefault().bin(), '-v']) >> false
}

def "should return true when binary is available"() {
Expand All @@ -97,7 +108,7 @@ class NotifySendNotifierSpec extends Specification {

then:
result
1 * executor.exec([NotifySendConfiguration.byDefault().bin(), '-v']) >> Stub(Process) { waitFor() >> 0 }
1 * executor.tryExec([NotifySendConfiguration.byDefault().bin(), '-v']) >> true
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@ package fr.jcgay.notification.notifier.toaster

import fr.jcgay.notification.Notification
import fr.jcgay.notification.TestIcon
import fr.jcgay.notification.notifier.executor.Executor
import fr.jcgay.notification.notifier.executor.RuntimeExecutor
import spock.lang.Specification


class ToasterNotifierSpec extends Specification {

List<String> executedCommand
ToasterNotifier notifier = new ToasterNotifier(ToasterConfiguration.byDefault(), { String[] command -> executedCommand = command; Stub(Process) })
ToasterNotifier notifier = new ToasterNotifier(ToasterConfiguration.byDefault(), new Executor() {
@Override
Process exec(String[] command) {
executedCommand = command
null
}

@Override
boolean tryExec(String[] command) {
throw new IllegalStateException("This method should not be called!")
}
})
Notification notification = Notification.builder('title', 'message', TestIcon.ok()).build()

def "should build command line to call notify-send"() {
Expand Down Expand Up @@ -40,7 +52,7 @@ class ToasterNotifierSpec extends Specification {

then:
!result
1 * executor.exec([ToasterConfiguration.byDefault().bin()]) >> Stub(Process) { waitFor() >> 127 }
1 * executor.tryExec([ToasterConfiguration.byDefault().bin()]) >> false
}

def "should return true when binary is not available"() {
Expand All @@ -55,6 +67,6 @@ class ToasterNotifierSpec extends Specification {

then:
result
1 * executor.exec([ToasterConfiguration.byDefault().bin()]) >> Stub(Process) { waitFor() >> 0 }
1 * executor.tryExec([ToasterConfiguration.byDefault().bin()]) >> true
}
}

0 comments on commit 6b1faaf

Please sign in to comment.