Skip to content

Instantly share code, notes, and snippets.

@LenarBad
LenarBad / Logger.java
Last active Oct 21, 2021
AspectJ. How to log method invocation and print parameters as name:value map from ProceedingJoinPoint for TestNG tests
View Logger.java
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.testng.Reporter;
@LenarBad
LenarBad / ReadFileIntoStringGuava.java
Created Dec 5, 2017
How to read file into String with Google Guava
View ReadFileIntoStringGuava.java
public String getFileIntoStringGuava(String fileName) throws IOException {
InputStream input = this.getClass().getResourceAsStream("/" + fileName);
return CharStreams.toString(new InputStreamReader(input, StandardCharsets.UTF_8));
}
@LenarBad
LenarBad / Skip TestNG tests based on condition.java
Last active Nov 20, 2020
How to skip TestNG tests based on condition using IInvokedMethodListener
View Skip TestNG tests based on condition.java
@Listeners(value = ConditionalSkipTestAnalyzer.class)
public class ExampleConditionalSkippingTest {
@NonProduction
@Test
public void test1() { }
@ProductionOnly
@Test
public void test2() { }
@LenarBad
LenarBad / BinaryTreePeers.java
Last active Aug 8, 2019
Binary Tree Peers. Everything is public for simplicity
View BinaryTreePeers.java
public class Node {
public int value;
public Node left;
public Node right;
public Node peer;
public Node(int value) {
this.value = value;
}
@LenarBad
LenarBad / Heroku - mLab MongoDB - Spring Boot - How to start example StatEntry.java
Last active Jun 5, 2019
Heroku - mLab MongoDB - Spring Boot - How to start example
View Heroku - mLab MongoDB - Spring Boot - How to start example StatEntry.java
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "stats")
public class StatEntry {
@Id
public String id;
@LenarBad
LenarBad / settings.xml
Last active Apr 16, 2019
Maven settings.xml for publishing open source projects
View settings.xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>sonatype-nexus-snapshots</id>
<username>your-sonatype-username</username>
<password>your-sonatype-password</password>
</server>
@LenarBad
LenarBad / pom.xml
Created Apr 15, 2019
POM.XML for Open Source Maven project example
View pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.lenar</groupId>
<artifactId>app-props</artifactId>
<version>0.9.3-SNAPSHOT</version>
<packaging>jar</packaging>
<name>AppProps</name>
@LenarBad
LenarBad / JenkinsScheduleFormat
Created Apr 8, 2019
Jenkins schedule format
View JenkinsScheduleFormat
┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday;
│ │ │ │ │ 7 is also Sunday on some systems)
│ │ │ │ │
│ │ │ │ │
* * * * * schedule command to execute
@LenarBad
LenarBad / queryToMap.java
Created Apr 8, 2019
Url query to parameter map
View queryToMap.java
private Map<String, String> queryToMap(String query) throws UnsupportedEncodingException {
Map<String, String> parameters = new HashMap<>();
String[] queryArray = query.split("&");
for (String string : queryArray) {
int idx = string.indexOf("=");
parameters.put(decode(string.substring(0, idx), "UTF-8"), decode(string.substring(idx + 1), "UTF-8"));
}
return parameters;
}
@LenarBad
LenarBad / mapToQuery.java
Created Apr 8, 2019
Parameter map to url query
View mapToQuery.java
private String mapToQuery(Map<String, String> parameters) {
String query = "";
if (parameters != null && !parameters.isEmpty()){
query = "?" +
parameters.entrySet().stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining("&"));
}
return query;
}