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   * Copyright 2012 Tim Wood
12   *
13   * Licensed under the Apache License, Version 2.0 (the "License");
14   * you may not use this file except in compliance with the License.
15   * You may obtain a copy of the License at
16   *
17   * http://www.apache.org/licenses/LICENSE-2.0
18   *
19   * Unless required by applicable law or agreed to in writing, software
20   * distributed under the License is distributed on an "AS IS" BASIS,
21   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   * See the License for the specific language governing permissions and
23   * limitations under the License.
24   */
25  
26  public class TestJoorExamples {
27      /**
28       * String world = on("java.lang.String")  // Like Class.forName()
29       *                 .create("Hello World") // Call the most specific matching constructor
30       *                 .call("substring", 6)  // Call the most specific matching substring() method
31       *                 .call("toString")      // Call toString()
32       *                 .get();                 // Get the wrapped object, in this case a String
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       * Employee[] employees = on(department).call("getEmployees").get();
46       * for (Employee employee : employees) {
47       *     Street street = on(employee).call("getAddress").call("getStreet").get();
48       *     System.out.println(street);
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  }