Merge branch 'master' of https://github.com/ahmedtawila/tutorials
This commit is contained in:
commit
a4d1a8909c
59
apache-cayenne/pom.xml
Normal file
59
apache-cayenne/pom.xml
Normal file
@ -0,0 +1,59 @@
|
||||
<?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>
|
||||
|
||||
<artifactId>apache-cayenne</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>apache-cayenne</name>
|
||||
<description>Introduction to Apache Cayenne</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<mysql.connector.version>5.1.44</mysql.connector.version>
|
||||
<cayenne.version>4.0.M5</cayenne.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.cayenne</groupId>
|
||||
<artifactId>cayenne-server</artifactId>
|
||||
<version>${cayenne.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>${mysql.connector.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.cayenne.plugins</groupId>
|
||||
<artifactId>cayenne-modeler-maven-plugin</artifactId>
|
||||
<version>${cayenne.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,9 @@
|
||||
package com.baeldung.apachecayenne.persistent;
|
||||
|
||||
import com.baeldung.apachecayenne.persistent.auto._Article;
|
||||
|
||||
public class Article extends _Article {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.baeldung.apachecayenne.persistent;
|
||||
|
||||
import com.baeldung.apachecayenne.persistent.auto._Author;
|
||||
|
||||
public class Author extends _Author {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package com.baeldung.apachecayenne.persistent.auto;
|
||||
|
||||
import org.apache.cayenne.CayenneDataObject;
|
||||
import org.apache.cayenne.exp.Property;
|
||||
|
||||
import com.baeldung.apachecayenne.persistent.Author;
|
||||
|
||||
/**
|
||||
* Class _Article was generated by Cayenne.
|
||||
* It is probably a good idea to avoid changing this class manually,
|
||||
* since it may be overwritten next time code is regenerated.
|
||||
* If you need to make any customizations, please use subclass.
|
||||
*/
|
||||
public abstract class _Article extends CayenneDataObject {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final String ID_PK_COLUMN = "id";
|
||||
|
||||
public static final Property<String> CONTENT = Property.create("content", String.class);
|
||||
public static final Property<String> TITLE = Property.create("title", String.class);
|
||||
public static final Property<Author> AUTHOR = Property.create("author", Author.class);
|
||||
|
||||
public void setContent(String content) {
|
||||
writeProperty("content", content);
|
||||
}
|
||||
public String getContent() {
|
||||
return (String)readProperty("content");
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
writeProperty("title", title);
|
||||
}
|
||||
public String getTitle() {
|
||||
return (String)readProperty("title");
|
||||
}
|
||||
|
||||
public void setAuthor(Author author) {
|
||||
setToOneTarget("author", author, true);
|
||||
}
|
||||
|
||||
public Author getAuthor() {
|
||||
return (Author)readProperty("author");
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.baeldung.apachecayenne.persistent.auto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.cayenne.CayenneDataObject;
|
||||
import org.apache.cayenne.exp.Property;
|
||||
|
||||
import com.baeldung.apachecayenne.persistent.Article;
|
||||
|
||||
/**
|
||||
* Class _Author was generated by Cayenne.
|
||||
* It is probably a good idea to avoid changing this class manually,
|
||||
* since it may be overwritten next time code is regenerated.
|
||||
* If you need to make any customizations, please use subclass.
|
||||
*/
|
||||
public abstract class _Author extends CayenneDataObject {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final String ID_PK_COLUMN = "id";
|
||||
|
||||
public static final Property<String> FIRSTNAME = Property.create("firstname", String.class);
|
||||
public static final Property<String> LASTNAME = Property.create("lastname", String.class);
|
||||
public static final Property<List<Article>> ARTICLES = Property.create("articles", List.class);
|
||||
|
||||
public void setFirstname(String firstname) {
|
||||
writeProperty("firstname", firstname);
|
||||
}
|
||||
public String getFirstname() {
|
||||
return (String)readProperty("firstname");
|
||||
}
|
||||
|
||||
public void setLastname(String lastname) {
|
||||
writeProperty("lastname", lastname);
|
||||
}
|
||||
public String getLastname() {
|
||||
return (String)readProperty("lastname");
|
||||
}
|
||||
|
||||
public void addToArticles(Article obj) {
|
||||
addToManyTarget("articles", obj, true);
|
||||
}
|
||||
public void removeFromArticles(Article obj) {
|
||||
removeToManyTarget("articles", obj, true);
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Article> getArticles() {
|
||||
return (List<Article>)readProperty("articles");
|
||||
}
|
||||
|
||||
|
||||
}
|
17
apache-cayenne/src/main/resources/cayenne-project.xml
Normal file
17
apache-cayenne/src/main/resources/cayenne-project.xml
Normal file
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<domain project-version="9">
|
||||
<map name="datamap"/>
|
||||
|
||||
<node name="datanode"
|
||||
factory="org.apache.cayenne.configuration.server.XMLPoolingDataSourceFactory"
|
||||
schema-update-strategy="org.apache.cayenne.access.dbsync.CreateIfNoSchemaStrategy"
|
||||
>
|
||||
<map-ref name="datamap"/>
|
||||
<data-source>
|
||||
<driver value="com.mysql.jdbc.Driver"/>
|
||||
<url value="jdbc:mysql://localhost:3306/intro_cayenne"/>
|
||||
<connectionPool min="1" max="1"/>
|
||||
<login userName="root" password="root"/>
|
||||
</data-source>
|
||||
</node>
|
||||
</domain>
|
34
apache-cayenne/src/main/resources/datamap.map.xml
Normal file
34
apache-cayenne/src/main/resources/datamap.map.xml
Normal file
@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<data-map xmlns="http://cayenne.apache.org/schema/9/modelMap"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://cayenne.apache.org/schema/9/modelMap http://cayenne.apache.org/schema/9/modelMap.xsd"
|
||||
project-version="9">
|
||||
<property name="defaultPackage" value="com.baeldung.apachecayenne.persistent"/>
|
||||
<db-entity name="article" catalog="intro_cayenne">
|
||||
<db-attribute name="author_id" type="INTEGER" isMandatory="true" length="10"/>
|
||||
<db-attribute name="content" type="VARCHAR" isMandatory="true" length="254"/>
|
||||
<db-attribute name="id" type="INTEGER" isPrimaryKey="true" isGenerated="true" isMandatory="true" length="10"/>
|
||||
<db-attribute name="title" type="VARCHAR" isMandatory="true" length="254"/>
|
||||
</db-entity>
|
||||
<db-entity name="author" catalog="intro_cayenne">
|
||||
<db-attribute name="firstname" type="VARCHAR" isMandatory="true" length="254"/>
|
||||
<db-attribute name="id" type="INTEGER" isPrimaryKey="true" isGenerated="true" isMandatory="true" length="10"/>
|
||||
<db-attribute name="lastname" type="VARCHAR" isMandatory="true" length="254"/>
|
||||
</db-entity>
|
||||
<obj-entity name="Article" className="com.baeldung.apachecayenne.persistent.Article" dbEntityName="article">
|
||||
<obj-attribute name="content" type="java.lang.String" db-attribute-path="content"/>
|
||||
<obj-attribute name="title" type="java.lang.String" db-attribute-path="title"/>
|
||||
</obj-entity>
|
||||
<obj-entity name="Author" className="com.baeldung.apachecayenne.persistent.Author" dbEntityName="author">
|
||||
<obj-attribute name="firstname" type="java.lang.String" db-attribute-path="firstname"/>
|
||||
<obj-attribute name="lastname" type="java.lang.String" db-attribute-path="lastname"/>
|
||||
</obj-entity>
|
||||
<db-relationship name="author" source="article" target="author" toMany="false">
|
||||
<db-attribute-pair source="author_id" target="id"/>
|
||||
</db-relationship>
|
||||
<db-relationship name="articles" source="author" target="article" toMany="true">
|
||||
<db-attribute-pair source="id" target="author_id"/>
|
||||
</db-relationship>
|
||||
<obj-relationship name="author" source="Article" target="Author" deleteRule="Nullify" db-relationship-path="author"/>
|
||||
<obj-relationship name="articles" source="Author" target="Article" deleteRule="Deny" db-relationship-path="articles"/>
|
||||
</data-map>
|
@ -0,0 +1,140 @@
|
||||
package com.baeldung.apachecayenne;
|
||||
|
||||
import com.baeldung.apachecayenne.persistent.Article;
|
||||
import com.baeldung.apachecayenne.persistent.Author;
|
||||
import org.apache.cayenne.ObjectContext;
|
||||
import org.apache.cayenne.configuration.server.ServerRuntime;
|
||||
import org.apache.cayenne.query.ObjectSelect;
|
||||
import org.apache.cayenne.query.SQLTemplate;
|
||||
import org.junit.After;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
|
||||
public class CayenneOperationTests {
|
||||
private static ObjectContext context = null;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupTheCayenneContext() {
|
||||
ServerRuntime cayenneRuntime = ServerRuntime.builder()
|
||||
.addConfig("cayenne-project.xml")
|
||||
.build();
|
||||
context = cayenneRuntime.newContext();
|
||||
}
|
||||
|
||||
@After
|
||||
public void deleteAllRecords() {
|
||||
SQLTemplate deleteArticles = new SQLTemplate(Article.class, "delete from article");
|
||||
SQLTemplate deleteAuthors = new SQLTemplate(Author.class, "delete from author");
|
||||
|
||||
context.performGenericQuery(deleteArticles);
|
||||
context.performGenericQuery(deleteAuthors);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthor_whenInsert_thenWeGetOneRecordInTheDatabase() {
|
||||
Author author = context.newObject(Author.class);
|
||||
author.setFirstname("Paul");
|
||||
author.setLastname("Smith");
|
||||
|
||||
context.commitChanges();
|
||||
|
||||
long records = ObjectSelect.dataRowQuery(Author.class).selectCount(context);
|
||||
assertEquals(1, records);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthor_whenInsert_andQueryByFirstName_thenWeGetTheAuthor() {
|
||||
Author author = context.newObject(Author.class);
|
||||
author.setFirstname("Paul");
|
||||
author.setLastname("Smith");
|
||||
|
||||
context.commitChanges();
|
||||
|
||||
Author expectedAuthor = ObjectSelect.query(Author.class)
|
||||
.where(Author.FIRSTNAME.eq("Paul"))
|
||||
.selectOne(context);
|
||||
|
||||
assertEquals("Paul", expectedAuthor.getFirstname());
|
||||
assertEquals("Smith", expectedAuthor.getLastname());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoAuthor_whenInsert_andQueryAll_thenWeGetTwoAuthors() {
|
||||
Author firstAuthor = context.newObject(Author.class);
|
||||
firstAuthor.setFirstname("Paul");
|
||||
firstAuthor.setLastname("Smith");
|
||||
|
||||
Author secondAuthor = context.newObject(Author.class);
|
||||
secondAuthor.setFirstname("Ludovic");
|
||||
secondAuthor.setLastname("Garcia");
|
||||
|
||||
context.commitChanges();
|
||||
|
||||
List<Author> authors = ObjectSelect.query(Author.class).select(context);
|
||||
assertEquals(2, authors.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthor_whenUpdating_thenWeGetAnUpatedeAuthor() {
|
||||
Author author = context.newObject(Author.class);
|
||||
author.setFirstname("Paul");
|
||||
author.setLastname("Smith");
|
||||
context.commitChanges();
|
||||
|
||||
Author expectedAuthor = ObjectSelect.query(Author.class)
|
||||
.where(Author.FIRSTNAME.eq("Paul"))
|
||||
.selectOne(context);
|
||||
expectedAuthor.setLastname("Smith 2");
|
||||
context.commitChanges();
|
||||
|
||||
assertEquals(author.getFirstname(), expectedAuthor.getFirstname());
|
||||
assertEquals(author.getLastname(), expectedAuthor.getLastname());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthor_whenDeleting_thenWeLostHisDetails() {
|
||||
Author author = context.newObject(Author.class);
|
||||
author.setFirstname("Paul");
|
||||
author.setLastname("Smith");
|
||||
context.commitChanges();
|
||||
|
||||
Author savedAuthor = ObjectSelect.query(Author.class)
|
||||
.where(Author.FIRSTNAME.eq("Paul")).selectOne(context);
|
||||
if(savedAuthor != null) {
|
||||
context.deleteObjects(author);
|
||||
context.commitChanges();
|
||||
}
|
||||
|
||||
Author expectedAuthor = ObjectSelect.query(Author.class)
|
||||
.where(Author.FIRSTNAME.eq("Paul")).selectOne(context);
|
||||
assertNull(expectedAuthor);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthor_whenAttachingToArticle_thenTheRelationIsMade() {
|
||||
Author author = context.newObject(Author.class);
|
||||
author.setFirstname("Paul");
|
||||
author.setLastname("Smith");
|
||||
|
||||
Article article = context.newObject(Article.class);
|
||||
article.setTitle("My post title");
|
||||
article.setContent("The content");
|
||||
article.setAuthor(author);
|
||||
|
||||
context.commitChanges();
|
||||
|
||||
Author expectedAuthor = ObjectSelect.query(Author.class)
|
||||
.where(Author.LASTNAME.eq("Smith"))
|
||||
.selectOne(context);
|
||||
|
||||
Article expectedArticle = (expectedAuthor.getArticles()).get(0);
|
||||
assertEquals(article.getTitle(), expectedArticle.getTitle());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.baeldung.stream;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class SupplierStreamTest {
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void givenStream_whenStreamUsedTwice_thenThrowException() {
|
||||
Stream<String> stringStream = Stream.of("A", "B", "C", "D");
|
||||
Optional<String> result1 = stringStream.findAny();
|
||||
System.out.println(result1.get());
|
||||
Optional<String> result2 = stringStream.findFirst();
|
||||
System.out.println(result2.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStream_whenUsingSupplier_thenNoExceptionIsThrown() {
|
||||
try {
|
||||
Supplier<Stream<String>> streamSupplier = () -> Stream.of("A", "B", "C", "D");
|
||||
Optional<String> result1 = streamSupplier.get().findAny();
|
||||
System.out.println(result1.get());
|
||||
Optional<String> result2 = streamSupplier.get().findFirst();
|
||||
System.out.println(result2.get());
|
||||
} catch (IllegalStateException e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -106,3 +106,4 @@
|
||||
- [Converting a List to String in Java](http://www.baeldung.com/java-list-to-string)
|
||||
- [CharSequence vs. String in Java](http://www.baeldung.com/java-char-sequence-string)
|
||||
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
|
||||
- [Guide to the Diamond Operator in Java](http://www.baeldung.com/java-diamond-operator)
|
@ -5,16 +5,16 @@ import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
||||
public class AdapterPatternDriver {
|
||||
|
||||
public static void main(String args[]) {
|
||||
LuxuryCars bugattiVeyron = new BugattiVeyron();
|
||||
LuxuryCarsAdapter bugattiVeyronAdapter = new LuxuryCarsAdapterImpl(bugattiVeyron);
|
||||
LOG.info("Bugatti Veyron Super Sport's top speed is " + bugattiVeyronAdapter.speedInKMPH() + " Kmph.");
|
||||
Movable bugattiVeyron = new BugattiVeyron();
|
||||
MovableAdapter bugattiVeyronAdapter = new MovableAdapterImpl(bugattiVeyron);
|
||||
LOG.info("Bugatti Veyron Super Sport's top speed is " + bugattiVeyronAdapter.getSpeed() + " Kmph.");
|
||||
|
||||
LuxuryCars mcLaren = new McLaren();
|
||||
LuxuryCarsAdapter mcLarenAdapter = new LuxuryCarsAdapterImpl(mcLaren);
|
||||
LOG.info("McLaren F1 top speed is " + mcLarenAdapter.speedInKMPH() + " Kmph.");
|
||||
Movable mcLaren = new McLaren();
|
||||
MovableAdapter mcLarenAdapter = new MovableAdapterImpl(mcLaren);
|
||||
LOG.info("McLaren F1 top speed is " + mcLarenAdapter.getSpeed() + " Kmph.");
|
||||
|
||||
LuxuryCars astonMartin = new AstonMartin();
|
||||
LuxuryCarsAdapter astonMartinAdapter = new LuxuryCarsAdapterImpl(astonMartin);
|
||||
LOG.info("McLaren F1 top speed is " + astonMartinAdapter.speedInKMPH() + " Kmph.");
|
||||
Movable astonMartin = new AstonMartin();
|
||||
MovableAdapter astonMartinAdapter = new MovableAdapterImpl(astonMartin);
|
||||
LOG.info("McLaren F1 top speed is " + astonMartinAdapter.getSpeed() + " Kmph.");
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public class AstonMartin implements LuxuryCars {
|
||||
public class AstonMartin implements Movable {
|
||||
@Override
|
||||
public double speedInMPH() {
|
||||
public double getSpeed() {
|
||||
return 220;
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public class BugattiVeyron implements LuxuryCars {
|
||||
public class BugattiVeyron implements Movable {
|
||||
@Override
|
||||
public double speedInMPH() {
|
||||
public double getSpeed() {
|
||||
return 268;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +0,0 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public interface LuxuryCars {
|
||||
public double speedInMPH();
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public interface LuxuryCarsAdapter {
|
||||
public double speedInKMPH();
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public class McLaren implements LuxuryCars {
|
||||
public class McLaren implements Movable {
|
||||
@Override
|
||||
public double speedInMPH() {
|
||||
public double getSpeed() {
|
||||
return 241;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,6 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public interface Movable {
|
||||
// returns speed in MPH
|
||||
double getSpeed();
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public interface MovableAdapter {
|
||||
// returns speed in KMPH
|
||||
double getSpeed();
|
||||
}
|
@ -1,15 +1,15 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public class LuxuryCarsAdapterImpl implements LuxuryCarsAdapter {
|
||||
private LuxuryCars luxuryCars;
|
||||
public class MovableAdapterImpl implements MovableAdapter {
|
||||
private Movable luxuryCars;
|
||||
|
||||
public LuxuryCarsAdapterImpl(LuxuryCars luxuryCars) {
|
||||
public MovableAdapterImpl(Movable luxuryCars) {
|
||||
this.luxuryCars = luxuryCars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double speedInKMPH() {
|
||||
double mph = luxuryCars.speedInMPH();
|
||||
public double getSpeed() {
|
||||
double mph = luxuryCars.getSpeed();
|
||||
return convertMPHtoKMPH(mph);
|
||||
}
|
||||
|
@ -1,12 +1,8 @@
|
||||
package com.baeldung.designpatterns.bridge;
|
||||
|
||||
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
||||
|
||||
public class Blue implements Color {
|
||||
|
||||
@Override
|
||||
public void fillColor() {
|
||||
LOG.info("Color : Blue");
|
||||
public String fill() {
|
||||
return "Color is Blue";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,10 +5,10 @@ public class BridgePatternDriver {
|
||||
public static void main(String[] args) {
|
||||
//a square with red color
|
||||
Shape square = new Square(new Red());
|
||||
square.drawShape();
|
||||
System.out.println(square.draw());
|
||||
|
||||
//a triangle with blue color
|
||||
Shape triangle = new Triangle(new Blue());
|
||||
triangle.drawShape();
|
||||
System.out.println(triangle.draw());
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
package com.baeldung.designpatterns.bridge;
|
||||
|
||||
public interface Color {
|
||||
public void fillColor();
|
||||
String fill();
|
||||
}
|
||||
|
@ -1,12 +1,10 @@
|
||||
package com.baeldung.designpatterns.bridge;
|
||||
|
||||
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
||||
|
||||
public class Red implements Color {
|
||||
|
||||
@Override
|
||||
public void fillColor() {
|
||||
LOG.info("Color : Red");
|
||||
public String fill() {
|
||||
return "Color is Red";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,5 +7,5 @@ public abstract class Shape {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
abstract public void drawShape();
|
||||
abstract public String draw();
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package com.baeldung.designpatterns.bridge;
|
||||
|
||||
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
||||
|
||||
public class Square extends Shape {
|
||||
|
||||
public Square(Color color) {
|
||||
@ -9,8 +7,7 @@ public class Square extends Shape {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawShape() {
|
||||
LOG.info("Square drawn. ");
|
||||
color.fillColor();
|
||||
public String draw() {
|
||||
return "Square drawn. " + color.fill();
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package com.baeldung.designpatterns.bridge;
|
||||
|
||||
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
||||
|
||||
public class Triangle extends Shape {
|
||||
|
||||
public Triangle(Color color) {
|
||||
@ -9,8 +7,7 @@ public class Triangle extends Shape {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawShape() {
|
||||
LOG.info("Triangle drawn. ");
|
||||
color.fillColor();
|
||||
public String draw() {
|
||||
return "Triangle drawn. "+ color.fill();
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
package com.baeldung.designpatterns.decorator;
|
||||
|
||||
public interface ChristmasTree {
|
||||
public String decorate();
|
||||
}
|
||||
String decorate();
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
package com.baeldung.designpatterns.proxy;
|
||||
|
||||
public interface ExpensiveObject {
|
||||
public void process();
|
||||
void process();
|
||||
}
|
||||
|
@ -6,15 +6,25 @@ import org.junit.Test;
|
||||
|
||||
import com.baeldung.designpatterns.adapter.AstonMartin;
|
||||
import com.baeldung.designpatterns.adapter.BugattiVeyron;
|
||||
import com.baeldung.designpatterns.adapter.LuxuryCarsAdapterImpl;
|
||||
import com.baeldung.designpatterns.adapter.McLaren;
|
||||
import com.baeldung.designpatterns.adapter.Movable;
|
||||
import com.baeldung.designpatterns.adapter.MovableAdapter;
|
||||
import com.baeldung.designpatterns.adapter.MovableAdapterImpl;
|
||||
|
||||
public class AdapterPatternIntegrationTest {
|
||||
@Test
|
||||
public void givenLuxuryCarsAdapter_WhenConvertingMPHToKMPH_thenSuccessfullyConverted() {
|
||||
assertEquals(new LuxuryCarsAdapterImpl(new BugattiVeyron()).speedInKMPH(), 431.30312, 0.00001);
|
||||
assertEquals(new LuxuryCarsAdapterImpl(new McLaren()).speedInKMPH(), 387.85094, 0.00001);
|
||||
assertEquals(new LuxuryCarsAdapterImpl(new AstonMartin()).speedInKMPH(), 354.0548, 0.00001);
|
||||
public void givenMovableAdapter_WhenConvertingMPHToKMPH_thenSuccessfullyConverted() {
|
||||
Movable bugattiVeyron = new BugattiVeyron();
|
||||
MovableAdapter bugattiVeyronAdapter = new MovableAdapterImpl(bugattiVeyron);
|
||||
assertEquals(bugattiVeyronAdapter.getSpeed(), 431.30312, 0.00001);
|
||||
|
||||
Movable mcLaren = new McLaren();
|
||||
MovableAdapter mcLarenAdapter = new MovableAdapterImpl(mcLaren);
|
||||
assertEquals(mcLarenAdapter.getSpeed(), 387.85094, 0.00001);
|
||||
|
||||
Movable astonMartin = new AstonMartin();
|
||||
MovableAdapter astonMartinAdapter = new MovableAdapterImpl(astonMartin);
|
||||
assertEquals(astonMartinAdapter.getSpeed(), 354.0548, 0.00001);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,7 @@
|
||||
package com.baeldung.designpatterns;
|
||||
|
||||
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.log4j.spi.LoggingEvent;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.designpatterns.bridge.Blue;
|
||||
@ -18,35 +11,16 @@ import com.baeldung.designpatterns.bridge.Square;
|
||||
import com.baeldung.designpatterns.bridge.Triangle;
|
||||
|
||||
public class BridgePatternIntegrationTest {
|
||||
public static TestAppenderDP appender;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
appender = new TestAppenderDP();
|
||||
LOG.addAppender(appender);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenBridgePatternInvoked_thenConfigSuccess() {
|
||||
//a square with red color
|
||||
Shape square = new Square(new Red());
|
||||
square.drawShape();
|
||||
assertEquals(square.draw(), "Square drawn. Color is Red");
|
||||
|
||||
//a triangle with blue color
|
||||
Shape triangle = new Triangle(new Blue());
|
||||
triangle.drawShape();
|
||||
|
||||
final List<LoggingEvent> log = appender.getLog();
|
||||
|
||||
assertThat((String) log.get(0).getMessage(), is("Square drawn. "));
|
||||
assertThat((String) log.get(1).getMessage(), is("Color : Red"));
|
||||
assertThat((String) log.get(2).getMessage(), is("Triangle drawn. "));
|
||||
assertThat((String) log.get(3).getMessage(), is("Color : Blue"));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
LOG.removeAppender(appender);
|
||||
assertEquals(triangle.draw(), "Triangle drawn. Color is Blue");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,19 +10,16 @@ import com.baeldung.designpatterns.decorator.ChristmasTreeImpl;
|
||||
import com.baeldung.designpatterns.decorator.Garland;
|
||||
|
||||
public class DecoratorPatternIntegrationTest {
|
||||
private ChristmasTree tree;
|
||||
|
||||
@Test
|
||||
public void givenDecoratorPattern_WhenDecoratorsInjectedAtRuntime_thenConfigSuccess() {
|
||||
//christmas tree with just one Garland
|
||||
tree = new Garland(new ChristmasTreeImpl());
|
||||
assertEquals(tree.decorate(), "Christmas tree with Garland");
|
||||
|
||||
//christmas tree with two Garlands and one Bubble lights
|
||||
tree = new BubbleLights(new Garland(
|
||||
new Garland(new ChristmasTreeImpl()))
|
||||
);
|
||||
assertEquals(tree.decorate(), "Christmas tree with Garland with Garland with Bubble Lights");
|
||||
ChristmasTree tree1 = new Garland(new ChristmasTreeImpl());
|
||||
assertEquals(tree1.decorate(),
|
||||
"Christmas tree with Garland");
|
||||
|
||||
ChristmasTree tree2 = new BubbleLights(
|
||||
new Garland(new Garland(new ChristmasTreeImpl())));
|
||||
assertEquals(tree2.decorate(),
|
||||
"Christmas tree with Garland with Garland with Bubble Lights");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
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>
|
||||
|
||||
<artifactId>kotlin</artifactId>
|
||||
<artifactId>core-kotlin</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
6
couchbase/.gitignore
vendored
Normal file
6
couchbase/.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
# Created by .ignore support plugin (hsz.mobi)
|
||||
|
||||
# IntelliJ project files
|
||||
.idea
|
||||
*.iml
|
||||
/target/
|
0
couchbase-sdk/mvnw → couchbase/mvnw
vendored
Executable file → Normal file
0
couchbase-sdk/mvnw → couchbase/mvnw
vendored
Executable file → Normal file
@ -6,7 +6,7 @@
|
||||
<artifactId>couchbase-sdk</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>couchbase-sdk</name>
|
||||
<name>couchbase</name>
|
||||
<description>Couchbase SDK Tutorials</description>
|
||||
|
||||
<parent>
|
||||
@ -23,6 +23,12 @@
|
||||
<version>${couchbase.client.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson-version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Context for Dependency Injection -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
@ -67,9 +73,10 @@
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<couchbase.client.version>2.4.0</couchbase.client.version>
|
||||
<couchbase.client.version>2.5.0</couchbase.client.version>
|
||||
<spring-framework.version>4.3.5.RELEASE</spring-framework.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<jackson-version>2.9.1</jackson-version>
|
||||
</properties>
|
||||
|
||||
</project>
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user