Merge remote-tracking branch 'origin/master'

This commit is contained in:
slavisa-baeldung
2017-04-08 06:20:27 +01:00
62 changed files with 930 additions and 99 deletions
@@ -2,10 +2,10 @@ package com.baeldung.algorithms.primechecker;
import java.math.BigInteger;
public class BigIntegerPrimeChecker implements PrimeChecker{
public class BigIntegerPrimeChecker implements PrimeChecker<Long>{
@Override
public boolean isPrime(int number) {
public boolean isPrime(Long number) {
BigInteger bigInt = BigInteger.valueOf(number);
return bigInt.isProbablePrime(100);
}
@@ -1,12 +1,15 @@
package com.baeldung.algorithms.primechecker;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class BruteForcePrimeChecker implements PrimeChecker{
public class BruteForcePrimeChecker implements PrimeChecker<Integer>{
@Override
public boolean isPrime(int number) {
return IntStream.range(2, number).noneMatch(n -> (number % n == 0));
public boolean isPrime(Integer number) {
return number > 2 ? IntStream.range(2, number)
.noneMatch(n -> (number % n == 0)) : false;
}
@@ -1,13 +1,14 @@
package com.baeldung.algorithms.primechecker;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class OptimisedPrimeChecker implements PrimeChecker{
public class OptimisedPrimeChecker implements PrimeChecker<Integer>{
@Override
public boolean isPrime(int number) {
return IntStream.range(2, (int)Math.sqrt(number) + 1)
.noneMatch(n -> (number % n == 0));
public boolean isPrime(Integer number) {
return number > 2 ? IntStream.rangeClosed(2, (int) Math.sqrt(number))
.noneMatch(n -> (number % n == 0)) : false;
}
@@ -1,6 +1,6 @@
package com.baeldung.algorithms.primechecker;
public interface PrimeChecker {
public interface PrimeChecker <T> {
public boolean isPrime( int number );
public boolean isPrime( T number );
}
@@ -2,10 +2,10 @@ package com.baeldung.algorithms.primechecker;
import org.apache.commons.math3.primes.Primes;
public class PrimesPrimeChecker implements PrimeChecker{
public class PrimesPrimeChecker implements PrimeChecker<Integer>{
@Override
public boolean isPrime(int number) {
public boolean isPrime(Integer number) {
return Primes.isPrime(number);
}
@@ -9,18 +9,20 @@ import com.baeldung.algorithms.primechecker.PrimeChecker;
public class BigIntegerPrimeCheckerTest {
PrimeChecker primeChecker = new BigIntegerPrimeChecker();
BigIntegerPrimeChecker primeChecker = new BigIntegerPrimeChecker();
@Test
public void givenPrimeNumber_whenCheckIsPrime_thenTrue(){
assertTrue(primeChecker.isPrime(13));
assertTrue(primeChecker.isPrime(1009));
assertTrue(primeChecker.isPrime(13l));
assertTrue(primeChecker.isPrime(1009L));
assertTrue(primeChecker.isPrime(74207281L));
}
@Test
public void givenNonPrimeNumber_whenCheckIsPrime_thenFalse(){
assertTrue(!primeChecker.isPrime(50));
assertTrue(!primeChecker.isPrime(1001));
assertTrue(!primeChecker.isPrime(50L));
assertTrue(!primeChecker.isPrime(1001L));
assertTrue(!primeChecker.isPrime(74207282L));
}
}
@@ -9,7 +9,7 @@ import com.baeldung.algorithms.primechecker.PrimeChecker;
public class OptimisedPrimeCheckerTest {
PrimeChecker primeChecker = new OptimisedPrimeChecker();
OptimisedPrimeChecker primeChecker = new OptimisedPrimeChecker();
@Test
public void givenPrimeNumber_whenCheckIsPrime_thenTrue(){
@@ -8,7 +8,7 @@ import com.baeldung.algorithms.primechecker.PrimeChecker;
import com.baeldung.algorithms.primechecker.PrimesPrimeChecker;
public class PrimesPrimeCheckerTest {
PrimeChecker primeChecker = new PrimesPrimeChecker();
PrimesPrimeChecker primeChecker = new PrimesPrimeChecker();
@Test
public void givenPrimeNumber_whenCheckIsPrime_thenTrue() {
+3 -3
View File
@@ -101,9 +101,7 @@
<dependency>
<groupId>org.dbdoclet</groupId>
<artifactId>herold</artifactId>
<version>6.1.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/test/resources/jars/herold.jar</systemPath>
<version>8.0.4</version>
</dependency>
<dependency>
@@ -140,6 +138,8 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<forkCount>3</forkCount>
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
@@ -19,21 +19,21 @@ import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.xmlgraphics.util.MimeConstants;
import org.dbdoclet.trafo.html.docbook.DocBookTransformer;
import org.dbdoclet.trafo.html.docbook.HtmlDocBookTrafo;
import org.dbdoclet.trafo.script.Script;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.tidy.Tidy;
public class ApacheFOPConvertHTMLIntegrationTest {
private String inputFile = "src/test/resources/input.html";
private String style = "src/test/resources/xhtml2fo.xsl";
private String style1 = "src/test/resources/docbook-xsl/fo/docbook.xsl";
private String output_jtidy = "src/test/resources/output_jtidy.pdf";
private String output_html2fo = "src/test/resources/output_html2fo.pdf";
private String output_herold = "src/test/resources/output_herold.pdf";
private String foFile = "src/test/resources/input.fo";
private String xmlFile = "src/test/resources/input.xml";
private final String inputFile = "src/test/resources/input.html";
private final String style = "src/test/resources/xhtml2fo.xsl";
private final String style1 = "src/test/resources/docbook-xsl/fo/docbook.xsl";
private final String output_jtidy = "src/test/resources/output_jtidy.pdf";
private final String output_html2fo = "src/test/resources/output_html2fo.pdf";
private final String output_herold = "src/test/resources/output_herold.pdf";
private final String foFile = "src/test/resources/input.fo";
private final String xmlFile = "src/test/resources/input.xml";
@Test
public void whenTransformHTMLToPDFUsingJTidy_thenCorrect() throws Exception {
@@ -114,8 +114,9 @@ public class ApacheFOPConvertHTMLIntegrationTest {
private void fromHTMLTOXMLUsingHerold() throws Exception {
final Script script = new Script();
final DocBookTransformer transformer = new DocBookTransformer();
transformer.setScript(script);
transformer.convert(new FileInputStream(inputFile), new FileOutputStream(xmlFile));
final HtmlDocBookTrafo transformer = new HtmlDocBookTrafo();
transformer.setInputStream(new FileInputStream(inputFile));
transformer.setOutputStream(new FileOutputStream(xmlFile));
transformer.transform(script);
}
}
@@ -10,6 +10,7 @@ import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.transform.Result;
@@ -25,19 +26,15 @@ import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.xmlgraphics.util.MimeConstants;
import org.dbdoclet.trafo.TrafoScriptManager;
import org.dbdoclet.trafo.html.docbook.DocBookTransformer;
import org.dbdoclet.trafo.html.docbook.HtmlDocBookTrafo;
import org.dbdoclet.trafo.script.Script;
import org.junit.Test;
import org.w3c.dom.Document;
public class ApacheFOPHeroldLiveTest {
private String[] inputUrls = {// @formatter:off
"http://www.baeldung.com/2011/10/20/bootstraping-a-web-application-with-spring-3-1-and-java-based-configuration-part-1/",
"http://www.baeldung.com/2011/10/25/building-a-restful-web-service-with-spring-3-1-and-java-based-configuration-part-2/",
"http://www.baeldung.com/2011/10/31/securing-a-restful-web-service-with-spring-security-3-1-part-3/",
"http://www.baeldung.com/spring-security-basic-authentication",
"http://www.baeldung.com/spring-security-digest-authentication",
"http://www.baeldung.com/2011/11/20/basic-and-digest-authentication-for-a-restful-service-with-spring-security-3-1/",
private final String[] inputUrls = {// @formatter:off
// "http://www.baeldung.com/spring-security-basic-authentication",
"http://www.baeldung.com/spring-security-digest-authentication"
//"http://www.baeldung.com/spring-httpmessageconverter-rest",
//"http://www.baeldung.com/2011/11/06/restful-web-service-discoverability-part-4/",
//"http://www.baeldung.com/2011/11/13/rest-service-discoverability-with-spring-part-5/",
@@ -49,10 +46,10 @@ public class ApacheFOPHeroldLiveTest {
//"http://www.baeldung.com/2013/01/18/testing-rest-with-multiple-mime-types/"
}; // @formatter:on
private String style_file = "src/test/resources/docbook-xsl/fo/docbook.xsl";
private String output_file = "src/test/resources/final_output.pdf";
private String xmlInput = "src/test/resources/input.xml";
private String xmlOutput = "src/test/resources/output.xml";
private final String style_file = "src/test/resources/docbook-xsl/fo/docbook.xsl";
private final String output_file = "src/test/resources/final_output.pdf";
private final String xmlInput = "src/test/resources/input.xml";
private final String xmlOutput = "src/test/resources/output.xml";
// tests
@@ -75,10 +72,11 @@ public class ApacheFOPHeroldLiveTest {
final TrafoScriptManager mgr = new TrafoScriptManager();
final File profileFile = new File("src/test/resources/default.her");
script = mgr.parseScript(profileFile);
final DocBookTransformer transformer = new DocBookTransformer();
transformer.setScript(script);
final HtmlDocBookTrafo transformer = new HtmlDocBookTrafo();
transformer.setInputStream(getInputStream(input));
transformer.setOutputStream(new FileOutputStream(xmlInput, append));
transformer.convert(getInputStream(input), new FileOutputStream(xmlInput, append));
transformer.transform(script);
}
private Document fromXMLFileToFO() throws Exception {
@@ -112,7 +110,9 @@ public class ApacheFOPHeroldLiveTest {
private InputStream getInputStream(final String input) throws IOException {
final URL url = new URL(input);
return url.openStream();
final HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
httpcon.addRequestProperty("User-Agent", "Mozilla/4.0");
return httpcon.getInputStream();
}
private void fixXML(final String input, final String output) throws IOException {
@@ -127,7 +127,7 @@ public class ApacheFOPHeroldLiveTest {
if (line.contains("info>")) {
writer.write(line.replace("info>", "section>"));
} else if (!((line.startsWith("<?xml") || line.startsWith("<article") || line.startsWith("</article")) && count > 4)) {
} else if (!((line.startsWith("<?xml") || line.startsWith("<article") || line.startsWith("</article")) && (count > 4))) {
writer.write(line.replaceAll("xml:id=\"", "xml:id=\"" + count));
}
writer.write("\n");
Binary file not shown.
+1
View File
@@ -11,6 +11,7 @@
- [Converting between a List and a Set in Java](http://www.baeldung.com/convert-list-to-set-and-set-to-list)
- [Convert a Map to an Array, List or Set in Java](http://www.baeldung.com/convert-map-values-to-array-list-set)
- [Java – Write to File](http://www.baeldung.com/java-write-to-file)
- [Java - Convert File to InputStream] (http://www.baeldung.com/convert-file-to-input-stream)
- [Java Scanner](http://www.baeldung.com/java-scanner)
- [Java Timer](http://www.baeldung.com/java-timer-and-timertask)
- [Java – Byte Array to Writer](http://www.baeldung.com/java-convert-byte-array-to-writer)
@@ -1,21 +1,52 @@
package com.baeldung.stringtokenizer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
public class Application {
public List<String> getTokens(String str) {
List<String> tokens = new ArrayList<String>();
// StringTokenizer tokenizer = new StringTokenizer( str );
StringTokenizer tokenizer = new StringTokenizer( str , "," );
// StringTokenizer tokenizer = new StringTokenizer( str , "," , true );
while (tokenizer.hasMoreElements()) {
tokens.add( tokenizer.nextToken() );
// tokens.add( tokenizer.nextToken( "," ) );
}
return tokens;
}
public List<String> getTokens(String str) {
List<String> tokens = new ArrayList<String>();
// StringTokenizer tokenizer = new StringTokenizer( str );
StringTokenizer tokenizer = new StringTokenizer(str, ",");
// StringTokenizer tokenizer = new StringTokenizer( str , "," , true );
while (tokenizer.hasMoreElements()) {
tokens.add(tokenizer.nextToken());
// tokens.add( tokenizer.nextToken("e") );
}
int tokenLength = tokens.size();
return tokens;
}
public List<String> getTokensWithCollection(String str) {
StringTokenizer tokenizer = new StringTokenizer(str, ",");
return Collections.list(tokenizer).stream()
.map(token -> (String) token)
.collect(Collectors.toList());
}
public List<String> getTokensFromFile(String path, String delim) {
List<String> tokens = new ArrayList<>();
String currLine = "";
StringTokenizer tokenizer;
try (BufferedReader br = new BufferedReader(new InputStreamReader(Application.class.getResourceAsStream("/" + path)))) {
while ((currLine = br.readLine()) != null) {
tokenizer = new StringTokenizer(currLine, delim);
while (tokenizer.hasMoreElements()) {
tokens.add(tokenizer.nextToken());
}
}
} catch (IOException e) {
e.printStackTrace();
}
return tokens;
}
}
+3
View File
@@ -0,0 +1,3 @@
1|IND|India
2|MY|Malaysia
3|AU|Australia
1 1 IND India
2 2 MY Malaysia
3 3 AU Australia
@@ -11,20 +11,37 @@ import static org.junit.Assert.assertEquals;
public class ApplicationTest {
Application application = new Application();
List<String> expectedTokens = new ArrayList<String>();
List<String> expectedTokensForString = new ArrayList<String>();
List<String> expectedTokensForFile = new ArrayList<String>();
@Before
public void init() {
expectedTokens.add( "Welcome" );
expectedTokens.add( "to" );
expectedTokens.add( "baeldung.com" );
expectedTokensForString.add("Welcome");
expectedTokensForString.add("to");
expectedTokensForString.add("baeldung.com");
expectedTokensForFile.add("1");
expectedTokensForFile.add("IND");
expectedTokensForFile.add("India");
expectedTokensForFile.add("2");
expectedTokensForFile.add("MY");
expectedTokensForFile.add("Malaysia");
expectedTokensForFile.add("3");
expectedTokensForFile.add("AU");
expectedTokensForFile.add("Australia");
}
@Test
public void givenString_thenGetListOfString() {
String str = "Welcome,to,baeldung.com";
List<String> actualTokens = application.getTokens(str);
assertEquals(expectedTokens, actualTokens);
assertEquals(expectedTokensForString, actualTokens);
}
@Test
public void givenFile_thenGetListOfString() {
List<String> actualTokens = application.getTokensFromFile("data.csv", "|");
assertEquals(expectedTokensForFile, actualTokens);
}
}
+93
View File
@@ -0,0 +1,93 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
<groupId>com.baeldung</groupId>
<artifactId>cucumber</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<java.source.version>1.8</java.source.version>
<java.target.version>1.8</java.target.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.plugin.version>3.5.1</maven.compiler.plugin.version>
<slf4j.version>1.7.21</slf4j.version>
<logback.version>1.1.7</logback.version>
<junit.version>4.12</junit.version>
<hamcrest.library.version>1.3</hamcrest.library.version>
<cucumber.version>1.2.5</cucumber.version>
<surefire.plugin.version>2.19.1</surefire.plugin.version>
</properties>
<modules>
</modules>
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${hamcrest.library.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<source>${java.source.version}</source>
<target>${java.target.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<includes>
<include>**/CalculatorTest.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,7 @@
package com.baeldung.cucumber.calculator;
public class Calculator {
public int add(int a, int b) {
return a + b;
}
}
@@ -0,0 +1,38 @@
package com.baeldung.cucumber.calculator;
import org.hamcrest.Matchers;
import org.junit.Assert;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class CalculatorRunSteps {
private int total;
private Calculator calculator;
@Before
private void init() {
total = -999;
}
@Given("^I have a calculator$")
public void initializeCalculator() throws Throwable {
calculator = new Calculator();
}
@When("^I add (-?\\d+) and (-?\\d+)$")
public void testAdd(int num1, int num2) throws Throwable {
total = calculator.add(num1, num2);
}
@Then("^the result should be (-?\\d+)$")
public void validateResult(int result) throws Throwable {
Assert.assertThat(total, Matchers.equalTo(result));
}
}
@@ -0,0 +1,15 @@
package com.baeldung.cucumber.calculator;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(
features={"classpath:features/calculator.feature", "classpath:features/calculator-scenario-outline.feature"}
, plugin = { "pretty", "json:target/reports/json/calculator.json" }
, glue = {"com.baeldung.cucumber.calculator"}
)
public class CalculatorTest {
}
@@ -0,0 +1,16 @@
Feature: Calculator
As a user
I want to use a calculator to add numbers
So that I don't need to add myself
Scenario Outline: Add two numbers <num1> & <num2>
Given I have a calculator
When I add <num1> and <num2>
Then the result should be <total>
Examples:
| num1 | num2 | total |
| -2 | 3 | 1 |
| 10 | 15 | 25 |
| 99 | -99 | 0 |
| -1 | -10 | -11 |
@@ -0,0 +1,24 @@
Feature: Calculator
As a user
I want to use a calculator to add numbers
So that I don't need to add myself
Scenario: Add two numbers -2 & 3
Given I have a calculator
When I add -2 and 3
Then the result should be 1
Scenario: Add two numbers 10 & 15
Given I have a calculator
When I add 10 and 15
Then the result should be 25
Scenario: Add two numbers 99 & -99
Given I have a calculator
When I add 99 and -99
Then the result should be 0
Scenario: Add two numbers -1 & -10
Given I have a calculator
When I add -1 and -10
Then the result should be -11
@@ -0,0 +1,13 @@
<configuration scan="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n%ex
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -1,13 +1,9 @@
package com.baeldung.jaxws.exception;
import java.io.Serializable;
import javax.xml.ws.WebFault;
@WebFault
public class EmployeeAlreadyExists extends Exception implements Serializable {
private static final long serialVersionUID = 1L;
public class EmployeeAlreadyExists extends Exception {
public EmployeeAlreadyExists() {
super("This employee already exists");
@@ -1,10 +1,9 @@
package com.baeldung.jaxws.exception;
import javax.xml.ws.WebFault;
import java.io.Serializable;
@WebFault
public class EmployeeNotFound extends Exception implements Serializable {
public class EmployeeNotFound extends Exception {
public EmployeeNotFound() {
super("The specified employee does not exist");
@@ -1,9 +1,6 @@
package com.baeldung.jaxws.model;
import java.io.Serializable;
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
public class Employee {
private int id;
private String firstName;
+3
View File
@@ -215,6 +215,7 @@
<module>rabbitmq</module>
<module>vertx</module>
<module>spring-data-gemfire</module>
<module>cucumber</module>
</modules>
<build>
@@ -233,6 +234,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>3</forkCount>
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
+2
View File
@@ -181,6 +181,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>3</forkCount>
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
+2
View File
@@ -32,6 +32,8 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<forkCount>3</forkCount>
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
+2
View File
@@ -21,6 +21,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>3</forkCount>
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>

Some files were not shown because too many files have changed in this diff Show More