BAEL-4848 Update "Java Adapter Pattern" article (#13624)

This commit is contained in:
Eugene Kovko
2023-03-13 03:43:43 +01:00
committed by GitHub
parent f0f9e25e83
commit 1db057ebc8
11 changed files with 87 additions and 98 deletions
@@ -1,23 +0,0 @@
package com.baeldung.adapter;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class AdapterPatternIntegrationTest {
@Test
public void givenMovableAdapter_WhenConvertingMPHToKMPH_thenSuccessfullyConverted() {
Movable bugattiVeyron = new BugattiVeyron();
MovableAdapter bugattiVeyronAdapter = new MovableAdapterImpl(bugattiVeyron);
assertEquals(bugattiVeyronAdapter.getSpeed(), 431.30312, 0.00001);
Movable mcLaren = new McLaren();
MovableAdapter mcLarenAdapter = new MovableAdapterImpl(mcLaren);
assertEquals(mcLarenAdapter.getSpeed(), 387.85094, 0.00001);
Movable astonMartin = new AstonMartin();
MovableAdapter astonMartinAdapter = new MovableAdapterImpl(astonMartin);
assertEquals(astonMartinAdapter.getSpeed(), 354.0548, 0.00001);
}
}
@@ -0,0 +1,34 @@
package com.baeldung.adapter;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.junit.jupiter.api.Test;
class IteratorAdapterUnitTest {
public static final String STRING_DATA = "Welcome to baeldung.com";
private final StringTokenizer tokenizer = new StringTokenizer(STRING_DATA);
private final Iterator<String> tokenizerAdapter = new StringTokenizerIteratorAdapter(STRING_DATA);
private final List<String> expectedTokensForString = Arrays.asList("Welcome", "to", "baeldung.com");
@Test
void givenTokenizer_thenAdapterProducesCorrectResult() {
List<String> actualTokens = new ArrayList<>();
new IteratorAdapter(tokenizer).forEachRemaining(s -> actualTokens.add((String) s));
assertEquals(expectedTokensForString, actualTokens);
}
@Test
void givenTokenizerAdapter_thenIteratorProducesCorrectResult() {
List<String> actualTokens = new ArrayList<>();
tokenizerAdapter.forEachRemaining(actualTokens::add);
assertEquals(expectedTokensForString, actualTokens);
}
}