BAEL-3091: The Prototype Pattern in Java (changed code based on valid comments from a reader)

This commit is contained in:
Vivek Balasubramaniam
2019-10-29 22:27:15 +05:30
parent db85c8f275
commit d3d5b060e7
20517 changed files with 1642290 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
## Vert.x
This module contains articles about Vert.x
### Relevant articles
- [Introduction to Vert.x](https://www.baeldung.com/vertx)
+71
View File
@@ -0,0 +1,71 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>vertx</artifactId>
<version>1.0-SNAPSHOT</version>
<name>vertx</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-unit</artifactId>
<version>${vertx.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>io.vertx.core.Starter</Main-Class>
<Main-Verticle>com.baeldung.SimpleServerVerticle</Main-Verticle>
</manifestEntries>
</transformer>
</transformers>
<artifactSet />
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-app.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<vertx.version>3.8.1</vertx.version>
<maven-shade-plugin.version>3.2.1</maven-shade-plugin.version>
</properties>
</project>
@@ -0,0 +1,28 @@
package com.baeldung;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
public class HelloVerticle extends AbstractVerticle {
private static final Logger LOGGER = LoggerFactory.getLogger(HelloVerticle.class);
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new HelloVerticle());
}
@Override
public void start(Future<Void> future) {
LOGGER.info("Welcome to Vertx");
}
@Override
public void stop() {
LOGGER.info("Shutting down application");
}
}
@@ -0,0 +1,24 @@
package com.baeldung;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
public class SimpleServerVerticle extends AbstractVerticle {
@Override
public void start(Future<Void> future) {
vertx.createHttpServer()
.requestHandler(
r -> r.response().end("Welcome to Vert.x Intro"))
.listen(config().getInteger("http.port", 8080), result -> {
if (result.succeeded()) {
future.complete();
} else {
future.fail(result.cause());
}
});
}
}
@@ -0,0 +1,59 @@
package com.baeldung.model;
public class Article {
private String id;
private String content;
private String author;
private String datePublished;
private int wordCount;
public Article(String id, String content, String author, String datePublished, int wordCount) {
super();
this.id = id;
this.content = content;
this.author = author;
this.datePublished = datePublished;
this.wordCount = wordCount;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDatePublished() {
return datePublished;
}
public void setDatePublished(String datePublished) {
this.datePublished = datePublished;
}
public int getWordCount() {
return wordCount;
}
public void setWordCount(int wordCount) {
this.wordCount = wordCount;
}
}
@@ -0,0 +1,41 @@
package com.baeldung.rest;
import com.baeldung.model.Article;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.json.Json;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
public class RestServiceVerticle extends AbstractVerticle {
@Override
public void start(Future<Void> future) {
Router router = Router.router(vertx);
router.get("/api/baeldung/articles/article/:id")
.handler(this::getArticles);
vertx.createHttpServer()
.requestHandler(router::accept)
.listen(config().getInteger("http.port", 8080), result -> {
if (result.succeeded()) {
future.complete();
} else {
future.fail(result.cause());
}
});
}
private void getArticles(RoutingContext routingContext) {
String articleId = routingContext.request()
.getParam("id");
Article article = new Article(articleId, "This is an intro to vertx", "baeldung", "01-02-2017", 1578);
routingContext.response()
.putHeader("content-type", "application/json")
.setStatusCode(200)
.end(Json.encodePrettily(article));
}
}
+3
View File
@@ -0,0 +1,3 @@
{
"http.port":8080
}
+19
View File
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -0,0 +1,66 @@
package com.baeldung;
import java.io.IOException;
import java.net.ServerSocket;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.baeldung.rest.RestServiceVerticle;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
@RunWith(VertxUnitRunner.class)
public class RestServiceVerticleUnitTest {
private Vertx vertx;
private int port = 8081;
@BeforeClass
public static void beforeClass() {
}
@Before
public void setup(TestContext testContext) throws IOException {
vertx = Vertx.vertx();
// Pick an available and random
ServerSocket socket = new ServerSocket(0);
port = socket.getLocalPort();
socket.close();
DeploymentOptions options = new DeploymentOptions().setConfig(new JsonObject().put("http.port", port));
vertx.deployVerticle(RestServiceVerticle.class.getName(), options, testContext.asyncAssertSuccess());
}
@After
public void tearDown(TestContext testContext) {
vertx.close(testContext.asyncAssertSuccess());
}
@Test
public void givenId_whenReceivedArticle_thenSuccess(TestContext testContext) {
final Async async = testContext.async();
vertx.createHttpClient()
.getNow(port, "localhost", "/api/baeldung/articles/article/12345", response -> {
response.handler(responseBody -> {
testContext.assertTrue(responseBody.toString()
.contains("\"id\" : \"12345\""));
async.complete();
});
});
}
}
@@ -0,0 +1,55 @@
package com.baeldung;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import java.io.IOException;
import java.net.ServerSocket;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(VertxUnitRunner.class)
public class SimpleServerVerticleUnitTest {
private Vertx vertx;
private int port = 8081;
@Before
public void setup(TestContext testContext) throws IOException {
vertx = Vertx.vertx();
// Pick an available and random
ServerSocket socket = new ServerSocket(0);
port = socket.getLocalPort();
socket.close();
DeploymentOptions options = new DeploymentOptions().setConfig(new JsonObject().put("http.port", port));
vertx.deployVerticle(SimpleServerVerticle.class.getName(), options, testContext.asyncAssertSuccess());
}
@After
public void tearDown(TestContext testContext) {
vertx.close(testContext.asyncAssertSuccess());
}
@Test
public void whenReceivedResponse_thenSuccess(TestContext testContext) {
final Async async = testContext.async();
vertx.createHttpClient()
.getNow(port, "localhost", "/", response -> response.handler(responseBody -> {
testContext.assertTrue(responseBody.toString()
.contains("Welcome"));
async.complete();
}));
}
}