| 11 | import onjava.*; |
| 12 | |
| 13 | public class AtUnitExample4 { |
| 14 | static String theory = "All brontosauruses " + |
| 15 | "are thin at one end, much MUCH thicker in the " + |
| 16 | "middle, and then thin again at the far end."; |
| 17 | private String word; |
| 18 | private Random rand = new Random(); // Time-based seed |
| 19 | public AtUnitExample4(String word) { |
| 20 | this.word = word; |
| 21 | } |
| 22 | public String getWord() { return word; } |
| 23 | public String scrambleWord() { |
| 24 | List<Character> chars = Arrays.asList( |
| 25 | ConvertTo.boxed(word.toCharArray())); |
| 26 | Collections.shuffle(chars, rand); |
| 27 | StringBuilder result = new StringBuilder(); |
| 28 | for(char ch : chars) |
| 29 | result.append(ch); |
| 30 | return result.toString(); |
| 31 | } |
| 32 | @TestProperty |
| 33 | static List<String> input = |
| 34 | Arrays.asList(theory.split(" ")); |
| 35 | @TestProperty |
| 36 | static Iterator<String> words = input.iterator(); |
| 37 | @TestObjectCreate |
| 38 | static AtUnitExample4 create() { |
| 39 | if(words.hasNext()) |
| 40 | return new AtUnitExample4(words.next()); |
| 41 | else |
| 42 | return null; |
| 43 | } |
| 44 | @Test |
| 45 | boolean words() { |
| 46 | System.out.println("'" + getWord() + "'"); |
| 47 | return getWord().equals("are"); |
| 48 | } |
| 49 | @Test |
| 50 | boolean scramble1() { |
| 51 | // Use specific seed to get verifiable results: |
| 52 | rand = new Random(47); |
| 53 | System.out.println("'" + getWord() + "'"); |
| 54 | String scrambled = scrambleWord(); |
| 55 | System.out.println(scrambled); |
| 56 | return scrambled.equals("lAl"); |
| 57 | } |
| 58 | @Test |
| 59 | boolean scramble2() { |
| 60 | rand = new Random(74); |
| 61 | System.out.println("'" + getWord() + "'"); |
| 62 | String scrambled = scrambleWord(); |
| 63 | System.out.println(scrambled); |
| 64 | return scrambled.equals("tsaeborornussu"); |
| 65 | } |
| 66 | } |
| 67 | /* Output: |
| 68 | annotations.AtUnitExample4 |
| 69 | . words 'All' |