fix conflicts and merge with remote
This commit is contained in:
@@ -34,6 +34,11 @@
|
||||
<artifactId>jenetics</artifactId>
|
||||
<version>3.7.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jgrapht</groupId>
|
||||
<artifactId>jgrapht-core</artifactId>
|
||||
<version>1.0.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.jgrapht;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.jgrapht.VertexFactory;
|
||||
import org.jgrapht.alg.HamiltonianCycle;
|
||||
import org.jgrapht.generate.CompleteGraphGenerator;
|
||||
import org.jgrapht.graph.DefaultEdge;
|
||||
import org.jgrapht.graph.SimpleWeightedGraph;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CompleteGraphTest {
|
||||
|
||||
static SimpleWeightedGraph<String, DefaultEdge> completeGraph;
|
||||
static int size = 10;
|
||||
|
||||
@Before
|
||||
public void createCompleteGraph() {
|
||||
completeGraph = new SimpleWeightedGraph<>(DefaultEdge.class);
|
||||
CompleteGraphGenerator<String, DefaultEdge> completeGenerator = new CompleteGraphGenerator<String, DefaultEdge>(size);
|
||||
VertexFactory<String> vFactory = new VertexFactory<String>() {
|
||||
private int id = 0;
|
||||
public String createVertex() {
|
||||
return "v" + id++;
|
||||
}
|
||||
};
|
||||
completeGenerator.generateGraph(completeGraph, vFactory, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCompleteGraph_whenGetHamiltonianCyclePath_thenGetVerticeListInSequence() {
|
||||
List<String> verticeList = HamiltonianCycle.getApproximateOptimalForCompleteGraph(completeGraph);
|
||||
assertEquals(verticeList.size(), completeGraph.vertexSet().size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.baeldung.jgrapht;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.jgrapht.DirectedGraph;
|
||||
import org.jgrapht.GraphPath;
|
||||
import org.jgrapht.alg.CycleDetector;
|
||||
import org.jgrapht.alg.KosarajuStrongConnectivityInspector;
|
||||
import org.jgrapht.alg.interfaces.StrongConnectivityAlgorithm;
|
||||
import org.jgrapht.alg.shortestpath.AllDirectedPaths;
|
||||
import org.jgrapht.alg.shortestpath.BellmanFordShortestPath;
|
||||
import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
|
||||
import org.jgrapht.graph.DefaultDirectedGraph;
|
||||
import org.jgrapht.graph.DefaultEdge;
|
||||
import org.jgrapht.graph.DirectedSubgraph;
|
||||
import org.jgrapht.traverse.BreadthFirstIterator;
|
||||
import org.jgrapht.traverse.DepthFirstIterator;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DirectedGraphTests {
|
||||
DirectedGraph<String, DefaultEdge> directedGraph;
|
||||
|
||||
@Before
|
||||
public void createDirectedGraph() {
|
||||
directedGraph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
|
||||
IntStream.range(1, 10).forEach(i -> {
|
||||
directedGraph.addVertex("v" + i);
|
||||
});
|
||||
directedGraph.addEdge("v1", "v2");
|
||||
directedGraph.addEdge("v2", "v4");
|
||||
directedGraph.addEdge("v4", "v3");
|
||||
directedGraph.addEdge("v3", "v1");
|
||||
directedGraph.addEdge("v5", "v4");
|
||||
directedGraph.addEdge("v5", "v6");
|
||||
directedGraph.addEdge("v6", "v7");
|
||||
directedGraph.addEdge("v7", "v5");
|
||||
directedGraph.addEdge("v8", "v5");
|
||||
directedGraph.addEdge("v9", "v8");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenGetStronglyConnectedSubgraphs_thenPathExistsBetweenStronglyconnectedVertices() {
|
||||
StrongConnectivityAlgorithm<String, DefaultEdge> scAlg = new KosarajuStrongConnectivityInspector<>(directedGraph);
|
||||
List<DirectedSubgraph<String, DefaultEdge>> stronglyConnectedSubgraphs = scAlg.stronglyConnectedSubgraphs();
|
||||
List<String> stronglyConnectedVertices = new ArrayList<>(stronglyConnectedSubgraphs.get(3).vertexSet());
|
||||
|
||||
String randomVertex1 = stronglyConnectedVertices.get(0);
|
||||
String randomVertex2 = stronglyConnectedVertices.get(3);
|
||||
AllDirectedPaths<String, DefaultEdge> allDirectedPaths = new AllDirectedPaths<>(directedGraph);
|
||||
|
||||
List<GraphPath<String, DefaultEdge>> possiblePathList = allDirectedPaths.getAllPaths(randomVertex1, randomVertex2, false, stronglyConnectedVertices.size());
|
||||
assertTrue(possiblePathList.size() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraphWithCycle_whenCheckCycles_thenDetectCycles() {
|
||||
CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<String, DefaultEdge>(directedGraph);
|
||||
assertTrue(cycleDetector.detectCycles());
|
||||
Set<String> cycleVertices = cycleDetector.findCycles();
|
||||
assertTrue(cycleVertices.size() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenCreateInstanceDepthFirstIterator_thenGetIterator() {
|
||||
DepthFirstIterator depthFirstIterator = new DepthFirstIterator<>(directedGraph);
|
||||
assertNotNull(depthFirstIterator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenCreateInstanceBreadthFirstIterator_thenGetIterator() {
|
||||
BreadthFirstIterator breadthFirstIterator = new BreadthFirstIterator<>(directedGraph);
|
||||
assertNotNull(breadthFirstIterator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenGetDijkstraShortestPath_thenGetNotNullPath() {
|
||||
DijkstraShortestPath dijkstraShortestPath = new DijkstraShortestPath(directedGraph);
|
||||
List<String> shortestPath = dijkstraShortestPath.getPath("v1", "v4").getVertexList();
|
||||
assertNotNull(shortestPath);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenGetBellmanFordShortestPath_thenGetNotNullPath() {
|
||||
BellmanFordShortestPath bellmanFordShortestPath = new BellmanFordShortestPath(directedGraph);
|
||||
List<String> shortestPath = bellmanFordShortestPath.getPath("v1", "v4").getVertexList();
|
||||
assertNotNull(shortestPath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.jgrapht;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.jgrapht.GraphPath;
|
||||
import org.jgrapht.alg.cycle.HierholzerEulerianCycle;
|
||||
import org.jgrapht.graph.DefaultEdge;
|
||||
import org.jgrapht.graph.SimpleWeightedGraph;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class EulerianCircuitTest {
|
||||
SimpleWeightedGraph<String, DefaultEdge> simpleGraph;
|
||||
|
||||
@Before
|
||||
public void createGraphWithEulerianCircuit() {
|
||||
simpleGraph = new SimpleWeightedGraph<>(DefaultEdge.class);
|
||||
IntStream.range(1, 6).forEach(i -> {
|
||||
simpleGraph.addVertex("v" + i);
|
||||
});
|
||||
IntStream.range(1, 6).forEach(i -> {
|
||||
int endVertexNo = (i + 1) > 5 ? 1 : i + 1;
|
||||
simpleGraph.addEdge("v" + i, "v" + endVertexNo);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGraph_whenCheckEluerianCycle_thenGetResult() {
|
||||
HierholzerEulerianCycle eulerianCycle = new HierholzerEulerianCycle<>();
|
||||
assertTrue(eulerianCycle.isEulerian(simpleGraph));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGraphWithEulerianCircuit_whenGetEulerianCycle_thenGetGraphPath() {
|
||||
HierholzerEulerianCycle eulerianCycle = new HierholzerEulerianCycle<>();
|
||||
GraphPath path = eulerianCycle.getEulerianCycle(simpleGraph);
|
||||
assertTrue(path.getEdgeList().containsAll(simpleGraph.edgeSet()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<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/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>animal-sniffer-mvn-plugin</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>example-animal-sniffer-mvn-plugin</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>animal-sniffer-maven-plugin</artifactId>
|
||||
<version>1.16</version>
|
||||
<configuration>
|
||||
<signature>
|
||||
<groupId>org.codehaus.mojo.signature</groupId>
|
||||
<artifactId>java16</artifactId>
|
||||
<version>1.0</version>
|
||||
</signature>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>animal-sniffer</id>
|
||||
<phase>verify</phase>
|
||||
<goals>
|
||||
<goal>check</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung;
|
||||
|
||||
//import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
System.out.println( "Hello World!" );
|
||||
//System.out.println(StandardCharsets.UTF_8.name());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
|
||||
assertTrue( true );
|
||||
|
||||
}
|
||||
}
|
||||
+5
-13
@@ -19,22 +19,14 @@ public abstract class _Author extends CayenneDataObject {
|
||||
|
||||
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<String> NAME = Property.create("name", String.class);
|
||||
public static final Property<List<Article>> ARTICLES = Property.create("articles", List.class);
|
||||
|
||||
public void setFirstname(String firstname) {
|
||||
writeProperty("firstname", firstname);
|
||||
public void setName(String name) {
|
||||
writeProperty("name", name);
|
||||
}
|
||||
public String getFirstname() {
|
||||
return (String)readProperty("firstname");
|
||||
}
|
||||
|
||||
public void setLastname(String lastname) {
|
||||
writeProperty("lastname", lastname);
|
||||
}
|
||||
public String getLastname() {
|
||||
return (String)readProperty("lastname");
|
||||
public String getName() {
|
||||
return (String)readProperty("name");
|
||||
}
|
||||
|
||||
public void addToArticles(Article obj) {
|
||||
|
||||
@@ -11,17 +11,15 @@
|
||||
<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-attribute name="name" 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-attribute name="name" type="java.lang.String" db-attribute-path="name"/>
|
||||
</obj-entity>
|
||||
<db-relationship name="author" source="article" target="author" toMany="false">
|
||||
<db-attribute-pair source="author_id" target="id"/>
|
||||
|
||||
+256
@@ -0,0 +1,256 @@
|
||||
package com.baeldung.apachecayenne;
|
||||
|
||||
import com.baeldung.apachecayenne.persistent.Author;
|
||||
import org.apache.cayenne.ObjectContext;
|
||||
import org.apache.cayenne.QueryResponse;
|
||||
import org.apache.cayenne.configuration.server.ServerRuntime;
|
||||
import org.apache.cayenne.exp.Expression;
|
||||
import org.apache.cayenne.exp.ExpressionFactory;
|
||||
import org.apache.cayenne.query.*;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class CayenneAdvancedOperationTests {
|
||||
private static ObjectContext context = null;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupTheCayenneContext() {
|
||||
ServerRuntime cayenneRuntime = ServerRuntime.builder()
|
||||
.addConfig("cayenne-project.xml")
|
||||
.build();
|
||||
context = cayenneRuntime.newContext();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void saveThreeAuthors() {
|
||||
Author authorOne = context.newObject(Author.class);
|
||||
authorOne.setName("Paul Xavier");
|
||||
Author authorTwo = context.newObject(Author.class);
|
||||
authorTwo.setName("pAuL Smith");
|
||||
Author authorThree = context.newObject(Author.class);
|
||||
authorThree.setName("Vicky Sarra");
|
||||
context.commitChanges();
|
||||
}
|
||||
|
||||
@After
|
||||
public void deleteAllAuthors() {
|
||||
SQLTemplate deleteAuthors = new SQLTemplate(Author.class, "delete from author");
|
||||
context.performGenericQuery(deleteAuthors);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenFindAllSQLTmplt_thenWeGetThreeAuthors() {
|
||||
SQLTemplate select = new SQLTemplate(Author.class, "select * from Author");
|
||||
List<Author> authors = context.performQuery(select);
|
||||
|
||||
assertEquals(authors.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenFindByNameSQLTmplt_thenWeGetOneAuthor() {
|
||||
SQLTemplate select = new SQLTemplate(Author.class, "select * from Author where name = 'Vicky Sarra'");
|
||||
List<Author> authors = context.performQuery(select);
|
||||
Author author = authors.get(0);
|
||||
|
||||
assertEquals(authors.size(), 1);
|
||||
assertEquals(author.getName(), "Vicky Sarra");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenLikeSltQry_thenWeGetOneAuthor() {
|
||||
Expression qualifier = ExpressionFactory.likeExp(Author.NAME.getName(), "Paul%");
|
||||
SelectQuery query = new SelectQuery(Author.class, qualifier);
|
||||
List<Author> authorsTwo = context.performQuery(query);
|
||||
|
||||
assertEquals(authorsTwo.size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenCtnsIgnorCaseSltQry_thenWeGetTwoAuthors() {
|
||||
Expression qualifier = ExpressionFactory.containsIgnoreCaseExp(Author.NAME.getName(), "Paul");
|
||||
SelectQuery query = new SelectQuery(Author.class, qualifier);
|
||||
List<Author> authors = context.performQuery(query);
|
||||
|
||||
assertEquals(authors.size(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenCtnsIgnorCaseEndsWSltQry_thenWeGetTwoAuthors() {
|
||||
Expression qualifier = ExpressionFactory.containsIgnoreCaseExp(Author.NAME.getName(), "Paul")
|
||||
.andExp(ExpressionFactory.endsWithExp(Author.NAME.getName(), "h"));
|
||||
SelectQuery query = new SelectQuery(Author.class, qualifier);
|
||||
List<Author> authors = context.performQuery(query);
|
||||
|
||||
Author author = authors.get(0);
|
||||
|
||||
assertEquals(authors.size(), 1);
|
||||
assertEquals(author.getName(), "pAuL Smith");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenAscOrderingSltQry_thenWeGetOrderedAuthors() {
|
||||
SelectQuery query = new SelectQuery(Author.class);
|
||||
query.addOrdering(Author.NAME.asc());
|
||||
|
||||
List<Author> authors = query.select(context);
|
||||
Author firstAuthor = authors.get(0);
|
||||
|
||||
assertEquals(authors.size(), 3);
|
||||
assertEquals(firstAuthor.getName(), "Paul Xavier");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenDescOrderingSltQry_thenWeGetOrderedAuthors() {
|
||||
SelectQuery query = new SelectQuery(Author.class);
|
||||
query.addOrdering(Author.NAME.desc());
|
||||
|
||||
List<Author> authors = query.select(context);
|
||||
Author firstAuthor = authors.get(0);
|
||||
|
||||
assertEquals(authors.size(), 3);
|
||||
assertEquals(firstAuthor.getName(), "pAuL Smith");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_onContainsObjS_thenWeGetOneRecord() {
|
||||
List<Author> authors = ObjectSelect.query(Author.class)
|
||||
.where(Author.NAME.contains("Paul"))
|
||||
.select(context);
|
||||
|
||||
assertEquals(authors.size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenLikeObjS_thenWeGetTwoAuthors() {
|
||||
List<Author> authors = ObjectSelect.query(Author.class)
|
||||
.where(Author.NAME.likeIgnoreCase("Paul%"))
|
||||
.select(context);
|
||||
|
||||
assertEquals(authors.size(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoAuthor_whenEndsWithObjS_thenWeGetOrderedAuthors() {
|
||||
List<Author> authors = ObjectSelect.query(Author.class)
|
||||
.where(Author.NAME.endsWith("Sarra"))
|
||||
.select(context);
|
||||
Author firstAuthor = authors.get(0);
|
||||
|
||||
assertEquals(authors.size(), 1);
|
||||
assertEquals(firstAuthor.getName(), "Vicky Sarra");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoAuthor_whenInObjS_thenWeGetAuthors() {
|
||||
String [] args = {"Paul Xavier", "pAuL Smith", "Vicky Sarra"};
|
||||
List<Author> authors = ObjectSelect.query(Author.class)
|
||||
.where(Author.NAME.in(Arrays.asList(args)))
|
||||
.select(context);
|
||||
|
||||
assertEquals(authors.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoAuthor_whenNinObjS_thenWeGetAuthors() {
|
||||
String [] args = {"Paul Xavier", "pAuL Smith"};
|
||||
List<Author> authors = ObjectSelect.query(Author.class)
|
||||
.where(Author.NAME.nin(Arrays.asList(args)))
|
||||
.select(context);
|
||||
Author author = authors.get(0);
|
||||
|
||||
assertEquals(authors.size(), 1);
|
||||
assertEquals(author.getName(), "Vicky Sarra");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoAuthor_whenIsNotNullObjS_thenWeGetAuthors() {
|
||||
List<Author> authors = ObjectSelect.query(Author.class)
|
||||
.where(Author.NAME.isNotNull())
|
||||
.select(context);
|
||||
|
||||
assertEquals(authors.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenFindAllEJBQL_thenWeGetThreeAuthors() {
|
||||
EJBQLQuery query = new EJBQLQuery("select a FROM Author a");
|
||||
List<Author> authors = context.performQuery(query);
|
||||
|
||||
assertEquals(authors.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenFindByNameEJBQL_thenWeGetOneAuthor() {
|
||||
EJBQLQuery query = new EJBQLQuery("select a FROM Author a WHERE a.name = 'Vicky Sarra'");
|
||||
List<Author> authors = context.performQuery(query);
|
||||
Author author = authors.get(0);
|
||||
|
||||
assertEquals(authors.size(), 1);
|
||||
assertEquals(author.getName(), "Vicky Sarra");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenUpdadingByNameEJBQL_thenWeGetTheUpdatedAuthor() {
|
||||
EJBQLQuery query = new EJBQLQuery("UPDATE Author AS a SET a.name = 'Vicky Edison' WHERE a.name = 'Vicky Sarra'");
|
||||
QueryResponse queryResponse = context.performGenericQuery(query);
|
||||
|
||||
EJBQLQuery queryUpdatedAuthor = new EJBQLQuery("select a FROM Author a WHERE a.name = 'Vicky Edison'");
|
||||
List<Author> authors = context.performQuery(queryUpdatedAuthor);
|
||||
Author author = authors.get(0);
|
||||
|
||||
assertNotNull(author);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenSeletingNamesEJBQL_thenWeGetListWithSizeThree() {
|
||||
String [] args = {"Paul Xavier", "pAuL Smith", "Vicky Sarra"};
|
||||
List<String> names = Arrays.asList(args);
|
||||
EJBQLQuery query = new EJBQLQuery("select a.name FROM Author a");
|
||||
List<String> nameList = context.performQuery(query);
|
||||
|
||||
Collections.sort(names);
|
||||
Collections.sort(nameList);
|
||||
|
||||
assertEquals(names.size(), 3);
|
||||
assertEquals(nameList.size(), 3);
|
||||
assertEquals(names, nameList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenDeletingAllWithEJB_thenWeGetNoAuthor() {
|
||||
EJBQLQuery deleteQuery = new EJBQLQuery("delete FROM Author");
|
||||
EJBQLQuery findAllQuery = new EJBQLQuery("select a FROM Author a");
|
||||
|
||||
context.performQuery(deleteQuery);
|
||||
List<Author> objects = context.performQuery(findAllQuery);
|
||||
|
||||
assertEquals(objects.size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenInsertingSQLExec_thenWeGetNewAuthor() {
|
||||
int inserted = SQLExec
|
||||
.query("INSERT INTO Author (name) VALUES ('Baeldung')")
|
||||
.update(context);
|
||||
|
||||
assertEquals(inserted, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenUpdatingSQLExec_thenItsUpdated() {
|
||||
int updated = SQLExec
|
||||
.query("UPDATE Author SET name = 'Baeldung' WHERE name = 'Vicky Sarra'")
|
||||
.update(context);
|
||||
|
||||
assertEquals(updated, 1);
|
||||
}
|
||||
}
|
||||
+15
-24
@@ -39,8 +39,7 @@ public class CayenneOperationTests {
|
||||
@Test
|
||||
public void givenAuthor_whenInsert_thenWeGetOneRecordInTheDatabase() {
|
||||
Author author = context.newObject(Author.class);
|
||||
author.setFirstname("Paul");
|
||||
author.setLastname("Smith");
|
||||
author.setName("Paul");
|
||||
|
||||
context.commitChanges();
|
||||
|
||||
@@ -51,28 +50,24 @@ public class CayenneOperationTests {
|
||||
@Test
|
||||
public void givenAuthor_whenInsert_andQueryByFirstName_thenWeGetTheAuthor() {
|
||||
Author author = context.newObject(Author.class);
|
||||
author.setFirstname("Paul");
|
||||
author.setLastname("Smith");
|
||||
author.setName("Paul");
|
||||
|
||||
context.commitChanges();
|
||||
|
||||
Author expectedAuthor = ObjectSelect.query(Author.class)
|
||||
.where(Author.FIRSTNAME.eq("Paul"))
|
||||
.where(Author.NAME.eq("Paul"))
|
||||
.selectOne(context);
|
||||
|
||||
assertEquals("Paul", expectedAuthor.getFirstname());
|
||||
assertEquals("Smith", expectedAuthor.getLastname());
|
||||
assertEquals("Paul", expectedAuthor.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoAuthor_whenInsert_andQueryAll_thenWeGetTwoAuthors() {
|
||||
Author firstAuthor = context.newObject(Author.class);
|
||||
firstAuthor.setFirstname("Paul");
|
||||
firstAuthor.setLastname("Smith");
|
||||
firstAuthor.setName("Paul");
|
||||
|
||||
Author secondAuthor = context.newObject(Author.class);
|
||||
secondAuthor.setFirstname("Ludovic");
|
||||
secondAuthor.setLastname("Garcia");
|
||||
secondAuthor.setName("Ludovic");
|
||||
|
||||
context.commitChanges();
|
||||
|
||||
@@ -83,44 +78,40 @@ public class CayenneOperationTests {
|
||||
@Test
|
||||
public void givenAuthor_whenUpdating_thenWeGetAnUpatedeAuthor() {
|
||||
Author author = context.newObject(Author.class);
|
||||
author.setFirstname("Paul");
|
||||
author.setLastname("Smith");
|
||||
author.setName("Paul");
|
||||
context.commitChanges();
|
||||
|
||||
Author expectedAuthor = ObjectSelect.query(Author.class)
|
||||
.where(Author.FIRSTNAME.eq("Paul"))
|
||||
.where(Author.NAME.eq("Paul"))
|
||||
.selectOne(context);
|
||||
expectedAuthor.setLastname("Smith 2");
|
||||
expectedAuthor.setName("Garcia");
|
||||
context.commitChanges();
|
||||
|
||||
assertEquals(author.getFirstname(), expectedAuthor.getFirstname());
|
||||
assertEquals(author.getLastname(), expectedAuthor.getLastname());
|
||||
assertEquals(author.getName(), expectedAuthor.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthor_whenDeleting_thenWeLostHisDetails() {
|
||||
Author author = context.newObject(Author.class);
|
||||
author.setFirstname("Paul");
|
||||
author.setLastname("Smith");
|
||||
author.setName("Paul");
|
||||
context.commitChanges();
|
||||
|
||||
Author savedAuthor = ObjectSelect.query(Author.class)
|
||||
.where(Author.FIRSTNAME.eq("Paul")).selectOne(context);
|
||||
.where(Author.NAME.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);
|
||||
.where(Author.NAME.eq("Paul")).selectOne(context);
|
||||
assertNull(expectedAuthor);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthor_whenAttachingToArticle_thenTheRelationIsMade() {
|
||||
Author author = context.newObject(Author.class);
|
||||
author.setFirstname("Paul");
|
||||
author.setLastname("Smith");
|
||||
author.setName("Paul");
|
||||
|
||||
Article article = context.newObject(Article.class);
|
||||
article.setTitle("My post title");
|
||||
@@ -130,7 +121,7 @@ public class CayenneOperationTests {
|
||||
context.commitChanges();
|
||||
|
||||
Author expectedAuthor = ObjectSelect.query(Author.class)
|
||||
.where(Author.LASTNAME.eq("Smith"))
|
||||
.where(Author.NAME.eq("Paul"))
|
||||
.selectOne(context);
|
||||
|
||||
Article expectedArticle = (expectedAuthor.getArticles()).get(0);
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
/target
|
||||
@@ -0,0 +1,46 @@
|
||||
<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.atomix.io</groupId>
|
||||
<artifactId>atomix</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.atomix</groupId>
|
||||
<artifactId>atomix-all</artifactId>
|
||||
<version>1.0.0-rc9</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.9</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.17</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.atomix.example;
|
||||
|
||||
import io.atomix.AtomixReplica;
|
||||
import io.atomix.catalyst.transport.Address;
|
||||
import io.atomix.catalyst.transport.netty.NettyTransport;
|
||||
import io.atomix.copycat.server.storage.Storage;
|
||||
import io.atomix.copycat.server.storage.StorageLevel;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class BootstrapingCluster {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Storage storage = Storage.builder()
|
||||
.withDirectory(new File("log"))
|
||||
.withStorageLevel(StorageLevel.DISK)
|
||||
.build();
|
||||
AtomixReplica replica = AtomixReplica.builder(new Address("localhost", 8700))
|
||||
.withStorage(storage)
|
||||
.withTransport(new NettyTransport())
|
||||
.build();
|
||||
|
||||
CompletableFuture<AtomixReplica> completableFuture = replica.bootstrap();
|
||||
completableFuture.join();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.atomix.example;
|
||||
|
||||
import io.atomix.AtomixReplica;
|
||||
import io.atomix.catalyst.transport.Address;
|
||||
import io.atomix.catalyst.transport.netty.NettyTransport;
|
||||
import io.atomix.concurrent.DistributedLock;
|
||||
import io.atomix.copycat.server.storage.Storage;
|
||||
import io.atomix.copycat.server.storage.StorageLevel;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class OtherNodes {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
List<Address> cluster = Arrays
|
||||
.asList(
|
||||
new Address("localhost", 8700),
|
||||
new Address("localhost", 8701),
|
||||
new Address("localhost", 8702));
|
||||
|
||||
Storage storage = Storage.builder()
|
||||
.withDirectory(new File("log"))
|
||||
.withStorageLevel(StorageLevel.DISK)
|
||||
.build();
|
||||
|
||||
AtomixReplica replica2 = AtomixReplica.builder(new Address("localhost", 8701))
|
||||
.withStorage(storage)
|
||||
.withTransport(new NettyTransport())
|
||||
.build();
|
||||
|
||||
WorkerThread WT1 = new WorkerThread(replica2, cluster);
|
||||
WT1.run();
|
||||
|
||||
AtomixReplica replica3 = AtomixReplica.builder(new Address("localhost", 8702))
|
||||
.withStorage(storage)
|
||||
.withTransport(new NettyTransport())
|
||||
.build();
|
||||
|
||||
WorkerThread WT2 = new WorkerThread(replica3, cluster);
|
||||
WT2.run();
|
||||
|
||||
Thread.sleep(6000);
|
||||
|
||||
DistributedLock lock = replica2.getLock("my-lock")
|
||||
.join();
|
||||
lock.lock()
|
||||
.thenRun(() -> System.out.println("Acquired a lock"));
|
||||
|
||||
replica2.getMap("map")
|
||||
.thenCompose(m -> m.put("bar", "Hello world!"))
|
||||
.thenRun(() -> System.out.println("Value is set in Distributed Map"))
|
||||
.join();
|
||||
}
|
||||
|
||||
private static class WorkerThread extends Thread {
|
||||
private AtomixReplica replica;
|
||||
private List<Address> cluster;
|
||||
|
||||
WorkerThread(AtomixReplica replica, List<Address> cluster) {
|
||||
this.replica = replica;
|
||||
this.cluster = cluster;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
replica.join(cluster)
|
||||
.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.atomix.exampletest;
|
||||
|
||||
import io.atomix.AtomixClient;
|
||||
import io.atomix.catalyst.transport.Address;
|
||||
import io.atomix.catalyst.transport.netty.NettyTransport;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class AtomixClientLiveTest {
|
||||
|
||||
private final AtomixClient client = AtomixClient.builder()
|
||||
.withTransport(new NettyTransport())
|
||||
.build();
|
||||
|
||||
@Test
|
||||
public void whenBootstrap_thenShouldGet() throws InterruptedException, ExecutionException {
|
||||
List<Address> cluster = Arrays.asList(
|
||||
new Address("localhost", 8700),
|
||||
new Address("localhsot", 8701));
|
||||
|
||||
String value = client.connect(cluster)
|
||||
.thenRun(() -> System.out.println("Client Connected"))
|
||||
.thenCompose(c -> client.getMap("map"))
|
||||
.thenCompose(m -> m.get("bar"))
|
||||
.thenApply(a -> (String) a)
|
||||
.get();
|
||||
|
||||
assertEquals("Hello world!", value);
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import java.nio.file.WatchKey;
|
||||
import java.nio.file.WatchService;
|
||||
|
||||
public class DirectoryWatcherExample {
|
||||
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
WatchService watchService = FileSystems.getDefault().newWatchService();
|
||||
Path path = Paths.get(System.getProperty("user.home"));
|
||||
@@ -21,5 +22,8 @@ public class DirectoryWatcherExample {
|
||||
}
|
||||
key.reset();
|
||||
}
|
||||
|
||||
watchService.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
Class-Path:
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||
id="WebApp_ID" version="3.1">
|
||||
<display-name>tomcat-app</display-name>
|
||||
<servlet>
|
||||
<servlet-name>tomcat-app</servlet-name>
|
||||
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
|
||||
<init-param>
|
||||
<param-name>javax.ws.rs.Application</param-name>
|
||||
<param-value>com.stackify.ApplicationInitializer</param-value>
|
||||
</init-param>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>tomcat-app</servlet-name>
|
||||
<url-pattern>/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
<filter>
|
||||
<filter-name>javamelody</filter-name>
|
||||
<filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
|
||||
<init-param>
|
||||
<param-name>gzip-compression-disabled</param-name>
|
||||
<param-value>true</param-value>
|
||||
</init-param>
|
||||
</filter>
|
||||
</web-app>
|
||||
@@ -0,0 +1,70 @@
|
||||
<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.stackify</groupId>
|
||||
<artifactId>tomcat-app</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.containers</groupId>
|
||||
<artifactId>jersey-container-servlet</artifactId>
|
||||
<version>2.25.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.media</groupId>
|
||||
<artifactId>jersey-media-moxy</artifactId>
|
||||
<version>2.25.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>rest-assured</artifactId>
|
||||
<version>3.0.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.195</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>2.8.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.bull.javamelody</groupId>
|
||||
<artifactId>javamelody-core</artifactId>
|
||||
<version>1.69.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<configuration>
|
||||
<warSourceDirectory>WebContent</warSourceDirectory>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.stackify;
|
||||
|
||||
import org.glassfish.jersey.server.ResourceConfig;
|
||||
|
||||
public class ApplicationInitializer extends ResourceConfig {
|
||||
public ApplicationInitializer() {
|
||||
packages("com.stackify.services");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.stackify.daos;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.stackify.models.User;
|
||||
import com.stackify.utils.ConnectionUtil;
|
||||
|
||||
public class UserDAO {
|
||||
|
||||
private Logger logger = LogManager.getLogger(UserDAO.class);
|
||||
|
||||
public void createTable() {
|
||||
try (Connection con = ConnectionUtil.getConnection()) {
|
||||
String createQuery = "CREATE TABLE IF NOT EXISTS users(email varchar(50) primary key, name varchar(50))";
|
||||
PreparedStatement pstmt = con.prepareStatement(createQuery);
|
||||
|
||||
pstmt.execute();
|
||||
} catch (SQLException exc) {
|
||||
logger.error(exc.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void add(User user) {
|
||||
try (Connection con = ConnectionUtil.getConnection()) {
|
||||
|
||||
String insertQuery = "INSERT INTO users(email,name) VALUES(?,?)";
|
||||
PreparedStatement pstmt = con.prepareStatement(insertQuery);
|
||||
pstmt.setString(1, user.getEmail());
|
||||
pstmt.setString(2, user.getName());
|
||||
|
||||
pstmt.executeUpdate();
|
||||
} catch (SQLException exc) {
|
||||
logger.error(exc.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public List<User> findAll() {
|
||||
List<User> users = new ArrayList<>();
|
||||
|
||||
try (Connection con = ConnectionUtil.getConnection()) {
|
||||
String query = "SELECT * FROM users";
|
||||
PreparedStatement pstmt = con.prepareStatement(query);
|
||||
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
User user = new User();
|
||||
user.setEmail(rs.getString("email"));
|
||||
user.setName(rs.getString("name"));
|
||||
users.add(user);
|
||||
}
|
||||
} catch (SQLException exc) {
|
||||
logger.error(exc.getMessage());
|
||||
}
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.stackify.models;
|
||||
|
||||
import javax.ws.rs.core.Link;
|
||||
|
||||
public class User {
|
||||
private String email;
|
||||
private String name;
|
||||
private Link link;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String email, String name) {
|
||||
super();
|
||||
this.email = email;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Link getLink() {
|
||||
return link;
|
||||
}
|
||||
|
||||
public void setLink(Link link) {
|
||||
this.link = link;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.stackify.services;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.ws.rs.container.ContainerRequestContext;
|
||||
import javax.ws.rs.container.ContainerResponseContext;
|
||||
import javax.ws.rs.container.ContainerResponseFilter;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
|
||||
@Provider
|
||||
public class CorsFilter implements ContainerResponseFilter {
|
||||
|
||||
@Override
|
||||
public void filter(final ContainerRequestContext requestContext,
|
||||
final ContainerResponseContext response) throws IOException {
|
||||
response.getHeaders().add("Access-Control-Allow-Origin", "*");
|
||||
response.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.stackify.services;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
import com.stackify.daos.UserDAO;
|
||||
import com.stackify.models.User;
|
||||
|
||||
@Path("/users")
|
||||
public class UserService {
|
||||
private UserDAO userDao = new UserDAO();
|
||||
|
||||
public UserService (){
|
||||
userDao.createTable();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public Response addUser(User user) {
|
||||
userDao.add(user);
|
||||
return Response.ok()
|
||||
.build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public List<User> getUsers() {
|
||||
return userDao.findAll();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.stackify.utils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class ConnectionUtil {
|
||||
|
||||
private static Logger logger = LogManager.getLogger(ConnectionUtil.class);
|
||||
|
||||
public static Connection getConnection() {
|
||||
try {
|
||||
String jndiName = "java:/comp/env/jdbc/MyDataSource";
|
||||
|
||||
Context initialContext = new InitialContext();
|
||||
DataSource datasource = (DataSource)initialContext.lookup(jndiName);
|
||||
if (datasource != null) {
|
||||
return datasource.getConnection();
|
||||
}
|
||||
else {
|
||||
logger.error("Failed to lookup datasource.");
|
||||
}
|
||||
}
|
||||
|
||||
catch (NamingException | SQLException exc) {
|
||||
logger.error(exc.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
<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.beanvalidation</groupId>
|
||||
<artifactId>beanvalidation</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>beanvalidation</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>2.0.0.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>6.0.2.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.el</groupId>
|
||||
<artifactId>javax.el-api</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.web</groupId>
|
||||
<artifactId>javax.el</artifactId>
|
||||
<version>2.2.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.beanvalidation.application;
|
||||
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import com.baeldung.beanvalidation.model.User;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main( String[] args ) {
|
||||
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
User user = new User();
|
||||
user.setName("Mary");
|
||||
user.setEmail("no-email");
|
||||
user.setAge(36);
|
||||
validator.validate(user).stream().forEach(violation -> System.out.println(violation.getMessage()));
|
||||
}
|
||||
}
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.beanvalidation.container;
|
||||
|
||||
import java.util.Optional;
|
||||
import javax.validation.constraints.Positive;
|
||||
|
||||
public class IntegerContainer {
|
||||
|
||||
private Optional<@Positive(message = "Value must be a positive integer") Integer> container = Optional.empty();
|
||||
|
||||
public void addElement(int element) {
|
||||
container = Optional.of(element);
|
||||
}
|
||||
}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
package com.baeldung.beanvalidation.container;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
public class StringContainer {
|
||||
|
||||
private List<@NotNull String> container = new ArrayList<>();
|
||||
|
||||
public void addElement(String element) {
|
||||
container.add(element);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user