Merge remote-tracking branch 'eugenp/master'
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class BinarySearch {
|
||||
|
||||
public int runBinarySearch() {
|
||||
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
|
||||
int key = 6;
|
||||
public int runBinarySearchIteratively(int[] sortedArray, int key, int low, int high) {
|
||||
|
||||
int low = 0;
|
||||
int high = sortedArray.length - 1;
|
||||
int index = Integer.MAX_VALUE;
|
||||
|
||||
while (low <= high) {
|
||||
@@ -23,4 +23,31 @@ public class BinarySearch {
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
public int runBinarySearchRecursively(int[] sortedArray, int key, int low, int high) {
|
||||
|
||||
int middle = (low + high) / 2;
|
||||
if (high < low) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (key == sortedArray[middle]) {
|
||||
return middle;
|
||||
} else if (key < sortedArray[middle]) {
|
||||
return runBinarySearchRecursively(sortedArray, key, low, middle - 1);
|
||||
} else {
|
||||
return runBinarySearchRecursively(sortedArray, key, middle + 1, high);
|
||||
}
|
||||
}
|
||||
|
||||
public int runBinarySearchUsingJavaArrays(int[] sortedArray, Integer key) {
|
||||
int index = Arrays.binarySearch(sortedArray, key);
|
||||
return index;
|
||||
}
|
||||
|
||||
public int runBinarySearchUsingJavaCollections(List<Integer> sortedList, Integer key) {
|
||||
int index = Collections.binarySearch(sortedList, key);
|
||||
return index;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,14 +1,41 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class BinarySearchTest {
|
||||
|
||||
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
|
||||
int key = 6;
|
||||
int expectedIndexForSearchKey = 7;
|
||||
int low = 0;
|
||||
int high = sortedArray.length - 1;
|
||||
List<Integer> sortedList = Arrays.asList(0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9);
|
||||
|
||||
@Test
|
||||
public void givenASortedArrayOfIntegers_whenBinarySearchRunForANumber_thenGetIndexOfTheNumber() {
|
||||
public void givenASortedArrayOfIntegers_whenBinarySearchRunIterativelyForANumber_thenGetIndexOfTheNumber() {
|
||||
BinarySearch binSearch = new BinarySearch();
|
||||
int expectedIndexForSearchKey = 7;
|
||||
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearch());
|
||||
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchIteratively(sortedArray, key, low, high));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenASortedArrayOfIntegers_whenBinarySearchRunRecursivelyForANumber_thenGetIndexOfTheNumber() {
|
||||
BinarySearch binSearch = new BinarySearch();
|
||||
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchRecursively(sortedArray, key, low, high));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenASortedArrayOfIntegers_whenBinarySearchRunUsingArraysClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
|
||||
BinarySearch binSearch = new BinarySearch();
|
||||
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaArrays(sortedArray, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenASortedListOfIntegers_whenBinarySearchRunUsingCollectionsClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
|
||||
BinarySearch binSearch = new BinarySearch();
|
||||
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaCollections(sortedList, key));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class CharSequenceVsStringUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenUsingString_whenInstantiatingString_thenWrong() {
|
||||
CharSequence firstString = "bealdung";
|
||||
String secondString = "baeldung";
|
||||
|
||||
assertNotEquals(firstString, secondString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIdenticalCharSequences_whenCastToString_thenEqual() {
|
||||
CharSequence charSequence1 = "baeldung_1";
|
||||
CharSequence charSequence2 = "baeldung_2";
|
||||
|
||||
assertTrue(charSequence1.toString().compareTo(charSequence2.toString()) > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenAppended_thenUnmodified() {
|
||||
String test = "a";
|
||||
int firstAddressOfTest = System.identityHashCode(test);
|
||||
test += "b";
|
||||
int secondAddressOfTest = System.identityHashCode(test);
|
||||
|
||||
assertEquals(firstAddressOfTest, secondAddressOfTest);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringBuilder_whenAppended_thenModified() {
|
||||
StringBuilder test = new StringBuilder();
|
||||
test.append("a");
|
||||
int firstAddressOfTest = System.identityHashCode(test);
|
||||
test.append("b");
|
||||
int secondAddressOfTest = System.identityHashCode(test);
|
||||
|
||||
assertEquals(firstAddressOfTest, secondAddressOfTest);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.baeldung.java.lists;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ListToSTring {
|
||||
|
||||
@Test
|
||||
public void whenListToString_thenPrintDefault() {
|
||||
List<Integer> intLIst = Arrays.asList(1, 2, 3);
|
||||
System.out.println(intLIst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCollectorsJoining_thenPrintCustom() {
|
||||
List<Integer> intList = Arrays.asList(1, 2, 3);
|
||||
System.out.println(intList.stream()
|
||||
.map(n -> String.valueOf(n))
|
||||
.collect(Collectors.joining("-", "{", "}")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringUtilsJoin_thenPrintCustom() {
|
||||
List<Integer> intList = Arrays.asList(1, 2, 3);
|
||||
System.out.println(StringUtils.join(intList, "|"));
|
||||
}
|
||||
}
|
||||
@@ -1,15 +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.easyrules</groupId>
|
||||
<artifactId>easy-rules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jeasy</groupId>
|
||||
<artifactId>easy-rules-core</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.baeldung.easyrules;
|
||||
|
||||
import org.jeasy.rules.annotation.Action;
|
||||
import org.jeasy.rules.annotation.Condition;
|
||||
import org.jeasy.rules.annotation.Rule;
|
||||
|
||||
@Rule(name = "Hello World rule", description = "Always say hello world")
|
||||
public class HelloWorldRule {
|
||||
|
||||
@Condition
|
||||
public boolean when() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Action
|
||||
public void then() throws Exception {
|
||||
System.out.println("hello world");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.easyrules;
|
||||
|
||||
import org.jeasy.rules.api.Facts;
|
||||
import org.jeasy.rules.api.Rules;
|
||||
import org.jeasy.rules.api.RulesEngine;
|
||||
import org.jeasy.rules.core.DefaultRulesEngine;
|
||||
|
||||
public class Launcher {
|
||||
public static void main(String... args) {
|
||||
// create facts
|
||||
Facts facts = new Facts();
|
||||
|
||||
// create rules
|
||||
Rules rules = new Rules();
|
||||
rules.register(new HelloWorldRule());
|
||||
|
||||
// create a rules engine and fire rules on known facts
|
||||
RulesEngine rulesEngine = new DefaultRulesEngine();
|
||||
rulesEngine.fire(rules, facts);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<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>logback-example</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.2.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.codehaus.janino</groupId>
|
||||
<artifactId>janino</artifactId>
|
||||
<version>3.0.7</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.stackify.logging;
|
||||
|
||||
import org.slf4j.Marker;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.turbo.TurboFilter;
|
||||
import ch.qos.logback.core.spi.FilterReply;
|
||||
|
||||
public class IgnoreLoggerFilter extends TurboFilter {
|
||||
|
||||
private String loggerName;
|
||||
|
||||
@Override
|
||||
public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
|
||||
if (loggerName == null) {
|
||||
return FilterReply.NEUTRAL;
|
||||
} else if (loggerName.equals(logger.getName())) {
|
||||
return FilterReply.DENY;
|
||||
} else
|
||||
return FilterReply.NEUTRAL;
|
||||
}
|
||||
|
||||
public void setLoggerName(String loggerName) {
|
||||
this.loggerName = loggerName;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.stackify.models;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private String email;
|
||||
private String name;
|
||||
|
||||
private double salary;
|
||||
|
||||
public Employee() {
|
||||
}
|
||||
|
||||
public Employee(String email, String name, double salary) {
|
||||
this.email = email;
|
||||
this.name = name;
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
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 double getSalary() {
|
||||
return salary;
|
||||
}
|
||||
|
||||
public void setSalary(double salary) {
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.stackify.services;
|
||||
|
||||
import com.stackify.models.Employee;
|
||||
|
||||
public class EmployeeService {
|
||||
|
||||
public double calculateBonus(Employee user) {
|
||||
return 0.1 * user.getSalary();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
env=dev
|
||||
@@ -0,0 +1,151 @@
|
||||
<configuration debug="true">
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
|
||||
</encoder>
|
||||
<target>System.out</target>
|
||||
</appender>
|
||||
|
||||
<property name="fileName" value="file.log" />
|
||||
<property scope="context" resource="application.properties" />
|
||||
|
||||
<!-- configure appenders -->
|
||||
<appender name="rollingFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>log-%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
<maxHistory>30</maxHistory>
|
||||
<totalSizeCap>3GB</totalSizeCap>
|
||||
</rollingPolicy>
|
||||
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
|
||||
<maxFileSize>3MB</maxFileSize>
|
||||
</triggeringPolicy>
|
||||
<encoder>
|
||||
<pattern>%d [%thread] %-5level %logger{50} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="roleSiftingAppender" class="ch.qos.logback.classic.sift.SiftingAppender">
|
||||
<discriminator>
|
||||
<key>userRole</key>
|
||||
<defaultValue>ANONYMOUS</defaultValue>
|
||||
</discriminator>
|
||||
<sift>
|
||||
<appender name="fileAppender" class="ch.qos.logback.core.FileAppender">
|
||||
<file>${userRole}.log</file>
|
||||
<layout class="ch.qos.logback.classic.PatternLayout">
|
||||
<pattern>%d [%thread] %level %mdc %logger{50} - %msg%n</pattern>
|
||||
</layout>
|
||||
</appender>
|
||||
</sift>
|
||||
</appender>
|
||||
|
||||
<!-- configure layouts -->
|
||||
<appender name="colorAppender" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<pattern>%d %green([%thread]) %highlight(%level) %logger{50} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="htmlAppender" class="ch.qos.logback.core.FileAppender">
|
||||
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
|
||||
<layout class="ch.qos.logback.classic.html.HTMLLayout">
|
||||
<pattern>%thread%level%logger%msg</pattern>
|
||||
</layout>
|
||||
</encoder>
|
||||
<file>log.html</file>
|
||||
</appender>
|
||||
|
||||
<!-- configure filters -->
|
||||
<appender name="STDOUT_LEVEL_FILTER_APPENDER" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>ERROR</level>
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
|
||||
</encoder>
|
||||
<target>System.err</target>
|
||||
</appender>
|
||||
|
||||
<appender name="STDOUT_THRESHOLD_FILTER_APPENDER" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
|
||||
<level>WARN</level>
|
||||
</filter>
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="STDOUT_EVALUATOR_FILTER_APPENDER" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
|
||||
<evaluator class="ch.qos.logback.classic.boolex.JaninoEventEvaluator">
|
||||
<expression>return (level > DEBUG && message.toLowerCase().contains("employee"));</expression>
|
||||
</evaluator>
|
||||
<OnMismatch>DENY</OnMismatch>
|
||||
<OnMatch>NEUTRAL</OnMatch>
|
||||
</filter>
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- configure turbo filters -->
|
||||
<turboFilter class="ch.qos.logback.classic.turbo.DuplicateMessageFilter">
|
||||
<AllowedRepetitions>2</AllowedRepetitions>
|
||||
</turboFilter>
|
||||
|
||||
<turboFilter class="com.stackify.logging.IgnoreLoggerFilter">
|
||||
<LoggerName>ignoredColorLogger</LoggerName>
|
||||
</turboFilter>
|
||||
|
||||
<!-- configure loggers -->
|
||||
<logger level="info" name="rollingFileLogger" additivity="false">
|
||||
<appender-ref ref="rollingFileAppender" />
|
||||
</logger>
|
||||
|
||||
<logger level="info" name="htmlLogger" additivity="false">
|
||||
<appender-ref ref="htmlAppender" />
|
||||
</logger>
|
||||
|
||||
<logger level="info" name="roleSiftingLogger" additivity="false">
|
||||
<appender-ref ref="roleSiftingAppender" />
|
||||
</logger>
|
||||
|
||||
<logger level="info" name="colorLogger" additivity="false">
|
||||
<appender-ref ref="colorAppender" />
|
||||
</logger>
|
||||
|
||||
<logger level="info" name="ignoredColorLogger">
|
||||
<appender-ref ref="colorAppender" />
|
||||
</logger>
|
||||
|
||||
<logger level="info" name="levelFilterLogger" additivity="false">
|
||||
<appender-ref ref="STDOUT_LEVEL_FILTER_APPENDER" />
|
||||
</logger>
|
||||
|
||||
<logger level="info" name="thresholdFilterLogger" additivity="false">
|
||||
<appender-ref ref="STDOUT_THRESHOLD_FILTER_APPENDER" />
|
||||
</logger>
|
||||
|
||||
<logger level="info" name="evaluatorFilterLogger" additivity="false">
|
||||
<appender-ref ref="STDOUT_EVALUATOR_FILTER_APPENDER" />
|
||||
</logger>
|
||||
|
||||
<!-- configure root logger -->
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
<!-- configure root logger conditionally -->
|
||||
<property scope="context" resource="application.properties" />
|
||||
|
||||
<if condition='property("env").equals("dev")'>
|
||||
<then>
|
||||
<root level="TRACE">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</then>
|
||||
</if>
|
||||
|
||||
</configuration>
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.stackify.services;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
import com.stackify.models.Employee;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
|
||||
public class EmployeeServiceTest {
|
||||
private static final Logger logger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||
|
||||
private EmployeeService employeeService = new EmployeeService();
|
||||
|
||||
@Test
|
||||
public void testAppenders() {
|
||||
Logger rollingFileLogger = LoggerFactory.getLogger("rollingFileLogger");
|
||||
rollingFileLogger.info("Testing rolling file logger");
|
||||
|
||||
MDC.put("userRole", "ADMIN");
|
||||
Logger siftingLogger = LoggerFactory.getLogger("roleSiftingLogger");
|
||||
siftingLogger.info("Admin Action");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLayouts() {
|
||||
Logger htmlLogger = LoggerFactory.getLogger("htmlLogger");
|
||||
htmlLogger.error("Employee Information Update Failed");
|
||||
htmlLogger.info("New Account Created");
|
||||
|
||||
Logger colorLogger = LoggerFactory.getLogger("colorLogger");
|
||||
colorLogger.error("Employee Information Update Failed");
|
||||
colorLogger.info("New Account Created");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLogLevel() {
|
||||
ch.qos.logback.classic.Logger rollingFileLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("rollingFileLogger");
|
||||
rollingFileLogger.setLevel(Level.DEBUG);
|
||||
rollingFileLogger.debug("Testing Log Level");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParameter() {
|
||||
Employee employee = new Employee("[email protected]", "John", 2000);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("The bonus for employee: " + employee.getName() + " is " + employeeService.calculateBonus(employee));
|
||||
}
|
||||
logger.debug("The bonus for employee {} is {}", employee.getName(), employeeService.calculateBonus(employee));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilters() {
|
||||
Logger levelFilterLogger = LoggerFactory.getLogger("levelFilterLogger");
|
||||
levelFilterLogger.error("Employee Information Update Failed");
|
||||
Logger thresholdFilterLogger = LoggerFactory.getLogger("thresholdFilterLogger");
|
||||
thresholdFilterLogger.trace("Employee record inserted");
|
||||
Logger evaluatorFilterLogger = LoggerFactory.getLogger("evaluatorFilterLogger");
|
||||
evaluatorFilterLogger.debug("Employee account deactivated");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoredLogger() {
|
||||
Logger colorLogger = LoggerFactory.getLogger("ignoredColorLogger");
|
||||
colorLogger.info("Ignored Log Message");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConditionalConfiguration() {
|
||||
logger.trace("Employee record updated");
|
||||
}
|
||||
}
|
||||
@@ -14,9 +14,135 @@
|
||||
<groupId>com.esotericsoftware</groupId>
|
||||
<artifactId>kryo</artifactId>
|
||||
<version>${kryo.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.goldmansachs.reladomo</groupId>
|
||||
<artifactId>reladomo</artifactId>
|
||||
<version>${reladomo.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.goldmansachs.reladomo</groupId>
|
||||
<artifactId>reladomo-test-util</artifactId>
|
||||
<version>${reladomo.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<!-- Reladomo -->
|
||||
<plugin>
|
||||
<artifactId>maven-antrun-plugin</artifactId>
|
||||
<version>1.8</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>generateMithra</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>run</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<tasks>
|
||||
<property name="plugin_classpath" refid="maven.plugin.classpath" />
|
||||
<taskdef name="gen-reladomo" classpath="plugin_classpath"
|
||||
classname="com.gs.fw.common.mithra.generator.MithraGenerator" />
|
||||
<gen-reladomo
|
||||
xml="${project.basedir}/src/main/resources/reladomo/ReladomoClassList.xml"
|
||||
generateGscListMethod="true"
|
||||
generatedDir="${project.build.directory}/generated-sources/reladomo"
|
||||
nonGeneratedDir="${project.basedir}/src/main/java" />
|
||||
|
||||
<taskdef name="gen-ddl"
|
||||
classname="com.gs.fw.common.mithra.generator.dbgenerator.MithraDbDefinitionGenerator"
|
||||
loaderRef="reladomoGenerator">
|
||||
<classpath refid="maven.plugin.classpath" />
|
||||
</taskdef>
|
||||
<gen-ddl
|
||||
xml="${project.basedir}/src/main/resources/reladomo/ReladomoClassList.xml"
|
||||
generatedDir="${project.build.directory}/generated-db/sql"
|
||||
databaseType="postgres" />
|
||||
</tasks>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.goldmansachs.reladomo</groupId>
|
||||
<artifactId>reladomogen</artifactId>
|
||||
<version>${reladomo.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.goldmansachs.reladomo</groupId>
|
||||
<artifactId>reladomo-gen-util</artifactId>
|
||||
<version>${reladomo.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>build-helper-maven-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>add-source</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>add-source</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sources>
|
||||
<source>${project.build.directory}/generated-sources/reladomo</source>
|
||||
</sources>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>add-resource</id>
|
||||
<phase>generate-resources</phase>
|
||||
<goals>
|
||||
<goal>add-resource</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>${project.build.directory}/generated-db/</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- /Reladomo-->
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<kryo.version>4.0.1</kryo.version>
|
||||
<h2.version>1.4.196</h2.version>
|
||||
<reladomo.version>16.5.1</reladomo.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<maven-compiler-plugin.version>3.6.2</maven-compiler-plugin.version>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.reladomo;
|
||||
|
||||
public class Department extends DepartmentAbstract {
|
||||
public Department() {
|
||||
super();
|
||||
// You must not modify this constructor. Mithra calls this internally.
|
||||
// You can call this constructor. You can also add new constructors.
|
||||
}
|
||||
|
||||
public Department(long id, String name) {
|
||||
super();
|
||||
this.setId(id);
|
||||
this.setName(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.reladomo;
|
||||
public class DepartmentDatabaseObject extends DepartmentDatabaseObjectAbstract
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.reladomo;
|
||||
import com.gs.fw.finder.Operation;
|
||||
import java.util.*;
|
||||
public class DepartmentList extends DepartmentListAbstract
|
||||
{
|
||||
public DepartmentList()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public DepartmentList(int initialSize)
|
||||
{
|
||||
super(initialSize);
|
||||
}
|
||||
|
||||
public DepartmentList(Collection c)
|
||||
{
|
||||
super(c);
|
||||
}
|
||||
|
||||
public DepartmentList(Operation operation)
|
||||
{
|
||||
super(operation);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.reladomo;
|
||||
public class Employee extends EmployeeAbstract
|
||||
{
|
||||
public Employee()
|
||||
{
|
||||
super();
|
||||
// You must not modify this constructor. Mithra calls this internally.
|
||||
// You can call this constructor. You can also add new constructors.
|
||||
}
|
||||
|
||||
public Employee(long id, String name){
|
||||
super();
|
||||
this.setId(id);
|
||||
this.setName(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.reladomo;
|
||||
public class EmployeeDatabaseObject extends EmployeeDatabaseObjectAbstract
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.reladomo;
|
||||
import com.gs.fw.finder.Operation;
|
||||
import java.util.*;
|
||||
public class EmployeeList extends EmployeeListAbstract
|
||||
{
|
||||
public EmployeeList()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public EmployeeList(int initialSize)
|
||||
{
|
||||
super(initialSize);
|
||||
}
|
||||
|
||||
public EmployeeList(Collection c)
|
||||
{
|
||||
super(c);
|
||||
}
|
||||
|
||||
public EmployeeList(Operation operation)
|
||||
{
|
||||
super(operation);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.reladomo;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.gs.fw.common.mithra.MithraManager;
|
||||
import com.gs.fw.common.mithra.MithraManagerProvider;
|
||||
|
||||
public class ReladomoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
ReladomoConnectionManager.getInstance().createTables();
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
MithraManager mithraManager = MithraManagerProvider.getMithraManager();
|
||||
mithraManager.setTransactionTimeout(120);
|
||||
|
||||
try (InputStream is = ReladomoApplication.class.getClassLoader().getResourceAsStream("ReladomoRuntimeConfig.xml")) {
|
||||
MithraManagerProvider.getMithraManager().readConfiguration(is);
|
||||
|
||||
Department department = new Department(1, "IT");
|
||||
Employee employee = new Employee(1, "John");
|
||||
department.getEmployees().add(employee);
|
||||
department.cascadeInsert();
|
||||
|
||||
Department depFound = DepartmentFinder.findByPrimaryKey(1);
|
||||
System.out.println("Department Name:" + department.getName());
|
||||
|
||||
Employee empFound = EmployeeFinder.findOne(EmployeeFinder.name().eq("John"));
|
||||
System.out.println("Employee Id:" + empFound.getId());
|
||||
empFound.setName("Steven");
|
||||
empFound.delete();
|
||||
Department depDetached = DepartmentFinder.findByPrimaryKey(1).getDetachedCopy();
|
||||
|
||||
mithraManager.executeTransactionalCommand(tx -> {
|
||||
Department dep = new Department(2, "HR");
|
||||
Employee emp = new Employee(2, "Jim");
|
||||
dep.getEmployees().add(emp);
|
||||
dep.cascadeInsert();
|
||||
return null;
|
||||
});
|
||||
|
||||
} catch (java.io.IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.baeldung.reladomo;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.TimeZone;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.h2.tools.RunScript;
|
||||
|
||||
import com.gs.fw.common.mithra.bulkloader.BulkLoader;
|
||||
import com.gs.fw.common.mithra.bulkloader.BulkLoaderException;
|
||||
import com.gs.fw.common.mithra.connectionmanager.SourcelessConnectionManager;
|
||||
import com.gs.fw.common.mithra.connectionmanager.XAConnectionManager;
|
||||
import com.gs.fw.common.mithra.databasetype.DatabaseType;
|
||||
import com.gs.fw.common.mithra.databasetype.H2DatabaseType;
|
||||
|
||||
public class ReladomoConnectionManager implements SourcelessConnectionManager {
|
||||
|
||||
private static ReladomoConnectionManager instance;
|
||||
|
||||
private XAConnectionManager xaConnectionManager;
|
||||
|
||||
private final String databaseName = "myDb";
|
||||
|
||||
public static synchronized ReladomoConnectionManager getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new ReladomoConnectionManager();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private ReladomoConnectionManager() {
|
||||
this.createConnectionManager();
|
||||
}
|
||||
|
||||
private XAConnectionManager createConnectionManager() {
|
||||
xaConnectionManager = new XAConnectionManager();
|
||||
xaConnectionManager.setDriverClassName("org.h2.Driver");
|
||||
xaConnectionManager.setJdbcConnectionString("jdbc:h2:mem:" + databaseName);
|
||||
xaConnectionManager.setJdbcUser("sa");
|
||||
xaConnectionManager.setJdbcPassword("");
|
||||
xaConnectionManager.setPoolName("My Connection Pool");
|
||||
xaConnectionManager.setInitialSize(1);
|
||||
xaConnectionManager.setPoolSize(10);
|
||||
xaConnectionManager.initialisePool();
|
||||
return xaConnectionManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BulkLoader createBulkLoader() throws BulkLoaderException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
return xaConnectionManager.getConnection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DatabaseType getDatabaseType() {
|
||||
return H2DatabaseType.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimeZone getDatabaseTimeZone() {
|
||||
return TimeZone.getDefault();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDatabaseIdentifier() {
|
||||
return databaseName;
|
||||
}
|
||||
|
||||
public void createTables() throws Exception {
|
||||
Path ddlPath = Paths.get(ClassLoader.getSystemResource("sql").toURI());
|
||||
|
||||
try (Connection conn = xaConnectionManager.getConnection(); Stream<Path> list = Files.list(ddlPath);) {
|
||||
list.forEach(path -> {
|
||||
try {
|
||||
RunScript.execute(conn, Files.newBufferedReader(path));
|
||||
} catch (SQLException | IOException exc) {
|
||||
exc.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<MithraRuntime>
|
||||
<ConnectionManager className="com.baeldung.reladomo.ReladomoConnectionManager ">
|
||||
<MithraObjectConfiguration className="com.baeldung.reladomo.Department" cacheType="partial"/>
|
||||
<MithraObjectConfiguration className="com.baeldung.reladomo.Employee " cacheType="partial"/>
|
||||
</ConnectionManager>
|
||||
</MithraRuntime>
|
||||
@@ -0,0 +1,11 @@
|
||||
<MithraObject objectType="transactional">
|
||||
<PackageName>com.baeldung.reladomo</PackageName>
|
||||
<ClassName>Department</ClassName>
|
||||
<DefaultTable>departments</DefaultTable>
|
||||
|
||||
<Attribute name="id" javaType="long" columnName="department_id" primaryKey="true"/>
|
||||
<Attribute name="name" javaType="String" columnName="name" maxLength="50" truncate="true"/>
|
||||
<Relationship name="employees" relatedObject="Employee" cardinality="one-to-many" reverseRelationshipName="department" relatedIsDependent="true">
|
||||
Employee.departmentId = this.id
|
||||
</Relationship>
|
||||
</MithraObject>
|
||||
@@ -0,0 +1,9 @@
|
||||
<MithraObject objectType="transactional">
|
||||
<PackageName>com.baeldung.reladomo</PackageName>
|
||||
<ClassName>Employee</ClassName>
|
||||
<DefaultTable>employees</DefaultTable>
|
||||
|
||||
<Attribute name="id" javaType="long" columnName="employee_id" primaryKey="true"/>
|
||||
<Attribute name="name" javaType="String" columnName="name" maxLength="50" truncate="true"/>
|
||||
<Attribute name="departmentId" javaType="long" columnName="department_id"/>
|
||||
</MithraObject>
|
||||
@@ -0,0 +1,4 @@
|
||||
<Mithra>
|
||||
<MithraObjectResource name="Department"/>
|
||||
<MithraObjectResource name="Employee"/>
|
||||
</Mithra>
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.reladomo;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.gs.fw.common.mithra.test.ConnectionManagerForTests;
|
||||
import com.gs.fw.common.mithra.test.MithraTestResource;
|
||||
|
||||
public class ReladomoTest {
|
||||
private MithraTestResource mithraTestResource;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.mithraTestResource = new MithraTestResource("reladomo/ReladomoTestConfig.xml");
|
||||
|
||||
final ConnectionManagerForTests connectionManager = ConnectionManagerForTests.getInstanceForDbName("testDb");
|
||||
this.mithraTestResource.createSingleDatabase(connectionManager);
|
||||
|
||||
mithraTestResource.addTestDataToDatabase("reladomo/test-data.txt", connectionManager);
|
||||
|
||||
this.mithraTestResource.setUp();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetTestData_thenOk() {
|
||||
Employee employee = EmployeeFinder.findByPrimaryKey(1);
|
||||
assertEquals(employee.getName(), "Paul");
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
this.mithraTestResource.tearDown();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<MithraRuntime>
|
||||
<ConnectionManager className="com.gs.fw.common.mithra.test.ConnectionManagerForTests">
|
||||
<Property name="resourceName" value="testDb"/>
|
||||
<MithraObjectConfiguration className="com.baeldung.reladomo.Department" cacheType="partial"/>
|
||||
<MithraObjectConfiguration className="com.baeldung.reladomo.Employee " cacheType="partial"/>
|
||||
</ConnectionManager>
|
||||
</MithraRuntime>
|
||||
@@ -0,0 +1,7 @@
|
||||
class com.baeldung.reladomo.Department
|
||||
id, name
|
||||
1, "Marketing"
|
||||
|
||||
class com.baeldung.reladomo.Employee
|
||||
id, name
|
||||
1, "Paul"
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user