Skip to content

Instantly share code, notes, and snippets.

View takawitter's full-sized avatar

Takao Nakaguchi takawitter

View GitHub Profile
@takawitter
takawitter / TestServiceSOAP.groovy
Last active March 9, 2017 06:16
SOAP Service and Client using language grid libraries and Groovy.
@Grab('org.eclipse.jetty:jetty-webapp:9.4.0.v20161208')
@Grab('org.langrid:jp.go.nict.langrid.service.language_1_2:1.0.10')
@Grab('org.langrid:jp.go.nict.langrid.servicecontainer.handler.axis:1.0.10')
import jp.go.nict.langrid.service_1_2.translation.TranslationService;
import jp.go.nict.langrid.servicecontainer.handler.annotation.Service;
import jp.go.nict.langrid.servicecontainer.handler.axis.SGAxisServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class TestServiceImpl implements TranslationService{
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.Consumer;
public class CollectionFixture<T> {
public CollectionFixture(Collection<T> collection){
@takawitter
takawitter / Test.java
Created October 19, 2016 04:20
Using Iterable.forEach by tunneling exception that may occur inside Consumer.
public class Test{
@Test
public void test_tunnelingExecute() throws Throwable{
try{
tunnelingExecute(
Arrays.asList(1, 2, 3)::forEach,
v -> {throw new IOException();}
);
Assert.fail();
} catch(IOException e){
Testing LinkedList
com.dzyoba.alex.TestAlloc: [7, 25, 276]
com.dzyoba.alex.TestInsert: [7069, 19437, 24856]
com.dzyoba.alex.TestInsertHead: [0, 1, 2]
com.dzyoba.alex.TestInsertTail: [0, 1, 1]
com.dzyoba.alex.TestSearch: [17372, 53823, 70404]
com.dzyoba.alex.TestDelete: [5, 9, 11]
com.dzyoba.alex.TestAlloc: [6, 36, 98]
com.dzyoba.alex.TestRemove: [6472, 8434, 9857]
@takawitter
takawitter / invoke.html
Last active September 7, 2016 08:44
New Langrid service invocation API for JavaScript.
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="${langrid-address}/js"></script>
<script>
new LangridClientFactory({
apiKey: "${your-api-key}"
})
.create("${service-id}")
.translate("en", "ja", "Hello")
.done(function(res){
console.log(res.result);
@takawitter
takawitter / reflectionInvoker.groovy
Created April 21, 2016 04:43
sample code to invoke ReflectionMethodInvoker of groovy
import org.codehaus.groovy.runtime.ReflectionMethodInvoker
class Base{}
class Derived extends Base{}
class Derived2 extends Derived{}
class Hello{
public void hello(Base b){
println "base"
}
public void hello(Derived d){
@takawitter
takawitter / LimitClassReferenceWhenDeserializingObject.java
Last active November 11, 2015 02:51
Limit class reference when deserializing object by using custom class loader.
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Paths;
import java.util.HashMap;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
public class JSONObjectExample {
@Test
public void java_json_literal() throws Throwable{
@takawitter
takawitter / EventListenerList.java
Created July 24, 2015 17:26
Typed and easily to fire event, EventListenerList
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.EventListener;
public class EventListenerList<T extends EventListener> extends javax.swing.event.EventListenerList{
public EventListenerList(Class<T> clazz){
this.clazz = clazz;
this.fireProxy = clazz.cast(Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),
@takawitter
takawitter / HtmlEncoderTest.java
Last active August 29, 2015 14:23
ちゃんと5回呼ばれるやつ(シングルクオートをバッククオートに修正)
import java.util.HashMap;
import java.util.Map;
public class HtmlEncoderTest {
static public void main(String[] args) {
System.out.println(new HtmlEncoder().encode("<script>alert('注意!');</script>"));
System.out.println(HtmlEncoder.count);
}
}