Strony

2016-12-30

Convert public certificate to Base64

String base64Encoded = Base64.encodeToString(pubkey.getEncoded(), Base64.DEFAULT).
    replaceAll("\n", "");

2016-12-29

Testy logów z log4j

Tworzymy klasę w /src/test/java/pakiet
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;

import java.util.ArrayList;
import java.util.List;

public class LogsHistorySaver extends AppenderSkeleton {
    private List logs;

    public void setUp() {
        logs = new ArrayList<>();
        Logger.getRootLogger().addAppender(this);
    }

    public void tearDown() {
        Logger.getRootLogger().removeAppender(this);
    }

    public List getLogs() {
        return logs;
    }

    @Override
    protected void append(LoggingEvent event) {
        logs.add(new Event(event.getLevel(), String.valueOf(event.getMessage())));
    }

    @Override
    public void close() {

    }

    @Override
    public boolean requiresLayout() {
        return false;
    }

    public class Event {
        private Level level;
        private String message;

        Event(Level level, String message) {
            this.level = level;
            this.message = message;
        }

        public Level getLevel() {
            return level;
        }

        public String getMessage() {
            return message;
        }

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder("Event{");
            sb.append("level=").append(level);
            sb.append(", message='").append(message).append('\'');
            sb.append('}');
            return sb.toString();
        }
    }
}
I korzystamy w niej w teście w spoób następujący:
public class KaboomTest {

    private Kaboom sut;

    private LogsHistorySaver logsHistorySaver = new LogsHistorySaver();

    @Before
    public void setUp() throws Exception {
        logsHistorySaver.setUp();
    }

    @Test
    public void log() throws Exception {
        //when
        sut.build();

        //then
        List logs = logsHistorySaver.getLogs();

        assertThat(logs.size()).isEqualTo(3);
        assertThat(logs.get(0).getLevel()).isEqualTo(Level.INFO);
        assertThat(logs.get(0).getMessage()).isEqualTo("First log");
        assertThat(logs.get(1).getLevel()).isEqualTo(Level.INFO);
        assertThat(logs.get(1).getMessage()).isEqualTo("Second log");
        assertThat(logs.get(2).getLevel()).isEqualTo(Level.INFO);
        assertThat(logs.get(2).getMessage()).isEqualTo("Third log");
    }
}

2016-12-28

Mockito - mock final class

Mockito w wersji 2.4.2 umożliwia mockowanie finalnych klas (oraz metod).

Wystarczy dodać plik
src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker
zawierający jedną linię tekstu:
mock-maker-inline
Maven dependency:

    org.mockito
    mockito-core
    2.4.2

Stream zamiast forEach

Bez użycia streama (trzeba definiować listę obiektów):
private List buildTransactions(AccountNumber accountNumber) {
    List transactions = new ArrayList<>();
    accountNumber
        .getAccountHistoryList()
        .forEach(accountHistory -> transactions.add(transactionBuilder.build(accountHistory)));

    return transactions;
}
Używając streama możemy tworzenie listy obiektów pominąć:
private List buildTransactions(AccountNumber accountNumber) {
    return accountNumber
        .getAccountHistoryList()
        .stream()
        .map(accountHistory -> transactionBuilder.build(accountHistory))
        .collect(Collectors.toList());
}
Update: Można jeszcze bardziej elegancko:
private List buildTransactions(AccountNumber accountNumber) {
    return accountNumber
        .getAccountHistoryList()
        .stream()
        .map(transactionBuilder::build)
        .collect(Collectors.toList());
}

Optional.ofNullable().orElseThrow()

Przykład wykorzystania klasy Optional dostępnej od Java 8.

Bez Optionala
public void test(Task task) throws VerificationException {
    if (task.getCustomer() == null) {
        logger.error(CUSTOMER_NOT_FOUND);
        throw BmVerificationException.badRequest(CUSTOMER_NOT_FOUND);
    }
}
Z Optionalem
public void test(Task task) throws VerificationException {
    Optional.ofNullable(task.getCustomer()).orElseThrow(this::getException);
}

private VerificationException getException() {
    logger.error(CUSTOMER_NOT_FOUND);
    return VerificationException.badRequest(CUSTOMER_NOT_FOUND);
}

2016-12-27

AssertJ - assertThatExceptionOfType

Przykład alternatywnego testowania wyjątku - assertThatExceptionOfType
public class FooTest {

    @Rule
    public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock
    private Validator validator;

    @InjectMocks
    private Foo sut;

    private HistoryRequest historyRequest;

    @Before
    public void setUp() throws Exception {
        historyRequest = new HistoryRequest();
    }

    @Test
    public void getHistoryThrowsExceptionDueToAccessDenied() throws Exception {
        when(validator.validate(historyRequest)).thenReturn(new Client());

        assertThatExceptionOfType(BloggerException.class)
                .isThrownBy(() -> sut.getHistory(historyRequest))
                .withMessage("Access denied");
    }
}

2016-12-20

GSON - ignorowanie pól przy pomocy własnej adnotacji

Tworzymy własną adnotację:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Exclude {
}

Klasę implementującą ExclusionStrategy:

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;

public class AnnotationExclusionStrategy implements ExclusionStrategy {

    @Override
    public boolean shouldSkipField(FieldAttributes f) {
        return f.getAnnotation(Exclude.class) != null;
    }

    @Override
    public boolean shouldSkipClass(Class clazz) {
        return false;
    }
}

Wykorzystujemy utworzoną adnotację w POJO:

public abstract class ExampleRequest {

    protected Long requestTime;
    protected String clientCode;
    @Exclude
    protected String hash;
}

Budujemy gsona np w taki sposób:

public class TestFacade {
    private static final Gson GSON;

    static {
        JsonDeserializer longToDateAdapter = (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong());
        GSON = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
                .registerTypeAdapter(Date.class, longToDateAdapter)
                .setExclusionStrategies(new AnnotationExclusionStrategy())
                .create();
    }
}
Najważniejsza w tym przykładzie jest metoda setExclusionStrategies(new AnnotationExclusionStrategy()) dzięki której cała magia działa. źródło: http://stackoverflow.com/a/27986860/767529