友善交流技术...

发布新日志

  • PHP 正则表达式

    2011-07-28 18:22:15

      //【CMS-HLD_0.001】userJoin
         $cont=$cmsapi->userJoin_001($YyhyID,$pinCode);
         echo "-------返回的数据----</br>";
         echo $cont.'</br>';
         preg_match('/tempconfernceid\":"([0-9])+"/',$cont,$arrlshyid);  //匹配数据
         preg_match('/tempuserid":"([0-9])+"/',$cont,$arrlsuserid);
          print_r($arrlshyid);
          print_r($arrlsuserid);
         $LshyID=substr($arrlshyid['0'],18,9);  //返回 的ID,
         $LsuserID=substr($arrlsuserid['0'],13,9);  //返回 ID,

     //备注:preg_match 匹配相关的信息; substr 截取想要的字符串.

  • PHP 监控memcache

    2011-07-28 18:17:33

    PHP 学习监控memcache 的代码(刚刚学习PHP,感觉功能挺强大的)

    下面的代码是监控了memcache几个参数 ,通过入数据库,然后再次展示在页面中的代码.具体的代码如下所示:

    <html>
    <title>Memcache montior ...</title>
    <h1>Memcache Monitor perform. data!</br></h1>

    <?php

    //memcache t����
    include 'cmysql.php';
    $memcache = new Memcache();
    $memcache->connect("10.1.71.102","11211");
    $arrConn=$memcache->getstats();


    $curr_connections = $arrConn["curr_connections"];
    $total_connections = $arrConn["total_connections"];
    $get_hits = $arrConn["get_hits"];
    $get_misses = $arrConn["get_misses"];
    $cmd_get = $arrConn["cmd_get"];
    $cmd_set = $arrConn["cmd_set"];
    $memcache->close();

    //��ݿ�,ȡ��������
    $mnow = time();

    $sql = "insert into memcache(curr_connections,total_connections,get_hits,get_misses,cmd_get,cmd_set,mdate)values($curr_connections,$total_connections,$get_hits,$get_misses,$cmd_get,$cmd_set,$mnow)";
    $mysql = new Mysql();
    $mysql->mysql_conn();
    $mysql->mysql_insert($sql);
    $mysql->mysql_close();


    //����ݿ�ȡ��ݣ�Ȼ��չʾ��4
    $select_sql = "select * from memcache order by mdate limit 10";
    $mysql = new Mysql();
    $mysql->mysql_conn();
    $arrData = $mysql->mysql_select($select_sql);

     

    echo "<table border=\"20\" color=red>";    
          echo "<tr>";     
          echo "<td>curr_connections</td>";
          echo "<td>total_connections</td>";
          echo "<td>cmd_get</td>";
          echo "<td>cmd_set</td>";
          echo "<td>get_hits</td>";
          echo "<td>get_misses</td>";
          echo "<td>delete_hits</td>";
          echo "<td>threads</td>";
          echo "</font>";
          
     foreach ( $arrData as $k => $v ) {
      echo"<tr>
      <td>{$v['curr_connections']}</td>
      <td>{$v['total_connections']}</td>
      <td>{$v['cmd_get']}</td>
      <td>{$v['cmd_set']}</td>
      <td>{$v['get_hits']}</td>
      <td>{$v['get_misses']}</td>
      <td>{$v['delete_hits']}</td>
      <td>{$v['threads']}</td>
      </tr>";
     }
     echo"<table>";

     $mysql->mysql_close();


    ?>
    </html>

     

  • PHP CURL API 接口测试

    2011-07-28 18:11:50

    class cmsApi {

    //初始化url参数
     function  urlinit($app_url,$url){
      $ch = curl_init();
      curl_setopt ($ch, CURLOPT_URL, $app_url.$url); //REST地址 GET  
      curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
      curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 15); 
      return $ch ;
     }
     
    //JSON 数据格式 
     function xml_to_json($source) { 
     if(is_file($source)){             //传的是文件,还是xml的string的判断 
      $xml_array=simplexml_load_file($source); 
     }else{ 
      $xml_array=simplexml_load_string($source); 
     } 
     $json = json_encode($xml_array);  //php5,以及以上,如果是更早版本,请下载JSON.php 
     return $json; 
       }
     
    //【CMS-HLD_0.011】获取billingcode
     function getBillingcode_011(){
         // Get method
        //$app_url = "http://IP:8081/";
        $app_url = "http://IP:9081/";
        $url ='conferenceReservManager/getBillingcode/' ;
      //print_r($post_data);
         $ch = $this->urlinit($app_url,$url);
      curl_setopt($ch, CURLOPT_GET, 1);   
      $file_contents = curl_exec($ch);//获得返回值
      curl_close($ch);  
      return $file_contents; 
     }
     

    // getPasscode_012
     function  getPasscode_012($num){
      // Get method
        $app_url = "http://ip:9081/";
        $url = 'conferenceReservManager/getPasscode/'.$num;
        //echo $url ;   
      //初始化URL
         $ch = $this->urlinit($app_url,$url);
        
      curl_setopt($ch, CURLOPT_GET, 1);    
      $file_contents = curl_exec($ch);//获得返回值
      curl_close($ch);  
      return $file_contents; 
     }
    //获取acquirePin
     function  acquirePin_014($billingcode){
           // Get method
        $app_url = "http://ip:9081/";
        $url = 'conferenceReservManager/acquirePin/'.$billingcode;  

      //初始化URL
         $ch = $this->urlinit($app_url,$url);  
      curl_setopt($ch, CURLOPT_GET, 1);   
      $file_contents = curl_exec($ch);//获得返回值
      curl_close($ch);  
      return $file_contents; 
     }
     
    //reservConference
     function reservConference_007($billingcode,$pcode1,$pcode2){
      
       // Get method
        $app_url = 'http://ip:9081/cmsrest/rs/';
        $url = 'conferenceReservManager/reservConference/'; 
        
        $post_data ='{"xmlTemplateName":"/root/sme.xml","userId":"","applicationId":"SME-MEETING","value":{"2":"3,4","7":"23"},
        "conference":{"valid":0,"noticetime":0,"conferenceuservalues":{},"callout":false,
        "starttime":"2011-07-29T18:57:22Z","reservtime":"2011-07-26T18:57:22Z","conferencedesc":"","confscale":4,
        "billingcode":"'.$billingcode.'","password":"","product":{"productroles":{},"productid":3,"conferences":{},
        "conferenceservices":{}},"conferenceminutes":120,"pin":false,"roles":"4,5","conferencename":" tester 2011/7/30 01",
        "language":2,"realreserve":true},"pcode1":"'.$pcode1.'","pcode2":"'.$pcode2.'"}';
        
      //初始化URL
         $ch = $this->urlinit($app_url,$url);      
         $datatype ='application/json';
        $accepttype='text/plain';
         curl_setopt($ch, CURLOPT_POST,1); //设置POST方式
         curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //发送的post值,目前已经放入url
         curl_setopt($ch, CURLOPT_HTTPHEADER, array (
              "Content-Type: ".$datatype."; charset=utf-8",
              "accept: ".$accepttype."; charset=utf-8"));  
      $file_contents = curl_exec($ch);//获得返回值
      curl_close($ch);  
      return $file_contents; 
     }
     
     
    //【CMS-HLD_0.001】userJoin
     function userJoin_001($LshyID,$pinCode){
           // Get method
         $app_url = "http://IP:9081/";
        $url = 'conferenceBusiness/userJoin/SME-MEETING/'.$LshyID;
        
         $post_data=$this->xml_to_json('<userDTO><clientType>2</clientType><domain>1</domain><ipaddr>192.168.12.155</ipaddr>
        <isowner>true</isowner><pinCode>'.$pinCode.'</pinCode><rolemap><entry><key>4</key><value/></entry><entry><key>7</key><value/>
        </entry><entry><key>8</key><value/></entry><entry><key>9</key><value/></entry><entry><key>10</key><value/></entry><entry>
        <key>11</key><value/></entry><entry><key>12</key><value/></entry><entry><key>13</key><value/></entry><entry><key>14</key><value/>
        </entry></rolemap><tempuserid>4</tempuserid><userName>liming</userName><userStatus>0</userStatus></userDTO>');
          
         //初始化URL
         $ch = $this->urlinit($app_url,$url);
      $datatype='application/json';
      $accepttype='application/json';
      //设置POST方式
       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //发送的post值,目前已经放入url
       curl_setopt($ch, CURLOPT_HTTPHEADER, array (
              "Content-Type: ".$datatype."; charset=utf-8",
              "accept: ".$accepttype."; charset=utf-8"));
           
      $file_contents = curl_exec($ch);//获得返回值
      curl_close($ch);  
      return $file_contents; 
     }
     }

     备注几点:

    1. GET 和 POST 提交是有区别的:

       其中get 请求的数据一般比较小的.POST提交的方法,可以提交比较大的数据量

    2. POST 方法提交

       注意数据格式和接受数据格式:如果错了,提交也会失败的.

     

  • Junit 包含参数列表的测试用例一起执行

    2011-01-26 18:02:38

    ============包含参数列表的 多个测试用例一起执行=====================
    package fnew;
    import org.junit.runner.RunWith;
    import org.junit.runners.Suite;
    import org.junit.runners.Suite.SuiteClasses;
    /**
      * @描述:util包的测试套件
     */
    @RunWith(Suite.class)
    @SuiteClasses
    ({FmoneyTest.class,DivTest.class})   //是执行的测试类,用逗号分开不同的测试类.
    public class Util4Suite {
     
    }
    =======================================================
    package fnew;
    import junit.framework.Test;
    import junit.framework.TestSuite;
    public class AllTests {
     public static Test suite() {
      TestSuite suite = new TestSuite("Test for fnew");
      //$JUnit-BEGIN$
      suite.addTestSuite(JianFa.class);       //要测试的类
      suite.addTestSuite(DivTest.class);      //要测试的类
      suite.addTestSuite(FmoneyTest.class);   //执行失败,提示错误,无法执行也是说这个方法不能用来执行带有虚函数的类
      //$JUnit-END$
      return suite;
     }
    }
     本测试方式 使用于没有构造方法的类 .... 本人实验已经通过了.
  • Junit

    2011-01-26 11:23:29

    /*单个测试类 进行测试*/

    import static org.junit.Assert.*;

    import org.junit.Test;
    import junit.framework.TestCase;


    public class JiaFaTest extends TestCase {
     
     @Test
     public   void testJiafa() {
      //fail("Not yet implemented");
      JiaFa t=new JiaFa();
      assertEquals(-2147483639,t.jiafa(2147483647, 10));
     }

    }

    ------------------------------------------

    增加测试suite

    import junit.framework.Test;
    import junit.framework.TestSuite;


    public class AllTests1 {

     public static Test suite() {
      TestSuite suite = new TestSuite("Test for default package");
      //$JUnit-BEGIN$
      suite.addTestSuite(ParameterTest.class);
      suite.addTestSuite(JiaFaTest.class);
      suite.addTestSuite(JianFaTest.class);
      suite.addTestSuite(DivTest.class);
      suite.addTest(AllTests.suite());
      //$JUnit-END$
      return suite;
     }

    }
    ----------------------------------------

    [被测试类


    public class JiaFa {
     public int jiafa(int a ,int b){
      return a+b;  
     }

    }

       几天的捉摸都算是明白了junit的基本用法,但是如果复杂的测试,这种代码就很可怜了.. 根本不够用的.

       还在不停的学习中...


     

  • junit 参数化

    2011-01-26 11:21:54

      
     
    import static org.junit.Assert.assertEquals;  
     
    import java.util.Arrays;  
    import java.util.Collection;   

    import junit.framework.TestCase;

    import org.junit.Test;  
    import org.junit.runner.RunWith;  
    import org.junit.runners.Parameterized;  
    import org.junit.runners.Parameterized.Parameters;   
     
    /** 
     * 参数化设置 
     *  
     * 1 测试类必须由parameterized测试运行器修饰 
     * 2 准备数据,数据的准备需要在一个方法中进行,该方法需要满足一定的要求 
     *   1)该方法必须有parameters注解修饰 
     *   2)该方法必须为public static的 
     *   3)该方法必须返回Collection类型 
     *   4)该方法的名字不作要求 
     *   5)该方法没有参数 
     *    
     *   int.class == Integer.TYPE != Integer.class 
     */ 
    // 测试运行器  
    @RunWith(Parameterized.class)  
    public class ParameterTest extends TestCase {  
        private int expeted;  
        private int input1;  
        private int input2;  
          
        @Parameters 
        @SuppressWarnings("unchecked")  
        public static Collection perpareData() {  
            Object[][] bjects = { {3,1,2}, {0,0,0}, {-4,-1,-3} };  
              
            return Arrays.asList(objects);  
        }  
          
        public ParameterTest(int expected, int input1, int input2){  
            this.expeted = expected;  
            this.input1 = input1;  
            this.input2 = input2;  
        }  
          
        @Test
         public void testAdd() {  
            Calculator cal = new Calculator();  
            assertEquals(expeted, cal.add(input1, input2));  
        }  

    ===========================================

    被测试类
    public class Calculator {

     public int add(int a,int b){
      
      int c ;
      c= a + b;
      return c;
     }
    }

Open Toolbar