Last active
December 17, 2015 12:49
-
-
Save kunst1080/5612594 to your computer and use it in GitHub Desktop.
tiny test framework
for a little act of test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.kontrapunkt.kunst1080.TinyTest; | |
import java.util.Calendar; | |
import java.util.Date; | |
public class TestTinyTest extends TinyTest { | |
public static void main(String[] args) { | |
testStart(new OKTest()); | |
//testStart(new NoTest()); | |
testStart(new NGTest()); | |
setOutFile("C:/temp/TestTinyTest.log"); | |
testStart(new FileTest()); | |
} | |
static class OKTest implements Tester { | |
public boolean testMain() { | |
p(new Date()); | |
p(Calendar.getInstance()); | |
p("fugafuga"); | |
return true; | |
} | |
} | |
static class NoTest implements Tester { | |
public boolean testMain() { | |
return true; | |
} | |
} | |
static class NGTest implements Tester { | |
public boolean testMain() { | |
p("1 == 2"); | |
return 1 == 2; | |
} | |
} | |
static class FileTest implements Tester { | |
public boolean testMain() { | |
p("1 == 2"); | |
return 1 == 2; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.kontrapunkt.kunst1080.TinyTest; | |
import java.io.PrintStream; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.List; | |
public abstract class TinyTest { | |
// Framework | |
public interface Tester{ | |
boolean testMain(); | |
} | |
public static void testStart(Tester t){ | |
p("[ClassName = " + t.getClass().getName() + "]"); | |
p("---------- Test Start ----------"); | |
if (t.testMain()){ | |
p("---------- Test OK End ----------"); | |
} else { | |
p("---------- Test Abnormal End ----------"); | |
} | |
p(""); | |
} | |
// Utilitiy functions | |
public static void p(Object o){ | |
if(o instanceof Calendar){ | |
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmSS"); | |
Calendar cal = (Calendar)o; | |
System.out.println(df.format(cal.getTime())); | |
} else if (o instanceof List){ | |
List<Object> list = autoCast(o); | |
for (Object obj : list){ | |
p(obj); | |
} | |
} else { | |
System.out.println(o.toString()); | |
} | |
} | |
public static void line(){ | |
p("------------------------------"); | |
} | |
public static String getThisClassName(){ | |
StackTraceElement[] st = new Throwable().getStackTrace(); | |
String s = st[st.length - 1].getClassName(); | |
return s; | |
} | |
@SuppressWarnings("unchecked") | |
public static <T> T autoCast(Object o){ | |
return (T)o; | |
} | |
/** | |
* Switch stdout for file | |
* @param filepath | |
*/ | |
public static void setOutFile(String filepath){ | |
try{ | |
PrintStream out = new PrintStream(filepath); | |
System.setOut(out); | |
} catch (Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment