OnJava8-Examples/annotations/AtUnitExample4.java

84 lines
2.2 KiB
Java
Raw Permalink Normal View History

2015-09-07 11:44:36 -06:00
// annotations/AtUnitExample4.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-07-28 12:48:23 -06:00
// {java onjava.atunit.AtUnit
// build/classes/java/main/annotations/AtUnitExample4.class}
// {VisuallyInspectOutput}
2015-06-15 17:47:35 -07:00
package annotations;
import java.util.*;
2015-12-06 11:45:16 -08:00
import onjava.atunit.*;
import onjava.*;
2015-06-15 17:47:35 -07:00
public class AtUnitExample4 {
static String theory = "All brontosauruses " +
"are thin at one end, much MUCH thicker in the " +
"middle, and then thin again at the far end.";
private String word;
private Random rand = new Random(); // Time-based seed
2017-01-20 21:30:44 -08:00
public AtUnitExample4(String word) {
this.word = word;
}
2015-06-15 17:47:35 -07:00
public String getWord() { return word; }
public String scrambleWord() {
2017-01-22 16:48:11 -08:00
List<Character> chars = Arrays.asList(
ConvertTo.boxed(word.toCharArray()));
2015-06-15 17:47:35 -07:00
Collections.shuffle(chars, rand);
StringBuilder result = new StringBuilder();
for(char ch : chars)
result.append(ch);
return result.toString();
}
2017-01-22 16:48:11 -08:00
@TestProperty
static List<String> input =
2015-06-15 17:47:35 -07:00
Arrays.asList(theory.split(" "));
@TestProperty
2017-01-22 16:48:11 -08:00
static Iterator<String> words = input.iterator();
@TestObjectCreate
static AtUnitExample4 create() {
2015-06-15 17:47:35 -07:00
if(words.hasNext())
return new AtUnitExample4(words.next());
else
return null;
}
2017-01-22 16:48:11 -08:00
@Test
boolean words() {
2015-11-03 12:00:44 -08:00
System.out.println("'" + getWord() + "'");
2015-06-15 17:47:35 -07:00
return getWord().equals("are");
}
2017-01-22 16:48:11 -08:00
@Test
boolean scramble1() {
2017-01-20 21:30:44 -08:00
// Use specific seed to get verifiable results:
2015-06-15 17:47:35 -07:00
rand = new Random(47);
2015-11-03 12:00:44 -08:00
System.out.println("'" + getWord() + "'");
2015-06-15 17:47:35 -07:00
String scrambled = scrambleWord();
2015-11-03 12:00:44 -08:00
System.out.println(scrambled);
2015-06-15 17:47:35 -07:00
return scrambled.equals("lAl");
}
2017-01-22 16:48:11 -08:00
@Test
boolean scramble2() {
2015-06-15 17:47:35 -07:00
rand = new Random(74);
2015-11-03 12:00:44 -08:00
System.out.println("'" + getWord() + "'");
2015-06-15 17:47:35 -07:00
String scrambled = scrambleWord();
2015-11-03 12:00:44 -08:00
System.out.println(scrambled);
2015-06-15 17:47:35 -07:00
return scrambled.equals("tsaeborornussu");
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
annotations.AtUnitExample4
2016-07-22 14:45:35 -06:00
. words 'All'
(failed)
. scramble1 'brontosauruses'
ntsaueorosurbs
(failed)
. scramble2 'are'
are
2016-07-22 14:45:35 -06:00
(failed)
(3 tests)
2016-01-25 18:05:55 -08:00
>>> 3 FAILURES <<<
2016-07-22 14:45:35 -06:00
annotations.AtUnitExample4: words
annotations.AtUnitExample4: scramble1
annotations.AtUnitExample4: scramble2
2015-09-07 11:44:36 -06:00
*/