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

投稿

10月, 2013の投稿を表示しています

Render FPS Text on Java Swing Component

In the previous post , I showed helper class for 2D animation on Java Swing component. If we can measure the frame rate of animation, it is very great, isn't it? In this post, I will show you that kind of class - rendering FPS text on swing component. Showing code is fast and snappy :p Here is the code - count frame per 1 sec. package com.dukesoftware.utils.awt.graphics; import java.awt.Color; import java.awt.Component; import java.awt.Font; import java.awt.Graphics; import java.text.DecimalFormat; public class FPSText { protected static final String FPS = "FPS: "; private static final int FONT_SIZE = 24; private static final int FONT_STYLE = Font.BOLD; private static final String FONT_NAME = "SansSerif"; protected final static Color COLOR = new Color(0.55f,0.55f,0.55f); protected double frameCount; protected final DecimalFormat format = new DecimalFormat("####.00"); protected String fpsText; protected fin

Java 2D Animation on Swing

In this post, I will show you some tips (or tools) for 2D animation on Java Swing component. There are 2 ways to rendering animation on Swing compinent - Thread vs Timer. I think Swing rendering is based on single thread model, so we should use Timer. But I have used separate thread for executing animation and it works fine, so should be fine for not critical application. Animator class Ok then, I can show you a simple animator canvas class I created. The class takes Model2D for actual animate model and render it. One thing you should remember - AnimateCanvas calculate model of animation (coodinates or position or whatever) on animation rendering process, so if the calculation takes takes log time, the animation cannot meet the frame per second passed to constructor. package com.dukesoftware.utils.swing.others; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.aw

Automated Testing: Pairwise Testing, Mutation Testing, CI, TDD

I Introduced basic techniques for automated testing in the following posts. Unit Test by JUnit Parameterized Test Mock Test Theory Test In this post, I mention some hints for software testing. Pairwise Testing It is unrealistic to test all the combination of parameters because the number of the tests increase explosively. Pairwise Testing aims to redeuce the number of tests with enough test quality and coverage by providing effective and enough combination of test parameters. This MSDN: Pairwise Testing in the Real World: Practical Extensions to Test-Case Scenarios helps your understanding Pairwise Testing. The article also introduces the tool for Pairwise Testing. Mutation Testing Change the logic in the test target code (for example, change reverse an inequality sign in the "if" condition check), and check if the test result is changed (= the test case can detect the code change). Mutation Testing is "Test for test" - verifying the effectiveness of

Automated Testing: Theory Test

Theory test is relatively new feature in JUnit testing. In theory test, we define "theory" for parameter combination and filter them by "assume". Then only tests passed parameters by assume. Let's show you some example theory test code. In Junit test, we put @RunWith(Theories.class) annotation on test class for deining theory test. put @DataPoints for parameter data set. The code below shows how theory test works in JUnit. Actually this is not good example because there is no advantedges compared with parameterized test in this case. package com.dukesoftware.exchangerate.service; import junit.framework.Assert; import org.junit.After; import org.junit.BeforeClass; import org.junit.experimental.theories.DataPoints; import org.junit.experimental.theories.Theories; import org.junit.experimental.theories.Theory; import org.junit.runner.RunWith; import org.mockito.Mockito; import com.dukesoftware.exchangerate.api.ExchangeRateApi; import com.dukesoftware.

Automated Testing: Mock Test

Today's topic is Mock Test . In the previous posts, I introduced bunch of test cases but all of target class is class which hits actual service or production environment. However in the real world, in many cases we cannot simply use actual service class. Isolate from actual production service. ex. storing DB, calling external environment API. External API is too slow and we don't have to use acual data from the API but just would like to test clsasses which uses the API. Would like to controla data from API. Would like to test internal logic. You can use Mock Object, which we can control behaviour of class, for the above cases. There are a lot of Mock libraries (especially in Java). In this post, we use Mockito for explanation. You just downalod mockito-all-x.x.x.jar and include it to classpath, then you can use Mockito. Mockito Example Code I think you can easily understand how to use Mockito from the below code example. package com.dukesoftware.exchangerate.s

Automated Testing: Parameterized Test

In the previous post , I introduced basic of Unit Testing. Next I introduce parameterized testing. Before explaning parameterized testing, let's define a simple service class which uses ExchangeRateApi calss. package com.dukesoftware.exchangerate.service; import java.util.HashMap; import java.util.Map; import com.dukesoftware.exchangerate.api.ExchangeRateApi; import com.dukesoftware.exchangerate.api.Rate; public class PriceCalculator { private final ExchangeRateApi api; private final Map<String, Rate> map = new HashMap<>(); private final static double FEE_RATE = 0.05; public PriceCalculator(ExchangeRateApi api) { this.api = api; } public void initialize() { } public void shutdown() { this.map.clear(); } public double calculatePrice(double price, String ccy1, String ccy2) { Rate rate = this.map.get(ccy1+":"+ccy2); if(rate == null) { // cac

Automated Testing: Unit Test by JUnit

Unit testing From this post, I will explain basic of automated testing. In this post, I will show you very very basic of unit testing using JUnit. We use YahooExchangeRateApi as testing target code introduced in http://dukesoftware00.blogspot.com/2013/10/java-get-exchange-rate-from-yahoo.html . Unit Test class & Annotaions The first of first, write simple unit testing code for YahooExchangeRateApi class. A few things you should remenber: Add @Test annotation to test method. Use @BeforeClass for execute something *once* before actual all tests defined in the test class. Use @Before for execute something before actual *each* tests defined in the test class. Use @AfterClass and @After are simply opposite meaning of @BeforeClass and @Before. e.g. executed after test methods. You can test exception thrown by @Test(expected=Exception.class) Yeah that's all! Let's see the actual test class code.... package com.dukesoftware.exchangerate.api; import static junit.fra

Java: Get Exchange Rate From Yahoo Finance Api

In this post, I will show you how to retrieve currency exchange rate from yahoo finance api by Java program. There are some apis for getting exchange rate - reuters, bloomberg, yahoo finance etc. If you don't have to get high-ratency realtime exchange rate, the most simple and free api - yahoo finance api - is enough. Okay, here is the actual code. The api returns exchange rate as a Rate object. package com.dukesoftware.exchangerate.api; import static java.lang.String.format; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URI; public class YahooExchangeRateApi implements ExchangeRateApi { // url for yahoo finance api private final static String URL_FORMAT = "http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=%s%s=X"; public Rate getRate(String ccy1, String ccy2) { assertNotNull(ccy1, "

Get Root Cause of Java Exception

I think sometimes you would like to simply extract root cause of exception. The following method will help you. public final static Throwable getRootCause(Throwable t){ for(Throwable s = t.getCause() ;s != null;){ s = t.getCause(); t = s; } return t; }

Create Stack Trace String from Java Exception

Maybe many times you would like to generate stack trace string from Exception thrown. The code below will help you! public final static String createStackTraceString(Exception e){ StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw, false); e.printStackTrace(writer); return sw.toString(); }

File Clean Up and Archiving Linux Command Tips

If you would like to delete files whose name ends with "batch.log" older than 30 days, Use the following command. find /var/log/ -name "*batch.log" -mtime +30 -type -f -delete If you would like to archive files whose name ends with "batch.log" older than 30 days, Use the following command. The archived file will have timestamp at the end of the filename. find /var/log/ -name "*batch.log" -mtime +30 -type -f -pront0 | tar -czvf /var/log/batch.log.tar.gz.`date +\%Y%m%d%H%M%S` -T - --null --remove-files

Solve Local Capistrano Deployment Issue

Local Capistrano Deployment Issue I have an inssue when deploying PHP application on "local" machine (e.g. deploying app to same machine where executing capistrano deploy.) using Capistrano. The cause is when capistrano creates temporary directory for destination (remote) directory and directory for source (local) directory point to same directory. This problem does not happen when you deploy to different machine. Solution Add the below code to your deploy.rb!! Just change the name for temoprary directory used during deployment name. # put this line at the top of app.yml in order to use SecureRandom methods require "securerandom" # set flag true when local deployment set :deploy_to_self, "true" # hook tasks before :deploy, "local:create_dir" after :deploy, "local:clean_dir" #################################### # setup copy_dir, copy_remote_dir # when deploying to machine which this script is running on to avoid copy issue # na