Skip to content

Instantly share code, notes, and snippets.

@irof
Forked from mike-neck/ExecutionPatterns.java
Last active December 10, 2015 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irof/4397706 to your computer and use it in GitHub Desktop.
Save irof/4397706 to your computer and use it in GitHub Desktop.
package org.mikeneck.twr.api;
import org.mikeneck.twr.exception.*;
/**
* @author : mike
* @since : 12/12/26
*/
public enum ExecutionPatterns {
ON_CONSTRUCTOR {
@Override
protected void work(Object by) throws ResourceException {
throw new ConstructorException(by);
}
@Override
public void construction(Object by) throws ConstructorException {
try {
this.work(by);
} catch (ResourceException e) {
throw ConstructorException.class.cast(e);
}
}
}, ON_OPEN {
@Override
protected void work(Object by) throws ResourceException {
throw new OpenException(by);
}
@Override
public void open(Object by) throws OpenException {
try {
this.work(by);
} catch (ResourceException e) {
throw OpenException.class.cast(e);
}
}
}, ON_EXECUTION {
@Override
protected void work(Object by) throws ResourceException {
throw new OperationalException(by);
}
@Override
public void execute(Object by) throws OperationalException {
try {
this.work(by);
} catch (ResourceException e) {
OperationalException.class.cast(e);
}
}
}, ON_CLOSE {
@Override
protected void work(Object by) throws ResourceException {
throw new CloseException(by);
}
@Override
public void close(Object by) throws CloseException {
try {
work(by);
} catch (ResourceException e) {
CloseException.class.cast(e);
}
}
};
public void construction(Object by) throws ConstructorException {}
public void open(Object by) throws OpenException {}
public void execute(Object by) throws OperationalException {}
public void close(Object by) throws CloseException {}
abstract protected void work (Object by) throws ResourceException;
}
package org.mikeneck.twr.api
import org.mikeneck.twr.api.util.MockOperator
import org.mikeneck.twr.exception.CloseException
import org.mikeneck.twr.exception.ConstructorException
import org.mikeneck.twr.exception.OpenException
import org.mikeneck.twr.exception.OperationalException
import spock.lang.Specification
import static org.mikeneck.twr.api.ExecutionPatterns.*
/**
* User: mike
* Date: 2012/12/28
*/
class ExecutionPatternsTestByGroovy extends Specification {
def "Exceptions thrown validly" () {
when :
pattern.work(new MockOperator(MockOperator, pattern))
then :
def e = thrown(exception)
assert e.class == exception
where :
pattern | exception
ON_CONSTRUCTOR | ConstructorException
ON_OPEN | OpenException
ON_EXECUTION | OperationalException
ON_CLOSE | CloseException
}
}
package org.mikeneck.twr.api
import org.mikeneck.twr.exception.CloseException
import org.mikeneck.twr.exception.ConstructorException
import org.mikeneck.twr.exception.OpenException
import org.mikeneck.twr.exception.OperationalException
import spock.lang.Specification
import static org.mikeneck.twr.api.ExecutionPatterns.*
/**
* User: mike
* Date: 2012/12/28
*/
class ExecutionPatternsTestByGroovy_OLD extends Specification {
def "Exceptions thrown validly" () {
setup :
def operator = new MockOperator(MockOperator, pattern)
def ex = new ResourceException()
try {
pattern.work(operator)
} catch (ResourceException e) {
ex = e
}
expect :
assert ex.class == exception
where :
pattern | exception
ON_CONSTRUCTOR | ConstructorException
ON_OPEN | OpenException
ON_EXECUTION | OperationalException
ON_CLOSE | CloseException
}
}
package org.mikeneck.twr.api;
import org.junit.Rule;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mikeneck.twr.api.util.MockOperator;
import org.mikeneck.twr.exception.CloseException;
import org.mikeneck.twr.exception.ConstructorException;
import org.mikeneck.twr.exception.OpenException;
import org.mikeneck.twr.exception.OperationalException;
import org.mikeneck.twr.exception.ResourceException;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
/**
* @author mike
*/
@RunWith(Theories.class)
public class ExecutionPatternsTestByJava {
@Rule
public ExpectedException ex = ExpectedException.none();
@DataPoints
public static Regulations[] regulations() {
Data data = new Data();
data.when(ExecutionPatterns.ON_CONSTRUCTOR).then(ConstructorException.class);
data.when(ExecutionPatterns.ON_OPEN).then(OpenException.class);
data.when(ExecutionPatterns.ON_EXECUTION).then(OperationalException.class);
data.when(ExecutionPatterns.ON_CLOSE).then(CloseException.class);
return data.toArray();
}
@Theory
public void exceptionThrownValidly(Regulations regulation) throws Exception {
ex.expect(is(instanceOf(regulation.expected)));
ExecutionPatterns ptn = regulation.executePattern;
Operator operator = new MockOperator(MockOperator.class, ptn);
ptn.work(operator);
}
private static class Data {
List<Regulations> list = new ArrayList<>();
ExecutionPatterns executePattern;
Data when(ExecutionPatterns executePattern) {
this.executePattern = executePattern;
return this;
}
void then(Class<? extends ResourceException> expected) {
Regulations regulations = new Regulations();
regulations.executePattern = executePattern;
regulations.expected = expected;
list.add(regulations);
}
Regulations[] toArray() {
return list.toArray(new Regulations[list.size()]);
}
}
private static class Regulations {
Class<? extends ResourceException> expected;
ExecutionPatterns executePattern;
}
}
@mike-neck
Copy link

なるほど、これは読みやすい!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment