@Test
  public void testAddingAndDeletingFactory() {
    URLProtocolManager manager;
    Red5HandlerFactory factory;
    IURLProtocolHandlerFactory oldFactory;

    manager = URLProtocolManager.getManager();
    assertTrue("could not find the protocol manager", manager != null);

    factory = Red5HandlerFactory.getFactory(mRed5URLProtocol);

    // register but make sure the old one is returned.
    oldFactory = manager.registerFactory(mRed5URLProtocol, factory);
    assertTrue("did not return old factory", oldFactory == factory);

    // register a null handler
    oldFactory = manager.registerFactory(mRed5URLProtocol, null);
    assertTrue("did not find old handler", oldFactory != null);

    // Make sure that getting a file with this protocol fails,
    // but fails without an error.
    String url = mRed5URLProtocol + ":nonExistentProtocol";
    int retval = -1;
    FfmpegIOHandle ffmpegHandle = new FfmpegIOHandle();
    retval = Helper.url_open(ffmpegHandle, url, IURLProtocolHandler.URL_RDONLY_MODE);
    assertTrue("Ffmpeg found the stream??: " + url, retval == -1);
  }
  @Test
  public void testFindProtocolHandler() {
    IRTMPEventIOHandler ioHandler =
        new IRTMPEventIOHandler() {
          public Red5Message read() {
            return null;
          }

          public void write(Red5Message msg) {}
        };
    IRTMPEventIOHandler oldHandler = null;
    IURLProtocolHandler handler = null;
    int retval = -1;
    URLProtocolManager manager = null;
    ;
    Red5HandlerFactory factory = null;
    IURLProtocolHandlerFactory oldFactory = null;
    FfmpegIOHandle ffmpegHandle = new FfmpegIOHandle();

    manager = URLProtocolManager.getManager();
    assertTrue("could not find the protocol manager", manager != null);

    factory = new Red5HandlerFactory();
    oldFactory = manager.registerFactory(mRed5URLProtocol, factory);
    assertTrue("no previously registered factory", oldFactory == null);

    ISimpleMediaFile file = new SimpleMediaFile();
    file.setURL(mRed5URLProtocol + ":input");
    oldHandler = factory.registerStream(ioHandler, file);
    assertTrue("a buffer already registered", oldHandler == null);

    String goodUrl = mRed5URLProtocol + ":" + "input";
    String badUrl = mRed5URLProtocol + ":" + "shouldNotExist";

    handler = factory.getHandler(mRed5URLProtocol, goodUrl, IURLProtocolHandler.URL_RDONLY_MODE);
    assertTrue("Did not find handler for registered stream", handler != null);

    handler = factory.getHandler(mRed5URLProtocol, badUrl, IURLProtocolHandler.URL_RDONLY_MODE);
    assertTrue("Found unexpected handler for stream", handler == null);

    // Now, test that we can successfully trick Ffmpeg into opening the
    // file.
    retval = Helper.url_open(ffmpegHandle, goodUrl, IURLProtocolHandler.URL_RDONLY_MODE);
    assertTrue("Ffmpeg could not find the stream: " + goodUrl, retval >= 0);

    retval = Helper.url_open(ffmpegHandle, badUrl, IURLProtocolHandler.URL_RDONLY_MODE);
    assertTrue("Ffmpeg found the unexpected stream: " + badUrl, retval == -1);

    // and make sure we can't open the bad URL.

    // now delete the stream
    oldHandler = factory.deleteStream("input");
    assertTrue("did not get a buffer back when deleting a stream", oldHandler != null);
    assertTrue("did not get readbuffer back", oldHandler == ioHandler);

    handler = factory.getHandler(mRed5URLProtocol, goodUrl, IURLProtocolHandler.URL_RDONLY_MODE);
    assertTrue("found unexpected handler for delete stream", handler == null);
  }