/* JFox, the OpenSource J2EE Application Server
 *
 * Copyright (C) 2002 huihoo.com
 * Distributable under GNU LGPL license
 * See the GNU Lesser General Public License for more details.
 */

package org.jfox.mx.test;

import junit.framework.TestCase;
import org.jfox.mx.Mxable;
import org.jfox.mx.MxServer;
import org.jfox.mx.ObjectName;

/**
 * 
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class MxTestCase extends TestCase {
  private Mxable mxObject = null;
  private MxServer mx = MxServer.getInstance();
  private ObjectName objectName = null;

  protected void setUp() throws Exception {
    objectName = new ObjectName(":class=org.jfox.mx.MxTest");
    if(!mx.isRegistered(objectName)) {
      mxObject = mx.createMX("org.jfox.mx.test.MxTest", objectName);
    }
    else {
      mxObject = mx.getMxObject(objectName);
    }
  }

  public void testMxDomain() {
    assertTrue(mx.getDefaultDomain().equals("Default"));
  }

  public void testCreate() {
    try {
      assertNotNull(mxObject);
      assertTrue(mx.getMxCount() == 1);
    }
    catch(Exception e){
      fail(e.toString());
    }
  }

  public void testInvoke(){
    try {
      String result = (String)mx.invoke(objectName,"mx_getWord",null,null);
      assertTrue(result.equals("Hello,World"));
    }
    catch(Exception e){
      fail(e.toString());
    }
  }

  public void testSource(){
    try {
      Test test= (Test)mx.invoke(objectName,"getSource");
      assertTrue(test.getWord().equals("Hello,World"));
    }
    catch(Exception e){
      fail(e.toString());
    }

  }

  public void testUnregister(){
    try {
      mx.unregisterMBean(objectName);
      assertTrue(mx.getMxCount() == 0);
    }
    catch(Exception e){
      fail(e.toString());
    }
  }

  public static void main(String[] args) {
    junit.textui.TestRunner.run(MxTestCase.class);
  }
}