Skip to content

Instantly share code, notes, and snippets.

@forketyfork
forketyfork / ProblemTest.java
Created April 21, 2020 06:36
JUnit 5 with Records - parameterized data source
public static Stream<TestCase> source() {
return Stream.of(
new TestCase(5, new int[0], false),
new TestCase(5, new int[] {5}, false),
new TestCase(3, new int[] {1, 4, 2}, true),
new TestCase(3, new int[] {1, 2}, true),
new TestCase(3, new int[] {1, 4, 5, 8, 12}, false),
new TestCase(13, new int[] {1, 4, 5, 8, 12}, true),
new TestCase(12, new int[] {1, 4, 5, 8, 12}, true)
);
@forketyfork
forketyfork / ProblemTest.java
Created April 21, 2020 06:35
JUnit 5 with Records - Record
public class Problem1Test {
private static record TestCase(int target, int[] array, boolean expected) {
}
}
@forketyfork
forketyfork / Problem.java
Created April 21, 2020 06:27
JUnit 5 with Records - class under test
/**
* Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
* <p>
* For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
* <p>
* Bonus: Can you do this in one pass?
*/
public class Problem {
public boolean check(int target, int[] array) {
@forketyfork
forketyfork / pom.xml
Created April 21, 2020 06:24
JUnit 5 with Records - build setup
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>14</source>
<target>14</target>
@forketyfork
forketyfork / pom.xml
Created April 21, 2020 06:20
JUnit 5 with Records - Maven setup
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
@forketyfork
forketyfork / XaRoute.java
Created February 27, 2020 19:34
XA with Spring Boot: XaRoute
@Component
public class XaRoute extends RouteBuilder {
@Override
public void configure() {
from("activemq:TestQueue")
.transacted("policyPropagationRequired")
.to("sql:insert into message(contents) values(:#${body})")
.log("Delay...")
.delay(10000)
@forketyfork
forketyfork / TransactionConfig.java
Created February 27, 2020 19:30
XA with Spring Boot: TransactionConfig
@Configuration
public class TransactionConfig {
@Bean("policyPropagationRequired")
public SpringTransactionPolicy transactionPolicyPropagationRequired(
@Autowired JtaTransactionManager transactionManager) {
SpringTransactionPolicy policy = new SpringTransactionPolicy(transactionManager);
policy.setPropagationBehaviorName("PROPAGATION_REQUIRED");
return policy;
}
@forketyfork
forketyfork / ActiveMQConfig.java
Created February 27, 2020 07:05
XA with Spring Boot: ActiveMQ config
@Configuration
public class ActiveMQConfig {
@Bean("activemq")
public ActiveMQComponent activeMq(
ConnectionFactory connectionFactory,
JtaTransactionManager jtaTransactionManager) {
ActiveMQComponent component = new ActiveMQComponent();
component.setAcknowledgementMode(JmsProperties.AcknowledgeMode.CLIENT.getMode());
component.setCacheLevelName("CACHE_CONSUMER");
@forketyfork
forketyfork / schema.sql
Created February 27, 2020 07:01
XA with Spring Boot
drop table if exists message;
create table message (
id serial primary key,
contents varchar(256) not null
);
@forketyfork
forketyfork / application.yml
Created February 27, 2020 06:55
XA with Spring Boot: application.yml
spring:
activemq:
# we'll use an external ActiveMQ broker
broker-url: tcp://localhost:61616
datasource:
# we'll use an external PostgreSQL database
url: jdbc:postgresql://localhost:5432/xatest
xa:
# this is needed to make sure the PostgreSQL data source is XA-aware
data-source-class-name: org.postgresql.xa.PGXADataSource