cleanup and testing work

This commit is contained in:
eugenp 2017-02-04 21:56:11 +02:00
parent ffd17c1b21
commit 791142c67e
22 changed files with 256 additions and 263 deletions

View File

@ -17,8 +17,7 @@ public class RunAlgorithm {
int decision = in.nextInt(); int decision = in.nextInt();
switch (decision) { switch (decision) {
case 1: case 1:
System.out.println( System.out.println("Optimized distance for travel: " + SimulatedAnnealing.simulateAnnealing(10, 10000, 0.9995));
"Optimized distance for travel: " + SimulatedAnnealing.simulateAnnealing(10, 10000, 0.9995));
break; break;
case 2: case 2:
SlopeOne.slopeOne(3); SlopeOne.slopeOne(3);

View File

@ -13,16 +13,14 @@ public class SimpleGeneticAlgorithm {
public static boolean runAlgorithm(int populationSize, String solution) { public static boolean runAlgorithm(int populationSize, String solution) {
if (solution.length() != SimpleGeneticAlgorithm.solution.length) { if (solution.length() != SimpleGeneticAlgorithm.solution.length) {
throw new RuntimeException( throw new RuntimeException("The solution needs to have " + SimpleGeneticAlgorithm.solution.length + " bytes");
"The solution needs to have " + SimpleGeneticAlgorithm.solution.length + " bytes");
} }
SimpleGeneticAlgorithm.setSolution(solution); SimpleGeneticAlgorithm.setSolution(solution);
Population myPop = new Population(populationSize, true); Population myPop = new Population(populationSize, true);
int generationCount = 1; int generationCount = 1;
while (myPop.getFittest().getFitness() < SimpleGeneticAlgorithm.getMaxFitness()) { while (myPop.getFittest().getFitness() < SimpleGeneticAlgorithm.getMaxFitness()) {
System.out.println( System.out.println("Generation: " + generationCount + " Correct genes found: " + myPop.getFittest().getFitness());
"Generation: " + generationCount + " Correct genes found: " + myPop.getFittest().getFitness());
myPop = SimpleGeneticAlgorithm.evolvePopulation(myPop); myPop = SimpleGeneticAlgorithm.evolvePopulation(myPop);
generationCount++; generationCount++;
} }

View File

@ -1,16 +0,0 @@
package com.baeldung.algorithms;
import org.junit.Assert;
import org.junit.Test;
import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm;
public class BinaryGeneticAlgorithmTest {
@Test
public void testGA() {
Assert.assertTrue(SimpleGeneticAlgorithm.runAlgorithm(50,
"1011000100000100010000100000100111001000000100000100000000001111"));
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.algorithms;
import org.junit.Assert;
import org.junit.Test;
import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm;
public class BinaryGeneticAlgorithmUnitTest {
@Test
public void testGA() {
Assert.assertTrue(SimpleGeneticAlgorithm.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111"));
}
}

View File

@ -37,7 +37,8 @@ public class PriorityBlockingQueueUnitTest {
try { try {
Integer poll = queue.take(); Integer poll = queue.take();
System.out.println("Polled: " + poll); System.out.println("Polled: " + poll);
} catch (InterruptedException e) {} } catch (InterruptedException e) {
}
} }
}); });
thread.start(); thread.start();

View File

@ -18,9 +18,7 @@ public class ConcurrentNavigableMapTests {
updateMapConcurrently(skipListMap, 4); updateMapConcurrently(skipListMap, 4);
Iterator<Integer> skipListIter = skipListMap Iterator<Integer> skipListIter = skipListMap.keySet().iterator();
.keySet()
.iterator();
int previous = skipListIter.next(); int previous = skipListIter.next();
while (skipListIter.hasNext()) { while (skipListIter.hasNext()) {
int current = skipListIter.next(); int current = skipListIter.next();

View File

@ -16,17 +16,14 @@ import static org.junit.Assert.assertTrue;
public class ThreadPoolInParallelStreamTest { public class ThreadPoolInParallelStreamTest {
@Test @Test
public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal() public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal() throws InterruptedException, ExecutionException {
throws InterruptedException, ExecutionException {
long firstNum = 1; long firstNum = 1;
long lastNum = 1_000_000; long lastNum = 1_000_000;
List<Long> aList = LongStream.rangeClosed(firstNum, lastNum).boxed() List<Long> aList = LongStream.rangeClosed(firstNum, lastNum).boxed().collect(Collectors.toList());
.collect(Collectors.toList());
ForkJoinPool customThreadPool = new ForkJoinPool(4); ForkJoinPool customThreadPool = new ForkJoinPool(4);
long actualTotal = customThreadPool.submit(() -> aList.parallelStream() long actualTotal = customThreadPool.submit(() -> aList.parallelStream().reduce(0L, Long::sum)).get();
.reduce(0L, Long::sum)).get();
assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal); assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal);
} }

View File

@ -2,9 +2,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>logmdc</artifactId> <artifactId>log-mdc</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<name>logmdc</name> <name>log-mdc</name>
<packaging>war</packaging> <packaging>war</packaging>
<description>tutorial on logging with MDC and NDC</description> <description>tutorial on logging with MDC and NDC</description>

View File

@ -13,7 +13,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ExternalPropertiesWithXmlConfig.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { ExternalPropertiesWithXmlConfig.class }, loader = AnnotationConfigContextLoader.class)
@Ignore("manual only") @Ignore("manual only")
public class ExternalPropertiesWithXmlIntegrationTest { public class ExternalPropertiesWithXmlManualTest {
@Autowired @Autowired
private Environment env; private Environment env;

View File

@ -12,7 +12,7 @@ import java.sql.SQLException;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
public class SpringRetryTest { public class SpringRetryIntegrationTest {
@Autowired @Autowired
private MyService myService; private MyService myService;

View File

@ -8,7 +8,8 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ThreadPoolTaskSchedulerConfig.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { ThreadPoolTaskSchedulerConfig.class }, loader = AnnotationConfigContextLoader.class)
public class ThreadPoolTaskSchedulerTest { public class ThreadPoolTaskSchedulerIntegrationTest {
@Test @Test
public void testThreadPoolTaskSchedulerAnnotation() throws InterruptedException { public void testThreadPoolTaskSchedulerAnnotation() throws InterruptedException {
Thread.sleep(2550); Thread.sleep(2550);

View File

@ -5,7 +5,7 @@ import org.baeldung.properties.basic.PropertiesWithMultipleXmlsIntegrationTest;
import org.baeldung.properties.basic.PropertiesWithXmlIntegrationTest; import org.baeldung.properties.basic.PropertiesWithXmlIntegrationTest;
import org.baeldung.properties.external.ExternalPropertiesWithJavaIntegrationTest; import org.baeldung.properties.external.ExternalPropertiesWithJavaIntegrationTest;
import org.baeldung.properties.external.ExternalPropertiesWithMultipleXmlsIntegrationTest; import org.baeldung.properties.external.ExternalPropertiesWithMultipleXmlsIntegrationTest;
import org.baeldung.properties.external.ExternalPropertiesWithXmlIntegrationTest; import org.baeldung.properties.external.ExternalPropertiesWithXmlManualTest;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Suite; import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses; import org.junit.runners.Suite.SuiteClasses;
@ -15,7 +15,7 @@ import org.junit.runners.Suite.SuiteClasses;
PropertiesWithXmlIntegrationTest.class, PropertiesWithXmlIntegrationTest.class,
ExternalPropertiesWithJavaIntegrationTest.class, ExternalPropertiesWithJavaIntegrationTest.class,
ExternalPropertiesWithMultipleXmlsIntegrationTest.class, ExternalPropertiesWithMultipleXmlsIntegrationTest.class,
ExternalPropertiesWithXmlIntegrationTest.class, ExternalPropertiesWithXmlManualTest.class,
ExtendedPropertiesWithJavaIntegrationTest.class, ExtendedPropertiesWithJavaIntegrationTest.class,
PropertiesWithMultipleXmlsIntegrationTest.class, PropertiesWithMultipleXmlsIntegrationTest.class,
})// @formatter:on })// @formatter:on

View File

@ -22,7 +22,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration @WebAppConfiguration
@ContextConfiguration(classes = BeanNameUrlHandlerMappingConfig.class) @ContextConfiguration(classes = BeanNameUrlHandlerMappingConfig.class)
public class BeanNameMappingConfigTest { public class BeanNameMappingConfigIntegrationTest {
@Autowired @Autowired
private WebApplicationContext webAppContext; private WebApplicationContext webAppContext;

View File

@ -22,7 +22,7 @@ import com.baeldung.config.ControllerClassNameHandlerMappingConfig;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration @WebAppConfiguration
@ContextConfiguration(classes = ControllerClassNameHandlerMappingConfig.class) @ContextConfiguration(classes = ControllerClassNameHandlerMappingConfig.class)
public class ControllerClassNameHandlerMappingTest { public class ControllerClassNameHandlerMappingIntegrationTest {
@Autowired @Autowired
private WebApplicationContext webAppContext; private WebApplicationContext webAppContext;

View File

@ -1,7 +1,10 @@
package com.baeldung.handlermappings; package com.baeldung.handlermappings;
import com.baeldung.config.HandlerMappingDefaultConfig; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import com.baeldung.config.HandlerMappingPrioritiesConfig; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -14,15 +17,12 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import com.baeldung.config.HandlerMappingDefaultConfig;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration @WebAppConfiguration
@ContextConfiguration(classes = HandlerMappingDefaultConfig.class) @ContextConfiguration(classes = HandlerMappingDefaultConfig.class)
public class HandlerMappingDefaultConfigTest { public class HandlerMappingDefaultConfigIntegrationTest {
@Autowired @Autowired
private WebApplicationContext webAppContext; private WebApplicationContext webAppContext;

View File

@ -21,7 +21,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration @WebAppConfiguration
@ContextConfiguration(classes = HandlerMappingPrioritiesConfig.class) @ContextConfiguration(classes = HandlerMappingPrioritiesConfig.class)
public class HandlerMappingPriorityConfigTest { public class HandlerMappingPriorityConfigIntegrationTest {
@Autowired @Autowired
private WebApplicationContext webAppContext; private WebApplicationContext webAppContext;

View File

@ -21,7 +21,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration @WebAppConfiguration
@ContextConfiguration(classes = SimpleUrlHandlerMappingConfig.class) @ContextConfiguration(classes = SimpleUrlHandlerMappingConfig.class)
public class SimpleUrlMappingConfigTest { public class SimpleUrlMappingConfigIntegrationTest {
@Autowired @Autowired
private WebApplicationContext webAppContext; private WebApplicationContext webAppContext;