1 package com.lexicalscope.fluentreflection.usecases;
2
3 import static com.lexicalscope.fluentreflection.FluentReflection.*;
4 import static com.lexicalscope.fluentreflection.ReflectionMatchers.hasName;
5 import static org.hamcrest.MatcherAssert.assertThat;
6 import static org.hamcrest.Matchers.equalTo;
7
8 import org.junit.Test;
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 public class TestJoorExamples {
27
28
29
30
31
32
33
34 @Test public void joorStringExample()
35 {
36 assertThat(type("java.lang.String").
37 construct("Hello World").
38 call("substring", 6).
39 call("toString").
40 as(String.class),
41 equalTo("World"));
42 }
43
44
45
46
47
48
49
50
51 @Test @SuppressWarnings("unused") public void joorArrayExample() {
52 class Address {
53 private final String street;
54 public Address(final String street) { this.street = street; }
55 String getStreet() { return street; }
56 }
57 class Employee {
58 private final String street;
59 public Employee(final String street) { this.street = street; }
60 Address getAddress() { return new Address(street); }
61 }
62 class Department {
63 Employee[] getEmployees() { return new Employee[]{new Employee("street0"), new Employee("street1")}; }
64 }
65
66 final Employee[] employees = object(new Department()).call("getEmployees").as(Employee[].class);
67 for (int i = 0; i < employees.length; i++) {
68 assertThat(
69 object(employees[i]).call("getAddress").call("getStreet").as(String.class),
70 equalTo("street" + i));
71 }
72 }
73
74 @Test public void joorStringExampleWithMatchers()
75 {
76 assertThat(type("java.lang.String").
77 construct("Hello World").
78 call(hasName("substring"), 6).
79 call(hasName("toString")).
80 as(String.class),
81 equalTo("World"));
82 }
83 }