Skip to content

Instantly share code, notes, and snippets.

View reyabreu's full-sized avatar
🏠
Working from home

Reynaldo Jose Abreu reyabreu

🏠
Working from home
View GitHub Profile
@reyabreu
reyabreu / TestUtils.java
Created December 9, 2019 15:51
A utility class that provides a method for finding a random freely available TCP/IP port. Mostly used in integration tests.
public class TestUtils {
// an AutoCloseable bean is needed to guarantee the found port is freed before we can use it
private static class SocketHolder implements Closeable {
private final ServerSocket socket;
public SocketHolder(ServerSocket socket) {
this.socket = socket;
}
@reyabreu
reyabreu / TestFooRequest.java
Last active December 3, 2019 10:43
Ensure properly created ObjectMapper is used in Retrofit's JacksonConverterFactory to serialise embedded Java Optionals in request body
@RunWith(SpringRunner.class)
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class })
@SpringBootTest(
classes = { TestFooRequest.FooController.class },
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)
@AutoConfigureMockMvc(secure = false)
public class TestFooRequest {
@reyabreu
reyabreu / UnitTest.java
Created November 8, 2019 18:22
Registering Spring Boot's default conversion service in unit tests (official docs are wrong!)
@Configuration
static class ConversionServiceConfiguration implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
beanFactory.setConversionService(ApplicationConversionService.getSharedInstance());
}
}
@reyabreu
reyabreu / CustomProperties.java
Created October 8, 2019 12:45
Binding Spring Boot 2+ environment properties to a Map of entries
@ConfigurationProperties("")
public class CustomProperties {
private Map<String, Object> mongodb = new HashMap<>();
public Map<String, Object> getMongodb() {
return ImmutableMap.copyOf(mongodb);
}
public void setMongodb(Map<String, Object> mongodb) {
@reyabreu
reyabreu / SpelUnitTest.java
Created September 11, 2019 11:00
How to unit test Spel (Spring Expression Language) expressions against standard web context
@RunWith(SpringRunner.class)
@TestPropertySource("classpath:server-test.properties")
public class SpelUnitTest {
public static final Logger logger = LoggerFactory.getLogger(Sample.class);
@Value("${testclock.start}")
private String startTime;
@Autowired
@reyabreu
reyabreu / TestFormatStringIndexer.Java
Created November 29, 2018 15:49
Unit tests for the format String indexer
import org.junit.Test;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class TestFormatStringIndexer {
@Test
public void index_simpleSpecifiers_ok() {
final String result = FormatStringIndexer.index("there are %d apples in the %s basket that cost %f", 1);
@reyabreu
reyabreu / FormatStringIndexer.java
Created November 29, 2018 15:47
Adds format index specifiers to format strings that don't have them
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//helper class that adds format index specifiers to plain specifiers in format strings
public class FormatStringIndexer {
private static Pattern pattern = Pattern.compile("%[-#+\\s0,(]?\\d*(\\.\\d)?[bBhHsScCdDoxXeEfgGaAtT%n]");
public static String index(final String formatString, int start) {
@reyabreu
reyabreu / TestFibonacciStateMachine.java
Created November 27, 2018 12:11
Implemented a Fibonacci sequence generator using a state machine design pattern - just to highlight how individual states should manage their own logic and transitions and not depend on an external controlling context.
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@reyabreu
reyabreu / featurebranchworkflow.md
Created September 28, 2018 14:12
Feature branch workflow on GitLab

Clone project:

git clone git@example.com:project-name.git

Create branch with your feature:

git checkout -b $feature_name

Write code. Commit changes:

git commit -am "My feature is ready"

Push your branch to GitLab:

@reyabreu
reyabreu / gist:08f181dd86a0a6ac32e28e71816ce8af
Created September 28, 2018 13:54
Delete tag from local & remote branches
# delete local tag 'mytag'
git tag -d mytag
# delete remote tag 'mytag' (e.g. GitHub version too)
git push origin :refs/tags/mytag
# alternative approach
git push --delete origin mytag
git tag -d mytag