发布新日志

  • Upcasting

    2014-09-02 14:33:55

    From http://stackoverflow.com/questions/5361999/whats-the-need-to-use-upcasting-in-java


    In most situations, the upcast is entirely unnecessary and has no effect. However, there are situations where the presence of the upcast changes the meaning of the statement / expression.

    One situation where it is necessary to use upcasting in Java is when you want to force a specific method override to be used; e.g. suppose that we have overloaded methods:

    public void doIt(Object o)...
    public void doIt(String s)...

    If I have a String and I want to call the first overload rather than the second, I have to do this:

    String arg = ...
    
    doIt((Object) arg);

    A related case is:

    doIt((Object) null);

    where the code won't compile without the type cast. (I'm not sure if this counts as an upcast, but here it is anyway.)

    A second situation involves varadic parameters:

    public void doIt(Object... args)...
    
    Object[] foo = ...
    
    doIt(foo);  // passes foo as the argument array
    doIt((Object) foo); // passes new Object[]{foo} as the argument array.

    A third situation is when performing operations on primitive numeric types; e.g.

    int i1 = ...
    int i2 = ...
    long res = i1 + i2;           // 32 bit signed arithmetic ... might overflow
    long res2 = ((long) i1) + i2; // 64 bit signed arithmetic ... won't overflow
  • "this" keyword

    2014-09-02 14:27:07

    The following is a copy & paste from here, but explains very well all different uses of the this keyword:

    Definition: Java’s this keyword is used to refer the current instance of the method on which it is used.

    Following are the ways to use this:

    1. To specifically denote that the instance variable is used instead of static or local variable. That is,

      private String javaFAQ;
      void methodName(String javaFAQ) {
          this.javaFAQ = javaFAQ;
      }

      Here this refers to the instance variable. Here the precedence is high for the local variable. Therefore the absence of the this denotes the local variable. If the local variable that is parameter’s name is not same as instance variable then irrespective of this is used or not it denotes the instance variable.

    2. This is used to refer the constructors

       public JavaQuestions(String javapapers) {
           this(javapapers, true);
       }

      This invokes the constructor of the same java class which has two parameters.

    3. This is used to pass the current java instance as parameter

      obj.itIsMe(this);
    4. Similar to the above this can also be used to return the current instance

      CurrentClassName startMethod() {
           return this;
      }

      Note: This may lead to undesired results while used in inner classes in the above two points. Since this will refer to the inner class and not the outer instance.

    5. This can be used to get the handle of the current class

      Class className = this.getClass(); // this methodology is preferable in java

      Though this can be done by

      Class className = ABC.class; // here ABC refers to the class name and you need to k
  • Inheritance vs Composition

    2014-09-02 11:53:24

    From http://www.artima.com/designtechniques/compoinh5.html


    a few guidelines that reflect how I tend to select between composition and inheritance.

    Make sure inheritance models the is-a relationship
    My main guiding philosophy is that inheritance should be used only when a subclass is-a superclass. In the example above, an Apple likely is-a Fruit, so I would be inclined to use inheritance.

    An important question to ask yourself when you think you have an is-a relationship is whether that is-a relationship will be constant throughout the lifetime of the application and, with luck, the lifecycle of the code. For example, you might think that an Employee is-a Person, when really Employee represents a role that a Person plays part of the time. What if the person becomes unemployed? What if the person is both an Employee and a Supervisor? Such impermanent is-a relationships should usually be modelled with composition.

    Don't use inheritance just to get code reuse
    If all you really want is to reuse code and there is no is-a relationship in sight, use composition.

    Don't use inheritance just to get at polymorphism
    If all you really want is polymorphism, but there is no natural is-a relationship, use composition with interfaces. I'll be talking about this subject next month.

  • Inheritance vs Delegation

    2014-09-02 11:16:36

    Found this very good explanation here
    http://www.jguru.com/faq/view.jsp?EID=27916


    Both delegation and inheritance are important concepts in object-oriented software design, but not everyone would label them as patterns. In particular, the seminal book on design patterns by the “Gang of Four” contains a discussion of inheritance and delegation, but the authors do not treat these topics as specific patterns. It is reasonable to think of them as design concepts which are more general than specific design patterns.

    Inheritance is a relationship between two classes where one class, called a subclass in this context, inherits the attributes and operations of another class, called its superclass. Inheritance can be a powerful design/reuse technique, especially when it is used in the context of the Liskov Substitution Principle. (The article by Robert Martin at http://www.objectmentor.com/publications/lsp.pdf provides an excellent explanation of the ideas behind Barbara Liskov’s original paper on using inheritance correctly.) The primary advantages of inheritance are

    1. it is directly supported by object-oriented languages, and
    2. it provides the context for polymorphism in strongly-typed object-oriented languages such as C++ and Java.
    But since the inheritance relationship is defined at compile-time, a class can’t change its superclass dynamically during program execution. Moreover, modifications to a superclass automatically propagate to the subclass, providing a two-edged sword for software maintenance and reuse. In summary, inheritance creates a strong, static coupling between a superclass and its subclasses.

    Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate. Delegation can also a powerful design/reuse technique. The primary advantage of delegation is run-time flexibility – the delegate can easily be changed at run-time. But unlike inheritance, delegation is not directly supported by most popular object-oriented languages, and it doesn’t facilitate dynamic polymorphism.

    As a simple example, consider the relationship between a Rectangle class and a Window class. With inheritance, a Window class would inherit its rectangular properties from class Rectangle. With delegation, a Window object would maintain a reference or pointer to a Rectangle object, and calls to rectangle-like methods of the Window object would be delegated to corresponding methods of the Rectangle object.

    Now let’s consider a slightly more complex example. Suppose employees can classified based on how they are paid; e.g., hourly or salaried. Using inheritance, we might design three classes: an Employee class which encapsulates the functionality common to all employees, and two subclasses HourlyEmployee and SalariedEmployee which encapsulates pay-specific details. While this design might be suitable for some applications, we would encounter problems in a scenario where a person changes, say from hourly to salaried. The class of an object and the inheritance relationship are both static, and objects can’t change their class easily (but see the State pattern for tips on how to fake it).

    A more flexible design would involve delegation – an Employee object could delegate pay-related method calls to an object whose responsibilities focused solely on how the employee is paid. In fact, we might still use inheritance here in a slightly different manner by creating an abstract class (or interface) called PayClassification with two subclasses HourlyPayClassification and SalariedPayClassification which implement classification-specific computations. Using delegation as shown, it would be much easier to change the pay classification of an existing Employee object.

    This second example illustrates an important point: In implementing delegation, we often want the capability to replace the delegate with another object, possibly of a different class. Therefore delegation will often use inheritance and polymorphism, with classes of potential delegates being subclasses of an abstract class which encapsulates general delegate responsibilities.

    One final point. Sometimes, the choice between delegation and inheritance is driven by external factors such as programming language support for multiple inheritance or design constraints requiring polymorphism. Consider threads in Java. You can associate a class with a thread in one of two ways: either by extending (inheriting) directly from class Thread, or by implementing the Runnable interface and then delegating to a Thread object. Often the approach taken is based on the restriction in Java that a class can only extend one class (i.e., Java does not support multiple inheritance). If the class you want to associate with a thread already extends some other class in the design, then you would have to use delegation; otherwise, extending class Thread would usually be the simpler approach.
     
  • Groovy - Create Folder

    2012-10-13 14:32:08

    // This will create a "test" folder with its subfoder "s" if not exists
    new File("d:/test/s").mkdirs()


  • Java premitive data and Object 赋值

    2012-10-02 23:02:59

    最近开始学习Java,在看Think in Java,看了没几页就发现自己之前在用Groovy写脚本时出现的一些问题在书中找到了理论解释 哈哈,很喜欢这本书,准备坚持看下去,然后结合自己在工作中的一些问题 在此记录一下,加深理解,不时的来看看

    今天看到Primitive Data Type 和 Object得赋值:

    之前在写脚本时曾经如下写:

    List l1 = new ArrayList()
    List l2 = new ArrayList()

    l1.add(1)
    l2.add(l1);

    l1.clear();
    print l2;

    本来是想输出l2作为一个多维List,结果每次运行l2都是空的,不明白是怎么回事。请教别人,知道是因为l1.clear()一句把l1清空了,但同时也清空了l2;

    Think in Java 中指出了原因:

    Java中有primitive data type的赋值,也有ojbect的赋值,两者是不一样的;

    Primitive data type: 例如
    b = 3;
    a = b; // b's contents are copied to a
    a = 4; // a is assigned with value 4
    print b; // b is still 3, not affected.

    Object:
    Class1 c1 = new Class1();// c1 is just a reference
    Class1 c2 = new Class1(); // same to c2
    c1=c2; // both c1 and c2 contain the same reference, thus pointing to the same object
    if you change C2 or C1's values, the other one will be changed accordingly
  • using AutoIt to automate WMP (play video)

    2009-12-30 17:31:30

    If embedded in IE:

    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #include <file.au3>
    #include <common.au3>
    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>
    #include <GuiEdit.au3>
    #include <date.au3>
    #include <ScreenCapture.au3>
    #include <IE.au3>

    ; Define global parameters;
    ; Set Global Values
    $Network_Path="\\_Projects\Test Projects\ WMP Plugin\Version 1.2\Test Data\"
    $Test_Scripts_Path=$Network_Path&"Test Scripts\"
    $Local_Path="C:\temp\ WMP Plugin\"
    dim $TestResultFiles[3]
    ;$VIQ_Page_URL="C:\Program Files\\ WMP Plug-in\.html"
    $VIQ_Page_URL="http://192.168.1.2:9091/.html"
    $WMP_Title = "Windows Media Player" ; Windows Media Player Main Screen Title
    Global $page

    Opt("GUIOnEventMode", 1)
    #Region ### START Koda GUI section ### Form=\\_projects\test projects\wmp plugin\version 1.2\test data\test scripts\form1.kxf
    $TestConsoleForm. = GUICreate("Test Console", 633, 454, 190, 111)
    GUISetOnEvent($GUI_EVENT_CLOSE, "TestConsoleFormClose")
    GUISetOnEvent($GUI_EVENT_MINIMIZE, "TestConsoleFormMinimize")
    GUISetOnEvent($GUI_EVENT_MAXIMIZE, "TestConsoleFormMaximize")
    GUISetOnEvent($GUI_EVENT_RESTORE, "TestConsoleFormRestore")
    $bSelectDataFile = GUICtrlCreateButton("Browse", 440, 120, 75, 25, 0)
    GUICtrlSetOnEvent(-1, "bSelectDataFileClick")
    $SelectDataFile = GUICtrlCreateLabel("Select test data file:", 16, 120, 97, 17)
    GUICtrlSetOnEvent(-1, "SelectDataFileClick")
    Global $iSelectDataFile = GUICtrlCreateInput("iSelectDataFile", 208, 120, 209, 21)
    GUICtrlSetOnEvent(-1, "iSelectDataFileChange")

    $bRunScript. = GUICtrlCreateButton("Run", 208, 216, 75, 25, 0)
    GUICtrlSetOnEvent(-1, "bRunScriptClick")
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $bSelectDataFile
               
        EndSwitch
    WEnd

    Func bSelectDataFileClick()
        $var = FileOpenDialog("Select a file", $Network_Path, "All (*.*)")
        ;Global $Test_Data_File
        _GUICtrlEdit_SetText($iSelectDataFile, $var)

    EndFunc   ;==>bSelectDataFileClick

    Func GetTestType()
        Global $sTestType
       
        if StringInStr($Test_Data_File, "Baseline") Then
            $sTestType= "Baseline"
        elseif StringInStr($Test_Data_File, "Web") Then
            $sTestType= "Web"
        Elseif StringInStr($Test_Data_File, "Others") Then
            $sTestType= "Others"
        EndIf
        return $sTestType
    EndFunc

    Func CreateTestResultFile($TestCase_ID,$File_Name,$CurrentDT)
       
        $sTestType=GetTestType()   
       
        $TestResult_Dir_Name = $Network_Path & "Test Results\" & $sTestType&"_" & $CurrentDT & "\"
        $TestResult_File = $TestResult_Dir_Name&$sTestType&"_" & $CurrentDT & ".txt";
        $Url_List_File = $Network_Path & "Test Results\" & "_WMP_URL_List-"&$sTestType&"_" & $CurrentDT & ".txt";
           
        DirCreate($TestResult_Dir_Name)
        DirCreate($Local_Path)
       
    ;~     ; Copy files to local path if they do not exist
    ;~         If Not FileExists($Local_Path & $File_Name) Then
    ;~             If $Codec == "Mpeg4" Then
    ;~                 FileCopy($Network_Path & "Mpeg4 Files\" & $File_Name, $Local_Path)
    ;~             ElseIf $Codec == "H.264" Then
    ;~                 FileCopy($Network_Path & "H.264 Files\" & $File_Name, $Local_Path)
    ;~             ElseIf $Codec == "Playlist" Then
    ;~                 FileCopy($Network_Path & "Playlist\" & $File_Name, $Local_Path)
    ;~             EndIf
    ;~             If $Codec == "External" Then
    ;~                 FileCopy($Network_Path & "External .mp4 files\" & $File_Name, $Local_Path)
    ;~             EndIf
    ;~         EndIf
           
            ; Create Test Result File
           
            If Not FileExists($TestResult_File) Then
               
                FileChangeDir($TestResult_Dir_Name)
                _FileCreate($TestResult_File)
                ;FileWrite($TestResult_File,"OS:"&$OS&@CRLF)
                FileWrite($TestResult_File, "Windows Media Player:" & $WMP_CurVersion & @CRLF)
                ;FileWrite($TestResult_File,"Web Browser:"&$Browser&@CRLF&@CRLF)
            EndIf
           
            WinActivate($WMP_Title)
           
            FileWrite($TestResult_File, "executing Test Case " & $TestCase_ID & " on " & $CurrentDT & @CRLF & @CRLF)
           
            $TestResultFiles[1]=$TestResult_File
            $TestResultFiles[0]=$TestResult_Dir_Name
            $TestResultFiles[2]=$Url_List_File
           
            Return $TestResultFiles
    EndFunc  

    Func OpenFile($File_Path,$File_Name)
        Send("^o")
        WinWaitActive("Open")
        $File_To_Enter = StringReplace($File_Path & $File_Name, "+", "{+}")
        ControlSend("Open", "", "[CLASS:Edit; INSTANCE:1]", $File_To_Enter)
        ControlClick("Open", "", "&Open")
        MsgBox(0, "Open File", $File_To_Enter, 5)
    EndFunc

    Func OpenURL($Url,$File_Name)
        Send("^u")
        WinWaitActive("Open URL")
        $URL_To_Enter = StringReplace($Url & $File_Name, "+", "{+}")
        ControlSend("Open URL", "", "[CLASS:Edit; INSTANCE:1]", $URL_To_Enter)
        ControlClick("Open URL", "Enter the URL", "OK")
        MsgBox(0, "Open URL", $URL_To_Enter, 5)
    EndFunc

    Func ExitRelaunchWMP()
        WinWaitActive($WMP_Title)
        ;MsgBox(0,"","test")
        Sleep(1000)
        Send("!{F4}")
        Sleep(3000)
        Run("C:\Program Files\Windows Media Player\wmplayer.exe");Launch Windows Media Player
        WinWaitActive($WMP_Title)
    EndFunc

    Func ExecTC()
       
        $sTestType=GetTestType()
       
        if $sTestType=="Web" Then
            ;Launch the web page;
                Global $page = _IECreate($VIQ_Page_URL)
                Send("{ENTER}")
                _IELoadWait($page)
               
        Else
            Run("C:\Program Files\Windows Media Player\wmplayer.exe");Launch Windows Media Player
        EndIf   
           
        ; Check if data file exists
        Dim $aRecords
        If Not _FileReadToArray($Test_Data_File, $aRecords) Then
            MsgBox(4096, "Error", " Error reading file to Array. Error:" & @error)
            Exit
        EndIf
       
        ; Get Current Date Time
        Global $CurrentDT = Get_Current_DT()
    ;~     $TestData_Line_Array = StringSplit(FileReadLine($Test_Data_File, 16), @TAB)
    ;~         $TestCase_ID = $TestData_Line_Array[1]
    ;~         MsgBox(0,"",$TestCase_ID)
        ; Read from test data file and execute test cases
        For $Line_Num = 2 To _FileCountLines($Test_Data_File)
            ;for $Line_Num = 16 To 17
            $TestData_Line_Array = StringSplit(FileReadLine($Test_Data_File, $Line_Num), @TAB)
            ;MsgBox(0,"",$TestData_Line_Array)
            $TestCase_ID = $TestData_Line_Array[1]
            $File_Path = $TestData_Line_Array[2]
            $File_Name = $TestData_Line_Array[3]
            $Url = $TestData_Line_Array[4]
            ;$Codec = $TestData_Line_Array[5]
            ;MsgBox(0,"",$TestCase_ID&$File_Path&$File_Name&$Url&$Codec)
            ;MsgBox(0,"",$TestCase_ID)
            $TestResultFiles=CreateTestResultFile($TestCase_ID,$File_Name,$CurrentDT)
           
            ; Open File or Open URL
            If ($File_Path == "" And $Url <> "") Then
                FileWrite($TestResultFiles[2], $TestCase_ID & ":  " & $Url & $File_Name & @CRLF & @CRLF)
                FileWrite($TestResultFiles[1], "URL is " & $Url & $File_Name & @CRLF & @CRLF)
                Switch $sTestType
                    case "Web"
                        WinActivate($VIQ_Page_URL&" - Microsoft Internet Explorer")
                        $oInput=_IEGetObjByName($page,"inputURL")
                        $oSetvalue=_IEFormElementSetValue($oInput,$url&$File_Name)
                        $oButton=_IEGetObjByName($page,"BtnPlay")
                        _IEAction($oButton,"click")
                        MsgBox(0,"",$url&$File_Name,1)       
                    Case "Baseline" or "Others"
                        WinWaitActive($WMP_Title)
                        OpenURL($Url,$File_Name)
                EndSwitch
                Sleep(5000)
               
            ElseIf ($File_Path <> "" And $Url == "") Then
                FileWrite($TestResultFiles[2], $TestCase_ID & ":  " & $File_Path & $File_Name & @CRLF & @CRLF)
                FileWrite($TestResultFiles[1], "URL is " & $File_Path & $File_Name & @CRLF & @CRLF)
                Switch $sTestType
                    case "Web"
                       
                        WinActivate($VIQ_Page_URL&" - Microsoft Internet Explorer")
                        ;MsgBox(0,"",$page)
                       
                        $oInput=_IEGetObjByName($page,"inputURL")
                        ;MsgBox(0,"",$oInput)
                        $oButton=_IEGetObjByName($page,"BtnPlay")
                        $oSetvalue=_IEFormElementSetValue($oInput,$File_Path&$File_Name)
                       
                        _IEAction($oButton,"click")
                        MsgBox(0,"",$File_Path&$File_Name,1)
                    Case "Baseline" or "Others"
                        WinWaitActive($WMP_Title)
                        OpenFile($File_Path,$File_Name)
                EndSwitch
                Sleep(1000)
            EndIf
           
            ; Check error messages;
            Sleep(1000)
            Error_W_Check()
            Screen_Capture_Save_to_File()
            Sleep(2000)
            FileWrite($TestResultFiles[1], $TestCase_ID & " is executed, see " & "screenshot saved under " & $TestResult_File_Snapshot & @CRLF & @CRLF)
        ExitRelaunchWMP()
        Next
    EndFunc
       
    Func bRunScriptClick()
       
        ExecTC()

    EndFunc   ;==>bRunScriptClick

    Func TestConsoleFormClose()
        ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE,
        ;@GUI_WINHANDLE will be either $mainwindow or $dummywindow
        If @GUI_WinHandle = $TestConsoleForm. Then
            ;MsgBox(0, "Exiting", "Exiting...",1)
            ;ExitOpt("GUIOnEventMode", 1)
            Exit
        EndIf
    EndFunc   ;==>TestConsoleFormClose

    Func iSelectDataFileChange()

        $Test_Data_File = _GUICtrlEdit_GetText($iSelectDataFile)
        Return $Test_Data_File
    EndFunc   ;==>iSelectDataFileChange

    Func SelectDataFileClick()

    EndFunc   ;==>SelectDataFileClick

    Func TestConsoleFormMaximize()

    EndFunc   ;==>TestConsoleFormMaximize
    Func TestConsoleFormMinimize()

    EndFunc   ;==>TestConsoleFormMinimize
    Func TestConsoleFormRestore()

    EndFunc   ;==>TestConsoleFormRestore


    Func RepPlayStopButton()
        Run($Test_Scripts_Path & "RepeatPlayStop.exe")
    EndFunc   ;==>RepPlayStopButton


    From Windows Media Player standalone

    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #include <file.au3>
    #include <common.au3>
    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>
    #include <GuiEdit.au3>
    #include <date.au3>
    #include <ScreenCapture.au3>
    #include <IE.au3>

    ; Define global parameters;
    ; Set Global Values
    $Network_Path="\Test Projects\WMP Plugin\Version 1.2\Test Data\"
    $Test_Scripts_Path=$Network_Path&"Test Scripts\"
    $Local_Path="C:\temp\WMP Plugin\"
    dim $TestResultFiles[3]
    $VIQ_Page_URL="http://192.168.1.2:9091//.htm"
    $WMP_Title = "Windows Media Player" ; Windows Media Player Main Screen Title
    $VIQ_Page_Title = "VIQPLUGIN ACTIVEX Test"

    Opt("GUIOnEventMode", 1)
    #Region ### START Koda GUI section ### Form=\wmp plugin\version 1.2\test data\test scripts\form1.kxf
    $TestConsoleForm. = GUICreate("Test Console", 633, 454, 190, 111)
    GUISetOnEvent($GUI_EVENT_CLOSE, "TestConsoleFormClose")
    GUISetOnEvent($GUI_EVENT_MINIMIZE, "TestConsoleFormMinimize")
    GUISetOnEvent($GUI_EVENT_MAXIMIZE, "TestConsoleFormMaximize")
    GUISetOnEvent($GUI_EVENT_RESTORE, "TestConsoleFormRestore")
    $bSelectDataFile = GUICtrlCreateButton("Browse", 440, 120, 75, 25, 0)
    GUICtrlSetOnEvent(-1, "bSelectDataFileClick")
    $SelectDataFile = GUICtrlCreateLabel("Select test data file:", 16, 120, 97, 17)
    GUICtrlSetOnEvent(-1, "SelectDataFileClick")
    Global $iSelectDataFile = GUICtrlCreateInput("iSelectDataFile", 208, 120, 209, 21)
    GUICtrlSetOnEvent(-1, "iSelectDataFileChange")

    $bRunScript. = GUICtrlCreateButton("Run", 208, 216, 75, 25, 0)
    GUICtrlSetOnEvent(-1, "bRunScriptClick")
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $bSelectDataFile
               
        EndSwitch
    WEnd

    Func bSelectDataFileClick()
        $var = FileOpenDialog("Select a file", $Network_Path, "All (*.*)")
        ;Global $Test_Data_File
        _GUICtrlEdit_SetText($iSelectDataFile, $var)

    EndFunc   ;==>bSelectDataFileClick

    Func GetTestType()
        Global $sTestType
       
        if StringInStr($Test_Data_File, "Baseline") Then
            $sTestType= "Baseline"
        elseif StringInStr($Test_Data_File, "Web") Then
            $sTestType= "Web"
        Elseif StringInStr($Test_Data_File, "Others") Then
            $sTestType= "Others"
        EndIf
        return $sTestType
    EndFunc

    Func CreateTestResultFile($TestCase_ID,$File_Name,$CurrentDT)
       
        $sTestType=GetTestType()   
       
        $TestResult_Dir_Name = $Network_Path & "Test Results\" & $sTestType&"_" & $CurrentDT & "\"
        $TestResult_File = $TestResult_Dir_Name&$sTestType&"_" & $CurrentDT & ".txt";
        $Url_List_File = $Network_Path & "_WMP_URL_List-"&$sTestType&"_" & $CurrentDT & ".txt";
           
        DirCreate($TestResult_Dir_Name)
        DirCreate($Local_Path)
       
    ;~     ; Copy files to local path if they do not exist
    ;~         If Not FileExists($Local_Path & $File_Name) Then
    ;~             If $Codec == "Mpeg4" Then
    ;~                 FileCopy($Network_Path & "Mpeg4 Files\" & $File_Name, $Local_Path)
    ;~             ElseIf $Codec == "H.264" Then
    ;~                 FileCopy($Network_Path & "H.264 Files\" & $File_Name, $Local_Path)
    ;~             ElseIf $Codec == "Playlist" Then
    ;~                 FileCopy($Network_Path & "Playlist\" & $File_Name, $Local_Path)
    ;~             EndIf
    ;~             If $Codec == "External" Then
    ;~                 FileCopy($Network_Path & "External .mp4 files\" & $File_Name, $Local_Path)
    ;~             EndIf
    ;~         EndIf
           
            ; Create Test Result File
           
            If Not FileExists($TestResult_File) Then
               
                FileChangeDir($TestResult_Dir_Name)
                _FileCreate($TestResult_File)
                ;FileWrite($TestResult_File,"OS:"&$OS&@CRLF)
                FileWrite($TestResult_File, "Windows Media Player:" & $WMP_CurVersion & @CRLF)
                ;FileWrite($TestResult_File,"Web Browser:"&$Browser&@CRLF&@CRLF)
            EndIf
           
            WinActivate($WMP_Title)
           
            FileWrite($TestResult_File, "executing Test Case " & $TestCase_ID & " on " & $CurrentDT & @CRLF & @CRLF)
           
            $TestResultFiles[1]=$TestResult_File
            $TestResultFiles[0]=$TestResult_Dir_Name
            $TestResultFiles[2]=$Url_List_File
           
            Return $TestResultFiles
    EndFunc  

    Func OpenFile($File_Path,$File_Name)
        Send("^o")
        WinWaitActive("Open")
        $File_To_Enter = StringReplace($File_Path & $File_Name, "+", "{+}")
        ControlSend("Open", "", "[CLASS:Edit; INSTANCE:1]", $File_To_Enter)
        ControlClick("Open", "", "&Open")
        MsgBox(0, "Open File", $File_To_Enter, 5)
    EndFunc

    Func OpenURL($Url,$File_Name)
        Send("^u")
        WinWaitActive("Open URL")
        $URL_To_Enter = StringReplace($Url & $File_Name, "+", "{+}")
        ControlSend("Open URL", "", "[CLASS:Edit; INSTANCE:1]", $URL_To_Enter)
        ControlClick("Open URL", "Enter the URL", "OK")
        MsgBox(0, "Open URL", $URL_To_Enter, 5)
    EndFunc

    Func ExecTC()
       
        $sTestType=GetTestType()
       
        if $sTestType=="Web" Then
            ;Launch the web page;
                $page = _IECreate($VIQ_Page_URL)
                Send("{ENTER}")
                _IELoadWait($page)
        Else
            Run("C:\Program Files\Windows Media Player\wmplayer.exe");Launch Windows Media Player
        EndIf   
           
        ; Check if data file exists
        Dim $aRecords
        If Not _FileReadToArray($Test_Data_File, $aRecords) Then
            MsgBox(4096, "Error", " Error reading file to Array. Error:" & @error)
            Exit
        EndIf
       
        ; Get Current Date Time
        Global $CurrentDT = Get_Current_DT()
    ;~     $TestData_Line_Array = StringSplit(FileReadLine($Test_Data_File, 16), @TAB)
    ;~         $TestCase_ID = $TestData_Line_Array[1]
    ;~         MsgBox(0,"",$TestCase_ID)
        ; Read from test data file and execute test cases
        For $Line_Num = 2 To _FileCountLines($Test_Data_File)
            ;for $Line_Num = 16 To 17
            $TestData_Line_Array = StringSplit(FileReadLine($Test_Data_File, $Line_Num), @TAB)
            ;MsgBox(0,"",$TestData_Line_Array)
            $TestCase_ID = $TestData_Line_Array[1]
            $File_Path = $TestData_Line_Array[2]
            $File_Name = $TestData_Line_Array[3]
            $Url = $TestData_Line_Array[4]
            ;$Codec = $TestData_Line_Array[5]
            ;MsgBox(0,"",$TestCase_ID&$File_Path&$File_Name&$Url&$Codec)
            ;MsgBox(0,"",$TestCase_ID)
            $TestResultFiles=CreateTestResultFile($TestCase_ID,$File_Name,$CurrentDT)
           
            ; Open File or Open URL
            If ($File_Path == "" And $Url <> "") Then
                FileWrite($TestResultFiles[2], $TestCase_ID & ":  " & $Url & $File_Name & @CRLF & @CRLF)
                FileWrite($TestResultFiles[1], "URL is " & $Url & $File_Name & @CRLF & @CRLF)
                Switch $sTestType
                    case "Web"
                        WinActivate($VIQ_Page_Title&" - Microsoft Internet Explorer")
                        $oInput=_IEGetObjByName($page,"inputURL")
                        $oSetvalue=_IEFormElementSetValue($oInput,$url&$File_Name)
                        $oButton=_IEGetObjByName($page,"BtnPlay")
                        _IEAction($oButton,"click")
                        MsgBox(0,"",$url&$File_Name,1)       
                    Case "Baseline" or "Others"
                        WinWaitActive($WMP_Title)
                        OpenURL($Url,$File_Name)
                EndSwitch
                Sleep(5000)
               
            ElseIf ($File_Path <> "" And $Url == "") Then
                FileWrite($TestResultFiles[2], $TestCase_ID & ":  " & $File_Path & $File_Name & @CRLF & @CRLF)
                FileWrite($TestResultFiles[1], "URL is " & $File_Path & $File_Name & @CRLF & @CRLF)
                Switch $sTestType
                    case "Web"
                        WinActivate($VIQ_Page_URL&" - Microsoft Internet Explorer")
                        $oInput=_IEGetObjByName($page,"inputURL")
                        $oSetvalue=_IEFormElementSetValue($oInput,$File_Path&$File_Name)
                        $oButton=_IEGetObjByName($page,"BtnPlay")
                        _IEAction($oButton,"click")
                        MsgBox(0,"",$File_Path&$File_Name,1)
                    Case "Baseline" or "Others"
                        WinWaitActive($WMP_Title)
                        OpenFile($File_Path,$File_Name)
                EndSwitch
                Sleep(1000)
            EndIf
           
            ; Check error messages;
            Sleep(1000)
            Error_W_Check()
            Screen_Capture_Save_to_File()
            Sleep(2000)
            FileWrite($TestResultFiles[1], $TestCase_ID & " is executed, see " & "screenshot saved under " & $TestResult_File_Snapshot & @CRLF & @CRLF)
        Next
    EndFunc
       
    Func bRunScriptClick()
       
        ExecTC()

    EndFunc   ;==>bRunScriptClick

    Func TestConsoleFormClose()
        ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE,
        ;@GUI_WINHANDLE will be either $mainwindow or $dummywindow
        If @GUI_WinHandle = $TestConsoleForm. Then
            ;MsgBox(0, "Exiting", "Exiting...",1)
            ;ExitOpt("GUIOnEventMode", 1)
            Exit
        EndIf
    EndFunc   ;==>TestConsoleFormClose

    Func iSelectDataFileChange()

        $Test_Data_File = _GUICtrlEdit_GetText($iSelectDataFile)
        Return $Test_Data_File
    EndFunc   ;==>iSelectDataFileChange

    Func SelectDataFileClick()

    EndFunc   ;==>SelectDataFileClick

    Func TestConsoleFormMaximize()

    EndFunc   ;==>TestConsoleFormMaximize
    Func TestConsoleFormMinimize()

    EndFunc   ;==>TestConsoleFormMinimize
    Func TestConsoleFormRestore()

    EndFunc   ;==>TestConsoleFormRestore


    Func RepPlayStopButton()
        Run($Test_Scripts_Path & "RepeatPlayStop.exe")
    EndFunc   ;==>RepPlayStopButton


  • how to monitor remote computer's performance counters

    2009-09-23 13:51:47

    Before monitoring, Make sure you are within the Administrators group on the remote computer;
    Start Remote Registry service on the remote computer; That is it!

    To monitor, simply go to Perfmon | Add Counter |, enter the remote computer's ip addr, then its performance counters will be retrieved for selection.

    More details please refer to http://support.microsoft.com/kb/922775

  • 很喜欢Ron Patton 对测试的定义

    2008-12-11 15:37:59

    最近在看Ron Patton的《软件测试》,买了很长时间了,前段时间才突然发现这本书(我竟然买了,差点在书城又买一本,惭愧很久不爱读书了)。

    看了几页有些爱不释手了,好像讲故事一般,通畅舒服,好久没有如此感觉了。

    他对测试定义的解释很到位,简洁,很喜欢, 指出了关键的三点:

    1) Find bugs
    2)  Find bugs as soon as possbile
    3)  Make the bugs fixed

    在自己的实践中就遇到了第三点,由于自己对所测对象的认识深度不够导致说服力不足以make some bugs fixed,最终导致客户处反馈回问题。
  • Installation Test 的注册表项检查 - 测试漏洞之四

    2008-12-11 15:17:34

    还是Media Player plugin的项目,这次问题出在installation上。

    客户反映,在某些机器上无法play stream,在有些机器上却可以。

    重现问题花了不少脑筋,因为在我们的测试机上都没此问题。最终是客户用Regmon记录了注册表项的变动,提醒了我scheme项下的一个参数导致的问题。程序的安装没有创建该scheme,只是在media player运行时会提醒用户是否要 yes always 允许播放。之所以在某些机器上可以播放,是因为用户在之前的某次操作中已经创建了该值。

    其实在最早期开始测试时,我看到并report了那个media player的warning message,当时只是从usability的角度看的问题,希望developer可以捕捉到此消息并在程序中Say YES ALWAYS. 但是我并没有意识到或深入研究过此消息对程序的其他影响,所以由于developer难以捕捉到该消息,最终同意close the defect。

    总结一下,未能捕捉到此bug的根本原因:对media player处理各file type以及customized protocol的方式流程没有熟悉,即测试没有做到深入了解所测对象。

    以后遇到类似installation issue 需要偶多注意注册表项处理了。也对regmon工具有了亲密接触,不错的工具。
  • Computer Basics 读书笔记 - Memory(2)

    2008-12-04 11:37:12

    CPU从Memory Card中读取数据所花费的时间比CPU处理数据的时间要长。-- 所以有了cache的概念

    cache: 存储CPU最经常要用的数据,CPU需要的时候可即时用上。

    如何实现:

    • 在CPU中建立一小块内存。分为:Primary or Level One cache.
      • Level One:2-64KB
    • 在内存中驻留一块,称secondary or level 2 cache,直接与CPU连接
      • Level 2:256KB- 2MB
      • 主板上有一块专用集成电路称L2 Controller,控制CPU对level 2 cache的使用

    当然现在许多高性能的CPU中,level 2 cache 直接建在其中了。

     

    RAM的速度取决于两个因素:

    bus width: RAM可同时发送的位数

    bus speed:

      
    Latency:The gap between processor's speed and a computer's bus speed.

    Virtual Memory: 将RAM中不常用的部分复制到硬盘中。


  • Load Large amount of data - 测试漏洞之三

    2008-12-04 10:48:29

    项目背景:在黑莓手机上开发某软件实现voip, 其中涉及到Address book中的contacts, 可以选择任意contact通过该软件拨打电话,并记录call log, call log中要记录通话人的名字和电话之类的。

    程序在每次启动时会根据call log中的电话去address book中查找通话人的姓名等。

    问题是如何发现的呢?自然不是我发现的,因为我疏忽了

    是在交付给客户后,我们老板要回美国,借用我们的黑莓手机,刷刷刷的往手机里导入了1000多条contacts。然后我们的程序启不来了。。。

    丢脸呢,直接给老板找出漏洞了,呵呵。其实也是程序架构的问题,如果我早点发现问题就好了。

    这也是暴露了我在performance/stress/load test 上的薄弱,要加强了~~

  • Memory Leak - 测试漏洞之二

    2008-12-04 10:32:29

    PC Resource的监控一直是我的软肋,已经有两个项目在客户处被反应memory leak,丢脸啊55。

    项目1:还是前面那个windows media player 插件的项目,客户写了一段代码,反复播放和停止streaming, 发现部分内存不释放,memroy usage 不断上升。

    当时看到问题反馈时,特后悔,为啥我就没想到呢,very typical scenario,实在不应该。而且该项目我都用Autoit自动化测试了, 虽然立即写了段脚本,复现了问题,并加入regression test了,还是有些自责,呵呵

    不过也因此学会了使用perfmon, 添加counter log, 记录并分析运行一段时间后各指标的走向。看的越多也发现自己的无知,对各指标的分析也有些beyond myself,不是科班出身又曾懒得看枯燥的计算机书,呵呵,所以对computer basics的系统学习不得不早点抓起来了。

  • CurrentDirectory Issue - 测试漏洞之一

    2008-12-04 10:06:31

    这是前端时间做的某项目交接后客户返回的一个问题。

    项目简介:开发一个windows media player的插件,可以播放mp4文件以及客户要求的其他stream。

    问题出在:每次open mp4 file后,程序会将current directory(OpenDir Registry)更改为程序的安装路径。

    复现程序有些tricky,之前测试时没有考虑该scenario,以后遇到类似OPEN,SAVE AS 之类的情况要记着了。

    具体的复现步骤:

    1. Open and play a .mp4 file
    2. From file menu select Open again. Then Press Cancel on the Open dialog
    3. From file menu select Open once again. Noticed that the directory list is changed to the application's installation root. Normally this should be the last accessed directory.

    比较疑惑的一点是:该问题只在某些测试机上出现,有的测试机上却很OK, 没有想通会是什么环境问题导致,有朋友清楚的请留言请教了哦

  • Computer Basics 读书笔记 - Memory

    2008-12-01 11:11:40

    准备恶补计算机基础知识,找到下面这个网站,开始了。请大家也推荐其它好网站哦

    http://computer.howstuffworks.com/computer-memory1.htm

    先从内存开始。

    CPU 访问硬盘的速度远远低于访问内存的速度。

    大部分类型的内存是用来暂时存储数据。

    Data process:
                      Most                             CPU
    Input/hard drive -----> RAM(Random Access Memory) ------> Cache
                                                      ------> Registry
    多数数据先进入RAM, 然后CPU将各数据存于cache,并在注册表中创建访问这些数据的一些Instruction。

    场景1 - 开机

    Computer ---> Load data from ROM

      • Perfrom POST (Power-on self -test)
      • Memory Controller: 检查内存地址的读写,确保内存条无误

          ----> Load BIOS from ROM

          ----> Load OS from the hard drive into RAM

    RAM holds the critical part of the OS.

    场景2 - 打开和关闭应用程序

    Open Application ------> Loaded into RAM

      • Initially only the essential parts
      • Other parts when needed
      • Any Files opened

    Save File/Close Application ------->

      • File written to storage device
      • File and application purged from RAM

    Memroy Leak



Open Toolbar