Skip to content

Instantly share code, notes, and snippets.

@jvmvik
jvmvik / SimpleBeanMapper.java
Created August 2, 2013 21:01
Simple Bean Mapper ---- Turn a Map<String, String> into an instance of any object an object. * key is the property name * value is the value associate to the property A key "class" must refer to the class name that must be instantiated
package io.milkyway.spendtracker.servlet;
import java.beans.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;
/**
@jvmvik
jvmvik / Groovy mocking static method
Created October 1, 2013 17:30
Groovy mocking static method. This is a fairly good example of how to correctly mock any groovy object without destroying your all regression.
/*
* Groovy Test Mocking explains.
*
* A modification of metaClass static method apply to the class inside the JVM.
* You run your test with fork mode enabled, each test is isolated from another at the leve of a class or method.
* However you can delete the metaClass behavior by
* MyClass.metaClass = null
*
* Must know
* - The metaClass method signature is very important. The documentation does not higlight that the method signature must match exactely:
@jvmvik
jvmvik / SimpleTemplate.groovy
Last active December 28, 2015 08:09
Very basic template engine... I wrote this to replace the horrible Groovy Template Engine. This is not designed to solve a large scope of problem.
import java.nio.charset.Charset
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardOpenOption
/**
* Simple template parser.
*
* This parser offer to:
* - Replace $variable by the value
@jvmvik
jvmvik / InteractiveRollingFileAppender.java
Created November 25, 2013 23:03
Add log4j capability to append two event message on a single line in the console output. Example: log.info("hello \n"); log.info("world !"); Results: [INFO] hello world !
import org.apache.log4j.RollingFileAppender
import org.apache.log4j.helpers.CountingQuietWriter
import org.apache.log4j.spi.LoggingEvent
/**
* Interactive file appender
*
* Provide a capability to append two event message on a single line in a log file.
*
@jvmvik
jvmvik / Experiment.groovy
Created December 5, 2013 18:15
Algorithm in Groovy to create union and exclusion, and intersection between map and list.
class Experiment
{
/**
* Experiment algorithm in Groovy
* to create union and exclusion,
* intersection between map and list.
*/
@Test
void code()
{
@jvmvik
jvmvik / BeanToJson.java
Last active December 30, 2015 21:09
Convert a POJO to a JsonObject (GSON 2.4 library...)
package io.milkyway.mongo.json;
import com.google.gson.JsonObject;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
/***
* How to add a callback to AngularJS $resource.
*
* Promises enables to add success and failure callback
* after the execution of $resource method.
*/
var app = angular.module("application", ['ngResource']);
app.factory('$crud', function ($resource) {
return $resource('url', {}, {
@jvmvik
jvmvik / JavaPathDotTest.java
Last active August 29, 2015 13:56
here a bug found in JDK 7.45 on Windows. I'm running Groovy 2.2.1 on the top of the JVM.
import org.junit.Test
import java.nio.file.FileVisitResult
import java.nio.file.Path
import java.nio.file.Paths
/***
* Bug with directory starting by '.'
*
* Here is test to reproduce this issue.
@jvmvik
jvmvik / app.js
Last active August 29, 2015 14:00
AngularJS and Websocket service implementation example... This example shows only the fronted
/**
* Created by victor on 1/19/14.
*/
var app = angular.module('app', []);
app.factory('ChatService', function () {
var service = {};
service.connect = function ()
{
@jvmvik
jvmvik / tar-them.sh
Created June 10, 2014 19:26
Bash script to tar multiples directories supplied by the user in a single command...
#!/bin/bash
if [ $# -eq 0 ]
then
echo "No directory supplied..."
fi
for f in $*;
do
echo "Compressing: $f";