From 3b38e8f1bdc41e83716e9ea2e853f2660e782c72 Mon Sep 17 00:00:00 2001 From: "ho.namkoong" Date: Wed, 4 Sep 2013 20:33:34 +0900 Subject: [PATCH] [Title] Add inputstream writer to the test framework and add more test cases [Type] [Module] [Priority] [CQ#] [Redmine#] 9903 [Problem] [Cause] [Solution] [TestCase] Change-Id: I12d834871e64a82a6cee8540951ba6550862780b --- .../test/src/org/tizen/common/IShell.java | 20 ++ .../test/src/org/tizen/common/Shell.java | 87 ++++++++- .../test/src/org/tizen/common/sdb/ConnectTest.java | 94 +++------- .../test/src/org/tizen/common/sdb/DevicesTest.java | 6 - .../src/org/tizen/common/sdb/GetSerialNoTest.java | 21 +-- .../src/org/tizen/common/sdb/GetStateTest.java | 23 +-- .../test/src/org/tizen/common/sdb/InstallTest.java | 19 +- .../src/org/tizen/common/sdb/PullPushTest.java | 204 ++++++++------------- .../test/src/org/tizen/common/sdb/RootTest.java | 106 +++++++++++ .../src/org/tizen/common/sdb/SdbTestSuite.java | 14 +- .../test/src/org/tizen/common/sdb/SdbTestUtil.java | 32 +++- .../test/src/org/tizen/common/sdb/ShellTest.java | 71 +++++++ .../src/org/tizen/common/sdb/StartKillTest.java | 59 +++--- .../src/org/tizen/common/sdb/StatusWindowTest.java | 72 ++++++++ .../test/src/org/tizen/common/sdb/VersionTest.java | 57 ++++++ 15 files changed, 588 insertions(+), 297 deletions(-) create mode 100644 org.tizen.common/test/src/org/tizen/common/sdb/RootTest.java create mode 100644 org.tizen.common/test/src/org/tizen/common/sdb/ShellTest.java create mode 100644 org.tizen.common/test/src/org/tizen/common/sdb/StatusWindowTest.java create mode 100644 org.tizen.common/test/src/org/tizen/common/sdb/VersionTest.java diff --git a/org.tizen.common/test/src/org/tizen/common/IShell.java b/org.tizen.common/test/src/org/tizen/common/IShell.java index ee967f0..c317940 100644 --- a/org.tizen.common/test/src/org/tizen/common/IShell.java +++ b/org.tizen.common/test/src/org/tizen/common/IShell.java @@ -37,6 +37,17 @@ public interface IShell { /** + * Terminate shell after delay and convert result to object of T + * + * @param instance instance for result + * @param delay + * @return + * + * @throws Exception + */ + T expect( final T instance, long delay) throws Exception; + + /** * Convert result to object of T * * @param instance instance for result @@ -46,4 +57,13 @@ IShell */ T expect( final T instance ) throws Exception; + /** + * Write data to the process shell executed + * + * @param data data written to the process + * @param delay time over which data written + * @throws Exception + */ + public void write( final String data, final long delay ) throws Exception; + } diff --git a/org.tizen.common/test/src/org/tizen/common/Shell.java b/org.tizen.common/test/src/org/tizen/common/Shell.java index 4eaaebf..fb5e4d2 100644 --- a/org.tizen.common/test/src/org/tizen/common/Shell.java +++ b/org.tizen.common/test/src/org/tizen/common/Shell.java @@ -29,6 +29,9 @@ import static org.tizen.common.util.StringUtil.nvl; import static org.tizen.sdblib.util.ThreadUtil.trySleep; import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.OutputStreamWriter; import java.io.StringReader; import java.lang.annotation.Annotation; import java.lang.reflect.Array; @@ -74,6 +77,24 @@ implements IShell { return new Shell( cmd ); } + + /** + * Starting entry method and wait until process is terminated + * + * @param cmd command to execute + * + * @throw Exception If execution fail + */ + public static + void + runAndWait( + final String cmd + ) + throws Exception + { + Shell shell = new Shell( cmd ); + shell.process.waitFor(); + } /** * process @@ -94,9 +115,14 @@ implements IShell * Error stream dispenser */ final StreamGobbler error; - + /** - * Constructor with command + * Writer for writing data to the process + */ + final BufferedWriter out; + + /** + * Constructor with command and boot the input and error stream * * @param cmd command to execution * @@ -109,15 +135,57 @@ implements IShell { process = Runtime.getRuntime().exec( cmd ); startTime = System.currentTimeMillis(); - in = new StreamGobbler( process.getInputStream() ); error = new StreamGobbler( process.getErrorStream() ); + out = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); in.boot(); error.boot(); } - /* (non-Javadoc) + /** (non-Javadoc) + * @see org.tizen.common.IShell#write(String, long) + */ + public + void + write( + final String data, + final long delay + ) + throws Exception + { + if(delay > 0) { + Thread.sleep(delay); + } + out.append(data); + out.newLine(); + try { + out.flush(); + } + catch (IOException e){ + e.printStackTrace(); + } + } + + /** (non-Javadoc) + * @see org.tizen.common.IShell#expect(Object, long) + */ + public + T + expect( + final T instance, + long delay + ) + throws Exception + { + trySleep(delay); + process.destroy(); + final String result = in.getResult(); + + return convert(instance, result); + } + + /** (non-Javadoc) * @see org.tizen.common.IShell#expect(java.lang.Object) */ public @@ -138,7 +206,16 @@ implements IShell } final String result = in.getResult(); - + return convert(instance, result); + } + + private + T + convert ( + final T instance, + String result + ) + throws Exception { final BufferedReader reader = new BufferedReader( new StringReader( result ) ); String line = null; diff --git a/org.tizen.common/test/src/org/tizen/common/sdb/ConnectTest.java b/org.tizen.common/test/src/org/tizen/common/sdb/ConnectTest.java index fb7e4f6..0f4e2df 100644 --- a/org.tizen.common/test/src/org/tizen/common/sdb/ConnectTest.java +++ b/org.tizen.common/test/src/org/tizen/common/sdb/ConnectTest.java @@ -27,11 +27,12 @@ package org.tizen.common.sdb; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.tizen.common.Shell.run; +import static org.junit.Assert.*; +import static org.tizen.common.Shell.*; import static org.tizen.common.sdb.SdbTestUtil.*; -import org.junit.BeforeClass; +import java.text.MessageFormat; + import org.junit.Test; import org.tizen.common.Pattern; @@ -47,10 +48,8 @@ import org.tizen.common.Pattern; */ public class ConnectTest { - @BeforeClass - public static void prepareTest() throws Exception{ - SdbTestUtil.prepareTest(); - } + //connect and disconnect works async + public static final int CONNECT_SLEEP = 1000; /** * Test {sdb connect} @@ -74,34 +73,18 @@ public class ConnectTest { try { int port = getPorts()[0]; - run( SDB_PATH + " disconnect").expect(new ConnectResult()); - run( SDB_PATH + " connect " + LOOPBACK_IP + ":" + port).expect(new ConnectResult()); - - boolean success = false; - String errMsg = ""; - for(int i=0; i < MAX_TRIAL; i++) { - final ConnectResult result = run( SDB_PATH + " devices").expect( new ConnectResult() ); - if(result == null) { - errMsg = "result for sdb devices for connect is null"; - } - else if(result.tcpPortNums == null) { - errMsg = "tcp port number of sdb devices for connect is null"; - } - else if(result.tcpPortNums.length <= 0) { - errMsg = "no devices are connected"; - } - else { - assertTrue("More than one devices are connected", result.tcpPortNums.length == 1); - int tcpPort = new Integer(result.tcpPortNums[0]); - assertEquals(String.format("Device port: '%s' and connected port: '%s' are not matched", port, tcpPort), port, tcpPort); - success = true; - break; - } - } - assertTrue(errMsg, success); + runAndWait(SDB_PATH + " disconnect"); + Thread.sleep(CONNECT_SLEEP); + runAndWait( SDB_PATH + " connect " + LOOPBACK_IP + ":" + port); + final ConnectResult result = run( SDB_PATH + " devices").expect( new ConnectResult() ); + assertNotNull("tcp port number of sdb devices for connect is null", result.tcpPortNums); + int tcpPort = new Integer(result.tcpPortNums[0]); + assertTrue("More than one devices are connected", result.tcpPortNums.length == 1); + assertEquals(MessageFormat.format("Device port: \'{0}\' and connected port: \'{1}\' are not matched", port, result.tcpPortNums[0]), + port, tcpPort); } finally { - run( SDB_PATH + " disconnect"); + runAndWait( SDB_PATH + " disconnect"); } } @@ -120,46 +103,21 @@ public class ConnectTest { @Test public void test_disconnect() throws Exception { - class DisconnectResult { - @Pattern( pattern = "{0}", index = 0) public String wholeStrings[]; - } - try { - run( SDB_PATH + " disconnect").expect(new DisconnectResult()); + runAndWait( SDB_PATH + " disconnect"); + Thread.sleep(CONNECT_SLEEP); int ports[] = getPorts(); - run( SDB_PATH + " connect " + LOOPBACK_IP + ":" + ports[0]).expect(new DisconnectResult()); - run( SDB_PATH + " disconnect " + LOOPBACK_IP + ":" + ports[0]).expect(new DisconnectResult()); - - boolean success = true; - String errMsg = ""; - for(int i=0; i< MAX_TRIAL; i++) { - final DisconnectResult result = run( SDB_PATH + " devices").expect( new DisconnectResult() ); - if(result == null) { - errMsg = "result of sdb devices for disconnect is null"; - success = false; - } - else { - String[] wholeStrings = result.wholeStrings; - - for(String wholeString: wholeStrings) { - if(wholeString.contains(LOOPBACK_IP)) { - errMsg = "disconnect failed"; - success = false; - break; - } - } - } - if(success) { - break; - } - success = true; + runAndWait( SDB_PATH + " connect " + LOOPBACK_IP + ":" + ports[0]); + Thread.sleep(CONNECT_SLEEP); + runAndWait( SDB_PATH + " disconnect " + LOOPBACK_IP + ":" + ports[0]); + Thread.sleep(CONNECT_SLEEP); + String[] results = SdbTestUtil.runAndGetWholeString(SDB_PATH + " devices", true); + for(String result: results) { + assertFalse(MessageFormat.format("fail to disconnect {0}", result), result.contains(LOOPBACK_IP)); } - - assertTrue(errMsg, success); - } finally { - run( SDB_PATH + " disconnect"); + runAndWait( SDB_PATH + " disconnect"); } } diff --git a/org.tizen.common/test/src/org/tizen/common/sdb/DevicesTest.java b/org.tizen.common/test/src/org/tizen/common/sdb/DevicesTest.java index cbdbe35..20ffaed 100644 --- a/org.tizen.common/test/src/org/tizen/common/sdb/DevicesTest.java +++ b/org.tizen.common/test/src/org/tizen/common/sdb/DevicesTest.java @@ -27,7 +27,6 @@ package org.tizen.common.sdb; import static org.junit.Assert.assertTrue; -import org.junit.BeforeClass; import org.junit.Test; /** @@ -42,11 +41,6 @@ import org.junit.Test; */ public class DevicesTest { - @BeforeClass - public static void prepareTest() throws Exception{ - SdbTestUtil.prepareTest(); - } - /** * Test {sdb devices} * diff --git a/org.tizen.common/test/src/org/tizen/common/sdb/GetSerialNoTest.java b/org.tizen.common/test/src/org/tizen/common/sdb/GetSerialNoTest.java index 2a80ae5..06405a9 100644 --- a/org.tizen.common/test/src/org/tizen/common/sdb/GetSerialNoTest.java +++ b/org.tizen.common/test/src/org/tizen/common/sdb/GetSerialNoTest.java @@ -27,12 +27,9 @@ package org.tizen.common.sdb; import static org.junit.Assert.*; -import static org.tizen.common.Shell.run; import static org.tizen.common.sdb.SdbTestUtil.SDB_PATH; -import org.junit.BeforeClass; import org.junit.Test; -import org.tizen.common.Pattern; /** *

@@ -46,11 +43,6 @@ import org.tizen.common.Pattern; */ public class GetSerialNoTest { - @BeforeClass - public static void prepareTest() throws Exception { - SdbTestUtil.prepareTest(); - } - /** * Test {sdb get-serialno} * @@ -58,18 +50,11 @@ public class GetSerialNoTest { */ @Test public void test_getserialno() throws Exception { - - class GetSerialNoResult { - @Pattern( pattern = "{0}", index = 0) public String serialNo[]; - } String[] serialNos = SdbTestUtil.getSerialNumber(); String serialNo = serialNos[0]; - final GetSerialNoResult result = run( String.format("%s -s %s get-serialno", SDB_PATH, serialNo)).expect(new GetSerialNoResult() ); - - assertNotNull("result of get-serialno is null", result); - assertNotNull("parsing result of get-serialno is null", result.serialNo); - assertTrue("cannot parse result of get-serialno", result.serialNo.length > 0); - assertEquals(serialNo, result.serialNo[0]); + String[] wholeStrings = SdbTestUtil.runAndGetWholeString(String.format("%s -s %s get-serialno", SDB_PATH, serialNo), true); + assertTrue("cannot parse result of get-serialno", wholeStrings.length > 0); + assertEquals(serialNo, wholeStrings[0]); } } diff --git a/org.tizen.common/test/src/org/tizen/common/sdb/GetStateTest.java b/org.tizen.common/test/src/org/tizen/common/sdb/GetStateTest.java index 441b13c..0f0ab78 100644 --- a/org.tizen.common/test/src/org/tizen/common/sdb/GetStateTest.java +++ b/org.tizen.common/test/src/org/tizen/common/sdb/GetStateTest.java @@ -27,14 +27,11 @@ package org.tizen.common.sdb; import static org.junit.Assert.*; -import static org.tizen.common.Shell.run; import static org.tizen.common.sdb.SdbTestUtil.SDB_PATH; import java.text.MessageFormat; -import org.junit.BeforeClass; import org.junit.Test; -import org.tizen.common.Pattern; /** *

@@ -48,11 +45,6 @@ import org.tizen.common.Pattern; */ public class GetStateTest { - @BeforeClass - public static void prepareTest() throws Exception { - SdbTestUtil.prepareTest(); - } - /** * Test {sdb get-serialno} * @@ -61,19 +53,12 @@ public class GetStateTest { @Test public void test_getstate() throws Exception { - class GetStateResult { - @Pattern( pattern = "{0}", index = 0) public String state[]; - } String[] serialNos = SdbTestUtil.getSerialNumber(); String serialNo = serialNos[0]; - - final GetStateResult result = run( String.format("%s -s %s get-state", SDB_PATH, serialNo)).expect(new GetStateResult() ); - - assertNotNull("result of get-state is null", result); - assertNotNull("parsing result of get-state is null", result.state); - assertTrue("cannot parse result of get-state", result.state.length > 0); - String _state = result.state[0]; - assertTrue(MessageFormat.format("unexpected state {0}", _state), _state.equals("offline") || _state.equals("device")); + String[] states = SdbTestUtil.runAndGetWholeString(String.format("%s -s %s get-state", SDB_PATH, serialNo), true); + assertTrue("cannot parse result of get-state", states.length > 0); + assertTrue(MessageFormat.format("unexpected state {0}", states[0]), + states[0].equals("offline") || states[0].equals("device")); } } diff --git a/org.tizen.common/test/src/org/tizen/common/sdb/InstallTest.java b/org.tizen.common/test/src/org/tizen/common/sdb/InstallTest.java index d509c8c..948fdab 100644 --- a/org.tizen.common/test/src/org/tizen/common/sdb/InstallTest.java +++ b/org.tizen.common/test/src/org/tizen/common/sdb/InstallTest.java @@ -27,7 +27,7 @@ package org.tizen.common.sdb; import static org.junit.Assert.assertTrue; -import static org.tizen.common.Shell.run; +import static org.tizen.common.Shell.*; import static org.tizen.common.sdb.SdbTestUtil.*; import java.io.BufferedInputStream; @@ -38,7 +38,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.text.MessageFormat; -import org.junit.BeforeClass; import org.junit.Test; import org.tizen.common.Pattern; import org.tizen.common.util.FileUtil; @@ -56,10 +55,7 @@ import org.tizen.common.util.IOUtil; */ public class InstallTest { - @BeforeClass - public static void prepareTest() throws Exception{ - SdbTestUtil.prepareTest(); - } + public static int INSTALL_WAIT = 1000; /** * Test {sdb install, sdb uninstall (native)} @@ -123,9 +119,8 @@ public class InstallTest { assertTrue("tpk file is not copied", new File(copiedName).exists()); String serialNumber = getSerialNumber()[0]; - run( SDB_PATH + String.format(" -s %s uninstall %s", serialNumber, appId)).expect(new InstallUninstallResult()); - run( SDB_PATH + String.format(" -s %s install %s", serialNumber, copiedName)).expect(new InstallUninstallResult()); - + runAndWait(SDB_PATH + String.format(" -s %s uninstall %s", serialNumber, appId)); + runAndWait( SDB_PATH + String.format(" -s %s install %s", serialNumber, copiedName)); boolean success = false; String errMsg = ""; for(int i=0; i < MAX_TRIAL; i++) { @@ -145,10 +140,10 @@ public class InstallTest { success = true; break; } + Thread.sleep(INSTALL_WAIT); } assertTrue(errMsg, success); - run( SDB_PATH + String.format(" -s %s uninstall %s", serialNumber, appId)).expect(new InstallUninstallResult()); - + runAndWait( SDB_PATH + String.format(" -s %s uninstall %s", serialNumber, appId)); success = false; for(int i=0; i< MAX_TRIAL; i++) { InstallUninstallResult result = run( SDB_PATH + String.format(" -s %s shell pkgcmd -l | grep %s", serialNumber, appId)). @@ -163,7 +158,7 @@ public class InstallTest { success = true; break; } - Thread.sleep(1000); + Thread.sleep(INSTALL_WAIT); } assertTrue(errMsg, success); } diff --git a/org.tizen.common/test/src/org/tizen/common/sdb/PullPushTest.java b/org.tizen.common/test/src/org/tizen/common/sdb/PullPushTest.java index 2e52c6f..4ac5a0c 100644 --- a/org.tizen.common/test/src/org/tizen/common/sdb/PullPushTest.java +++ b/org.tizen.common/test/src/org/tizen/common/sdb/PullPushTest.java @@ -28,16 +28,14 @@ package org.tizen.common.sdb; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.tizen.common.Shell.run; +import static org.tizen.common.Shell.*; import static org.tizen.common.sdb.SdbTestUtil.*; import java.io.File; import java.text.MessageFormat; import java.util.List; -import org.junit.BeforeClass; import org.junit.Test; -import org.tizen.common.Pattern; import org.tizen.common.util.FileUtil; import org.tizen.common.util.FileUtilTest; @@ -52,11 +50,6 @@ import org.tizen.common.util.FileUtilTest; * @author Ho Namkoong{@literal } (S-Core) */ public class PullPushTest { - - @BeforeClass - public static void prepareTest() throws Exception{ - SdbTestUtil.prepareTest(); - } /** * Test {sdb push, sdb pull (file)} @@ -74,73 +67,20 @@ public class PullPushTest { @Test public void test_pushPullFile() throws Exception { - class PushResult { - @Pattern( pattern = "{0}", index = 0 ) public String wholeString; - } - final String pushFileName = "push_file"; final String pullFileName = "pull_file"; final String tmpDir = "/tmp"; final String targetDst = tmpDir + "/" + pushFileName; - String fileContent = generateRandomString(400, FILE_CONTENT_CHAR_LIST); - File pushFile = new File(pushFileName); - if(pushFile.exists()) { - FileUtil.recursiveDelete(pushFile); - } - - File pullFile = new File(pullFileName); - if(pullFile.exists()) { - FileUtil.recursiveDelete(pullFile); - } try { - FileUtil.createTextFile(pushFile, fileContent, null); - - assertTrue(MessageFormat.format("fail to create file {0}", pushFile.getAbsolutePath()), pushFile.exists()); - - String serialNumber = getSerialNumber()[0]; - - run( SDB_PATH + String.format(" -s %s shell rm -rf %s", serialNumber, targetDst)).expect(new PushResult()); - run( SDB_PATH + String.format(" -s %s push %s %s", serialNumber, pushFileName, targetDst)).expect(new PushResult()); - - boolean success = false; - String errMsg = ""; - for(int i=0; i< MAX_TRIAL; i++) { - PushResult result = run(SDB_PATH + String.format(" -s %s shell ls %s", serialNumber, targetDst)).expect(new PushResult()); - if(result == null) { - errMsg = "result of shell is null"; - } - else if(result.wholeString == null) { - errMsg = "wholeString of shell is null"; - } - else if(result.wholeString.equals(targetDst)) { - success = true; - break; - } - Thread.sleep(1000); - } - assertTrue(errMsg, success); - run( SDB_PATH + String.format(" -s %s pull %s %s", serialNumber, targetDst, pullFileName)).expect(new PushResult()); - - success = false; - for(int i=0; i< MAX_TRIAL; i++) { - if(pullFile.exists()) { - success = true; - break; - } - Thread.sleep(1000); - } - - assertTrue(MessageFormat.format("fail to pull file {0}", pullFile.getAbsolutePath()), success); - String result = FileUtil.readTextFile(pullFile, null); - - assertEquals(MessageFormat.format("pushed file {0} and pull file {1} are not same", pushFile.getAbsolutePath(), pullFile.getAbsolutePath()), - result, fileContent); + pushPullAndCheck(pushFileName, targetDst, pullFileName, true); } finally { + File pushFile = new File(pushFileName); if(pushFile.exists()) { FileUtil.recursiveDelete(pushFile); } + File pullFile = new File(pullFileName); if(pullFile.exists()) { FileUtil.recursiveDelete(pullFile); } @@ -163,82 +103,72 @@ public class PullPushTest { @Test public void test_pushPullDir() throws Exception { - class PullResult { - @Pattern( pattern = "{0}", index = 0 ) public String wholeString; - } - final String pushDirName = "push_dir"; final String pullDirName = "pull_dir"; final String tmpDir = "/tmp"; final String targetDst = tmpDir + "/" + pushDirName; - - - File pushDir = new File(pushDirName); - if(pushDir.exists()) { - FileUtil.recursiveDelete(pushDir); - } - - File pullDir = new File(pullDirName); - if(pullDir.exists()) { - FileUtil.recursiveDelete(pullDir); - } - try { - generateRandomDir(pushDir, 5, 5, 3); - - assertTrue(MessageFormat.format("fail to create push directory {0}", pushDir.getAbsolutePath()), pushDir.exists()); - - String serialNumber = getSerialNumber()[0]; - run( SDB_PATH + String.format(" -s %s shell rm -rf %s", serialNumber, targetDst)).expect(new PullResult()); - run( SDB_PATH + String.format(" -s %s push %s %s", serialNumber, pushDirName, targetDst)).expect(new PullResult()); - - boolean success = false; - String errMsg = ""; - for(int i=0; i< MAX_TRIAL; i++) { - PullResult result = run(SDB_PATH + String.format(" -s %s shell ls -d %s", serialNumber, targetDst)).expect(new PullResult()); - if(result == null) { - errMsg = "result of shell is null"; - } - else if(result.wholeString == null) { - errMsg = "wholeString of shell is null"; - } - else if(result.wholeString.equals(targetDst)) { - success = true; - break; - } - else { - errMsg = MessageFormat.format("Expected {0}, Actual {1}", targetDst, result.wholeString); - } - Thread.sleep(1000); - } - - assertTrue(errMsg, success); - run( SDB_PATH + String.format(" -s %s pull %s %s", serialNumber, targetDst, pullDirName)).expect(new PullResult()); - - success = false; - for(int i=0; i< MAX_TRIAL; i++) { - if(pullDir.exists()) { - success = true; - break; - } - } - - assertTrue(MessageFormat.format("fail to pull directory {0}", pullDir.getAbsolutePath()), success); - List notPushPullList = FileUtilTest.compareFiles(pushDir.getAbsolutePath(), pullDir.getAbsolutePath()); - assertTrue(String.format("Following files are not pushed or pulled\n%s", listFiles(notPushPullList)), notPushPullList.size() == 0); - List morePushPullList = FileUtilTest.compareFiles(pullDir.getAbsolutePath(), pushDir.getAbsolutePath()); - assertTrue(String.format("Following files are pushed or pulled but not in source\n%s", listFiles(morePushPullList)), FileUtilTest.compareFiles(pullDir.getAbsolutePath(), pushDir.getAbsolutePath()).size() == 0); + pushPullAndCheck(pushDirName, targetDst, pullDirName, false); } finally { + File pushDir = new File(pushDirName); if(pushDir.exists()) { FileUtil.recursiveDelete(pushDir); } + File pullDir = new File(pullDirName); if(pullDir.exists()) { FileUtil.recursiveDelete(pullDir); } } } + private void pushPullAndCheck(String pushSrc, String pushDst, String pullDst, boolean file) throws Exception { + + File pushFile = new File(pushSrc); + if(pushFile.exists()) { + FileUtil.recursiveDelete(pushFile); + } + + File pullFile = new File(pullDst); + if(pullFile.exists()) { + FileUtil.recursiveDelete(pullFile); + } + + String fileContent = ""; + if(file) { + fileContent = generateRandomString(400, FILE_CONTENT_CHAR_LIST); + FileUtil.createTextFile(pushFile, fileContent, null); + } + else { + generateRandomDir(pushFile, 5, 5, 3); + } + + assertTrue(MessageFormat.format("fail to create file {0}", pushFile.getAbsolutePath()), pushFile.exists()); + String serialNumber = getSerialNumber()[0]; + runAndWait( SDB_PATH + String.format(" -s %s shell rm -rf %s", serialNumber, pushDst)); + assertTrue(MessageFormat.format("rm fail to remove {0}", pushDst), + checkTargetFileExist(pushDst, serialNumber, file, false)); + runAndWait( SDB_PATH + String.format(" -s %s push %s %s", serialNumber, pushSrc, pushDst)); + assertTrue(MessageFormat.format("{0} does not exist in target {1}", pushDst, serialNumber), + checkTargetFileExist(pushDst, serialNumber, file, true)); + runAndWait( SDB_PATH + String.format(" -s %s pull %s %s", serialNumber, pushDst, pullDst)); + assertTrue(MessageFormat.format("{0} does not exist in host", pullFile.getAbsolutePath()), pullFile.exists()); + + if( file ) { + String result = FileUtil.readTextFile(pullFile, null); + assertEquals(MessageFormat.format("pushed file {0} and pull file {1} are not same", pushFile.getAbsolutePath(), pullFile.getAbsolutePath()), + result, fileContent); + } + else { + List notPushPullList = FileUtilTest.compareFiles(pushFile.getAbsolutePath(), pullFile.getAbsolutePath()); + assertTrue(String.format("Following files are not pushed or pulled\n%s", + listFiles(notPushPullList)), notPushPullList.size() == 0); + List morePushPullList = FileUtilTest.compareFiles(pullFile.getAbsolutePath(), pushFile.getAbsolutePath()); + assertTrue(String.format("Following files are pushed or pulled but not in source\n%s", listFiles(morePushPullList)), + FileUtilTest.compareFiles(pullFile.getAbsolutePath(), pushFile.getAbsolutePath()).size() == 0); + } + } + private String listFiles(List fileList) { StringBuffer buffer = new StringBuffer(); for(File file: fileList) { @@ -246,4 +176,30 @@ public class PullPushTest { } return buffer.toString(); } + + private boolean checkTargetFileExist(String fileName, String serial, boolean file, boolean exist) throws Exception { + String[] results = null; + for( int i =0; i < MAX_TRIAL; i++) { + if(file) { + results = runAndGetWholeString(SDB_PATH + String.format(" -s %s shell ls %s", serial, fileName), true); + } + else { + results = runAndGetWholeString(SDB_PATH + String.format(" -s %s shell ls -d %s", serial, fileName), true); + } + for(String result: results) { + if(exist) { + if(result.equals(fileName)) { + return true; + } + } + else { + if(result.contains("No such file or directory")) { + return true; + } + } + } + Thread.sleep(1000); + } + return false; + } } diff --git a/org.tizen.common/test/src/org/tizen/common/sdb/RootTest.java b/org.tizen.common/test/src/org/tizen/common/sdb/RootTest.java new file mode 100644 index 0000000..0815341 --- /dev/null +++ b/org.tizen.common/test/src/org/tizen/common/sdb/RootTest.java @@ -0,0 +1,106 @@ +/* + * Common + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Ho Namkoong + * BonYong Lee + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + +package org.tizen.common.sdb; + +import static org.junit.Assert.*; +import static org.tizen.common.sdb.SdbTestUtil.*; +import static org.tizen.common.Shell.*; + +import java.text.MessageFormat; + +import org.junit.Test; +import org.tizen.common.IShell; +import org.tizen.common.Pattern; +import org.tizen.common.Token; + +/** + *

+ * RootTest + * + * Test case for sdb root on and sdb root off + * + *

+ * + * @author Ho Namkoong{@literal } (S-Core) + */ +public class RootTest { + + class RootResult { + @Token(from = "Switched to", to = "account mode") + public String result; + } + + /** + * Test {sdb root on} + * + * @author Ho Namkoong{@literal } (S-Core) + */ + @Test + public void test_root_on() throws Exception{ + + String[] serials = getSerialNumber(); + runAndWait(MessageFormat.format("{0} -s {1} {2}", SDB_PATH, serials[0], "root off")); + RootResult result = run(MessageFormat.format("{0} -s {1} {2}", SDB_PATH, serials[0], "root on")) + .expect(new RootResult()); + + assertNotNull("output of root on is null", result); + assertEquals("'root'", result.result); + runShellAndExpect(serials[0], "#"); + } + + /** + * Test {sdb root off} + * + * @author Ho Namkoong{@literal } (S-Core) + */ + @Test + public void test_root_off() throws Exception { + + String[] serials = getSerialNumber(); + runAndWait(MessageFormat.format("{0} -s {1} {2}", SDB_PATH, serials[0], "root on")); + RootResult result = run(MessageFormat.format("{0} -s {1} {2}", SDB_PATH, serials[0], "root off")). + expect(new RootResult()); + + assertNotNull("output of root off is null", result); + assertEquals("'developer'", result.result); + runShellAndExpect(serials[0], "$"); + } + + private static void runShellAndExpect(String serial, String expect) throws Exception { + class RootShellResult { + @Pattern(pattern = "sh-4.1{0}exit", index = 0) + public String token; + } + + IShell shell = run(String.format("%s -s %s %s", SDB_PATH, serial, "shell")); + shell.write("exit", 1000); + RootShellResult result = shell.expect(new RootShellResult()); + + assertNotNull("output of shell is null", result.token); + assertEquals(expect, result.token); + } +} diff --git a/org.tizen.common/test/src/org/tizen/common/sdb/SdbTestSuite.java b/org.tizen.common/test/src/org/tizen/common/sdb/SdbTestSuite.java index 24c6932..f577cc2 100644 --- a/org.tizen.common/test/src/org/tizen/common/sdb/SdbTestSuite.java +++ b/org.tizen.common/test/src/org/tizen/common/sdb/SdbTestSuite.java @@ -25,6 +25,9 @@ */ package org.tizen.common.sdb; +import static org.tizen.common.Shell.runAndWait; + +import org.junit.BeforeClass; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @@ -41,7 +44,16 @@ import org.junit.runners.Suite.SuiteClasses; */ @RunWith(Suite.class) @SuiteClasses({ ConnectTest.class, DevicesTest.class, GetSerialNoTest.class, - GetStateTest.class, InstallTest.class, PullPushTest.class, StartKillTest.class}) + GetStateTest.class, InstallTest.class, PullPushTest.class, VersionTest.class, + ShellTest.class, StatusWindowTest.class, RootTest.class, StartKillTest.class}) public class SdbTestSuite { + @BeforeClass + public static void prepareTest() throws Exception { + runAndWait( SdbTestUtil.SDB_PATH + " kill-server"); + Thread.sleep(1000); + runAndWait(SdbTestUtil.SDB_PATH + " start-server"); + Thread.sleep(1000); + } + } diff --git a/org.tizen.common/test/src/org/tizen/common/sdb/SdbTestUtil.java b/org.tizen.common/test/src/org/tizen/common/sdb/SdbTestUtil.java index ffbc74e..5350fe9 100644 --- a/org.tizen.common/test/src/org/tizen/common/sdb/SdbTestUtil.java +++ b/org.tizen.common/test/src/org/tizen/common/sdb/SdbTestUtil.java @@ -27,10 +27,12 @@ package org.tizen.common.sdb; import static org.junit.Assert.*; -import static org.tizen.common.Shell.run; +import static org.tizen.common.Shell.*; import java.io.File; +import java.text.MessageFormat; import java.util.Random; +import org.tizen.common.IShell; import org.tizen.common.Pattern; import org.tizen.common.util.FileUtil; import org.tizen.common.util.StringUtil; @@ -49,6 +51,7 @@ class SdbTestUtil { static final String LOOPBACK_IP = "127.0.0.1"; static final int MAX_TRIAL = 10; + static final char CTRL_C = '\u0003'; static String SDB_PATH; static { @@ -81,7 +84,6 @@ class SdbTestUtil { static int[] getPorts() throws Exception { class PortResult { @Pattern( pattern = "emulator-{0}\t{1}", index = 0) public String[] devicePortNum; - @Pattern( pattern = "{0}", index = 0) public String[] wholeString; }; final PortResult resultString = run( SDB_PATH + " devices" ).expect( new PortResult() ); @@ -112,7 +114,6 @@ class SdbTestUtil { class SerialResult { @Pattern( pattern = "{0}\t{1}", index = 0 ) public String[] deviceSerialName; - @Pattern(pattern = "{0}", index = 0) public String[] wholeString; }; String[] result = null; @@ -197,8 +198,27 @@ class SdbTestUtil { } } - static void prepareTest() throws Exception { - run( SDB_PATH + " kill-server").expect(new SdbTestUtil()); - run(SDB_PATH + " start-server").expect(new SdbTestUtil()); + static String[] getWholeString(IShell shell, boolean assertNotNull) throws Exception { + class WholeResult { + @Pattern( pattern = "{0}", index = 0) public String wholeStrings[]; + } + + WholeResult result = shell.expect(new WholeResult()); + if(assertNotNull) { + assertNotNull("output of shell is null", result.wholeStrings); + } + return result.wholeStrings; + } + + static String[] runAndGetWholeString(String command, boolean assertNotNull) throws Exception { + class WholeResult { + @Pattern( pattern = "{0}", index = 0) public String wholeStrings[]; + } + + WholeResult result = run(command).expect(new WholeResult()); + if(assertNotNull) { + assertNotNull(MessageFormat.format("Result of command {0} is null", command), result.wholeStrings); + } + return result.wholeStrings; } } diff --git a/org.tizen.common/test/src/org/tizen/common/sdb/ShellTest.java b/org.tizen.common/test/src/org/tizen/common/sdb/ShellTest.java new file mode 100644 index 0000000..e336112 --- /dev/null +++ b/org.tizen.common/test/src/org/tizen/common/sdb/ShellTest.java @@ -0,0 +1,71 @@ +/* + * Common + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Ho Namkoong + * BonYong Lee + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + +package org.tizen.common.sdb; + +import static org.junit.Assert.*; +import static org.tizen.common.Shell.*; +import static org.tizen.common.sdb.SdbTestUtil.*; + +import org.junit.Test; +import org.tizen.common.IShell; + +/** + *

+ * ShellTest + * + * Test case for sdb shell + * + *

+ * + * @author Ho Namkoong{@literal } (S-Core) + */ +public class ShellTest { + + /** + * Test {sdb shell} + * + * @author Ho Namkoong{@literal } (S-Core) + */ + @Test + public void test_shell() throws Exception{ + String[] serials = getSerialNumber(); + IShell shell = run(String.format("%s -s %s %s", SDB_PATH, serials[0], "shell")); + shell.write("exit", 1000); + String[] results = getWholeString(shell, true); + + boolean success = false; + for(String result: results) { + if(result.contains("sh-4.1")) { + success = true; + break; + } + } + + assertTrue("fail to launch shell", success); + } + +} diff --git a/org.tizen.common/test/src/org/tizen/common/sdb/StartKillTest.java b/org.tizen.common/test/src/org/tizen/common/sdb/StartKillTest.java index bfdb7cb..b57d808 100644 --- a/org.tizen.common/test/src/org/tizen/common/sdb/StartKillTest.java +++ b/org.tizen.common/test/src/org/tizen/common/sdb/StartKillTest.java @@ -27,11 +27,10 @@ package org.tizen.common.sdb; import static org.junit.Assert.*; -import static org.tizen.common.Shell.run; -import static org.tizen.common.sdb.SdbTestUtil.SDB_PATH; +import static org.tizen.common.Shell.*; +import static org.tizen.common.sdb.SdbTestUtil.*; import org.junit.Test; -import org.tizen.common.Pattern; /** *

@@ -44,10 +43,6 @@ import org.tizen.common.Pattern; * @author Ho Namkoong{@literal } (S-Core) */ public class StartKillTest { - - class StartKillResult { - @Pattern( pattern = "{0}", index = 0) public String wholeString[]; - } /** * Test {sdb start-server, kill-server} @@ -58,42 +53,30 @@ public class StartKillTest { public void test_start_kill_server() throws Exception { try { - run(SDB_PATH + " kill-server").expect(new StartKillResult()); - StartKillResult result = run(SDB_PATH + " start-server").expect(new StartKillResult()); - assertNotNull("result of start-server is null", result); - assertNotNull("parsing result of start-server is null", result.wholeString); - - boolean success = false; - for(int i = 0; i< result.wholeString.length; i++) { - if(result.wholeString[i].contains("daemon not running")) { - success = true; - break; - } - } + runAndWait(SDB_PATH + " kill-server"); + startServerAndCheck("sdb is not started or killed"); - assertTrue("sdb is not started or killed", success); + String[] results = runAndGetWholeString(SDB_PATH + " start-server", false); + assertNull("sdb is not started", results); - result = run(SDB_PATH + " start-server").expect(new StartKillResult()); - assertNotNull("result of start-server is null", result); - assertNull("sdb is not started", result.wholeString); - - run(SDB_PATH + " kill-server").expect(new StartKillResult()); - result = run(SDB_PATH + " start-server").expect(new StartKillResult()); - assertNotNull("result of start-server is null", result); - assertNotNull("parsing result of start-server is null", result.wholeString); - - success = false; - for(int i = 0; i< result.wholeString.length; i++) { - if(result.wholeString[i].contains("daemon not running")) { - success = true; - break; - } - } - - assertTrue("sdb is not killed", success); + runAndWait(SDB_PATH + " kill-server"); + startServerAndCheck("sdb is not killed"); } finally { run(SDB_PATH + " kill-server"); } } + + private static void startServerAndCheck(String msg) throws Exception { + String[] results = runAndGetWholeString(SDB_PATH + " start-server", true); + + boolean success = false; + for(String result: results) { + if(result.contains("daemon not running")) { + success = true; + break; + } + } + assertTrue(msg, success); + } } diff --git a/org.tizen.common/test/src/org/tizen/common/sdb/StatusWindowTest.java b/org.tizen.common/test/src/org/tizen/common/sdb/StatusWindowTest.java new file mode 100644 index 0000000..9e764c2 --- /dev/null +++ b/org.tizen.common/test/src/org/tizen/common/sdb/StatusWindowTest.java @@ -0,0 +1,72 @@ +/* + * Common + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Ho Namkoong + * BonYong Lee + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + +package org.tizen.common.sdb; + +import static org.junit.Assert.*; +import static org.tizen.common.sdb.SdbTestUtil.*; +import static org.tizen.common.Shell.*; + +import java.text.MessageFormat; + +import org.junit.Test; +import org.tizen.common.Pattern; + +/** + *

+ * StatusWindowTest + * + * Test case for sdb status-window + * + *

+ * + * @author Ho Namkoong{@literal } (S-Core) + */ +public class StatusWindowTest { + + /** + * Test {sdb status-window} + * + * @author Ho Namkoong{@literal } (S-Core) + */ + @Test + public void test_status_window() throws Exception{ + + class StatusWindowResult { + @Pattern(pattern = "State:{0}", index = 0) + public String state; + } + + String[] serials = getSerialNumber(); + StatusWindowResult result = run(String.format("%s -s %s %s", SDB_PATH, serials[0], "status-window")). + expect(new StatusWindowResult(), 2000); + assertNotNull("result of status-window is null", result.state); + assertTrue(MessageFormat.format("unexpected state: {0}", result.state), + result.state.equals("device") || result.state.equals("offline")); + + } + +} diff --git a/org.tizen.common/test/src/org/tizen/common/sdb/VersionTest.java b/org.tizen.common/test/src/org/tizen/common/sdb/VersionTest.java new file mode 100644 index 0000000..9ac6c00 --- /dev/null +++ b/org.tizen.common/test/src/org/tizen/common/sdb/VersionTest.java @@ -0,0 +1,57 @@ +/* + * Common + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Ho Namkoong + * BonYong Lee + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + +package org.tizen.common.sdb; + +import static org.junit.Assert.*; +import static org.tizen.common.sdb.SdbTestUtil.*; + +import org.junit.Test; + +/** + *

+ * VersionTest + * + * Test case for sdb version + * + *

+ * + * @author Ho Namkoong{@literal } (S-Core) + */ +public class VersionTest { + + /** + * Test {sdb version} + * + * @author Ho Namkoong{@literal } (S-Core) + */ + @Test + public void test_version() throws Exception { + String[] results = runAndGetWholeString(String.format("%s %s", SdbTestUtil.SDB_PATH, "version"), true); + assertTrue(results[0].contains("Smart Development Bridge version")); + } + +} -- 2.7.4