BAEL-518 google protocol buffers
* BEEL-518 code for protobuf article * BEEL-518 add generated protobuf class * BAEL-701 updated the method argument * BEEL-550 use newest version of protobuff * Bael 389 - Building URL dynamically between host and pathname (#1323) * Project for " A Guide to the Java API for WebSocket" article * Setting dependencies correctly * Formatting adjustments * Removing tomcat7 maven plugin * Applying formatt - No spaces * BAEL-389 - Building URL dynamically between host and pathname * Rename classes (#1331) * BAEL-550 Axon framework * BEEL-550 create axon module * BEEL-550 proper naming * BEEL-550 better example of message service * BEEL-550 proper name of method * BEEL-550 remove not needed comments * BEEL-550 proper message * BEEL-550 axon test scope test * BEEL-550 tries to migrate to axon 3 * BEEL-550 migrate to vesrion 3 successfull * ACO refactor * BAEL-518 Small refactoring in protobuffer module
This commit is contained in:
parent
6e00fd16d5
commit
8dcb9af712
2
pom.xml
2
pom.xml
|
@ -94,6 +94,7 @@
|
|||
|
||||
<module>patterns</module>
|
||||
<module>pdf</module>
|
||||
<module>protobuffer</module>
|
||||
|
||||
<module>querydsl</module>
|
||||
|
||||
|
@ -204,7 +205,6 @@
|
|||
|
||||
<module>rabbitmq</module>
|
||||
|
||||
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>protobuffer</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.protobuf</groupId>
|
||||
<artifactId>protobuf-java</artifactId>
|
||||
<version>${protobuf.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<protobuf.version>3.2.0</protobuf.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,27 @@
|
|||
package protobuf;
|
||||
|
||||
option java_package = "com.baeldung.protobuf";
|
||||
option java_outer_classname = "AddressBookProtos";
|
||||
|
||||
message Person {
|
||||
required string name = 1;
|
||||
required int32 id = 2;
|
||||
optional string email = 3;
|
||||
|
||||
enum PhoneType {
|
||||
MOBILE = 0;
|
||||
HOME = 1;
|
||||
WORK = 2;
|
||||
}
|
||||
|
||||
message PhoneNumber {
|
||||
required string number = 1;
|
||||
optional PhoneType type = 2 [default = HOME];
|
||||
}
|
||||
|
||||
repeated PhoneNumber phones = 4;
|
||||
}
|
||||
|
||||
message AddressBook {
|
||||
repeated Person people = 1;
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
package com.baeldung.protobuf;
|
||||
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ProtobufTest {
|
||||
private final String filePath = "address_book";
|
||||
|
||||
@After
|
||||
public void cleanup() throws IOException {
|
||||
Files.deleteIfExists(Paths.get(filePath));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGeneratedProtobufClass_whenCreateClass_thenShouldCreateJavaInstance() {
|
||||
//when
|
||||
String email = "j@baeldung.com";
|
||||
int id = new Random().nextInt();
|
||||
String name = "Michael Program";
|
||||
String number = "01234567890";
|
||||
AddressBookProtos.Person.PhoneType type = AddressBookProtos.Person.PhoneType.HOME;
|
||||
AddressBookProtos.Person person =
|
||||
AddressBookProtos.Person.newBuilder()
|
||||
.setId(id)
|
||||
.setName(name)
|
||||
.setEmail(email)
|
||||
.addPhones(
|
||||
AddressBookProtos.Person.PhoneNumber.newBuilder()
|
||||
.setNumber(number)
|
||||
.setType(type))
|
||||
.build();
|
||||
//then
|
||||
assertEquals(person.getEmail(), email);
|
||||
assertEquals(person.getId(), id);
|
||||
assertEquals(person.getName(), name);
|
||||
assertEquals(person.getPhones(0).getNumber(), number);
|
||||
assertEquals(person.getPhones(0).getType(), type);
|
||||
assertEquals(person.getPhonesList().size(), 1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenAddressBookWithOnePerson_whenSaveAsAFile_shouldLoadFromFileToJavaClass() throws IOException {
|
||||
//given
|
||||
String email = "j@baeldung.com";
|
||||
int id = new Random().nextInt();
|
||||
String name = "Michael Program";
|
||||
String number = "01234567890";
|
||||
AddressBookProtos.Person.PhoneType type = AddressBookProtos.Person.PhoneType.HOME;
|
||||
AddressBookProtos.Person person =
|
||||
AddressBookProtos.Person.newBuilder()
|
||||
.setId(id)
|
||||
.setName(name)
|
||||
.setEmail(email)
|
||||
.addPhones(
|
||||
AddressBookProtos.Person.PhoneNumber.newBuilder()
|
||||
.setNumber(number)
|
||||
.setType(type))
|
||||
.build();
|
||||
|
||||
//when
|
||||
AddressBookProtos.AddressBook addressBook = AddressBookProtos.AddressBook.newBuilder().addPeople(person).build();
|
||||
FileOutputStream fos = new FileOutputStream(filePath);
|
||||
addressBook.writeTo(fos);
|
||||
fos.close();
|
||||
|
||||
//then
|
||||
FileInputStream fis = new FileInputStream(filePath);
|
||||
AddressBookProtos.AddressBook deserialized =
|
||||
AddressBookProtos.AddressBook.newBuilder().mergeFrom(fis).build();
|
||||
fis.close();
|
||||
assertEquals(deserialized.getPeople(0).getEmail(), email);
|
||||
assertEquals(deserialized.getPeople(0).getId(), id);
|
||||
assertEquals(deserialized.getPeople(0).getName(), name);
|
||||
assertEquals(deserialized.getPeople(0).getPhones(0).getNumber(), number);
|
||||
assertEquals(deserialized.getPeople(0).getPhones(0).getType(), type);
|
||||
assertEquals(deserialized.getPeople(0).getPhonesList().size(), 1);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue