Package org.easymock

Examples of org.easymock.MockControl


    sessionControl.verify();
    xaResControl.verify();
  }

  public void testTransactionRollbackOnlyWithPrebound() throws Exception {
    MockControl sfControl = MockControl.createControl(SessionFactory.class);
    final SessionFactory sf = (SessionFactory) sfControl.getMock();
    MockControl sessionControl = MockControl.createControl(XASession.class);
    final XASession session = (XASession) sessionControl.getMock();

    MockControl xaResControl = MockControl.createControl(XAResource.class);
    XAResource xaRes = (XAResource) xaResControl.getMock();

    sessionControl.expectAndReturn(session.getXAResource(), xaRes);
    session.save();

    sfControl.replay();
    sessionControl.replay();
    xaResControl.replay();

    PlatformTransactionManager tm = new LocalTransactionManager(sf);
    TransactionTemplate tt = new TransactionTemplate(tm);
    UserTxSessionHolder uTx = new UserTxSessionHolder(session);
    TransactionSynchronizationManager.bindResource(sf, uTx);

    assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
    uTx.setRollbackOnly();

    try {
      tt.execute(new TransactionCallbackWithoutResult() {
        protected void doInTransactionWithoutResult(TransactionStatus status) {
          assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
          JcrTemplate template = new JcrTemplate(sf);
          template.save();
        }
      });

    }
    catch (UnexpectedRollbackException e) {
      System.out.println(e);
    }

    assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
    TransactionSynchronizationManager.unbindResource(sf);
    assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

    sfControl.verify();
    sessionControl.verify();
    xaResControl.verify();
  }
View Full Code Here


    this.directory = null;
  }

  final public void testRegisterDocumentHandler() {
    SimpleAnalyzer analyzer = new SimpleAnalyzer();
    MockControl indexFactoryControl = MockControl.createControl(IndexFactory.class);
    IndexFactory indexFactory = (IndexFactory)indexFactoryControl.getMock();

    indexFactoryControl.replay();
   
    //Indexer
    DefaultDirectoryIndexer indexer = new DefaultDirectoryIndexer(indexFactory);

    //Register a document handler
    DocumentMatching matching = new DocumentMatching() {
      public boolean match(String name) {
        return name.equals("test");
      }
    };

    //Check that the document handler test is not found
    try {
      assertNull(indexer.getDocumentHandler("test"));
      fail();
    } catch(Exception ex) {}

    //Register a document handler
    indexer.registerDocumentHandler(matching,new DocumentHandler() {
      public Document getDocument(Map description, Object object) throws IOException {
        return null;
      }

      public boolean supports(Class clazz) {
        return true;
      }
    });

    //Check that the document handler test is found now
    try {
      assertNotNull(indexer.getDocumentHandler("test"));
    } catch(Exception ex) {
      fail();
    }

    indexFactoryControl.verify();
  }
View Full Code Here

    indexFactoryControl.verify();
  }

  final public void testUnregisterDocumentHandler() {
    SimpleAnalyzer analyzer = new SimpleAnalyzer();
    MockControl indexFactoryControl = MockControl.createControl(IndexFactory.class);
    IndexFactory indexFactory = (IndexFactory)indexFactoryControl.getMock();

    indexFactoryControl.replay();
   
    //Indexer
    DefaultDirectoryIndexer indexer=new DefaultDirectoryIndexer(indexFactory);

    //Register a document handler
    DocumentMatching matching=new DocumentMatching() {
      public boolean match(String name) {
        return name.equals("test");
      }
    };

    //Register a document handler
    indexer.registerDocumentHandler(matching,new DocumentHandler() {
      public Document getDocument(Map description, Object object) throws IOException {
        return null;
      }

      public boolean supports(Class clazz) {
        return true;
      }
    });

    //Check that the document handler test is found
    try {
      assertNotNull(indexer.getDocumentHandler("test"));
    } catch(Exception ex) {
      fail();
    }

    //Unregister a document handler
    indexer.unregisterDocumentHandler(matching);

    //Check that the document handler test is not found now
    try {
      assertNull(indexer.getDocumentHandler("test"));
      fail();
    } catch(Exception ex) {}

    indexFactoryControl.verify();
  }
View Full Code Here

    indexFactoryControl.verify();
  }

  final public void testAddListener() {
    SimpleAnalyzer analyzer = new SimpleAnalyzer();
    MockControl indexFactoryControl = MockControl.createControl(IndexFactory.class);
    IndexFactory indexFactory = (IndexFactory)indexFactoryControl.getMock();

    indexFactoryControl.replay();
   
    //Indexer
    DefaultDirectoryIndexer indexer=new DefaultDirectoryIndexer(indexFactory);

    //Register a document handler
    assertEquals(indexer.getListeners().size(), 0);

    FileDocumentIndexingListener listener = new FileDocumentIndexingListenerAdapter();
    indexer.addListener(listener);
    assertEquals(indexer.getListeners().size(), 1);
    FileDocumentIndexingListener tmpListener = (FileDocumentIndexingListener)indexer.getListeners().get(0);
    assertEquals(listener, tmpListener);

    indexFactoryControl.verify();
  }
View Full Code Here

    indexFactoryControl.verify();
  }

  final public void testRemoveListener() {
    SimpleAnalyzer analyzer = new SimpleAnalyzer();
    MockControl indexFactoryControl = MockControl.createControl(IndexFactory.class);
    IndexFactory indexFactory = (IndexFactory)indexFactoryControl.getMock();

    indexFactoryControl.replay();
   
    //Indexer
    DefaultDirectoryIndexer indexer = new DefaultDirectoryIndexer(indexFactory);

    //Register a document handler
    FileDocumentIndexingListener listener = new FileDocumentIndexingListenerAdapter();
    indexer.addListener(listener);
    assertEquals(indexer.getListeners().size(), 1);
    FileDocumentIndexingListener tmpListener = (FileDocumentIndexingListener)indexer.getListeners().get(0);
    assertEquals(listener, tmpListener);

    //Unregister a document handler
    indexer.removeListener(listener);
    assertEquals(indexer.getListeners().size(), 0);

    indexFactoryControl.verify();
  }
View Full Code Here

  /*
   * Test pour void index(String)
   */
  final public void testIndexString() throws Exception {
    SimpleAnalyzer analyzer = new SimpleAnalyzer();
    MockControl indexFactoryControl = MockControl.createStrictControl(IndexFactory.class);
    IndexFactory indexFactory = (IndexFactory)indexFactoryControl.getMock();
    MockControl indexWriterControl = MockControl.createStrictControl(LuceneIndexWriter.class);
    LuceneIndexWriter indexWriter = (LuceneIndexWriter)indexWriterControl.getMock();
    MockControl listenerControl = MockControl.createStrictControl(FileDocumentIndexingListener.class);
    FileDocumentIndexingListener listener = (FileDocumentIndexingListener)listenerControl.getMock();

    //document
    Document document = new Document();
    document.add(new Field("field", "a sample", Field.Store.YES, Field.Index.TOKENIZED));
    document.add(new Field("filter", "a sample filter", Field.Store.YES, Field.Index.TOKENIZED));
    document.add(new Field("sort", "2", Field.Store.YES, Field.Index.UN_TOKENIZED));

    indexFactory.getIndexWriter();
    indexFactoryControl.setReturnValue(indexWriter, 1);
   
    listener.beforeIndexingDirectory(getBaseDirectoryToIndex());
    listenerControl.setVoidCallable(1);

    listener.beforeIndexingFile(getFileFromClasspath("test.foo"));
    listenerControl.setVoidCallable(1);

    listener.onNotAvailableHandler(getFileFromClasspath("test.foo"));
    listenerControl.setVoidCallable(1);

    listener.beforeIndexingFile(getFileFromClasspath("test.txt"));
    listenerControl.setVoidCallable(1);

    indexWriter.addDocument(document);
    indexWriterControl.setMatcher(new AbstractMatcher() {
      protected boolean argumentMatches(Object expected, Object actual) {
        if( expected instanceof Document && actual instanceof Document ) {
          return true;
        } else {
          return expected.equals(actual);
        }
      }
    });
    indexWriterControl.setVoidCallable(1);
   
    listener.afterIndexingFile(getFileFromClasspath("test.txt"));
    listenerControl.setVoidCallable(1);

    listener.afterIndexingDirectory(getBaseDirectoryToIndex());
    listenerControl.setVoidCallable(1);

    indexWriter.close();
    indexWriterControl.setVoidCallable(1);
   
    indexFactoryControl.replay();
    indexWriterControl.replay();
    listenerControl.replay();

    //Indexer
    DefaultDirectoryIndexer indexer = new DefaultDirectoryIndexer(indexFactory);
    indexer.addListener(listener);
    File baseDirectory = getBaseDirectoryToIndex();
    indexer.index(baseDirectory.getAbsolutePath());

    indexFactoryControl.verify();
    indexWriterControl.verify();
    listenerControl.verify();
  }
View Full Code Here

  /*
   * Test for void deleteDocument(int)
   */
  final public void testDeleteDocumentint() throws Exception {
    SimpleAnalyzer analyzer = new SimpleAnalyzer();
    MockControl indexFactoryControl = MockControl.createStrictControl(IndexFactory.class);
    IndexFactory indexFactory = (IndexFactory)indexFactoryControl.getMock();
    MockControl indexReaderControl = MockControl.createStrictControl(LuceneIndexReader.class);
    LuceneIndexReader indexReader = (LuceneIndexReader)indexReaderControl.getMock();

    indexFactory.getIndexReader();
    indexFactoryControl.setReturnValue(indexReader, 1);
   
    indexReader.deleteDocument(0);
    indexReaderControl.setVoidCallable(1);
   
    indexReader.close();
    indexReaderControl.setVoidCallable(1);
   
    indexFactoryControl.replay();
    indexReaderControl.replay();
   
    //Lucene template
    LuceneIndexTemplate template = new DefaultLuceneIndexTemplate(indexFactory, analyzer);
    template.deleteDocument(0);

    indexFactoryControl.verify();
    indexReaderControl.verify();
  }
View Full Code Here

  /*
   * Test for void index(String) with document handler registered
   */
  final public void testIndexStringDocumentHandler() throws Exception {
    SimpleAnalyzer analyzer = new SimpleAnalyzer();
    MockControl indexFactoryControl = MockControl.createStrictControl(IndexFactory.class);
    IndexFactory indexFactory = (IndexFactory)indexFactoryControl.getMock();
    MockControl indexWriterControl = MockControl.createStrictControl(LuceneIndexWriter.class);
    LuceneIndexWriter indexWriter = (LuceneIndexWriter)indexWriterControl.getMock();
    MockControl listenerControl = MockControl.createStrictControl(FileDocumentIndexingListener.class);
    FileDocumentIndexingListener listener = (FileDocumentIndexingListener)listenerControl.getMock();

    //document
    Document document = new Document();
    document.add(new Field("field", "a sample", Field.Store.YES, Field.Index.TOKENIZED));
    document.add(new Field("filter", "a sample filter", Field.Store.YES, Field.Index.TOKENIZED));
    document.add(new Field("sort", "2", Field.Store.YES, Field.Index.UN_TOKENIZED));

    indexFactory.getIndexWriter();
    indexFactoryControl.setReturnValue(indexWriter, 1);
   
    listener.beforeIndexingDirectory(getBaseDirectoryToIndex());
    listenerControl.setVoidCallable(1);

    listener.beforeIndexingFile(getFileFromClasspath("test.foo"));
    listenerControl.setVoidCallable(1);

    indexWriter.addDocument(document);
    indexWriterControl.setMatcher(new AbstractMatcher() {
      protected boolean argumentMatches(Object expected, Object actual) {
        if( expected instanceof Document && actual instanceof Document ) {
          return true;
        } else {
          return expected.equals(actual);
        }
      }
    });
    indexWriterControl.setVoidCallable(1);
   
    listener.afterIndexingFile(getFileFromClasspath("test.foo"));
    listenerControl.setVoidCallable(1);

    listener.beforeIndexingFile(getFileFromClasspath("test.txt"));
    listenerControl.setVoidCallable(1);

    indexWriter.addDocument(document);
    indexWriterControl.setVoidCallable(1);
   
    listener.afterIndexingFile(getFileFromClasspath("test.txt"));
    listenerControl.setVoidCallable(1);

    listener.afterIndexingDirectory(getBaseDirectoryToIndex());
    listenerControl.setVoidCallable(1);

    indexWriter.close();
    indexWriterControl.setVoidCallable(1);
   
    indexFactoryControl.replay();
    indexWriterControl.replay();
    listenerControl.replay();

    //Indexer
    DefaultDirectoryIndexer indexer = new DefaultDirectoryIndexer(indexFactory);
    indexer.registerDocumentHandler(new ExtensionDocumentMatching("foo"),
                    new TextDocumentHandler());
    indexer.addListener(listener);
    File baseDirectory = getBaseDirectoryToIndex();
    indexer.index(baseDirectory.getAbsolutePath());

    indexFactoryControl.verify();
    indexWriterControl.verify();
    listenerControl.verify();
  }
View Full Code Here

  /*
   * Test for void deleteDocument(Term)
   */
  final public void testDeleteDocumentTerm() throws Exception {
    SimpleAnalyzer analyzer = new SimpleAnalyzer();
    MockControl indexFactoryControl = MockControl.createStrictControl(IndexFactory.class);
    IndexFactory indexFactory = (IndexFactory)indexFactoryControl.getMock();
    MockControl indexReaderControl = MockControl.createStrictControl(LuceneIndexReader.class);
    LuceneIndexReader indexReader = (LuceneIndexReader)indexReaderControl.getMock();

    indexFactory.getIndexReader();
    indexFactoryControl.setReturnValue(indexReader, 1);
   
    indexReader.deleteDocuments(new Term("field","lucene"));
    indexReaderControl.setReturnValue(1, 1);
   
    indexReader.close();
    indexReaderControl.setVoidCallable(1);
   
    indexFactoryControl.replay();
    indexReaderControl.replay();
   
    //Lucene template
    LuceneIndexTemplate template = new DefaultLuceneIndexTemplate(indexFactory, analyzer);
    template.deleteDocuments(new Term("field","lucene"));

    indexFactoryControl.verify();
    indexReaderControl.verify();
  }
View Full Code Here

  /*
   * Test pour void index(String) with document handler registered
   */
  final public void testIndexStringListener() throws Exception {
    SimpleAnalyzer analyzer = new SimpleAnalyzer();
    MockControl indexFactoryControl = MockControl.createStrictControl(IndexFactory.class);
    IndexFactory indexFactory = (IndexFactory)indexFactoryControl.getMock();
    MockControl indexWriterControl = MockControl.createStrictControl(LuceneIndexWriter.class);
    LuceneIndexWriter indexWriter = (LuceneIndexWriter)indexWriterControl.getMock();
    MockControl listener1Control = MockControl.createStrictControl(FileDocumentIndexingListener.class);
    FileDocumentIndexingListener listener1 = (FileDocumentIndexingListener)listener1Control.getMock();
    MockControl listener2Control = MockControl.createStrictControl(FileDocumentIndexingListener.class);
    FileDocumentIndexingListener listener2 = (FileDocumentIndexingListener)listener2Control.getMock();

    //document
    Document document = new Document();
    document.add(new Field("field", "a sample", Field.Store.YES, Field.Index.TOKENIZED));
    document.add(new Field("filter", "a sample filter", Field.Store.YES, Field.Index.TOKENIZED));
    document.add(new Field("sort", "2", Field.Store.YES, Field.Index.UN_TOKENIZED));

    indexFactory.getIndexWriter();
    indexFactoryControl.setReturnValue(indexWriter, 1);
   
    listener1.beforeIndexingDirectory(getBaseDirectoryToIndex());
    listener1Control.setVoidCallable(1);

    listener2.beforeIndexingDirectory(getBaseDirectoryToIndex());
    listener2Control.setVoidCallable(1);

    listener1.beforeIndexingFile(getFileFromClasspath("test.foo"));
    listener1Control.setVoidCallable(1);

    listener2.beforeIndexingFile(getFileFromClasspath("test.foo"));
    listener2Control.setVoidCallable(1);

    listener1.onNotAvailableHandler(getFileFromClasspath("test.foo"));
    listener1Control.setVoidCallable(1);

    listener2.onNotAvailableHandler(getFileFromClasspath("test.foo"));
    listener2Control.setVoidCallable(1);

    listener1.beforeIndexingFile(getFileFromClasspath("test.txt"));
    listener1Control.setVoidCallable(1);

    listener2.beforeIndexingFile(getFileFromClasspath("test.txt"));
    listener2Control.setVoidCallable(1);

    indexWriter.addDocument(document);
    indexWriterControl.setMatcher(new AbstractMatcher() {
      protected boolean argumentMatches(Object expected, Object actual) {
        if( expected instanceof Document && actual instanceof Document ) {
          return true;
        } else {
          return expected.equals(actual);
        }
      }
    });
    indexWriterControl.setVoidCallable(1);
   
    listener1.afterIndexingFile(getFileFromClasspath("test.txt"));
    listener1Control.setVoidCallable(1);

    listener2.afterIndexingFile(getFileFromClasspath("test.txt"));
    listener2Control.setVoidCallable(1);

    listener1.afterIndexingDirectory(getBaseDirectoryToIndex());
    listener1Control.setVoidCallable(1);

    listener2.afterIndexingDirectory(getBaseDirectoryToIndex());
    listener2Control.setVoidCallable(1);

    indexWriter.close();
    indexWriterControl.setVoidCallable(1);
   
    indexFactoryControl.replay();
    indexWriterControl.replay();
    listener1Control.replay();
    listener2Control.replay();

    //Indexer
    DefaultDirectoryIndexer indexer = new DefaultDirectoryIndexer(indexFactory);
    indexer.addListener(listener1);
    indexer.addListener(listener2);
    File baseDirectory = getBaseDirectoryToIndex();
    indexer.index(baseDirectory.getAbsolutePath());

    indexFactoryControl.verify();
    indexWriterControl.verify();
    listener1Control.verify();
    listener2Control.verify();
  }
View Full Code Here

TOP

Related Classes of org.easymock.MockControl

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.