Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#567 beanFriendlyModifiables style option
- Loading branch information
Showing
9 changed files
with
115 additions
and
45 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
value-fixture/src/org/immutables/fixture/modifiable/BeanFriendly.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,24 @@ | |||
package org.immutables.fixture.modifiable; | |||
|
|||
import java.util.List; | |||
import java.util.Map; | |||
import org.immutables.value.Value; | |||
|
|||
@Value.Immutable | |||
@Value.Modifiable | |||
@Value.Style( | |||
beanFriendlyModifiables = true, | |||
create = "new", | |||
get = {"get*", "is*"}) | |||
public interface BeanFriendly { | |||
|
|||
boolean isPrimary(); | |||
|
|||
int getId(); | |||
|
|||
String getDescription(); | |||
|
|||
List<String> getNames(); | |||
|
|||
Map<String, String> getOptions(); | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
value-fixture/test/org/immutables/fixture/modifiable/BeanFriendlyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,42 @@ | |||
package org.immutables.fixture.modifiable; | |||
|
|||
import com.google.common.collect.ImmutableList; | |||
import com.google.common.collect.ImmutableMap; | |||
import com.google.common.collect.ImmutableSet; | |||
import java.beans.BeanInfo; | |||
import java.beans.Introspector; | |||
import org.junit.Test; | |||
import static org.immutables.check.Checkers.check; | |||
|
|||
public class BeanFriendlyTest { | |||
|
|||
@Test | |||
public void modifiableAsJavaBean() throws Exception { | |||
ImmutableSet<String> rwProperties = ImmutableSet.of("primary", "id", "description", "names", "options"); | |||
|
|||
BeanInfo beanInfo = Introspector.getBeanInfo(ModifiableBeanFriendly.class); | |||
|
|||
for (java.beans.PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) { | |||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
elucash
Author
Member
|
|||
if (rwProperties.contains(pd.getName())) { | |||
check(pd.getReadMethod()).notNull(); | |||
check(pd.getWriteMethod()).notNull(); | |||
} | |||
} | |||
|
|||
ModifiableBeanFriendly bean = new ModifiableBeanFriendly(); | |||
bean.setPrimary(true); | |||
bean.setDescription("description"); | |||
bean.setId(1000); | |||
bean.setNames(ImmutableList.of("name")); | |||
bean.addNames("name2"); | |||
bean.putOptions("foo", "bar"); | |||
|
|||
// This bean can become immutable. | |||
BeanFriendly immutableBean = bean.toImmutable(); | |||
check(immutableBean.isPrimary()); | |||
check(immutableBean.getDescription()).is("description"); | |||
check(immutableBean.getId()).is(1000); | |||
check(immutableBean.getNames()).isOf("name", "name2"); | |||
check(immutableBean.getOptions()).is(ImmutableMap.of("foo", "bar")); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Thanks a lot for this commit! Code looks good, but I think this test doesn't actually test whether the bean properties were detected. This iterates on the detected properties, whereas we should iterate on the expected properties to make sure they're actually present. It's why my version collected these into a map before checking and would recommend something along those lines.