1 // Copyright 2021 Code Intelligence GmbH 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package com.code_intelligence.jazzer.autofuzz; 16 17 import static com.code_intelligence.jazzer.autofuzz.TestHelpers.consumeTestCase; 18 19 import java.util.Arrays; 20 import java.util.Objects; 21 import org.junit.Test; 22 23 class Employee { 24 private final String firstName; 25 private final String lastName; 26 private final String jobTitle; 27 private final int age; 28 29 @Override equals(Object o)30 public boolean equals(Object o) { 31 if (this == o) 32 return true; 33 if (o == null || getClass() != o.getClass()) 34 return false; 35 Employee hero = (Employee) o; 36 return age == hero.age && Objects.equals(firstName, hero.firstName) 37 && Objects.equals(lastName, hero.lastName) && Objects.equals(jobTitle, hero.jobTitle); 38 } 39 40 @Override hashCode()41 public int hashCode() { 42 return Objects.hash(firstName, lastName, jobTitle, age); 43 } 44 Employee(Builder builder)45 private Employee(Builder builder) { 46 this.jobTitle = builder.jobTitle; 47 this.firstName = builder.firstName; 48 this.lastName = builder.lastName; 49 this.age = builder.age; 50 } 51 52 public static class Builder { 53 private final String firstName; 54 private final String lastName; 55 private String jobTitle; 56 private int age; 57 Builder(String firstName, String lastName)58 public Builder(String firstName, String lastName) { 59 this.firstName = firstName; 60 this.lastName = lastName; 61 } 62 withAge(int age)63 public Builder withAge(int age) { 64 this.age = age; 65 return this; 66 } 67 withJobTitle(String jobTitle)68 public Builder withJobTitle(String jobTitle) { 69 this.jobTitle = jobTitle; 70 return this; 71 } 72 build()73 public Employee build() { 74 return new Employee(this); 75 } 76 } 77 } 78 79 public class BuilderPatternTest { 80 @Test testBuilderPattern()81 public void testBuilderPattern() { 82 consumeTestCase(new Employee.Builder("foo", "bar").withAge(20).withJobTitle("baz").build(), 83 "new com.code_intelligence.jazzer.autofuzz.Employee.Builder(\"foo\", \"bar\").withAge(20).withJobTitle(\"baz\").build()", 84 Arrays.asList((byte) 1, // do not return null 85 0, // Select the first Builder 86 2, // Select two Builder methods returning a builder object (fluent design) 87 0, // Select the first build method 88 0, // pick the first remaining builder method (withAge) 89 0, // pick the first remaining builder method (withJobTitle) 90 0, // pick the first build method 91 (byte) 1, // do not return null 92 6, // remaining bytes 93 "foo", // firstName 94 (byte) 1, // do not return null 95 6, // remaining bytes 96 "bar", // lastName 97 20, // age 98 (byte) 1, // do not return null 99 6, // remaining bytes 100 "baz" // jobTitle 101 )); 102 } 103 } 104