スキップしてメイン コンテンツに移動

投稿

Google App Engine Java: Setup Test Configuration

If you want to test a class which depends on Google App Engine infrastructure, such as data storing functionality with "PersistenceManagerFactory", you should set up LocalServiceTestHelper before testing. I don't tell you the details very much, but I will show you the minimum test setup in the code below. This code was enough for me to test my data access object functionality. package com.dukesoftware.gaej.test; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; import com.google.appengine.tools.development.testing.LocalServiceTestHelper; public class GoogleAppEngineTest { private static final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()); @BeforeClass public static void setUp() { helper.setUp(); } @AfterClass public static void tearDown() { he

Google App Engine Task Queue - Using DeferredTask

When to Use Google App Engine Task Queue? You should use task queue when you try to do something taking time such as storing or updating bunch of data to datastore. In Google App Engine, a http request which isn't returned by timeout should be simply failed. The timeout configured on Google App Engine is 60 seconds. Please see https://developers.google.com/appengine/articles/deadlineexceedederrors . So in order to response back to the client quickly, you should use Task Queue for tasks which takes long time. Push Tasks to Task Queue I have read an official document of Task Queue and tried to use it. What frustrates me is the document only explains how to create a task with parameters, push the task to the task queue and pass to the worker servlet. i.e. actual heavy part is treated in the worker servlet. Of course it is fine, but I felt it was a bit indirect way to push tasks to queue. What I desired to do is creating task object and pushing it to the queue in one single

Java: Get All Static Fields Defined in Class by Reflection

If you want to get all static fields in a class, use code snippet below. The key part is "Modifier.isStatic", "getDeclaredFields" methods package com.dukesoftware.reflection; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class ReflectionUtils { public static List<Field> getStaticFields(Class<?> clazz) { Field[] declaredFields = clazz.getDeclaredFields(); return Arrays.stream(declaredFields) .filter(field -> Modifier.isStatic(field.getModifiers())) .collect(Collectors.toList()) ; } }

Create Indexed PNG Using C# .Net

Currently I am working on optimizing image size. In this post, I will demonstrate the C# code for creating indexed png image. The code might not be perfect yet, but hope this code will give you some hints ;) A few things I am struggling with while I am writing the example code: Palette: you should take ColorPalette from Bitmap, modify the ColorPalette object and set buck it to the Bitmap object. BitmapData.Stride is often bigger than BitmapData.Width. You should be careful to check both values difference when you manipulating the image array index. The example code uses classes in System.Drawing namespace. But looks there is another option to manipulate image in C# .Net - System.Windows.Media namespace. C# Source Code for Creating Indexed PNG The following implementation of CreateIndexedPng method is able to take only non-indexed image like Format24bppRgb or Format32bppArgb. (I might add other image formats in future.) If the source image has only 2 colors, we can use For

Excel VBA: Output Sheet as UTF-8 CSV

Save Excel Sheet as UTF-8 CSV file The biggest problem of Excel is that Excel does not support saving file as UTF-8 CSV format. I have googled and tried to find the solution for saving excel as UTF-8 CSV but all of them requires 2 steps - 1) saving Excel file as Unicode csv 2) Open it by text editor and save it again as UTF-8 CSV. If you are a software developer, it is easy to write Java or C# program to manipulate Excel. But it means people (non developer users) need to install or launch the application. So I have wrriten Excel VBA addin for saving Excel sheet as UTF-8 CSV file so that we can save UTF-8 excel files only using Excel. Code for saving UTF-8 CSV Okie!, here is my solution! Sme notes for the code: The code saves an active sheet to UTF-8 CSV file in temp folder. The error handring part is not robust. I have only wrriten minimum error handling. You might need to add your custome error handring. If you are okay to add BOM on the file, please remove the corresp

Java Reflection: Getter and Setter Method

Reflective Getter and Setter Method In this post, I will show you Java code for getting getter and setter method from given object. The code is below: public final static Method getSetterMethod(Object o, String propertyName, Class<?> paramterType) throws SecurityException, NoSuchMethodException{ return o.getClass().getMethod("set"+toUpperFirstChar(propertyName), paramterType); } public final static Method getGetterMethod(Object o, String propertyName) throws SecurityException, NoSuchMethodException{ return o.getClass().getMethod("get"+toUpperFirstChar(propertyName)); } public final static String toUpperFirstChar(String str){ if(str.isEmpty()) return ""; return str.substring(0, 1).toUpperCase()+str.substring(1, str.length()); } Where to Use Reflective Getter and Setter? You may have a question - "Where should we use reflective getter and setter method?" If you already know where to use them, you can skip the fo

C#: Convert 1 Dimensional Array to Fixed Size 2 Dimensional Array

Code using System; using System.Collections.Generic; using System.Linq; namespace Utility { public static class ArrayUtils { public static T[,] To2DimensionalArray (this T[] source, int block){ var ret = new T[block, source.Length/block]; for (int i = 0, offset = 0, len = ret.Length; offset < len; i++, offset = i * block) { for(int j = 0; j < block; j++){ ret[j, i] = source[offset + j]; } } return ret; } } } Example Usage int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9}; int[,] array2 = array1.To2DimensionalArray(3); /* array2 will be... { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } } */