Search in sources :

Example 11 with GwtIncompatible

use of com.google.common.annotations.GwtIncompatible in project guava by google.

the class CacheBuilderTest method testRemovalNotification_clear.

// QueuingRemovalListener
@GwtIncompatible
public void testRemovalNotification_clear() throws InterruptedException {
    // If a clear() happens while a computation is pending, we should not get a removal
    // notification.
    final AtomicBoolean shouldWait = new AtomicBoolean(false);
    final CountDownLatch computingLatch = new CountDownLatch(1);
    CacheLoader<String, String> computingFunction = new CacheLoader<String, String>() {

        @Override
        public String load(String key) throws InterruptedException {
            if (shouldWait.get()) {
                computingLatch.await();
            }
            return key;
        }
    };
    QueuingRemovalListener<String, String> listener = queuingRemovalListener();
    final LoadingCache<String, String> cache = CacheBuilder.newBuilder().concurrencyLevel(1).removalListener(listener).build(computingFunction);
    // seed the map, so its segment's count > 0
    cache.getUnchecked("a");
    shouldWait.set(true);
    final CountDownLatch computationStarted = new CountDownLatch(1);
    final CountDownLatch computationComplete = new CountDownLatch(1);
    new Thread(new Runnable() {

        @Override
        public void run() {
            computationStarted.countDown();
            cache.getUnchecked("b");
            computationComplete.countDown();
        }
    }).start();
    // wait for the computingEntry to be created
    computationStarted.await();
    cache.invalidateAll();
    // let the computation proceed
    computingLatch.countDown();
    // don't check cache.size() until we know the get("b") call is complete
    computationComplete.await();
    // At this point, the listener should be holding the seed value (a -> a), and the map should
    // contain the computed value (b -> b), since the clear() happened before the computation
    // completed.
    assertEquals(1, listener.size());
    RemovalNotification<String, String> notification = listener.remove();
    assertEquals("a", notification.getKey());
    assertEquals("a", notification.getValue());
    assertEquals(1, cache.size());
    assertEquals("b", cache.getUnchecked("b"));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 12 with GwtIncompatible

use of com.google.common.annotations.GwtIncompatible in project guava by google.

the class CharMatcherTest method testSmallCharMatcher.

// java.util.Random, java.util.BitSet
@GwtIncompatible
public void testSmallCharMatcher() {
    CharMatcher len1 = SmallCharMatcher.from(bitSet("#"), "#");
    CharMatcher len2 = SmallCharMatcher.from(bitSet("ab"), "ab");
    CharMatcher len3 = SmallCharMatcher.from(bitSet("abc"), "abc");
    CharMatcher len4 = SmallCharMatcher.from(bitSet("abcd"), "abcd");
    assertTrue(len1.matches('#'));
    assertFalse(len1.matches('!'));
    assertTrue(len2.matches('a'));
    assertTrue(len2.matches('b'));
    for (char c = 'c'; c < 'z'; c++) {
        assertFalse(len2.matches(c));
    }
    assertTrue(len3.matches('a'));
    assertTrue(len3.matches('b'));
    assertTrue(len3.matches('c'));
    for (char c = 'd'; c < 'z'; c++) {
        assertFalse(len3.matches(c));
    }
    assertTrue(len4.matches('a'));
    assertTrue(len4.matches('b'));
    assertTrue(len4.matches('c'));
    assertTrue(len4.matches('d'));
    for (char c = 'e'; c < 'z'; c++) {
        assertFalse(len4.matches(c));
    }
    Random rand = new Random(1234);
    for (int testCase = 0; testCase < 100; testCase++) {
        char[] chars = randomChars(rand, rand.nextInt(63) + 1);
        CharMatcher m = SmallCharMatcher.from(bitSet(chars), new String(chars));
        checkExactMatches(m, chars);
    }
}
Also used : Random(java.util.Random) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 13 with GwtIncompatible

use of com.google.common.annotations.GwtIncompatible in project guava by google.

the class EnumsTest method testNullPointerExceptions.

// NullPointerTester
@GwtIncompatible
public void testNullPointerExceptions() {
    NullPointerTester tester = new NullPointerTester();
    tester.testAllPublicStaticMethods(Enums.class);
}
Also used : NullPointerTester(com.google.common.testing.NullPointerTester) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 14 with GwtIncompatible

use of com.google.common.annotations.GwtIncompatible in project guava by google.

the class EnumsTest method testGetField.

// reflection
@GwtIncompatible
public void testGetField() {
    Field foo = Enums.getField(AnEnum.FOO);
    assertEquals("FOO", foo.getName());
    assertTrue(foo.isAnnotationPresent(ExampleAnnotation.class));
    Field bar = Enums.getField(AnEnum.BAR);
    assertEquals("BAR", bar.getName());
    assertFalse(bar.isAnnotationPresent(ExampleAnnotation.class));
}
Also used : Field(java.lang.reflect.Field) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Example 15 with GwtIncompatible

use of com.google.common.annotations.GwtIncompatible in project guava by google.

the class EnumsTest method doTestClassUnloading.

// Create a second ClassLoader and use it to get a second version of the TestEnum class.
// Run Enums.getIfPresent on that other TestEnum and then return a WeakReference containing the
// new ClassLoader. If Enums.getIfPresent does caching that prevents the shadow TestEnum
// (and therefore its ClassLoader) from being unloaded, then this WeakReference will never be
// cleared.
// weak references
@GwtIncompatible
private WeakReference<?> doTestClassUnloading() throws Exception {
    URLClassLoader shadowLoader = new URLClassLoader(getClassPathUrls(), null);
    @SuppressWarnings("unchecked") Class<TestEnum> shadowTestEnum = (Class<TestEnum>) Class.forName(TestEnum.class.getName(), false, shadowLoader);
    assertNotSame(shadowTestEnum, TestEnum.class);
    // We can't write Set<TestEnum> because that is a Set of the TestEnum from the original
    // ClassLoader.
    Set<Object> shadowConstants = new HashSet<>();
    for (TestEnum constant : TestEnum.values()) {
        Optional<TestEnum> result = Enums.getIfPresent(shadowTestEnum, constant.name());
        assertThat(result).isPresent();
        shadowConstants.add(result.get());
    }
    assertEquals(ImmutableSet.<Object>copyOf(shadowTestEnum.getEnumConstants()), shadowConstants);
    Optional<TestEnum> result = Enums.getIfPresent(shadowTestEnum, "blibby");
    assertThat(result).isAbsent();
    return new WeakReference<>(shadowLoader);
}
Also used : URLClassLoader(java.net.URLClassLoader) WeakReference(java.lang.ref.WeakReference) HashSet(java.util.HashSet) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Aggregations

GwtIncompatible (com.google.common.annotations.GwtIncompatible)361 NullPointerTester (com.google.common.testing.NullPointerTester)105 TestSuite (junit.framework.TestSuite)54 BigInteger (java.math.BigInteger)40 RoundingMode (java.math.RoundingMode)39 CountDownLatch (java.util.concurrent.CountDownLatch)25 ExecutorService (java.util.concurrent.ExecutorService)18 CancellationException (java.util.concurrent.CancellationException)17 Random (java.util.Random)16 ListTestSuiteBuilder (com.google.common.collect.testing.ListTestSuiteBuilder)15 ExecutionException (java.util.concurrent.ExecutionException)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 IOException (java.io.IOException)14 BigDecimal (java.math.BigDecimal)14 TestStringSetGenerator (com.google.common.collect.testing.TestStringSetGenerator)11 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)11 TimeoutException (java.util.concurrent.TimeoutException)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 EqualsTester (com.google.common.testing.EqualsTester)10 List (java.util.List)10