Skip to content

Instantly share code, notes, and snippets.

View eranharel's full-sized avatar
🚵‍♂️
Typing...

Eran Harel eranharel

🚵‍♂️
Typing...
View GitHub Profile
@eranharel
eranharel / Calculator
Created May 25, 2011 07:14
Calculator class for unit test tutorial
public class Calculator {
public int add(final int x, final int y) {
if (x < 0 || y < 0) {
throw new IllegalArgumentException("arguments must be positive");
}
return x + y;
}
public int substract(final int x, final int y) {
@eranharel
eranharel / CalculatorTest
Created May 25, 2011 07:21
A calculator class example Junit 4 test case
public class CalculatorTest {
private Calculator classUnderTest;
@Before
public void setUp() throws Exception {
this.classUnderTest = new Calculator();
}
@After
@eranharel
eranharel / EmployeeServiceSingleton
Created May 25, 2011 12:22
Don't copy this implementation please :P
public class EmployeeServiceSingleton {
private static final EmployeeServiceSingleton instance = new EmployeeServiceSingleton();
private final EmployeeDao employeeDao;
private EmployeeServiceSingleton() {
employeeDao = ServiceLocator.getEmployeeDao();
}
public class EmployeeService {
private final EmployeeDao employeeDao;
public EmployeeService(final EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}
public String findManagerName(final Long employeeId) {
if (null == employeeId) {
@RunWith(MockitoJUnitRunner.class)
public class EmployeeServiceTest {
@Mock
private EmployeeDao daoMock;
@Test
public void testFindManagerName() throws Exception {
final Long employeeId = 666L;
final String expectedManagerName = "Eran Harel";
@eranharel
eranharel / מחלקה_סגורה
Created June 9, 2011 09:07
Hebrew unicode Java class
/**
* המחלקה העברית הראשונה
* @author Eran Harel
*/
public final class מחלקה_סגורה {
int שלם = 1;
// בנאי
public מחלקה_סגורה() {
@eranharel
eranharel / ApplicationContext.xml
Created July 2, 2011 13:01
feature flags made easy
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="properties">
<map>
<entry key="imaginaryFeature.implementation.bean" value="oldImaginaryFeature"/>
@eranharel
eranharel / gist:4545510
Last active December 11, 2015 04:29
Exception handling sadness :P
} catch (final Throwable e) {
_logger.error("Failed to init blah", e);
try {
throw e;
} catch (final Throwable e1) {
_logger.error("Failed to throw throwable excpetion", e1);
}
}
@eranharel
eranharel / gist:4546234
Created January 16, 2013 10:34
Yet another Throwable catching evilness
} catch (final Throwable e) {
throw new MyException("Failed to blah", (Exception) e);
}
@eranharel
eranharel / gist:4556258
Created January 17, 2013 14:24
try catch anti-patterns continued
try {
...
try {
...
} catch (final Throwable t) {
logger.error("...", t);
throw new RuntimeException(msg, t);
}
} catch (final Throwable t) {
logger.error("..", t);