Skip to content

Commit

Permalink
Merge branch 'master' into thombergs-patch-6
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit authored Jun 6, 2018
2 parents 3723648 + be608ae commit 1a58d62
Show file tree
Hide file tree
Showing 49 changed files with 251 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class CayenneAdvancedOperationIntegrationTest {
public class CayenneAdvancedOperationLiveTest {
private static ObjectContext context = null;

@BeforeClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import static org.junit.Assert.assertNull;


public class CayenneOperationIntegrationTest {
public class CayenneOperationLiveTest {
private static ObjectContext context = null;

@BeforeClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@RunWith(SpringRunner.class)
@SpringBootTest
public class AzureApplicationTests {
public class AzureApplicationIntegrationTest {

@Test
public void contextLoads() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import org.junit.Test;

public class MethodParamNameTest {
public class MethodParamNameUnitTest {

@Test
public void whenGetConstructorParams_thenOk()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException {
bw.newLine();
bw.close();

assertThat(StreamUtils.getStringFromInputStream(new FileInputStream(fileName))).isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\n");
assertThat(StreamUtils.getStringFromInputStream(new FileInputStream(fileName))).isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void whenReadTwoFilesWithSequenceInputStream_thenCorrect() throws IOExcep

@Test
public void whenReadUTFEncodedFile_thenCorrect() throws IOException {
final String expected_value = "�空";
final String expected_value = "青空";
final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("src/test/resources/test_read7.in"), "UTF-8"));
final String currentLine = reader.readLine();
reader.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import org.junit.Test;

public class JaggedArrayTest {
public class JaggedArrayUnitTest {

private JaggedArray obj = new JaggedArray();

Expand Down
33 changes: 33 additions & 0 deletions core-kotlin/src/main/kotlin/com/baeldung/enums/CardType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.baeldung.enums

enum class CardType(val color: String) : ICardLimit {
SILVER("gray") {
override fun getCreditLimit(): Int {
return 100000
}

override fun calculateCashbackPercent(): Float {
return 0.25f
}
},
GOLD("yellow") {
override fun getCreditLimit(): Int {
return 200000
}

override fun calculateCashbackPercent(): Float {
return 0.5f
}
},
PLATINUM("black") {
override fun getCreditLimit(): Int {
return 300000
}

override fun calculateCashbackPercent(): Float {
return 0.75f
}
};

abstract fun calculateCashbackPercent(): Float
}
16 changes: 16 additions & 0 deletions core-kotlin/src/main/kotlin/com/baeldung/enums/CardTypeHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baeldung.enums

class CardTypeHelper {
fun getCardTypeByColor(color: String): CardType? {
for (cardType in CardType.values()) {
if (cardType.color.equals(color)) {
return cardType;
}
}
return null
}

fun getCardTypeByName(name: String): CardType {
return CardType.valueOf(name.toUpperCase())
}
}
5 changes: 5 additions & 0 deletions core-kotlin/src/main/kotlin/com/baeldung/enums/ICardLimit.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.enums

interface ICardLimit {
fun getCreditLimit(): Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.baeldung.enums

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

internal class CardTypeHelperUnitTest {

@Test
fun whenGetCardTypeByColor_thenSilverCardType() {
val cardTypeHelper = CardTypeHelper()
Assertions.assertEquals(CardType.SILVER, cardTypeHelper.getCardTypeByColor("gray"))
}

@Test
fun whenGetCardTypeByColor_thenGoldCardType() {
val cardTypeHelper = CardTypeHelper()
Assertions.assertEquals(CardType.GOLD, cardTypeHelper.getCardTypeByColor("yellow"))
}

@Test
fun whenGetCardTypeByColor_thenPlatinumCardType() {
val cardTypeHelper = CardTypeHelper()
Assertions.assertEquals(CardType.PLATINUM, cardTypeHelper.getCardTypeByColor("black"))
}

@Test
fun whenGetCardTypeByName_thenSilverCardType() {
val cardTypeHelper = CardTypeHelper()
Assertions.assertEquals(CardType.SILVER, cardTypeHelper.getCardTypeByName("silver"))
}

@Test
fun whenGetCardTypeByName_thenGoldCardType() {
val cardTypeHelper = CardTypeHelper()
Assertions.assertEquals(CardType.GOLD, cardTypeHelper.getCardTypeByName("gold"))
}

@Test
fun whenGetCardTypeByName_thenPlatinumCardType() {
val cardTypeHelper = CardTypeHelper()
Assertions.assertEquals(CardType.PLATINUM, cardTypeHelper.getCardTypeByName("platinum"))
}
}
52 changes: 52 additions & 0 deletions core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeUnitTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.baeldung.enums

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

internal class CardTypeUnitTest {

@Test
fun givenSilverCardType_whenCalculateCashbackPercent_thenReturnCashbackValue() {
assertEquals(0.25f, CardType.SILVER.calculateCashbackPercent())
}

@Test
fun givenGoldCardType_whenCalculateCashbackPercent_thenReturnCashbackValue() {
assertEquals(0.5f, CardType.GOLD.calculateCashbackPercent())
}

@Test
fun givenPlatinumCardType_whenCalculateCashbackPercent_thenReturnCashbackValue() {
assertEquals(0.75f, CardType.PLATINUM.calculateCashbackPercent())
}

@Test
fun givenSilverCardType_whenGetCreditLimit_thenReturnCreditLimit() {
assertEquals(100000, CardType.SILVER.getCreditLimit())
}

@Test
fun givenGoldCardType_whenGetCreditLimit_thenReturnCreditLimit() {
assertEquals(200000, CardType.GOLD.getCreditLimit())
}

@Test
fun givenPlatinumCardType_whenGetCreditLimit_thenReturnCreditLimit() {
assertEquals(300000, CardType.PLATINUM.getCreditLimit())
}

@Test
fun givenSilverCardType_whenCheckColor_thenReturnColor() {
assertEquals("gray", CardType.SILVER.color)
}

@Test
fun givenGoldCardType_whenCheckColor_thenReturnColor() {
assertEquals("yellow", CardType.GOLD.color)
}

@Test
fun givenPlatinumCardType_whenCheckColor_thenReturnColor() {
assertEquals("black", CardType.PLATINUM.color)
}
}
Binary file modified custom-pmd-0.0.1.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ public class UnitTestNamingConventionRule extends AbstractJavaRule {
"ManualTest",
"JdbcTest",
"LiveTest",
"UnitTest");
"UnitTest",
"jmhTest");

public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
String className = node.getImage();
Objects.requireNonNull(className);

if (className.endsWith("Test") || className.endsWith("Tests")) {
if (allowedEndings.stream()
.noneMatch(className::endsWith)) {
addViolation(data, node);
}
if (className.endsWith("Tests")
|| (className.endsWith("Test") && allowedEndings.stream().noneMatch(className::endsWith))) {
addViolation(data, node);
}

return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}, webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureMockMvc
@ActiveProfiles("dev")
public class FlipControllerTest {
public class FlipControllerIntegrationTest {

@Autowired private MockMvc mvc;

Expand Down
55 changes: 30 additions & 25 deletions libraries/src/main/java/com/baeldung/commons/math3/Histogram.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@

public class Histogram {

private Map distributionMap;
private int classWidth;

public Histogram() {

distributionMap = new TreeMap();
classWidth = 10;
Map distributionMap = processRawData();
List yData = new ArrayList();
yData.addAll(distributionMap.values());
Expand Down Expand Up @@ -46,43 +51,43 @@ private Map processRawData() {
Frequency frequency = new Frequency();
datasetList.forEach(d -> frequency.addValue(Double.parseDouble(d.toString())));

int classWidth = 10;

Map distributionMap = new TreeMap();
List processed = new ArrayList();
datasetList.forEach(d -> {
double observation = Double.parseDouble(d.toString());

double observation = Double.parseDouble(d.toString());

if(processed.contains(observation))
return;

long observationFrequency = frequency.getCount(observation);
int upperBoundary = (observation > classWidth) ? Math.multiplyExact( (int) Math.ceil(observation / classWidth), classWidth) : classWidth;
int lowerBoundary = (upperBoundary > classWidth) ? Math.subtractExact(upperBoundary, classWidth) : 0;
String bin = lowerBoundary + "-" + upperBoundary;
if(processed.contains(observation))
return;

int prevUpperBoundary = lowerBoundary;
int prevLowerBoundary = (lowerBoundary > classWidth) ? lowerBoundary - classWidth : 0;
String prevBin = prevLowerBoundary + "-" + prevUpperBoundary;
if(!distributionMap.containsKey(prevBin))
distributionMap.put(prevBin, 0);
long observationFrequency = frequency.getCount(observation);
int upperBoundary = (observation > classWidth) ? Math.multiplyExact( (int) Math.ceil(observation / classWidth), classWidth) : classWidth;
int lowerBoundary = (upperBoundary > classWidth) ? Math.subtractExact(upperBoundary, classWidth) : 0;
String bin = lowerBoundary + "-" + upperBoundary;

if(!distributionMap.containsKey(bin)) {
distributionMap.put(bin, observationFrequency);
}
else {
long oldFrequency = Long.parseLong(distributionMap.get(bin).toString());
distributionMap.replace(bin, oldFrequency + observationFrequency);
}
updateDistributionMap(lowerBoundary, bin, observationFrequency);

processed.add(observation);
processed.add(observation);

});

return distributionMap;
}

private void updateDistributionMap(int lowerBoundary, String bin, long observationFrequency) {

int prevLowerBoundary = (lowerBoundary > classWidth) ? lowerBoundary - classWidth : 0;
String prevBin = prevLowerBoundary + "-" + lowerBoundary;
if(!distributionMap.containsKey(prevBin))
distributionMap.put(prevBin, 0);

if(!distributionMap.containsKey(bin)) {
distributionMap.put(bin, observationFrequency);
}
else {
long oldFrequency = Long.parseLong(distributionMap.get(bin).toString());
distributionMap.replace(bin, oldFrequency + observationFrequency);
}
}

public static void main(String[] args) {
new Histogram();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import static org.assertj.core.api.Assertions.assertThat;

public class TodoMustacheServiceTest {
public class TodoMustacheServiceUnitTest {

private String executeTemplate(Mustache m, Map<String, Object> context) throws IOException {
StringWriter writer = new StringWriter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @author Donato Rimenti
*
*/
public class SingletonSynchronizationUnitTest {
public class SingletonSynchronizationIntegrationTest {

/**
* Size of the thread pools used.
Expand All @@ -33,7 +33,7 @@ public class SingletonSynchronizationUnitTest {
@Test
public void givenDraconianSingleton_whenMultithreadInstancesEquals_thenTrue() {
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
Set<DraconianSingleton> resultSet = Collections.synchronizedSet(new HashSet<DraconianSingleton>());
Set<DraconianSingleton> resultSet = Collections.synchronizedSet(new HashSet<>());

// Submits the instantiation tasks.
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
Expand All @@ -51,7 +51,7 @@ public void givenDraconianSingleton_whenMultithreadInstancesEquals_thenTrue() {
@Test
public void givenDclSingleton_whenMultithreadInstancesEquals_thenTrue() {
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
Set<DclSingleton> resultSet = Collections.synchronizedSet(new HashSet<DclSingleton>());
Set<DclSingleton> resultSet = Collections.synchronizedSet(new HashSet<>());

// Submits the instantiation tasks.
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
Expand All @@ -69,7 +69,7 @@ public void givenDclSingleton_whenMultithreadInstancesEquals_thenTrue() {
@Test
public void givenEarlyInitSingleton_whenMultithreadInstancesEquals_thenTrue() {
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
Set<EarlyInitSingleton> resultSet = Collections.synchronizedSet(new HashSet<EarlyInitSingleton>());
Set<EarlyInitSingleton> resultSet = Collections.synchronizedSet(new HashSet<>());

// Submits the instantiation tasks.
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
Expand All @@ -87,7 +87,7 @@ public void givenEarlyInitSingleton_whenMultithreadInstancesEquals_thenTrue() {
@Test
public void givenInitOnDemandSingleton_whenMultithreadInstancesEquals_thenTrue() {
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
Set<InitOnDemandSingleton> resultSet = Collections.synchronizedSet(new HashSet<InitOnDemandSingleton>());
Set<InitOnDemandSingleton> resultSet = Collections.synchronizedSet(new HashSet<>());

// Submits the instantiation tasks.
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
Expand All @@ -105,7 +105,7 @@ public void givenInitOnDemandSingleton_whenMultithreadInstancesEquals_thenTrue()
@Test
public void givenEnumSingleton_whenMultithreadInstancesEquals_thenTrue() {
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
Set<EnumSingleton> resultSet = Collections.synchronizedSet(new HashSet<EnumSingleton>());
Set<EnumSingleton> resultSet = Collections.synchronizedSet(new HashSet<>());

// Submits the instantiation tasks.
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
Expand Down
Loading

0 comments on commit 1a58d62

Please sign in to comment.