Merge remote-tracking branch 'upstream/master'

This commit is contained in:
tschiman 2017-05-31 16:31:17 -06:00
commit 73b3c09dd0
66 changed files with 2010 additions and 166 deletions

View File

@ -0,0 +1,85 @@
package com.baeldung.java9;
public class Java9OptionalTest {
@Test
public void givenOptionalOfSome_whenToStream_thenShouldTreatItAsOneElementStream() {
//given
Optional<String> value = Optional.of("a");
//when
List<String> collect = value.stream().map(String::toUpperCase).collect(Collectors.toList());
//then
assertThat(collect).hasSameElementsAs(List.of("A"));
}
@Test
public void givenOptionalOfNone_whenToStream_thenShouldTreatItAsZeroElementStream() {
//given
Optional<String> value = Optional.empty();
//when
List<String> collect = value.stream().map(String::toUpperCase).collect(Collectors.toList());
//then
assertThat(collect).isEmpty();
}
@Test
public void givenOptional_whenPresent_thenShouldExecuteProperCallback() {
//given
Optional<String> value = Optional.of("properValue");
AtomicInteger successCounter = new AtomicInteger(0);
AtomicInteger onEmptyOptionalCounter = new AtomicInteger(0);
//when
value.ifPresentOrElse((v) -> successCounter.incrementAndGet(), onEmptyOptionalCounter::incrementAndGet);
//then
assertThat(successCounter.get()).isEqualTo(1);
assertThat(onEmptyOptionalCounter.get()).isEqualTo(0);
}
@Test
public void givenOptional_whenNotPresent_thenShouldExecuteProperCallback() {
//given
Optional<String> value = Optional.empty();
AtomicInteger successCounter = new AtomicInteger(0);
AtomicInteger onEmptyOptionalCounter = new AtomicInteger(0);
//when
value.ifPresentOrElse((v) -> successCounter.incrementAndGet(), onEmptyOptionalCounter::incrementAndGet);
//then
assertThat(successCounter.get()).isEqualTo(0);
assertThat(onEmptyOptionalCounter.get()).isEqualTo(1);
}
@Test
public void givenOptional_whenPresent_thenShouldTakeAValueFromIt() {
//given
String expected = "properValue";
Optional<String> value = Optional.of(expected);
Optional<String> defaultValue = Optional.of("default");
//when
Optional<String> result = value.or(() -> defaultValue);
//then
assertThat(result.get()).isEqualTo(expected);
}
@Test
public void givenOptional_whenEmpty_thenShouldTakeAValueFromOr() {
//given
String defaultString = "default";
Optional<String> value = Optional.empty();
Optional<String> defaultValue = Optional.of(defaultString);
//when
Optional<String> result = value.or(() -> defaultValue);
//then
assertThat(result.get()).isEqualTo(defaultString);
}
}

View File

@ -102,8 +102,10 @@
- [How to Perform a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request)
- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection)
- [Guide to DelayQueue](http://www.baeldung.com/java-delay-queue)
- [Guide to UUID in JAVA] (http://www.baeldung.com/guide-to-uuid-in-java)
- [Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java](http://www.baeldung.com/java-path)
- [How to Add a Single Element to a Stream](http://www.baeldung.com/java-stream-append-prepend)
- [Iterating Over Enum Values in Java](http://www.baeldung.com/java-enum-iteration)
- [A Guide to Java SynchronousQueue](http://www.baeldung.com/java-synchronous-queue)
- [Guide to the Java TransferQueue](http://www.baeldung.com/java-transfer-queue)
- [Kotlin Java Interoperability](http://www.baeldung.com/kotlin-java-interoperability)

View File

@ -0,0 +1,29 @@
package com.baeldung.concurrent.diningphilosophers;
public class DiningPhilosophers {
public static void main(String[] args) throws Exception {
Philosopher[] philosophers = new Philosopher[5];
Object[] forks = new Object[philosophers.length];
for (int i = 0; i < forks.length; i++) {
forks[i] = new Object();
}
for (int i = 0; i < philosophers.length; i++) {
Object leftFork = forks[i];
Object rightFork = forks[(i + 1) % forks.length];
if (i == philosophers.length - 1) {
philosophers[i] = new Philosopher(rightFork, leftFork); // The last philosopher picks up the right fork first
} else {
philosophers[i] = new Philosopher(leftFork, rightFork);
}
Thread t = new Thread(philosophers[i], "Philosopher " + (i + 1));
t.start();
}
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.concurrent.diningphilosophers;
public class Philosopher implements Runnable {
private final Object leftFork;
private final Object rightFork;
public Philosopher(Object left, Object right) {
this.leftFork = left;
this.rightFork = right;
}
private void doAction(String action) throws InterruptedException {
System.out.println(Thread.currentThread().getName() + " " + action);
Thread.sleep(((int) (Math.random() * 100)));
}
@Override public void run() {
try {
while (true) {
doAction(System.nanoTime() + ": Thinking"); // thinking
synchronized (leftFork) {
doAction(System.nanoTime() + ": Picked up left fork");
synchronized (rightFork) {
doAction(System.nanoTime() + ": Picked up right fork - eating"); // eating
doAction(System.nanoTime() + ": Put down right fork");
}
doAction(System.nanoTime() + ": Put down left fork. Returning to thinking");
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
}

View File

@ -1,25 +0,0 @@
package com.baeldung.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EscapingChars {
public boolean isMatching(String input, String pattern) {
return input.matches(pattern);
}
public int splitAndCountWords(String input, String pattern) {
return input.split(pattern).length;
}
public int splitAndCountWordsUsingQuoteMethod(String input, String pattern) {
return input.split(Pattern.quote(pattern)).length;
}
public String changeCurrencySymbol(String input, String pattern,
String correctStr) {
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(input);
return m.replaceAll(correctStr);
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.stream;
import java.util.List;
import java.util.stream.Stream;
public class StreamApi {
public String getLastElementUsingReduce(List<String> valueList) {
Stream<String> stream = valueList.stream();
return stream.reduce((first, second) -> second).orElse(null);
}
public Integer getInfiniteStreamLastElementUsingReduce() {
Stream<Integer> stream = Stream.iterate(0, i -> i + 1);
return stream.limit(20).reduce((first, second) -> second).orElse(null);
}
public String getLastElementUsingSkip(List<String> valueList) {
long count = valueList.stream().count();
Stream<String> stream = valueList.stream();
return stream.skip(count - 1).findFirst().orElse(null);
}
}

View File

@ -2,23 +2,23 @@ package com.baeldung.string;
import java.util.Optional;
public class StringHelper {
public static String removeLastChar(String s) {
class StringHelper {
static String removeLastChar(String s) {
return (s == null || s.length() == 0) ? s : (s.substring(0, s.length() - 1));
}
public static String removeLastCharRegex(String s) {
static String removeLastCharRegex(String s) {
return (s == null) ? s : s.replaceAll(".$", "");
}
public static String removeLastCharOptional(String s) {
static String removeLastCharOptional(String s) {
return Optional.ofNullable(s)
.filter(str -> str.length() != 0)
.map(str -> str.substring(0, str.length() - 1))
.orElse(s);
}
public static String removeLastCharRegexOptional(String s) {
static String removeLastCharRegexOptional(String s) {
return Optional.ofNullable(s)
.map(str -> str.replaceAll(".$", ""))
.orElse(s);

View File

@ -0,0 +1,118 @@
package com.baeldung.uuid;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
public class UUIDGenerator {
/**
* These are predefined UUID for name spaces
*/
private static final String NAMESPACE_DNS = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
private static final String NAMESPACE_URL = "6ba7b811-9dad-11d1-80b4-00c04fd430c8";
private static final String NAMESPACE_OID = "6ba7b812-9dad-11d1-80b4-00c04fd430c8";
private static final String NAMESPACE_X500 = "6ba7b814-9dad-11d1-80b4-00c04fd430c8";
private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
public static void main(String[] args) {
try {
System.out.println("Type 3 : " + generateType3UUID(NAMESPACE_DNS, "google.com"));
System.out.println("Type 4 : " + generateType4UUID());
System.out.println("Type 5 : " + generateType5UUID(NAMESPACE_URL, "google.com"));
System.out.println("Unique key : " + generateUniqueKeysWithUUIDAndMessageDigest());
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* Type 4 UUID Generation
*/
public static UUID generateType4UUID() {
UUID uuid = UUID.randomUUID();
return uuid;
}
/**
* Type 3 UUID Generation
*
* @throws UnsupportedEncodingException
*/
public static UUID generateType3UUID(String namespace, String name) throws UnsupportedEncodingException {
String source = namespace + name;
byte[] bytes = source.getBytes("UTF-8");
UUID uuid = UUID.nameUUIDFromBytes(bytes);
return uuid;
}
/**
* Type 5 UUID Generation
*
* @throws UnsupportedEncodingException
*/
public static UUID generateType5UUID(String namespace, String name) throws UnsupportedEncodingException {
String source = namespace + name;
byte[] bytes = source.getBytes("UTF-8");
UUID uuid = type5UUIDFromBytes(bytes);
return uuid;
}
public static UUID type5UUIDFromBytes(byte[] name) {
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException nsae) {
throw new InternalError("MD5 not supported", nsae);
}
byte[] bytes = md.digest(name);
bytes[6] &= 0x0f; /* clear version */
bytes[6] |= 0x50; /* set to version 5 */
bytes[8] &= 0x3f; /* clear variant */
bytes[8] |= 0x80; /* set to IETF variant */
return constructType5UUID(bytes);
}
private static UUID constructType5UUID(byte[] data) {
long msb = 0;
long lsb = 0;
assert data.length == 16 : "data must be 16 bytes in length";
for (int i=0; i<8; i++)
msb = (msb << 8) | (data[i] & 0xff);
for (int i=8; i<16; i++)
lsb = (lsb << 8) | (data[i] & 0xff);
return new UUID(msb, lsb);
}
/**
* Unique Keys Generation Using Message Digest and Type 4 UUID
*
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
*/
public static String generateUniqueKeysWithUUIDAndMessageDigest() throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest salt = MessageDigest.getInstance("SHA-256");
salt.update(UUID.randomUUID()
.toString()
.getBytes("UTF-8"));
String digest = bytesToHex(salt.digest());
return digest;
}
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
}

View File

@ -3,6 +3,8 @@ package com.baeldung.regexp;
import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
@ -11,57 +13,59 @@ public class EscapingCharsTest {
public void givenRegexWithDot_whenMatchingStr_thenMatches() {
String strInput = "foof";
String strRegex = "foo.";
EscapingChars e = new EscapingChars();
assertEquals(true, e.isMatching(strInput, strRegex));
assertEquals(true, strInput.matches(strRegex));
}
@Test
public void givenRegexWithDotEsc_whenMatchingStr_thenNotMatching() {
String strInput = "foof";
String strRegex = "foo\\.";
EscapingChars e = new EscapingChars();
assertEquals(false, e.isMatching(strInput, strRegex));
assertEquals(false, strInput.matches(strRegex));
}
@Test
public void givenRegexWithPipeEscaped_whenSplitStr_thenSplits() {
String strInput = "foo|bar|hello|world";
String strRegex = "\\Q|\\E";
EscapingChars e = new EscapingChars();
assertEquals(4, e.splitAndCountWords(strInput, strRegex));
assertEquals(4, strInput.split(strRegex).length);
}
@Test
public void givenRegexWithPipeEscQuoteMeth_whenSplitStr_thenSplits() {
String strInput = "foo|bar|hello|world";
String strRegex = "|";
EscapingChars e = new EscapingChars();
assertEquals(4, e.splitAndCountWordsUsingQuoteMethod(strInput, strRegex));
assertEquals(4,strInput.split(Pattern.quote(strRegex)).length);
}
@Test
public void givenRegexWithDollar_whenReplacing_thenNotReplace() {
String strInput = "I gave $50 to my brother.He bought candy for $35. Now he has $15 left.";
String strInput = "I gave $50 to my brother."
+ "He bought candy for $35. Now he has $15 left.";
String strRegex = "$";
String strReplacement = "<EFBFBD>";
String output = "I gave <20>50 to my brother.He bought candy for <20>35. Now he has <20>15 left.";
EscapingChars e = new EscapingChars();
assertThat(output, not(equalTo(e.changeCurrencySymbol(strInput, strRegex, strReplacement))));
String strReplacement = "£";
String output = "I gave £50 to my brother."
+ "He bought candy for £35. Now he has £15 left.";
Pattern p = Pattern.compile(strRegex);
Matcher m = p.matcher(strInput);
assertThat(output, not(equalTo(m.replaceAll(strReplacement))));
}
@Test
public void givenRegexWithDollarEsc_whenReplacing_thenReplace() {
String strInput = "I gave $50 to my brother. He bought candy for $35. Now he has $15 left.";
String strInput = "I gave $50 to my brother."
+ "He bought candy for $35. Now he has $15 left.";
String strRegex = "\\$";
String strReplacement = "<EFBFBD>";
String output = "I gave <20>50 to my brother. He bought candy for <20>35. Now he has <20>15 left.";
EscapingChars e = new EscapingChars();
assertEquals(output, e.changeCurrencySymbol(strInput, strRegex, strReplacement));
String strReplacement = "£";
String output = "I gave £50 to my brother."
+ "He bought candy for £35. Now he has £15 left.";
Pattern p = Pattern.compile(strRegex);
Matcher m = p.matcher(strInput);
assertEquals(output,m.replaceAll(strReplacement));
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.stream;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class StreamApiTest {
private StreamApi streamApi = new StreamApi();
@Test
public void givenList_whenGetLastElementUsingReduce_thenReturnLastElement() {
List<String> valueList = new ArrayList<String>();
valueList.add("Joe");
valueList.add("John");
valueList.add("Sean");
String last = streamApi.getLastElementUsingReduce(valueList);
assertEquals("Sean", last);
}
@Test
public void givenInfiniteStream_whenGetInfiniteStreamLastElementUsingReduce_thenReturnLastElement() {
Integer last = streamApi.getInfiniteStreamLastElementUsingReduce();
assertEquals(new Integer(19), last);
}
@Test
public void givenListAndCount_whenGetLastElementUsingSkip_thenReturnLastElement() {
List<String> valueList = new ArrayList<String>();
valueList.add("Joe");
valueList.add("John");
valueList.add("Sean");
String last = streamApi.getLastElementUsingSkip(valueList);
assertEquals("Sean", last);
}
}

View File

@ -167,9 +167,11 @@ public class JavaRandomUnitTest {
final int leftLimit = 97; // letter 'a'
final int rightLimit = 122; // letter 'z'
final int targetStringLength = 10;
final Random random = new Random();
final StringBuilder buffer = new StringBuilder(targetStringLength);
for (int i = 0; i < targetStringLength; i++) {
final int randomLimitedInt = leftLimit + (int) (new Random().nextFloat() * (rightLimit - leftLimit + 1));
final int randomLimitedInt = leftLimit + (int) (random.nextFloat() * (rightLimit - leftLimit + 1));
buffer.append((char) randomLimitedInt);
}
final String generatedString = buffer.toString();

View File

@ -0,0 +1,106 @@
<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>
<parent>
<groupId>com.baeldung.ejb</groupId>
<artifactId>ejb</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>ejb-session-beans</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<arquillian-bom.version>1.1.12.Final</arquillian-bom.version>
<shrinkwrap-resolver-impl-maven.version>2.2.6</shrinkwrap-resolver-impl-maven.version>
<arquillian-junit-container.version>1.1.12.Final</arquillian-junit-container.version>
<arquillian-glassfish-embedded-3.1.version>1.0.0.Final</arquillian-glassfish-embedded-3.1.version>
<junit.version>4.12</junit.version>
<javaee-api.version>7.0</javaee-api.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.13.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>arquillian-glassfish-embedded</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-glassfish-embedded-3.1</artifactId>
<version>1.0.0.CR4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,11 @@
package com.baeldung.ejb.stateful;
import javax.ejb.EJB;
public class EJBClient1 {
@EJB
public StatefulEJB statefulEJB;
}

View File

@ -0,0 +1,11 @@
package com.baeldung.ejb.stateful;
import javax.ejb.EJB;
public class EJBClient2 {
@EJB
public StatefulEJB statefulEJB;
}

View File

@ -0,0 +1,10 @@
package com.baeldung.ejb.stateful;
import javax.ejb.Stateful;
@Stateful
public class StatefulEJB {
public String name;
}

View File

@ -0,0 +1,10 @@
package com.baeldung.ejb.stateless;
import javax.ejb.EJB;
public class EJBClient1 {
@EJB
public StatelessEJB statelessEJB;
}

View File

@ -0,0 +1,11 @@
package com.baeldung.ejb.stateless;
import javax.ejb.EJB;
public class EJBClient2 {
@EJB
public StatelessEJB statelessEJB;
}

View File

@ -0,0 +1,11 @@
package com.baeldung.ejb.stateless;
import javax.ejb.Stateless;
@Stateless
public class StatelessEJB {
public String name;
}

View File

@ -0,0 +1,51 @@
package com.baeldung.ejb.test.stateful;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.baeldung.ejb.stateful.EJBClient1;
import com.baeldung.ejb.stateful.EJBClient2;
import com.baeldung.ejb.stateful.StatefulEJB;
import javax.inject.Inject;
@RunWith(Arquillian.class)
public class StatefulEJBTest {
@Inject
private EJBClient1 ejbClient1;
@Inject
private EJBClient2 ejbClient2;
@Test
public void givenOneStatefulBean_whenTwoClientsSetValueOnBean_thenClientStateIsMaintained() {
// act
ejbClient1.statefulEJB.name = "Client 1";
ejbClient2.statefulEJB.name = "Client 2";
// assert
Assert.assertNotEquals(ejbClient1.statefulEJB.name, ejbClient2.statefulEJB.name);
Assert.assertEquals("Client 1", ejbClient1.statefulEJB.name);
Assert.assertEquals("Client 2", ejbClient2.statefulEJB.name);
}
@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class)
.addClass(StatefulEJB.class)
.addClass(EJBClient1.class)
.addClass(EJBClient2.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
}

View File

@ -0,0 +1,62 @@
package com.baeldung.ejb.test.stateless;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.baeldung.ejb.stateless.EJBClient1;
import com.baeldung.ejb.stateless.EJBClient2;
import com.baeldung.ejb.stateless.StatelessEJB;
import javax.inject.Inject;
@RunWith(Arquillian.class)
public class StatelessEJBTest {
@Inject
private EJBClient1 ejbClient1;
@Inject
private EJBClient2 ejbClient2;
@Test
public void givenOneStatelessBean_whenStateIsSetInOneBean_secondBeanShouldHaveSameState() {
// act
ejbClient1.statelessEJB.name = "Client 1";
// assert
Assert.assertEquals("Client 1", ejbClient1.statelessEJB.name);
Assert.assertEquals("Client 1", ejbClient2.statelessEJB.name);
}
@Test
public void givenOneStatelessBean_whenStateIsSetInBothBeans_secondBeanShouldHaveSecondBeanState() {
// act
ejbClient1.statelessEJB.name = "Client 1";
ejbClient2.statelessEJB.name = "Client 2";
// assert
Assert.assertEquals("Client 2", ejbClient2.statelessEJB.name);
}
@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class)
.addClass(StatelessEJB.class)
.addClass(EJBClient1.class)
.addClass(EJBClient2.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
}

View File

@ -40,7 +40,12 @@
<version>1.0-SNAPSHOT</version>
<type>ejb</type>
</dependency>
<dependency>
<groupId>com.baeldung.ejb</groupId>
<artifactId>ejb-session-beans</artifactId>
<version>1.0-SNAPSHOT</version>
<type>ejb</type>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
@ -75,5 +80,7 @@
<modules>
<module>ejb-remote</module>
<module>ejb-client</module>
<module>ejb-session-beans</module>
<module>ejb-session-beans-client</module>
</modules>
</project>

View File

@ -15,6 +15,8 @@
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Maven plugins -->
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
</properties>
<dependencies>
<dependency>
@ -22,6 +24,11 @@
<artifactId>hibernate-core</artifactId>
<version>5.2.9.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
@ -37,6 +44,32 @@
</resource>
</resources>
<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>
<plugin>
<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>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,5 +1,9 @@
package com.baeldung.hibernate;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
@ -20,44 +24,56 @@ import com.baeldung.hibernate.pojo.Supplier;
public class HibernateMultiTenantUtil {
private static SessionFactory sessionFactory;
private static Map<String, ConnectionProvider> connectionProviderMap = new HashMap<>();
private static final String[] tenantDBNames = { "mydb1","mydb2"};
private static final String[] tenantDBNames = { "mydb1", "mydb2" };
public static SessionFactory getSessionFactory() throws UnsupportedTenancyException {
public static SessionFactory getSessionFactory() throws UnsupportedTenancyException, IOException {
if (sessionFactory == null) {
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = configureServiceRegistry(configuration);
sessionFactory = makeSessionFactory (serviceRegistry);
// sessionFactory = configuration.buildSessionFactory(serviceRegistry);
// Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = configureServiceRegistry();
sessionFactory = makeSessionFactory(serviceRegistry);
}
return sessionFactory;
}
private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry) {
MetadataSources metadataSources = new MetadataSources( serviceRegistry );
for(Class annotatedClasses : getAnnotatedClasses()) {
metadataSources.addAnnotatedClass( annotatedClasses );
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
for (Class annotatedClasses : getAnnotatedClasses()) {
metadataSources.addAnnotatedClass(annotatedClasses);
}
Metadata metadata = metadataSources.buildMetadata();
return metadata.getSessionFactoryBuilder().build();
return metadata.getSessionFactoryBuilder()
.build();
}
private static Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Supplier.class
};
return new Class<?>[] { Supplier.class };
}
private static ServiceRegistry configureServiceRegistry(Configuration configuration) throws UnsupportedTenancyException {
Properties properties = configuration.getProperties();
private static ServiceRegistry configureServiceRegistry() throws UnsupportedTenancyException, IOException {
// Properties properties = configuration.getProperties();
Properties properties = getProperties();
connectionProviderMap = setUpConnectionProviders(properties, tenantDBNames);
properties.put(AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER, new ConfigurableMultiTenantConnectionProvider(connectionProviderMap));
return new StandardServiceRegistryBuilder().applySettings(properties).build();
return new StandardServiceRegistryBuilder().applySettings(properties)
.build();
}
private static Properties getProperties() throws IOException {
Properties properties = new Properties();
URL propertiesURL = Thread.currentThread()
.getContextClassLoader()
.getResource("hibernate.properties");
FileInputStream inputStream = new FileInputStream(propertiesURL.getFile());
properties.load(inputStream);
System.out.println("LOADED PROPERTIES FROM hibernate.properties");
return properties;
}
private static Map<String, ConnectionProvider> setUpConnectionProviders(Properties properties, String[] tenantNames) throws UnsupportedTenancyException {
@ -66,23 +82,27 @@ public class HibernateMultiTenantUtil {
DriverManagerConnectionProviderImpl connectionProvider = new DriverManagerConnectionProviderImpl();
String tenantStrategy = properties.getProperty("hibernate.multiTenancy");
System.out.println("Strategy:"+tenantStrategy);
System.out.println("Strategy:" + tenantStrategy);
properties.put(Environment.URL, tenantUrl(properties.getProperty(Environment.URL), tenant, tenantStrategy));
System.out.println("URL:"+properties.getProperty(Environment.URL));
System.out.println("URL:" + properties.getProperty(Environment.URL));
connectionProvider.configure(properties);
System.out.println("Tenant:"+tenant);
System.out.println("Tenant:" + tenant);
providerMap.put(tenant, connectionProvider);
}
System.out.println("Added connections for:");
providerMap.keySet().stream().forEach(System.out::println);
providerMap.keySet()
.stream()
.forEach(System.out::println);
return providerMap;
}
private static Object tenantUrl(String originalUrl, String tenant, String tenantStrategy) throws UnsupportedTenancyException {
if (tenantStrategy.toUpperCase().equals("DATABASE")) {
if (tenantStrategy.toUpperCase()
.equals("DATABASE")) {
return originalUrl.replace(DEFAULT_DB_NAME, tenant);
} else if (tenantStrategy.toUpperCase().equals("SCHEMA")) {
} else if (tenantStrategy.toUpperCase()
.equals("SCHEMA")) {
return originalUrl + String.format(SCHEMA_TOKEN, tenant);
} else {
throw new UnsupportedTenancyException("Not yet supported");

View File

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1</property>
<property name="hibernate.connection.username">sa</property>
<property name="connection.password"/>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.multiTenancy">DATABASE</property>
</session-factory>
</hibernate-configuration>

View File

@ -0,0 +1,8 @@
hibernate.connection.driver_class=org.h2.Driver
hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1
hibernate.connection.username=sa
jdbc.password=
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
hibernate.multiTenancy=DATABASE

View File

@ -1,65 +1,71 @@
package com.baeldung.hibernate;
import com.baeldung.hibernate.pojo.Supplier;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test;
import static org.junit.Assert.assertNotEquals;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Before;
import org.junit.Test;
import com.baeldung.hibernate.pojo.Supplier;
public class MultiTenantHibernateIntegrationTest {
@Test
public void givenDBMode_whenFetchingSuppliers_thenComparingFromDbs () {
SessionFactory sessionFactory;
try {
sessionFactory = HibernateMultiTenantUtil.getSessionFactory();
Session db1Session = sessionFactory
.withOptions().tenantIdentifier("mydb1").openSession();
initDb1(db1Session);
Transaction transaction = db1Session.getTransaction();
transaction.begin();
Supplier supplierFromDB1 = (Supplier)db1Session.createCriteria(Supplier.class).list().get(0);
transaction.commit();
Session db2Session = sessionFactory
.withOptions().tenantIdentifier("mydb2").openSession();
initDb2(db2Session);
db2Session.getTransaction().begin();
Supplier supplierFromDB2 = (Supplier) db2Session.createCriteria(Supplier.class).list().get(0);
db2Session.getTransaction().commit();
System.out.println(supplierFromDB1);
System.out.println(supplierFromDB2);
assertNotEquals(supplierFromDB1, supplierFromDB2);
} catch (UnsupportedTenancyException e) {
e.printStackTrace();
}
public void givenDBMode_whenFetchingSuppliers_thenComparingFromDbs() throws UnsupportedTenancyException, IOException {
SessionFactory sessionFactory = HibernateMultiTenantUtil.getSessionFactory();
Session db1Session = sessionFactory.withOptions().tenantIdentifier("mydb1").openSession();
initDb1(db1Session);
Transaction transaction = db1Session.getTransaction();
transaction.begin();
Supplier supplierFromDB1 = (Supplier) db1Session.createCriteria(Supplier.class).list().get(0);
transaction.commit();
Session db2Session = sessionFactory.withOptions().tenantIdentifier("mydb2").openSession();
initDb2(db2Session);
db2Session.getTransaction().begin();
Supplier supplierFromDB2 = (Supplier) db2Session.createCriteria(Supplier.class).list().get(0);
db2Session.getTransaction().commit();
System.out.println(supplierFromDB1);
System.out.println(supplierFromDB2);
assertNotEquals(supplierFromDB1, supplierFromDB2);
}
private void initDb1(Session db1Session) {
System.out.println("Init DB1");
Transaction transaction = db1Session.getTransaction();
transaction.begin();
db1Session.createSQLQuery("DROP ALL OBJECTS").executeUpdate();
db1Session.createSQLQuery("create table Supplier (id integer generated by default as identity, country varchar(255), name varchar(255), primary key (id))").executeUpdate();
db1Session
.createSQLQuery(
"create table Supplier (id integer generated by default as identity, country varchar(255), name varchar(255), primary key (id))")
.executeUpdate();
db1Session.createSQLQuery("insert into Supplier (id, country, name) values (null, 'John', 'USA')").executeUpdate();
transaction.commit();
}
private void initDb2(Session db2Session) {
System.out.println("Init DB2");
Transaction transaction = db2Session.getTransaction();
transaction.begin();
db2Session.createSQLQuery("DROP ALL OBJECTS").executeUpdate();
db2Session.createSQLQuery("create table Supplier (id integer generated by default as identity, country varchar(255), name varchar(255), primary key (id))").executeUpdate();
db2Session
.createSQLQuery(
"create table Supplier (id integer generated by default as identity, country varchar(255), name varchar(255), primary key (id))")
.executeUpdate();
db2Session.createSQLQuery("insert into Supplier (id, country, name) values (null, 'Miller', 'UK')").executeUpdate();
transaction.commit();
}

View File

@ -0,0 +1,8 @@
hibernate.connection.driver_class=org.h2.Driver
hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1
hibernate.connection.username=sa
jdbc.password=
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
hibernate.multiTenancy=DATABASE

View File

@ -3,7 +3,6 @@ package com.baeldung;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
@ -56,32 +55,12 @@ public class DynamicTestsExample {
// sample input and output
List<String> inputList =
new ArrayList<>(Arrays.asList("www.somedomain.com", "www.anotherdomain.com", "www.yetanotherdomain.com"));
Arrays.asList("www.somedomain.com", "www.anotherdomain.com", "www.yetanotherdomain.com");
List<String> outputList =
new ArrayList<>(Arrays.asList("154.174.10.56", "211.152.104.132", "178.144.120.156"));
Arrays.asList("154.174.10.56", "211.152.104.132", "178.144.120.156");
// input generator that generates inputs using inputList
Iterator<String> inputGenerator = new Iterator<String>() {
String current;
int size = inputList.size();
int index = 0;
@Override
public boolean hasNext() {
if(index == size) {
return false;
}
current = inputList.get(index);
index++;
return true;
}
@Override
public String next() {
return current;
}
};
Iterator<String> inputGenerator = inputList.iterator();
// a display name generator that creates a different name based on the input
Function<String, String> displayNameGenerator = (input) -> "Resolving: " + input;

View File

@ -276,6 +276,16 @@
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>one.util</groupId>
<artifactId>streamex</artifactId>
<version>0.6.5</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jool</artifactId>
<version>0.9.12</version>
</dependency>
</dependencies>
<properties>
<multiverse.version>0.7.0</multiverse.version>

View File

@ -0,0 +1,35 @@
package com.baeldung.stream;
import org.jooq.lambda.Seq;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import static junit.framework.TestCase.assertEquals;
public class JoolMergeStreamsTest {
@Test
public void givenTwoStreams_whenMergingStreams_thenResultingStreamContainsElementsFromBothStreams() {
Seq<Integer> seq1 = Seq.of(1, 3, 5);
Seq<Integer> seq2 = Seq.of(2, 4, 6);
Seq<Integer> resultingSeq = seq1.append(seq2);
assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6),
resultingSeq.toList());
}
@Test
public void givenThreeStreams_whenAppendingAndPrependingStreams_thenResultingStreamContainsElementsFromAllStreams() {
Seq<String> seq = Seq.of("foo", "bar");
Seq<String> openingBracketSeq = Seq.of("[");
Seq<String> closingBracketSeq = Seq.of("]");
Seq<String> resultingStream = seq.append(closingBracketSeq)
.prepend(openingBracketSeq);
Assert.assertEquals(Arrays.asList("[", "foo", "bar", "]"),
resultingStream.toList());
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.stream;
import org.junit.Test;
import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.assertEquals;
public class MergeStreamsTest {
@Test
public void givenTwoStreams_whenMergingStreams_thenResultingStreamContainsElementsFromBothStreams() {
Stream<Integer> stream1 = Stream.of(1, 3, 5);
Stream<Integer> stream2 = Stream.of(2, 4, 6);
Stream<Integer> resultingStream = Stream.concat(stream1,
stream2);
assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6),
resultingStream.collect(Collectors.toList()));
}
@Test
public void givenThreeStreams_whenMergingStreams_thenResultingStreamContainsElementsFromAllStreams() {
Stream<Integer> stream1 = Stream.of(1, 3, 5);
Stream<Integer> stream2 = Stream.of(2, 4, 6);
Stream<Integer> stream3 = Stream.of(18, 15, 36);
Stream<Integer> resultingStream = Stream.concat(Stream.concat(stream1,
stream2),
stream3);
assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6, 18, 15, 36),
resultingStream.collect(Collectors.toList()));
}
@Test
public void givenFourStreams_whenMergingStreams_thenResultingStreamContainsElementsFromAllStreams() {
Stream<Integer> stream1 = Stream.of(1, 3, 5);
Stream<Integer> stream2 = Stream.of(2, 4, 6);
Stream<Integer> stream3 = Stream.of(18, 15, 36);
Stream<Integer> stream4 = Stream.of(99);
Stream<Integer> resultingStream = Stream.of(stream1, stream2, stream3, stream4).flatMap(Function.identity());
assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6, 18, 15, 36, 99),
resultingStream.collect(Collectors.toList()));
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.stream;
import one.util.streamex.StreamEx;
import org.junit.Test;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
public class StreamExMergeStreamsTest {
@Test
public void givenTwoStreams_whenMergingStreams_thenResultingStreamContainsElementsFromBothStreams() {
StreamEx<Integer> stream1 = StreamEx.of(1, 3, 5);
StreamEx<Integer> stream2 = StreamEx.of(2, 4, 6);
StreamEx<Integer> resultingStream = stream1.append(stream2);
assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6),
resultingStream.toList());
}
@Test
public void givenFourStreams_whenMergingStreams_thenResultingStreamContainsElementsFromAllStreams() {
StreamEx<Integer> stream1 = StreamEx.of(1, 3, 5);
StreamEx<Integer> stream2 = StreamEx.of(2, 4, 6);
StreamEx<Integer> stream3 = StreamEx.of(18, 15, 36);
StreamEx<Integer> stream4 = StreamEx.of(99);
StreamEx<Integer> resultingStream = stream1.append(stream2)
.append(stream3)
.append(stream4);
assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6, 18, 15, 36, 99),
resultingStream.toList());
}
@Test
public void givenThreeStreams_whenAppendingAndPrependingStreams_thenResultingStreamContainsElementsFromAllStreams() {
StreamEx<String> stream1 = StreamEx.of("foo", "bar");
StreamEx<String> openingBracketStream = StreamEx.of("[");
StreamEx<String> closingBracketStream = StreamEx.of("]");
StreamEx<String> resultingStream = stream1.append(closingBracketStream)
.prepend(openingBracketStream);
assertEquals(Arrays.asList("[", "foo", "bar", "]"),
resultingStream.toList());
}
}

View File

@ -122,6 +122,7 @@
<module>solr</module>
<module>spark-java</module>
<module>spring-5</module>
<module>spring-5-mvc</module>
<module>spring-akka</module>
<module>spring-amqp</module>
<module>spring-all</module>

20
spring-5-mvc/.gitignore vendored Normal file
View File

@ -0,0 +1,20 @@
*.class
# Mobile Tools for Java (J2ME)
.mtj.tmp/
.settings/
.classpath
.project
target/
*.iml
.idea
# Package Files #
*.jar
*.war
*.ear
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

154
spring-5-mvc/pom.xml Normal file
View File

@ -0,0 +1,154 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>spring-5-mvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-5-mvc</name>
<description>spring 5 MVC sample project about new features</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M1</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- utils -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<!-- runtime and test scoped -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<failIfNoTests>false</failIfNoTests>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<properties>
<rest-assured.version>2.9.0</rest-assured.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
</project>

View File

@ -0,0 +1,13 @@
package com.baeldung;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Spring5Application {
public static void main(String[] args) {
SpringApplication.run(Spring5Application.class, args);
}
}

View File

@ -0,0 +1,84 @@
package com.baeldung.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Foo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
public Foo() {
super();
}
public Foo(final String name) {
super();
this.name = name;
}
public Foo(final long id, final String name) {
super();
this.id = id;
this.name = name;
}
// API
public String getName() {
return name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public void setName(final String name) {
this.name = name;
}
//
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Foo other = (Foo) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Foo [name=" + name + "]";
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.persistence;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import java.util.stream.IntStream;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.baeldung.model.Foo;
@Component
public class DataSetupBean implements InitializingBean {
@Autowired
private FooRepository repo;
//
@Override
public void afterPropertiesSet() throws Exception {
IntStream.range(1, 5).forEach(i -> repo.save(new Foo(randomAlphabetic(8))));
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.persistence;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import com.baeldung.model.Foo;
public interface FooRepository extends JpaRepository<Foo, Long>, JpaSpecificationExecutor<Foo> {
}

View File

@ -0,0 +1,66 @@
package com.baeldung.web;
import java.util.List;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import com.baeldung.model.Foo;
import com.baeldung.persistence.FooRepository;
@RestController
public class FooController {
@Autowired
private FooRepository repo;
// API - read
@GetMapping("/foos/{id}")
@Validated
public Foo findById(@PathVariable @Min(0) final long id) {
return repo.findById(id).orElse(null);
}
@GetMapping("/foos")
public List<Foo> findAll() {
return repo.findAll();
}
@GetMapping( value="/foos", params = { "page", "size" })
@Validated
public List<Foo> findPaginated(@RequestParam("page") @Min(0) final int page, @Max(100) @RequestParam("size") final int size) {
return repo.findAll(PageRequest.of(page, size)).getContent();
}
// API - write
@PutMapping("/foos/{id}")
@ResponseStatus(HttpStatus.OK)
public Foo update(@PathVariable("id") final String id, @RequestBody final Foo foo) {
return foo;
}
@PostMapping("/foos")
@ResponseStatus(HttpStatus.CREATED)
public void create( @RequestBody final Foo foo) {
if (null == foo || null == foo.getName()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST," 'name' is required");
}
repo.save(foo);
}
}

View File

@ -0,0 +1,6 @@
server.port=8081
security.user.name=user
security.user.password=pass
logging.level.root=INFO

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Spring Functional Application</display-name>
<servlet>
<servlet-name>functional</servlet-name>
<servlet-class>com.baeldung.functional.RootServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>functional</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

View File

@ -0,0 +1,54 @@
package com.baeldung.test;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.http.MediaType;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.RequestSpecification;
public class LiveTest {
private static String APP_ROOT = "http://localhost:8081";
@Test
public void givenUser_whenResourceCreatedWithNullName_then400BadRequest() {
final Response response = givenAuth("user", "pass").contentType(MediaType.APPLICATION_JSON.toString()).body(resourceWithNullName()).post(APP_ROOT + "/foos");
assertEquals(400, response.getStatusCode());
}
@Test
public void givenUser_whenResourceCreated_then201Created() {
final Response response = givenAuth("user", "pass").contentType(MediaType.APPLICATION_JSON.toString()).body(resourceString()).post(APP_ROOT + "/foos");
assertEquals(201, response.getStatusCode());
}
/*@Test
public void givenUser_whenGetAllFoos_thenOK() {
final Response response = givenAuth("user", "pass").get(APP_ROOT + "/foos");
assertEquals(200, response.getStatusCode());
}*/
//
private final String resourceWithNullName() {
final String roleData = "{\"name\":null}";
return roleData;
}
private final String resourceString() {
final String roleData = "{\"name\":\"" + randomAlphabetic(8) + "\"}";
return roleData;
}
private final RequestSpecification givenAuth(String username, String password) {
return RestAssured.given().auth().preemptive().basic(username, password);
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.Spring5Application;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Spring5Application.class)
public class Spring5ApplicationIntegrationTest {
@Test
public void contextLoads() {
}
}

View File

@ -58,6 +58,12 @@
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>${mockito.spring.boot.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -112,6 +118,7 @@
<properties>
<mockito.version>1.10.19</mockito.version>
<mockito.spring.boot.version>1.4.4.RELEASE</mockito.spring.boot.version>
<spring.version>4.3.4.RELEASE</spring.version>
<javax.inject.version>1</javax.inject.version>
<guava.version>20.0</guava.version>

View File

@ -0,0 +1,19 @@
package com.baeldung.beaninjection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class FileReader {
@Autowired
private Location location;
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.beaninjection;
public class FtpReader {
private String ftpHost = null;
private Integer ftpPort = null;
// Constructor with arguments
public FtpReader(String host, Integer port) {
this.ftpHost = host;
this.ftpPort = port;
}
public String getFtpHost() {
return ftpHost;
}
public void setFtpHost(String ftpHost) {
this.ftpHost = ftpHost;
}
public Integer getFtpPort() {
return ftpPort;
}
public void setFtpPort(Integer ftpPort) {
this.ftpPort = ftpPort;
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.beaninjection;
import org.springframework.stereotype.Component;
@Component
public class Location {
private String filePath;
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.beaninjection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = { "com.baeldung.beaninjection" })
public class ReaderApplicationConfig {
@Bean
public FtpReader exampleDAO() {
return new FtpReader("localhost", 21);
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.beaninjection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ReaderManager {
private FileReader fileReader;
private FtpReader ftpReader;
@Autowired
public ReaderManager(FtpReader ftpReader) {
this.ftpReader = ftpReader;
}
@Autowired
public void setFileReader(FileReader fileReader) {
this.fileReader = fileReader;
}
public FileReader getFileReader() {
return fileReader;
}
public void setFtpReader(FtpReader ftpReader) {
this.ftpReader = ftpReader;
}
public FtpReader getFtpReader() {
return ftpReader;
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.beaninjection;
public class ReaderService {
private FtpReader ftpReader;
public void setFtpReader(FtpReader reader) {
this.ftpReader = reader;
}
public FtpReader getFtpReader() {
return ftpReader;
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.value;
public class ClassNotManagedBySpring {
private String customVariable;
private String anotherCustomVariable;
public ClassNotManagedBySpring(String someInitialValue, String anotherManagedValue) {
this.customVariable = someInitialValue;
this.anotherCustomVariable = anotherManagedValue;
}
public String getCustomVariable() {
return customVariable;
}
public void setCustomVariable(String customVariable) {
this.customVariable = customVariable;
}
public String getAnotherCustomVariable() {
return anotherCustomVariable;
}
public void setAnotherCustomVariable(String anotherCustomVariable) {
this.anotherCustomVariable = anotherCustomVariable;
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.value;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class InitializerBean {
private String someInitialValue;
private String anotherManagedValue;
public InitializerBean(@Value("someInitialValue") String someInitialValue, @Value("anotherValue") String anotherManagedValue) {
this.someInitialValue = someInitialValue;
this.anotherManagedValue = anotherManagedValue;
}
public ClassNotManagedBySpring initClass() {
return new ClassNotManagedBySpring(this.someInitialValue, this.anotherManagedValue);
}
}

View File

@ -0,0 +1,2 @@
someInitialValue=This is only sample value
anotherValue=Another configured value

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="ftpReader" class="com.baeldung.beaninjection.FtpReader">
<constructor-arg value="localhost" />
<constructor-arg value="21" />
</bean>
<bean name="readerService" class="com.baeldung.beaninjection.ReaderService">
<property name="ftpReader" ref="ftpReader" />
</bean>
</beans>

View File

@ -0,0 +1,24 @@
package com.baeldung.beaninjection;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ReaderApplicationConfig.class)
public class FileReaderTest {
@Autowired
private ApplicationContext context;
@Test
public void testAutowiredAnnotation_WhenField_ThenInjected() {
FileReader service = context.getBean(FileReader.class);
assertNotNull(service.getLocation());
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.beaninjection;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:injectiontypes.xml")
public class FtpReaderTest {
@Autowired
private ApplicationContext context;
@Test
public void testXMLBeanConfig_WhenConstructorArguments_ThenInjected() {
FtpReader service = context.getBean(FtpReader.class);
assertNotNull(service.getFtpHost());
assertNotNull(service.getFtpPort());
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.beaninjection;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ReaderApplicationConfig.class)
public class ReaderManagerTest {
@Autowired
private ApplicationContext context;
@Test
public void testAutowiredAnnotation_WhenConstructorAndSetter_ThenInjected() {
ReaderManager service = context.getBean(ReaderManager.class);
assertNotNull(service.getFtpReader());
assertNotNull(service.getFileReader());
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.beaninjection;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:injectiontypes.xml")
public class ReaderServiceTest {
@Autowired
private ApplicationContext context;
@Test
public void testXMLBeanConfig_WhenSetter_ThenInjected() {
ReaderService service = context.getBean(ReaderService.class);
assertNotNull(service.getFtpReader());
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.value;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import static junit.framework.TestCase.assertEquals;
import static org.mockito.Mockito.when;
@RunWith(SpringRunner.class)
public class ClassNotManagedBySpringTest {
@MockBean
private InitializerBean initializerBean;
@Before
public void init() {
when(initializerBean.initClass())
.thenReturn(new ClassNotManagedBySpring("This is only sample value", "Another configured value"));
}
@Test
public void givenInitializerBean_whenInvokedInitClass_shouldInitialize() throws Exception {
//given
ClassNotManagedBySpring classNotManagedBySpring = initializerBean.initClass();
//when
String initializedValue = classNotManagedBySpring.getCustomVariable();
String anotherCustomVariable = classNotManagedBySpring.getAnotherCustomVariable();
//then
assertEquals("This is only sample value", initializedValue);
assertEquals("Another configured value", anotherCustomVariable);
}
}

54
structurizr/pom.xml Normal file
View File

@ -0,0 +1,54 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>structurizr</artifactId>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.structurizr</groupId>
<artifactId>structurizr-core</artifactId>
<version>1.0.0-RC3</version>
</dependency>
<dependency>
<groupId>com.structurizr</groupId>
<artifactId>structurizr-spring</artifactId>
<version>1.0.0-RC3</version>
</dependency>
<dependency>
<groupId>com.structurizr</groupId>
<artifactId>structurizr-client</artifactId>
<version>0.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
</dependencies>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>bintray</name>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
</project>

View File

@ -0,0 +1,155 @@
package com.baeldung.structurizr;
import java.io.File;
import java.io.StringWriter;
import com.structurizr.Workspace;
import com.structurizr.api.StructurizrClient;
import com.structurizr.api.StructurizrClientException;
import com.structurizr.componentfinder.ComponentFinder;
import com.structurizr.componentfinder.ReferencedTypesSupportingTypesStrategy;
import com.structurizr.componentfinder.SourceCodeComponentFinderStrategy;
import com.structurizr.componentfinder.SpringComponentFinderStrategy;
import com.structurizr.io.WorkspaceWriterException;
import com.structurizr.io.plantuml.PlantUMLWriter;
import com.structurizr.model.Component;
import com.structurizr.model.Container;
import com.structurizr.model.Model;
import com.structurizr.model.Person;
import com.structurizr.model.SoftwareSystem;
import com.structurizr.model.Tags;
import com.structurizr.view.ComponentView;
import com.structurizr.view.ContainerView;
import com.structurizr.view.Routing;
import com.structurizr.view.Shape;
import com.structurizr.view.Styles;
import com.structurizr.view.SystemContextView;
import com.structurizr.view.View;
import com.structurizr.view.ViewSet;
public class StructurizrSimple {
public static final String PAYMENT_TERMINAL = "Payment Terminal";
public static final String FRAUD_DETECTOR = "Fraud Detector";
public static final String SOFTWARE_SYSTEM_VIEW = "SoftwareSystemView";
public static final String CONTAINER_VIEW = "ContainerView";
public static final String COMPONENT_VIEW = "ComponentView";
public static final String JVM2_COMPONENT_VIEW = "JVM2ComponentView";
public static void main(String[] args) throws Exception {
Workspace workspace = getSoftwareSystem();
addContainers(workspace);
addComponents(workspace);
addSpringComponents(workspace);
exportToPlantUml(workspace.getViews().getViewWithKey(SOFTWARE_SYSTEM_VIEW));
exportToPlantUml(workspace.getViews().getViewWithKey(CONTAINER_VIEW));
exportToPlantUml(workspace.getViews().getViewWithKey(COMPONENT_VIEW));
exportToPlantUml(workspace.getViews().getViewWithKey(JVM2_COMPONENT_VIEW));
addStyles(workspace.getViews());
//uploadToExternal(workspace);
}
private static void addSpringComponents(Workspace workspace) throws Exception {
Container jvm2 = workspace.getModel().getSoftwareSystemWithName(PAYMENT_TERMINAL).getContainerWithName("JVM-2");
findComponents(jvm2);
ComponentView view = workspace.getViews().createComponentView(jvm2, JVM2_COMPONENT_VIEW, "JVM2ComponentView");
view.addAllComponents();
}
private static void findComponents(Container jvm) throws Exception {
ComponentFinder componentFinder = new ComponentFinder(
jvm,
"com.baeldung.structurizr",
new SpringComponentFinderStrategy(
new ReferencedTypesSupportingTypesStrategy()
),
new SourceCodeComponentFinderStrategy(new File("."), 150));
componentFinder.findComponents();
}
private static void addComponents(Workspace workspace) {
Model model = workspace.getModel();
SoftwareSystem paymentTerminal = model.getSoftwareSystemWithName(PAYMENT_TERMINAL);
Container jvm1 = paymentTerminal.getContainerWithName("JVM-1");
Component jaxrs = jvm1.addComponent("jaxrs-jersey", "restful webservice implementation", "rest");
Component gemfire = jvm1.addComponent("gemfire", "Clustered Cache Gemfire", "cache");
Component hibernate = jvm1.addComponent("hibernate", "Data Access Layer", "jpa");
jaxrs.uses(gemfire, "");
gemfire.uses(hibernate, "");
ComponentView componentView = workspace.getViews().createComponentView(jvm1, COMPONENT_VIEW, "JVM Components");
componentView.addAllComponents();
}
private static void addContainers(Workspace workspace) {
Model model = workspace.getModel();
SoftwareSystem paymentTerminal = model.getSoftwareSystemWithName(PAYMENT_TERMINAL);
Container f5 = paymentTerminal.addContainer("Payment Load Balancer", "Payment Load Balancer", "F5");
Container jvm1 = paymentTerminal.addContainer("JVM-1", "JVM-1", "Java Virtual Machine");
Container jvm2 = paymentTerminal.addContainer("JVM-2", "JVM-2", "Java Virtual Machine");
Container jvm3 = paymentTerminal.addContainer("JVM-3", "JVM-3", "Java Virtual Machine");
Container oracle = paymentTerminal.addContainer("oracleDB", "Oracle Database", "RDBMS");
f5.uses(jvm1, "route");
f5.uses(jvm2, "route");
f5.uses(jvm3, "route");
jvm1.uses(oracle, "storage");
jvm2.uses(oracle, "storage");
jvm3.uses(oracle, "storage");
ContainerView view = workspace.getViews().createContainerView(paymentTerminal, CONTAINER_VIEW, "Container View");
view.addAllContainers();
}
private static void uploadToExternal(Workspace workspace) throws StructurizrClientException {
StructurizrClient client = new StructurizrClient("e94bc0c9-76ef-41b0-8de7-82afc1010d04", "78d555dd-2a31-487c-952c-50508f1da495");
client.putWorkspace(32961L, workspace);
}
private static void exportToPlantUml(View view) throws WorkspaceWriterException {
StringWriter stringWriter = new StringWriter();
PlantUMLWriter plantUMLWriter = new PlantUMLWriter();
plantUMLWriter.write(view, stringWriter);
System.out.println(stringWriter.toString());
}
private static Workspace getSoftwareSystem() {
Workspace workspace = new Workspace("Payment Gateway", "Payment Gateway");
Model model = workspace.getModel();
Person user = model.addPerson("Merchant", "Merchant");
SoftwareSystem paymentTerminal = model.addSoftwareSystem(PAYMENT_TERMINAL, "Payment Terminal");
user.uses(paymentTerminal, "Makes payment");
SoftwareSystem fraudDetector = model.addSoftwareSystem(FRAUD_DETECTOR, "Fraud Detector");
paymentTerminal.uses(fraudDetector, "Obtains fraud score");
ViewSet viewSet = workspace.getViews();
SystemContextView contextView = viewSet.createSystemContextView(workspace.getModel().getSoftwareSystemWithName(PAYMENT_TERMINAL), SOFTWARE_SYSTEM_VIEW, "Payment Gateway Diagram");
contextView.addAllElements();
return workspace;
}
private static void addStyles(ViewSet viewSet) {
Styles styles = viewSet.getConfiguration().getStyles();
styles.addElementStyle(Tags.ELEMENT).color("#000000");
styles.addElementStyle(Tags.PERSON).background("#ffbf00").shape(Shape.Person);
styles.addElementStyle(Tags.CONTAINER).background("#facc2E");
styles.addRelationshipStyle(Tags.RELATIONSHIP).routing(Routing.Orthogonal);
styles.addRelationshipStyle(Tags.ASYNCHRONOUS).dashed(true);
styles.addRelationshipStyle(Tags.SYNCHRONOUS).dashed(false);
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.structurizr.spring;
import org.springframework.stereotype.Component;
@Component
public class GenericComponent {
}

View File

@ -0,0 +1,14 @@
package com.baeldung.structurizr.spring;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
@Controller
public class PaymentController {
@Resource
private PaymentRepository repository;
@Resource
private GenericComponent component;
}

View File

@ -0,0 +1,7 @@
package com.baeldung.structurizr.spring;
import org.springframework.stereotype.Repository;
@Repository
public class PaymentRepository {
}

View File

@ -9,4 +9,4 @@
- [AssertJ for Guava](http://www.baeldung.com/assertJ-for-guava)
- [Introduction to AssertJ](http://www.baeldung.com/introduction-to-assertj)
- [Cucumber and Scenario Outline](http://www.baeldung.com/cucumber-scenario-outline)
- [Testing with Google Truth](http://www.baeldung.com/google-truth)