alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (PromiseTest.java)

This example Play Framework source code file (PromiseTest.java) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

expectedexception, lib, library, long, play, play framework, promise, promisetest, rule, runtimeexception, string, test

The PromiseTest.java Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play;

import java.util.Arrays;
import java.util.List;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import play.libs.F;

import static org.fest.assertions.Assertions.assertThat;

public class PromiseTest {

    // timeout
    private Long t = 1000L;

    @Test
    public void testSupertypeMap() {
        F.Promise<Integer> a = F.Promise.pure(1);

        F.Promise<String> b = a.map(new F.Function<Object, String>() {
          public String apply(Object o) {
            return o.toString();
          }
        });
        assertThat(b.get(t)).isEqualTo("1");
    }

    @Test
    public void testSupertypeFlatMap() {
        F.Promise<Integer> a = F.Promise.pure(1);

        F.Promise<String> b = a.flatMap(new F.Function<Object, F.Promise<String>>() {
          public F.Promise<String> apply(Object o) {
            return F.Promise.pure(o.toString());
          }
        });
        assertThat(b.get(t)).isEqualTo("1");
    }

    @Test
    public void testEmptyPromise() {
        F.RedeemablePromise<Integer> a = F.RedeemablePromise.empty();

        F.Promise<String> b = a.map(new F.Function<Integer, String>() {
          public String apply(Integer i) {
            return i.toString();
          }
        });

        a.success(1);

        assertThat(a.get(t)).isEqualTo(1);
        assertThat(b.get(t)).isEqualTo("1");
    }

    @Rule
    public ExpectedException exception = ExpectedException.none();

    @Test
    public void testFailPromise() {
        exception.expect(RuntimeException.class);
        F.RedeemablePromise<Integer> a = F.RedeemablePromise.empty();

        a.failure(new RuntimeException("test"));

        a.get(t);
    }

    @Test
    public void testDualSuccessPromise() {
        exception.expect(IllegalStateException.class);
        F.RedeemablePromise<Integer> a = F.RedeemablePromise.empty();

        a.success(1);
        a.success(2);
    }

    @Test
    public void testCompleteWithPromise() {
        F.RedeemablePromise<Integer> a = F.RedeemablePromise.empty();
        F.RedeemablePromise<Integer> b = F.RedeemablePromise.empty();

        a.success(1);

        F.Promise<Void> c = b.completeWith(a);
        assertThat(c.get(t)).isEqualTo(null);
        assertThat(b.get(t)).isEqualTo(1);



        // Complete a second time
        F.Promise<Void> d = b.completeWith(a);

        // And we should get an exception !
        exception.expect(IllegalStateException.class);
        exception.expectMessage("Promise already completed.");
        d.get(t);
    }

    @Test
    public void testTryCompleteWithPromise() {
        F.RedeemablePromise<Integer> a = F.RedeemablePromise.empty();
        F.RedeemablePromise<Integer> b = F.RedeemablePromise.empty();

        a.success(1);

        // Assertions not placed on bottom on this method to enforce
        // the Promise to be completed and avoid raceconditions
        F.Promise<Boolean> c = b.tryCompleteWith(a);
        assertThat(c.get(t)).isEqualTo(true);
        F.Promise<Boolean> d = b.tryCompleteWith(a);
        assertThat(d.get(t)).isEqualTo(false);

        assertThat(b.get(t)).isEqualTo(1);
    }

    @Test
    public void testCombinePromiseSequence() {
        F.Promise<Integer> a = F.Promise.pure(1);
        F.Promise<Integer> b = F.Promise.pure(2);
        F.Promise<Integer> c = F.Promise.pure(3);

        F.Promise<List<Integer>> combined = F.Promise.sequence(Arrays.asList(a, b, c));

        assertThat(combined.get(t)).isEqualTo(Arrays.asList(1, 2, 3));
    }
}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework PromiseTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.