PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lucene/lucene-v3/src/test/java/org/infinispan/lucene/cachestore/IndexCacheLoaderTest.java

https://bitbucket.org/cprenzberg/infinispan
Java | 112 lines | 69 code | 13 blank | 30 comment | 2 complexity | 1c2dbfa4897aa71e0f76da69de1138d8 MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
  4. * as indicated by the @author tags. All rights reserved.
  5. * See the copyright.txt in the distribution for a
  6. * full listing of individual contributors.
  7. *
  8. * This copyrighted material is made available to anyone wishing to use,
  9. * modify, copy, or redistribute it subject to the terms and conditions
  10. * of the GNU Lesser General Public License, v. 2.1.
  11. * This program is distributed in the hope that it will be useful, but WITHOUT A
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  13. * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  14. * You should have received a copy of the GNU Lesser General Public License,
  15. * v.2.1 along with this distribution; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. * MA 02110-1301, USA.
  18. */
  19. package org.infinispan.lucene.cachestore;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import org.apache.lucene.store.Directory;
  23. import org.infinispan.Cache;
  24. import org.infinispan.configuration.cache.ConfigurationBuilder;
  25. import org.infinispan.lucene.cachestore.LuceneCacheLoader;
  26. import org.infinispan.lucene.cachestore.LuceneCacheLoaderConfig;
  27. import org.infinispan.lucene.directory.DirectoryBuilder;
  28. import org.infinispan.manager.EmbeddedCacheManager;
  29. import org.infinispan.test.CacheManagerCallable;
  30. import org.infinispan.test.TestingUtil;
  31. import org.infinispan.test.fwk.TestCacheManagerFactory;
  32. import org.testng.annotations.AfterMethod;
  33. import org.testng.annotations.BeforeMethod;
  34. import org.testng.annotations.Test;
  35. /**
  36. * Verify we can write to a FSDirectory, and when using it via the {@link LuceneCacheLoader}
  37. * we can find the same contents as by searching it directly.
  38. * This implicitly verifies configuration settings passed it, as it won't work if the CacheLoader
  39. * is unable to find the specific index path.
  40. *
  41. * @author Sanne Grinovero
  42. * @since 5.2
  43. */
  44. @Test(groups = "functional", testName = "lucene.cachestore.IndexCacheLoaderTest")
  45. public class IndexCacheLoaderTest {
  46. private static final int SCALE = 600;
  47. protected final String parentDir = ".";
  48. protected File rootDir = null;
  49. @BeforeMethod(alwaysRun = true)
  50. public void setUp() {
  51. rootDir = TestHelper.createRootDir(parentDir, getIndexPathName());
  52. }
  53. /**
  54. * @return a unique name for this test, where we store indexes
  55. */
  56. protected String getIndexPathName() {
  57. return this.getClass().getSimpleName();
  58. }
  59. @AfterMethod(alwaysRun = true)
  60. public void tearDown() {
  61. if(rootDir != null) {
  62. TestingUtil.recursiveFileRemove(rootDir);
  63. }
  64. }
  65. @Test
  66. public void testReadExistingIndex() throws IOException {
  67. TestHelper.createIndex(rootDir, "index-A", 10 * SCALE, true);
  68. TestHelper.createIndex(rootDir, "index-B", 20 * SCALE, false);
  69. TestHelper.verifyIndex(rootDir, "index-A", 10 * SCALE, true);
  70. verifyDirectory(rootDir, "index-A", 10 * SCALE, true);
  71. TestHelper.verifyIndex(rootDir, "index-B", 20 * SCALE, false);
  72. verifyDirectory(rootDir, "index-B", 20 * SCALE, false);
  73. }
  74. private void verifyDirectory (final File rootDir, final String indexName, final int termsAdded, final boolean inverted)
  75. throws IOException {
  76. final EmbeddedCacheManager cacheManager = initializeInfinispan(rootDir);
  77. TestingUtil.withCacheManager(new CacheManagerCallable(cacheManager) {
  78. public void call() {
  79. Cache<Object, Object> cache = cacheManager.getCache();
  80. Directory directory = DirectoryBuilder.newDirectoryInstance(cache, cache, cache, indexName).create();
  81. try {
  82. TestHelper.verifyOnDirectory(directory, termsAdded, inverted);
  83. } catch (IOException e) {
  84. throw new RuntimeException(e);
  85. }
  86. }
  87. });
  88. }
  89. protected EmbeddedCacheManager initializeInfinispan(File rootDir) {
  90. ConfigurationBuilder builder = new ConfigurationBuilder();
  91. builder
  92. .loaders()
  93. .addLoader()
  94. .cacheLoader( new LuceneCacheLoader() )
  95. .addProperty(LuceneCacheLoaderConfig.LOCATION_OPTION, rootDir.getAbsolutePath())
  96. .addProperty(LuceneCacheLoaderConfig.AUTO_CHUNK_SIZE_OPTION, "1024");
  97. return TestCacheManagerFactory.createCacheManager(builder);
  98. }
  99. }