@@ -1851,6 +1851,91 @@ public void testBuilderWithPropertyBuilders() {
18511851 }
18521852 }
18531853
1854+ @ AutoValue
1855+ public abstract static class BuilderWithOptionalPropertyBuilders {
1856+ public abstract com .google .common .base .Optional <BuilderWithSetAndGet > getFoo ();
1857+
1858+ public abstract BuilderWithOptionalPropertyBuilders .Builder toBuilder ();
1859+
1860+ public static Builder builder () {
1861+ return new AutoValue_AutoValueTest_BuilderWithOptionalPropertyBuilders .Builder ();
1862+ }
1863+
1864+ @ AutoValue .Builder
1865+ public abstract static class Builder {
1866+ public abstract com .google .common .base .Optional <BuilderWithSetAndGet > getFoo ();
1867+
1868+ public abstract Builder foo (com .google .common .base .Optional <BuilderWithSetAndGet > foo );
1869+
1870+ public abstract BuilderWithSetAndGet .Builder fooBuilder ();
1871+
1872+ public abstract BuilderWithOptionalPropertyBuilders build ();
1873+ }
1874+ }
1875+
1876+ @ Test
1877+ public void testBuilderWithOptionalPropertyBuilders () {
1878+ BuilderWithOptionalPropertyBuilders a =
1879+ BuilderWithOptionalPropertyBuilders .builder ()
1880+ .build ();
1881+
1882+ assertThat (a .getFoo ()).isAbsent ();
1883+
1884+ BuilderWithSetAndGet foo = BuilderWithSetAndGet .builder ()
1885+ .setAnInt (0 )
1886+ .setAList (ImmutableList .of (1 ))
1887+ .build ();
1888+ BuilderWithOptionalPropertyBuilders b =
1889+ BuilderWithOptionalPropertyBuilders .builder ()
1890+ .foo (com .google .common .base .Optional .of (foo ))
1891+ .build ();
1892+
1893+ assertThat (b .getFoo ()).hasValue (foo );
1894+
1895+ BuilderWithOptionalPropertyBuilders c =
1896+ BuilderWithOptionalPropertyBuilders .builder ()
1897+ .foo (com .google .common .base .Optional .<BuilderWithSetAndGet >absent ())
1898+ .build ();
1899+
1900+ assertThat (c .getFoo ()).isAbsent ();
1901+
1902+ BuilderWithOptionalPropertyBuilders .Builder buildD =
1903+ BuilderWithOptionalPropertyBuilders .builder ()
1904+ .foo (com .google .common .base .Optional .of (foo ));
1905+
1906+ // get fooBuilder to trigger toBuilder logic
1907+ buildD .fooBuilder ();
1908+ BuilderWithOptionalPropertyBuilders d = buildD .build ();
1909+
1910+ assertThat (d .getFoo ()).hasValue (foo );
1911+
1912+ BuilderWithOptionalPropertyBuilders .Builder buildE =
1913+ BuilderWithOptionalPropertyBuilders .builder ()
1914+ .foo (com .google .common .base .Optional .of (foo ));
1915+
1916+ buildE .fooBuilder ().setAnInt (1 );
1917+ BuilderWithOptionalPropertyBuilders e = buildE .build ();
1918+
1919+ assertThat (e .getFoo ()).hasValue (foo .toBuilder ().setAnInt (1 ).build ());
1920+
1921+ BuilderWithOptionalPropertyBuilders .Builder buildF =
1922+ BuilderWithOptionalPropertyBuilders .builder ();
1923+
1924+ buildF .fooBuilder ().setAList (ImmutableList .of (1 )).setAnInt (0 );
1925+ BuilderWithOptionalPropertyBuilders f = buildF .build ();
1926+
1927+ assertThat (f .getFoo ()).hasValue (foo );
1928+
1929+ try {
1930+ BuilderWithOptionalPropertyBuilders .builder ().foo (null ).build ();
1931+ fail ("Did not get expected exception" );
1932+ } catch (RuntimeException expected ) {
1933+ // We don't specify whether you get the exception on foo(null) or on
1934+ // build(), nor
1935+ // which exception it is exactly.
1936+ }
1937+ }
1938+
18541939 @ AutoValue
18551940 public abstract static class
18561941 BuilderWithExoticPropertyBuilders <K extends Number , V extends Comparable <K >> {
0 commit comments