Skip to content

Instantly share code, notes, and snippets.

View moelholm's full-sized avatar
:bowtie:

Nicky Moelholm moelholm

:bowtie:
View GitHub Profile
@moelholm
moelholm / assertEventualCollectionCondition.java
Created September 22, 2017 08:02
assertEventualCollectionCondition ... awaiting some future condition
// >>> How to use
assertEventualCollectionCondition(
() -> em.createQuery("from Dog").getResultList(),
Collection::isEmpty);
// >>>
private <T> void assertEventualCollectionCondition(Supplier<Collection<T>> supplier, Predicate<Collection<T>> test) throws Exception {
assertEventualCollectionCondition(30, TimeUnit.SECONDS, supplier, test);
@moelholm
moelholm / unwrap.java
Created September 20, 2017 20:37
Unwrapping a Spring Bean from its proxy (if any)
/**
* Gets a bean from the Spring context - stripping the proxy (if any).
*
* @param clazz type of the bean to find
* @return the bean (not a proxy)
*/
protected <T> T getBeanWithoutProxy(Class<T> clazz) {
T bean = applicationContext.getBean(clazz);
if (AopUtils.isAopProxy(bean) && bean instanceof Advised) {
try {
@moelholm
moelholm / Sample.java
Created September 12, 2017 15:02
Converting a string into Windows-1252 preserving as much information as possible
private static final Charset WINDOWS_1252_CHARSET = Charset.forName("Windows-1252");
// Can below be done more effectively (/robust/secure) ?
@Test
public void test() {
// Given
String inputString = "ĢoogleŰberButÅ";
@moelholm
moelholm / Misc - other details
Created August 3, 2017 17:37
Recipe for: Elastic Search log appender with Logback (from a Spring Boot application)
logback-core 1.1.7
logback-classic 1.1.7
Spring Boot cfg...:
# Log configuration file
logging.config=classpath:logback-for-server.xml
@RunWith(WildFlyEmbeddedArquillianRunner.class)
public class MultiDeploymentIntegrationTest {
@Deployment(name = "myapp", order = 1)
public static EnterpriseArchive createEar() {
return ShrinkWrap.create(EnterpriseArchive.class, "myapp.ear")
.addAsModule(
ShrinkWrap.create(JavaArchive.class, "myejbmodule.jar")
.addClass(BusinessServiceBean.class)
.addClass(AnotherBusinessServiceBean.class)
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.reactive.WebClient;
import reactor.core.publisher.Mono;
import static org.springframework.web.client.reactive.ClientWebRequestBuilders.get;
import static org.springframework.web.client.reactive.ResponseExtractors.body;
class Hello {
public static void main(String[] args) {
}
}
@moelholm
moelholm / OffsetDateTimeConverter.java
Last active November 5, 2016 12:10
Failed attempt: JPA attribute converter to "force" dates into UTC when writing to the database (and reading again)
/**
* Failed experiment: Attempting to force datetimes to be written/loaded using the UTC time zone.
*
*
* Good: OffsetDateTime is successfully converted into a "normal" Temporal data type ( here Calendar ) - resulting in Timestamp DDL etc.
*
* Bad : Datetimes are still stored using the JVM default time zone. ( In my part of the world: UTC+1 right now )
*
* The working solution I can come up with right now is setting the entire JVM into the UTC time zone. That surely does the job: Datetimes
* in the database show up as UTC versions.
@moelholm
moelholm / About
Last active October 13, 2016 16:31
Spring Framework needs @Nonbinding like support
This example shows why we could use @Nonbinding like support in Qualifier annotations (as known from CDI)
@moelholm
moelholm / GreeterService.java
Created October 10, 2016 18:13
Spring Framework needs @Nonbinding like support
package com.moelholm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class GreeterService {
@Autowired
@Uppercase("hElLo WoRlD")