Skip to content

Commit

Permalink
CookieProvider.getLong/ShortCookie(int) added
Browse files Browse the repository at this point in the history
  • Loading branch information
oboehm committed Dec 25, 2017
1 parent 325b996 commit c77f18a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
20 changes: 19 additions & 1 deletion src/main/java/jfortune/CookieProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,32 @@ default Cookie getLongCookie() {
}

/**
* Returns a cookie which belongs the given random. I.e. the next call
* Returns a cookie which belongs to the given random. I.e. the next call
* with the same random value will return the same cookie.
*
* @param random whole range of 'int' is allowed as value
* @return a cookie
*/
Cookie getCookie(int random);

/**
* Returns a short cookie which belongs to the given random. I.e. the next
* call to this method will return the same short cookie.
*
* @param random whole range of 'int' is allowed as value
* @return a cookie
*/
Cookie getShortCookie(int random);

/**
* Returns a long cookie which belongs to the given random. I.e. the next
* call to this method will return the same long cookie.
*
* @param random whole range of 'int' is allowed as value
* @return a cookie
*/
Cookie getLongCookie(int random);

/**
* Set the longest fortune length (in characters) considered to be "short"
* (the default is 160). All fortunes longer than this are considered "long".
Expand Down
43 changes: 40 additions & 3 deletions src/main/java/jfortune/provider/CookieResourceProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,52 @@ public Cookie getCookie() {
*/
@Override
public Cookie getCookie(int random) {
int n = Math.abs(random % this.getNumberOfSayings());
int n = Math.abs(random % this.getNumberOfCookies());
return new Cookie(getSaying(n));
}

/**
* Returns a short cookie which belongs to the given random. I.e. the next
* call to this method will return the same short cookie.
*
* @param random whole range of 'int' is allowed as value
* @return a cookie
*/
@Override
public Cookie getShortCookie(int random) {
Cookie cookie = getCookie(random);
if (cookie.length() > getShortLength()) {
return getShortCookie(random + 1);
}
return cookie;
}

/**
* Returns a long cookie which belongs to the given random. I.e. the next
* call to this method will return the same long cookie.
*
* @param random whole range of 'int' is allowed as value
* @return a cookie
*/
@Override
public Cookie getLongCookie(int random) {
Cookie cookie = getCookie(random);
if (cookie.length() > getShortLength()) {
return cookie;
}
return getLongCookie(random + 1);
}

private String getSaying(int n) {
return getSayings().get(n);
}

public int getNumberOfSayings() {

/**
* Calculates the number of cookies.
*
* @return number of cookiess
*/
public int getNumberOfCookies() {
return getSayings().size();
}

Expand Down
28 changes: 25 additions & 3 deletions src/test/java/jfortune/provider/CookieResourceProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class CookieResourceProviderTest {

@Test
public void testInit() {
assertThat(provider.getNumberOfSayings(), is(greaterThan(1)));
assertThat(provider.getNumberOfCookies(), is(greaterThan(1)));
}

/**
Expand All @@ -52,7 +52,6 @@ public void testGetCookie() {
Cookie one = provider.getCookie(n);
Cookie two = provider.getCookie(n);
assertEquals(one, two);
LOG.info(one);
}

/**
Expand All @@ -74,13 +73,36 @@ public void testGetLongCookie() {
assertThat(cookie.length(), greaterThanOrEqualTo(provider.getShortLength()));
}

/**
* Test method for {@link CookieResourceProvider#getShortCookie(int)}.
*/
@Test
public void testGetShortCookieInt() {
int n = (int) System.currentTimeMillis();
Cookie one = provider.getShortCookie(n);
Cookie two = provider.getShortCookie(n);
assertEquals(one, two);
LOG.info(one);
}

/**
* Test method for {@link CookieResourceProvider#getLongCookie(int)}.
*/
@Test
public void testGetLongCookieInt() {
int n = (int) System.currentTimeMillis();
Cookie one = provider.getLongCookie(n);
Cookie two = provider.getLongCookie(n);
assertEquals(one, two);
}

/**
* Test method for {@link CookieResourceProvider#CookieResourceProvider(String)}.
*/
@Test
public void testCookieResourceProviderString() {
CookieResourceProvider literature = new CookieResourceProvider("literature");
assertThat(literature.getNumberOfSayings(), is(greaterThan(1)));
assertThat(literature.getNumberOfCookies(), is(greaterThan(1)));
}

}

0 comments on commit c77f18a

Please sign in to comment.