diff --git a/.travis.yml b/.travis.yml index 4b4f314..195280c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: java jdk: - openjdk8 install: - - mvn install -Dgpg.skip=true -B -V + - mvn install -Dgpg.skip=true -Djar.finalName=metter -B -V - cd demo - gradle test - cd .. diff --git a/README.md b/README.md index 3856cba..7c474f7 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,11 @@ Metter (***met***a get***ter*** / set***ter***) is an annotation processor for generating getter and setter suppliers. -Ignores private methods. +- Supports [Lombok](https://github.com/rzwitserloot/lombok) annotations: `@Data`, `@Getter` and `@Setter`. +- Supports inheritance (getters/setters of superclasses). +- Ignores private methods. -Supports [Lombok](https://github.com/rzwitserloot/lombok) annotations: `@Data`, `@Getter` and `@Setter`. +[See](demo) examples. ## Table of Contents @@ -400,9 +402,9 @@ The generation solution is faster than the reflection solution (reflection is sl This project is [licensed](LICENSE) under [Apache License, version 2.0](https://www.apache.org/licenses/LICENSE-2.0). [JetBrains Mono typeface](https://www.jetbrains.com/lp/mono) -used in [logo](docs/images/metter-logo.png) -and [preview](docs/images/metter-preview.png) is [licensed](https://www.jetbrains.com/lp/mono/#license) -under [Apache License, version 2.0](https://www.apache.org/licenses/LICENSE-2.0). +under [Apache License, version 2.0](https://www.apache.org/licenses/LICENSE-2.0) +and it used in [logo](docs/images/metter-logo.png) +and [preview](docs/images/metter-preview.png). [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Falexengrig%2Fmetter.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Falexengrig%2Fmetter?ref=badge_large) diff --git a/demo/README.md b/demo/README.md new file mode 100644 index 0000000..14e5ce6 --- /dev/null +++ b/demo/README.md @@ -0,0 +1,35 @@ +# metter demo + +- [Simple domain](src/main/java/dev/alexengrig/metter/demo/simple/SimpleDomain.java) +- [Domain with custom name](src/main/java/dev/alexengrig/metter/demo/naming/NamedDomain.java) +- [Excluding](src/main/java/dev/alexengrig/metter/demo/excluding) + - [Domain with excluded getters/setters](src/main/java/dev/alexengrig/metter/demo/excluding/ExcludedDomain.java) + - [Inherited domain with excluded getters/setters](src/main/java/dev/alexengrig/metter/demo/excluding/InheritedExcludedDomain.java) +- [Including](src/main/java/dev/alexengrig/metter/demo/including) + - [Domain with included getters/setters](src/main/java/dev/alexengrig/metter/demo/including/IncludedDomain.java) + - [Inherited domain with included getters/setters](src/main/java/dev/alexengrig/metter/demo/including/InheritedIncludedDomain.java) +- [Including and excluding](src/main/java/dev/alexengrig/metter/demo/includingandexcluding) + - [Domain with included and excluded getters/setters](src/main/java/dev/alexengrig/metter/demo/includingandexcluding/IncludedAndExcludedDomain.java) + - [Inherited domain with included and excluded getters/setters](src/main/java/dev/alexengrig/metter/demo/includingandexcluding/InheritedIncludedAndExcludedDomain.java) +- [Inheritance](src/main/java/dev/alexengrig/metter/demo/inheritance) +- [Lombok Data annotation](src/main/java/dev/alexengrig/metter/demo/lombokdata) + - [Domain with Lombok Data annotation](src/main/java/dev/alexengrig/metter/demo/lombokdata/LombokDataDomain.java) + - [Inherited domain with Lombok Data annotation](src/main/java/dev/alexengrig/metter/demo/lombokdata/InheritedLombokDataDomain.java) +- [Lombok Getter and Setter annotations on class](src/main/java/dev/alexengrig/metter/demo/lombokgetterandsetter) + - [Domain with Lombok Getter and Setter annotations on class](src/main/java/dev/alexengrig/metter/demo/lombokgetterandsetter/LombokGetterAndSetterDomain.java) + - [Inherited domain with Lombok Getter and Setter annotations on class](src/main/java/dev/alexengrig/metter/demo/lombokgetterandsetter/InheritedLombokGetterAndSetterDomain.java) +- [Lombok Getter and Setter annotations on field](src/main/java/dev/alexengrig/metter/demo/lombokgettersandsetters) + - [Domain with Lombok Getter and Setter annotations on field](src/main/java/dev/alexengrig/metter/demo/lombokgettersandsetters/LombokGettersAndSettersDomain.java) + - [Inherited domain with Lombok Getter and Setter annotations on field](src/main/java/dev/alexengrig/metter/demo/lombokgettersandsetters/InheritedLombokGettersAndSettersDomain.java) +- [Private getters and setters](src/main/java/dev/alexengrig/metter/demo/privategettersandsetters) + - [Domain with private getters and setters](src/main/java/dev/alexengrig/metter/demo/privategettersandsetters/PrivateGettersAndSettersDomain.java) + - [Inherited domain with private getters and setters](src/main/java/dev/alexengrig/metter/demo/privategettersandsetters/InheritedPrivateGettersAndSettersDomain.java) +- [Lombok Getter and Setter annotations on class with private access](src/main/java/dev/alexengrig/metter/demo/privatelombokgetterandsetter) + - [Domain with Lombok Getter and Setter annotations on class with private access](src/main/java/dev/alexengrig/metter/demo/privatelombokgetterandsetter/PrivateLombokGetterAndSetterDomain.java) + - [Inherited domain with Lombok Getter and Setter annotations on class with private access](src/main/java/dev/alexengrig/metter/demo/privatelombokgetterandsetter/InheritedPrivateLombokGetterAndSetterDomain.java) +- [Lombok Getter and Setter annotations on field with private access](src/main/java/dev/alexengrig/metter/demo/privatelombokgettersandsetters) + - [Domain with Lombok Getter and Setter annotations on field with private access](src/main/java/dev/alexengrig/metter/demo/privatelombokgettersandsetters/PrivateLombokGettersAndSettersDomain.java) + - [Inherited domain with Lombok Getter and Setter annotations on field with private access](src/main/java/dev/alexengrig/metter/demo/privatelombokgettersandsetters/InheritedPrivateLombokGettersAndSettersDomain.java) +- [Domain without package](src/main/java/NoPackageDomain.java) + +- [Motivation code](src/main/java/dev/alexengrig/metter/motivation) diff --git a/demo/build.gradle b/demo/build.gradle index df57730..26867e8 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -19,15 +19,15 @@ plugins { } group 'dev.alexengrig' -version '0.1.1' +version '0.2.0-SNAPSHOT' repositories { mavenCentral() } dependencies { - compileOnly files('../target/metter-0.2.0-SNAPSHOT.jar') - annotationProcessor files('../target/metter-0.2.0-SNAPSHOT.jar') + compileOnly files('../target/metter.jar') + annotationProcessor files('../target/metter.jar') compileOnly 'org.projectlombok:lombok:1.18.12' annotationProcessor 'org.projectlombok:lombok:1.18.12' diff --git a/demo/src/main/java/dev/alexengrig/metter/demo/excluding/InheritedExcludedDomain.java b/demo/src/main/java/dev/alexengrig/metter/demo/excluding/InheritedExcludedDomain.java new file mode 100644 index 0000000..d72ee78 --- /dev/null +++ b/demo/src/main/java/dev/alexengrig/metter/demo/excluding/InheritedExcludedDomain.java @@ -0,0 +1,39 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.excluding; + +import dev.alexengrig.metter.annotation.GetterSupplier; +import dev.alexengrig.metter.annotation.SetterSupplier; + +@GetterSupplier(excludedFields = {"excluded", "alsoExcluded"}) +@SetterSupplier(excludedFields = {"excluded", "alsoExcluded"}) +public class InheritedExcludedDomain extends ExcludedDomain { + private int alsoExcluded; + + public InheritedExcludedDomain(int integer, int excluded, int alsoExcluded) { + super(integer, excluded); + this.alsoExcluded = alsoExcluded; + } + + public int getAlsoExcluded() { + return alsoExcluded; + } + + public void setAlsoExcluded(int alsoExcluded) { + this.alsoExcluded = alsoExcluded; + } +} diff --git a/demo/src/main/java/dev/alexengrig/metter/demo/including/InheritedIncludedDomain.java b/demo/src/main/java/dev/alexengrig/metter/demo/including/InheritedIncludedDomain.java new file mode 100644 index 0000000..d7a8c55 --- /dev/null +++ b/demo/src/main/java/dev/alexengrig/metter/demo/including/InheritedIncludedDomain.java @@ -0,0 +1,49 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.including; + +import dev.alexengrig.metter.annotation.GetterSupplier; +import dev.alexengrig.metter.annotation.SetterSupplier; + +@GetterSupplier(includedFields = {"included", "alsoIncluded"}) +@SetterSupplier(includedFields = {"included", "alsoIncluded"}) +public class InheritedIncludedDomain extends IncludedDomain { + private int alsoIncluded; + private int alsoIgnored; + + public InheritedIncludedDomain(int included, int ignored, int alsoIncluded, int alsoIgnored) { + super(included, ignored); + this.alsoIncluded = alsoIncluded; + this.alsoIgnored = alsoIgnored; + } + + public int getAlsoIncluded() { + return alsoIncluded; + } + + public void setAlsoIncluded(int alsoIncluded) { + this.alsoIncluded = alsoIncluded; + } + + public int getAlsoIgnored() { + return alsoIgnored; + } + + public void setAlsoIgnored(int alsoIgnored) { + this.alsoIgnored = alsoIgnored; + } +} diff --git a/demo/src/main/java/dev/alexengrig/metter/demo/includingandexcluding/InheritedIncludedAndExcludedDomain.java b/demo/src/main/java/dev/alexengrig/metter/demo/includingandexcluding/InheritedIncludedAndExcludedDomain.java new file mode 100644 index 0000000..344c016 --- /dev/null +++ b/demo/src/main/java/dev/alexengrig/metter/demo/includingandexcluding/InheritedIncludedAndExcludedDomain.java @@ -0,0 +1,59 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.includingandexcluding; + +import dev.alexengrig.metter.annotation.GetterSupplier; +import dev.alexengrig.metter.annotation.SetterSupplier; + +@GetterSupplier(includedFields = {"included", "alsoIncluded"}, excludedFields = {"excluded", "alsoExcluded"}) +@SetterSupplier(includedFields = {"included", "alsoIncluded"}, excludedFields = {"excluded", "alsoExcluded"}) +public class InheritedIncludedAndExcludedDomain extends IncludedAndExcludedDomain { + private int alsoIncluded; + private int alsoIgnored; + private int alsoExcluded; + + public InheritedIncludedAndExcludedDomain(int included, int ignored, int excluded, int alsoIncluded, int alsoIgnored, int alsoExcluded) { + super(included, ignored, excluded); + this.alsoIncluded = alsoIncluded; + this.alsoIgnored = alsoIgnored; + this.alsoExcluded = alsoExcluded; + } + + public int getAlsoIncluded() { + return alsoIncluded; + } + + public void setAlsoIncluded(int alsoIncluded) { + this.alsoIncluded = alsoIncluded; + } + + public int getAlsoIgnored() { + return alsoIgnored; + } + + public void setAlsoIgnored(int alsoIgnored) { + this.alsoIgnored = alsoIgnored; + } + + public int getAlsoExcluded() { + return alsoExcluded; + } + + public void setAlsoExcluded(int alsoExcluded) { + this.alsoExcluded = alsoExcluded; + } +} diff --git a/demo/src/main/java/dev/alexengrig/metter/demo/inheritance/FatherDomain.java b/demo/src/main/java/dev/alexengrig/metter/demo/inheritance/FatherDomain.java new file mode 100644 index 0000000..f370e78 --- /dev/null +++ b/demo/src/main/java/dev/alexengrig/metter/demo/inheritance/FatherDomain.java @@ -0,0 +1,38 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.inheritance; + +import dev.alexengrig.metter.annotation.GetterSupplier; +import dev.alexengrig.metter.annotation.SetterSupplier; + +@GetterSupplier +@SetterSupplier +public class FatherDomain { + private int fatherInt; + + public FatherDomain(int fatherInt) { + this.fatherInt = fatherInt; + } + + public int getFatherInt() { + return fatherInt; + } + + public void setFatherInt(int fatherInt) { + this.fatherInt = fatherInt; + } +} diff --git a/demo/src/main/java/dev/alexengrig/metter/demo/inheritance/GrandsonDomain.java b/demo/src/main/java/dev/alexengrig/metter/demo/inheritance/GrandsonDomain.java new file mode 100644 index 0000000..75c69c2 --- /dev/null +++ b/demo/src/main/java/dev/alexengrig/metter/demo/inheritance/GrandsonDomain.java @@ -0,0 +1,39 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.inheritance; + +import dev.alexengrig.metter.annotation.GetterSupplier; +import dev.alexengrig.metter.annotation.SetterSupplier; + +@GetterSupplier +@SetterSupplier +public class GrandsonDomain extends SonDomain { + private int grandsonInt; + + public GrandsonDomain(int fatherInt, int sonInt, int grandsonInt) { + super(fatherInt, sonInt); + this.grandsonInt = grandsonInt; + } + + public int getGrandsonInt() { + return grandsonInt; + } + + public void setGrandsonInt(int grandsonInt) { + this.grandsonInt = grandsonInt; + } +} diff --git a/demo/src/main/java/dev/alexengrig/metter/demo/inheritance/SonDomain.java b/demo/src/main/java/dev/alexengrig/metter/demo/inheritance/SonDomain.java new file mode 100644 index 0000000..59687e9 --- /dev/null +++ b/demo/src/main/java/dev/alexengrig/metter/demo/inheritance/SonDomain.java @@ -0,0 +1,39 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.inheritance; + +import dev.alexengrig.metter.annotation.GetterSupplier; +import dev.alexengrig.metter.annotation.SetterSupplier; + +@GetterSupplier +@SetterSupplier +public class SonDomain extends FatherDomain { + private int sonInt; + + public SonDomain(int fatherInt, int sonInt) { + super(fatherInt); + this.sonInt = sonInt; + } + + public int getSonInt() { + return sonInt; + } + + public void setSonInt(int sonInt) { + this.sonInt = sonInt; + } +} diff --git a/demo/src/main/java/dev/alexengrig/metter/demo/lombokdata/InheritedLombokDataDomain.java b/demo/src/main/java/dev/alexengrig/metter/demo/lombokdata/InheritedLombokDataDomain.java new file mode 100644 index 0000000..8a2644c --- /dev/null +++ b/demo/src/main/java/dev/alexengrig/metter/demo/lombokdata/InheritedLombokDataDomain.java @@ -0,0 +1,35 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.lombokdata; + +import dev.alexengrig.metter.annotation.GetterSupplier; +import dev.alexengrig.metter.annotation.SetterSupplier; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@GetterSupplier +@SetterSupplier +@EqualsAndHashCode(callSuper = true) +public class InheritedLombokDataDomain extends LombokDataDomain { + private long longer; + + public InheritedLombokDataDomain(int integer, boolean bool, long longer) { + super(integer, bool); + this.longer = longer; + } +} diff --git a/demo/src/main/java/dev/alexengrig/metter/demo/lombokdata/LombokDataDomain.java b/demo/src/main/java/dev/alexengrig/metter/demo/lombokdata/LombokDataDomain.java index c4cac10..b7684ec 100644 --- a/demo/src/main/java/dev/alexengrig/metter/demo/lombokdata/LombokDataDomain.java +++ b/demo/src/main/java/dev/alexengrig/metter/demo/lombokdata/LombokDataDomain.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 Alexengrig Dev. + * Copyright 2020-2021 Alexengrig Dev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,10 +20,12 @@ import dev.alexengrig.metter.annotation.SetterSupplier; import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; @Data @GetterSupplier @SetterSupplier +@NoArgsConstructor @AllArgsConstructor public class LombokDataDomain { private int integer; diff --git a/demo/src/main/java/dev/alexengrig/metter/demo/lombokgetterandsetter/InheritedLombokGetterAndSetterDomain.java b/demo/src/main/java/dev/alexengrig/metter/demo/lombokgetterandsetter/InheritedLombokGetterAndSetterDomain.java new file mode 100644 index 0000000..c7f7ae7 --- /dev/null +++ b/demo/src/main/java/dev/alexengrig/metter/demo/lombokgetterandsetter/InheritedLombokGetterAndSetterDomain.java @@ -0,0 +1,35 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.lombokgetterandsetter; + +import dev.alexengrig.metter.annotation.GetterSupplier; +import dev.alexengrig.metter.annotation.SetterSupplier; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@GetterSupplier +@SetterSupplier +public class InheritedLombokGetterAndSetterDomain extends LombokGetterAndSetterDomain { + private long longer; + + public InheritedLombokGetterAndSetterDomain(int integer, boolean bool, long longer) { + super(integer, bool); + this.longer = longer; + } +} diff --git a/demo/src/main/java/dev/alexengrig/metter/demo/lombokgettersandsetters/InheritedLombokGettersAndSettersDomain.java b/demo/src/main/java/dev/alexengrig/metter/demo/lombokgettersandsetters/InheritedLombokGettersAndSettersDomain.java new file mode 100644 index 0000000..bbbbac2 --- /dev/null +++ b/demo/src/main/java/dev/alexengrig/metter/demo/lombokgettersandsetters/InheritedLombokGettersAndSettersDomain.java @@ -0,0 +1,35 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.lombokgettersandsetters; + +import dev.alexengrig.metter.annotation.GetterSupplier; +import dev.alexengrig.metter.annotation.SetterSupplier; +import lombok.Getter; +import lombok.Setter; + +@GetterSupplier +@SetterSupplier +public class InheritedLombokGettersAndSettersDomain extends LombokGettersAndSettersDomain { + @Getter + @Setter + private long longer; + + public InheritedLombokGettersAndSettersDomain(int integer, boolean bool, int ignored, long longer) { + super(integer, bool, ignored); + this.longer = longer; + } +} diff --git a/demo/src/main/java/dev/alexengrig/metter/demo/lombokgettersandsetters/LombokGettersAndSettersDomain.java b/demo/src/main/java/dev/alexengrig/metter/demo/lombokgettersandsetters/LombokGettersAndSettersDomain.java index d3c4c68..740cbda 100644 --- a/demo/src/main/java/dev/alexengrig/metter/demo/lombokgettersandsetters/LombokGettersAndSettersDomain.java +++ b/demo/src/main/java/dev/alexengrig/metter/demo/lombokgettersandsetters/LombokGettersAndSettersDomain.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 Alexengrig Dev. + * Copyright 2020-2021 Alexengrig Dev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ @GetterSupplier @SetterSupplier public class LombokGettersAndSettersDomain { + @SuppressWarnings({"unused", "FieldCanBeLocal"}) private final int ignored; @Getter @Setter diff --git a/demo/src/main/java/dev/alexengrig/metter/demo/privategettersandsetters/InheritedPrivateGettersAndSettersDomain.java b/demo/src/main/java/dev/alexengrig/metter/demo/privategettersandsetters/InheritedPrivateGettersAndSettersDomain.java new file mode 100644 index 0000000..6e85349 --- /dev/null +++ b/demo/src/main/java/dev/alexengrig/metter/demo/privategettersandsetters/InheritedPrivateGettersAndSettersDomain.java @@ -0,0 +1,34 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.privategettersandsetters; + +import dev.alexengrig.metter.annotation.GetterSupplier; +import dev.alexengrig.metter.annotation.SetterSupplier; + +@GetterSupplier +@SetterSupplier +public class InheritedPrivateGettersAndSettersDomain extends PrivateGettersAndSettersDomain { + private long longer; + + private long getLonger() { + return longer; + } + + private void setLonger(long longer) { + this.longer = longer; + } +} diff --git a/demo/src/main/java/dev/alexengrig/metter/demo/privatelombokgetterandsetter/InheritedPrivateLombokGetterAndSetterDomain.java b/demo/src/main/java/dev/alexengrig/metter/demo/privatelombokgetterandsetter/InheritedPrivateLombokGetterAndSetterDomain.java new file mode 100644 index 0000000..71c13bc --- /dev/null +++ b/demo/src/main/java/dev/alexengrig/metter/demo/privatelombokgetterandsetter/InheritedPrivateLombokGetterAndSetterDomain.java @@ -0,0 +1,31 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.privatelombokgetterandsetter; + +import dev.alexengrig.metter.annotation.GetterSupplier; +import dev.alexengrig.metter.annotation.SetterSupplier; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; + +@GetterSupplier +@SetterSupplier +@Getter(AccessLevel.PRIVATE) +@Setter(AccessLevel.PRIVATE) +public class InheritedPrivateLombokGetterAndSetterDomain extends PrivateLombokGetterAndSetterDomain { + private long longer; +} diff --git a/demo/src/main/java/dev/alexengrig/metter/demo/privatelombokgettersandsetters/InheritedPrivateLombokGettersAndSettersDomain.java b/demo/src/main/java/dev/alexengrig/metter/demo/privatelombokgettersandsetters/InheritedPrivateLombokGettersAndSettersDomain.java new file mode 100644 index 0000000..7dc9a16 --- /dev/null +++ b/demo/src/main/java/dev/alexengrig/metter/demo/privatelombokgettersandsetters/InheritedPrivateLombokGettersAndSettersDomain.java @@ -0,0 +1,31 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.privatelombokgettersandsetters; + +import dev.alexengrig.metter.annotation.GetterSupplier; +import dev.alexengrig.metter.annotation.SetterSupplier; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; + +@GetterSupplier +@SetterSupplier +public class InheritedPrivateLombokGettersAndSettersDomain extends PrivateLombokGettersAndSettersDomain { + @Getter(AccessLevel.PRIVATE) + @Setter(AccessLevel.PRIVATE) + private long longer; +} diff --git a/demo/src/test/java/NoPackageDomainTest.java b/demo/src/test/java/NoPackageDomainTest.java index 0b251c7..f944567 100644 --- a/demo/src/test/java/NoPackageDomainTest.java +++ b/demo/src/test/java/NoPackageDomainTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 Alexengrig Dev. + * Copyright 2020-2021 Alexengrig Dev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,36 +14,29 @@ * limitations under the License. */ +import dev.alexengrig.metter.demo.BaseDomainTest; import org.junit.Test; import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Function; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -public class NoPackageDomainTest { +public class NoPackageDomainTest extends BaseDomainTest { @Test public void should_contains_allGetters() { - Map> getterByField = new NoPackageDomainGetterSupplier().get(); - assertNotNull("Map is null", getterByField); - assertEquals("Map size not equal to 1", 1, getterByField.size()); - assertTrue("Map not contain getter for 'integer' field", getterByField.containsKey("integer")); + Map> getterByField = getGetterMap(new NoPackageDomainGetterSupplier()); + assertSize(getterByField, 1); + assertGetterFields(getterByField, "integer"); NoPackageDomain domain = new NoPackageDomain(1); - assertEquals("Getter for 'integer' field returns wrong value", - 1, getterByField.get("integer").apply(domain)); + assertGetterValue(getterByField, domain, "integer", 1); } @Test public void should_contains_allSetters() { - Map> setterByField = new NoPackageDomainSetterSupplier().get(); - assertNotNull("Map is null", setterByField); - assertEquals("Map size not equal to 1", 1, setterByField.size()); - assertTrue("Map not contain setter for 'integer' field", setterByField.containsKey("integer")); - NoPackageDomain domain = new NoPackageDomain(0); - setterByField.get("integer").accept(domain, 1); - assertEquals("Setter for 'integer' field sets wrong value", 1, domain.getInteger()); + Map> setterByField = getSetterMap(new NoPackageDomainSetterSupplier()); + assertSize(setterByField, 1); + assertSetterFields(setterByField, "integer"); + NoPackageDomain domain = new NoPackageDomain(1); + assertSetterValue(setterByField, domain, "integer", 10, NoPackageDomain::getInteger); } } \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/BaseDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/BaseDomainTest.java new file mode 100644 index 0000000..0f40b75 --- /dev/null +++ b/demo/src/test/java/dev/alexengrig/metter/demo/BaseDomainTest.java @@ -0,0 +1,69 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo; + +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Function; +import java.util.function.Supplier; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public abstract class BaseDomainTest { + protected Map> getGetterMap(Supplier>> domainGetterSupplier) { + Map> getterByField = domainGetterSupplier.get(); + assertNotNull("Map is null", getterByField); + return getterByField; + } + + protected Map> getSetterMap(Supplier>> domainSetterSupplier) { + Map> setterByField = domainSetterSupplier.get(); + assertNotNull("Map is null", setterByField); + return setterByField; + } + + protected void assertSize(Map map, int size) { + assertEquals("Map size is incorrect", size, map.size()); + } + + protected void assertEmpty(Map map) { + assertTrue("Map isn't empty", map.isEmpty()); + } + + protected void assertGetterFields(Map> getterByField, String... fields) { + for (String field : fields) { + assertTrue("Map not contain getter for field '" + field + "'", getterByField.containsKey(field)); + } + } + + protected void assertSetterFields(Map> setterByField, String... fields) { + for (String field : fields) { + assertTrue("Map not contain setter for field '" + field + "'", setterByField.containsKey(field)); + } + } + + protected void assertGetterValue(Map> getterByField, T domain, String field, Object value) { + assertEquals("Getter for field '" + field + "' returns wrong value", value, getterByField.get(field).apply(domain)); + } + + protected void assertSetterValue(Map> setterByField, T domain, String field, Object value, Function getter) { + setterByField.get(field).accept(domain, value); + assertEquals("Setter for field '" + field + "' sets wrong value", value, getter.apply(domain)); + } +} diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/excluding/ExcludedDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/excluding/ExcludedDomainTest.java index 9f05cd3..3c8d7cd 100644 --- a/demo/src/test/java/dev/alexengrig/metter/demo/excluding/ExcludedDomainTest.java +++ b/demo/src/test/java/dev/alexengrig/metter/demo/excluding/ExcludedDomainTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 Alexengrig Dev. + * Copyright 2020-2021 Alexengrig Dev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package dev.alexengrig.metter.demo.excluding; +import dev.alexengrig.metter.demo.BaseDomainTest; import org.junit.Test; import java.util.Map; @@ -23,32 +24,26 @@ import java.util.function.Function; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -public class ExcludedDomainTest { +public class ExcludedDomainTest extends BaseDomainTest { @Test public void should_contains_allNotExcludedGetters() { - Map> getterByField = new ExcludedDomainGetterSupplier().get(); - assertNotNull("Map is null", getterByField); - assertEquals("Map size not equal to 1", 1, getterByField.size()); - assertTrue("Map not contain getter for 'integer' field", getterByField.containsKey("integer")); - assertFalse("Map contains getter for 'excluded' field", getterByField.containsKey("excluded")); + Map> getterByField = getGetterMap(new ExcludedDomainGetterSupplier()); + assertSize(getterByField, 1); + assertGetterFields(getterByField, "integer"); ExcludedDomain domain = new ExcludedDomain(1, 2); - assertEquals("Getter for 'integer' field returns wrong value", - 1, getterByField.get("integer").apply(domain)); + assertGetterValue(getterByField, domain, "integer", 1); + assertEquals("Excluded field value is incorrect", 2, domain.getExcluded()); } @Test public void should_contains_allNotExcludedSetters() { - Map> setterByField = new ExcludedDomainSetterSupplier().get(); - assertNotNull("Map is null", setterByField); - assertEquals("Map size not equal to 1", 1, setterByField.size()); - assertTrue("Map not contain setter for 'integer' field", setterByField.containsKey("integer")); - assertFalse("Map contains setter for 'excluded' field", setterByField.containsKey("excluded")); - ExcludedDomain domain = new ExcludedDomain(0, 2); - setterByField.get("integer").accept(domain, 1); - assertEquals("Setter for 'integer' field sets wrong value", 1, domain.getInteger()); + Map> setterByField = getSetterMap(new ExcludedDomainSetterSupplier()); + assertSize(setterByField, 1); + assertSetterFields(setterByField, "integer"); + ExcludedDomain domain = new ExcludedDomain(1, 2); + assertSetterValue(setterByField, domain, "integer", 10, ExcludedDomain::getInteger); + domain.setExcluded(20); + assertEquals("Excluded field value is incorrect", 20, domain.getExcluded()); } } \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/excluding/InheritedExcludedDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/excluding/InheritedExcludedDomainTest.java new file mode 100644 index 0000000..fd472ef --- /dev/null +++ b/demo/src/test/java/dev/alexengrig/metter/demo/excluding/InheritedExcludedDomainTest.java @@ -0,0 +1,52 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.excluding; + +import dev.alexengrig.metter.demo.BaseDomainTest; +import org.junit.Test; + +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Function; + +import static org.junit.Assert.assertEquals; + +public class InheritedExcludedDomainTest extends BaseDomainTest { + @Test + public void should_contains_allNotExcludedGettersWithSuper() { + Map> getterByField = getGetterMap(new InheritedExcludedDomainGetterSupplier()); + assertSize(getterByField, 1); + assertGetterFields(getterByField, "integer"); + InheritedExcludedDomain domain = new InheritedExcludedDomain(1, 2, 3); + assertGetterValue(getterByField, domain, "integer", 1); + assertEquals("Excluded field value is incorrect", 2, domain.getExcluded()); + assertEquals("Also excluded field value is incorrect", 3, domain.getAlsoExcluded()); + } + + @Test + public void should_contains_allNotExcludedSetters() { + Map> setterByField = getSetterMap(new InheritedExcludedDomainSetterSupplier()); + assertSize(setterByField, 1); + assertSetterFields(setterByField, "integer"); + InheritedExcludedDomain domain = new InheritedExcludedDomain(1, 2, 3); + assertSetterValue(setterByField, domain, "integer", 10, InheritedExcludedDomain::getInteger); + domain.setExcluded(20); + assertEquals("Excluded field value is incorrect", 20, domain.getExcluded()); + domain.setAlsoExcluded(30); + assertEquals("Also excluded field value is incorrect", 30, domain.getAlsoExcluded()); + } +} \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/including/IncludedDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/including/IncludedDomainTest.java index 66c062b..d1e0523 100644 --- a/demo/src/test/java/dev/alexengrig/metter/demo/including/IncludedDomainTest.java +++ b/demo/src/test/java/dev/alexengrig/metter/demo/including/IncludedDomainTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 Alexengrig Dev. + * Copyright 2020-2021 Alexengrig Dev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package dev.alexengrig.metter.demo.including; +import dev.alexengrig.metter.demo.BaseDomainTest; import org.junit.Test; import java.util.Map; @@ -23,32 +24,26 @@ import java.util.function.Function; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -public class IncludedDomainTest { +public class IncludedDomainTest extends BaseDomainTest { @Test public void should_contains_allIncludedGetters() { - Map> getterByField = new IncludedDomainGetterSupplier().get(); - assertNotNull("Map is null", getterByField); - assertEquals("Map size not equal to 1", 1, getterByField.size()); - assertTrue("Map not contain getter for 'included' field", getterByField.containsKey("included")); - assertFalse("Map contains getter for 'ignored' field", getterByField.containsKey("ignored")); + Map> getterByField = getGetterMap(new IncludedDomainGetterSupplier()); + assertSize(getterByField, 1); + assertGetterFields(getterByField, "included"); IncludedDomain domain = new IncludedDomain(1, 2); - assertEquals("Getter for 'included' field returns wrong value", - 1, getterByField.get("included").apply(domain)); + assertGetterValue(getterByField, domain, "included", 1); + assertEquals("Ignored field value is incorrect", 2, domain.getIgnored()); } @Test public void should_contains_allIncludedSetters() { - Map> setterByField = new IncludedDomainSetterSupplier().get(); - assertNotNull("Map is null", setterByField); - assertEquals("Map size not equal to 1", 1, setterByField.size()); - assertTrue("Map not contain setter for 'included' field", setterByField.containsKey("included")); - assertFalse("Map contains setter for 'ignored' field", setterByField.containsKey("ignored")); - IncludedDomain domain = new IncludedDomain(0, 2); - setterByField.get("included").accept(domain, 1); - assertEquals("Setter for 'included' field sets wrong value", 1, domain.getIncluded()); + Map> setterByField = getSetterMap(new IncludedDomainSetterSupplier()); + assertSize(setterByField, 1); + assertSetterFields(setterByField, "included"); + IncludedDomain domain = new IncludedDomain(1, 2); + assertSetterValue(setterByField, domain, "included", 10, IncludedDomain::getIncluded); + domain.setIgnored(20); + assertEquals("Ignored field value is incorrect", 20, domain.getIgnored()); } } \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/including/InheritedIncludedDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/including/InheritedIncludedDomainTest.java new file mode 100644 index 0000000..b1a2d3a --- /dev/null +++ b/demo/src/test/java/dev/alexengrig/metter/demo/including/InheritedIncludedDomainTest.java @@ -0,0 +1,54 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.including; + +import dev.alexengrig.metter.demo.BaseDomainTest; +import org.junit.Test; + +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Function; + +import static org.junit.Assert.assertEquals; + +public class InheritedIncludedDomainTest extends BaseDomainTest { + @Test + public void should_contains_allIncludedGetters() { + Map> getterByField = getGetterMap(new InheritedIncludedDomainGetterSupplier()); + assertSize(getterByField, 2); + assertGetterFields(getterByField, "included", "alsoIncluded"); + InheritedIncludedDomain domain = new InheritedIncludedDomain(1, 2, 3, 4); + assertGetterValue(getterByField, domain, "included", 1); + assertGetterValue(getterByField, domain, "alsoIncluded", 3); + assertEquals("Ignored field value is incorrect", 2, domain.getIgnored()); + assertEquals("Also ignored field value is incorrect", 4, domain.getAlsoIgnored()); + } + + @Test + public void should_contains_allIncludedSetters() { + Map> setterByField = getSetterMap(new InheritedIncludedDomainSetterSupplier()); + assertSize(setterByField, 2); + assertSetterFields(setterByField, "included", "alsoIncluded"); + InheritedIncludedDomain domain = new InheritedIncludedDomain(1, 2, 3, 4); + assertSetterValue(setterByField, domain, "included", 10, InheritedIncludedDomain::getIncluded); + assertSetterValue(setterByField, domain, "alsoIncluded", 30, InheritedIncludedDomain::getAlsoIncluded); + domain.setIgnored(20); + assertEquals("Ignored field value is incorrect", 20, domain.getIgnored()); + domain.setAlsoIgnored(40); + assertEquals("Also ignored field value is incorrect", 40, domain.getAlsoIgnored()); + } +} \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/includingandexcluding/IncludedAndExcludedDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/includingandexcluding/IncludedAndExcludedDomainTest.java index b8c2c54..15a7593 100644 --- a/demo/src/test/java/dev/alexengrig/metter/demo/includingandexcluding/IncludedAndExcludedDomainTest.java +++ b/demo/src/test/java/dev/alexengrig/metter/demo/includingandexcluding/IncludedAndExcludedDomainTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 Alexengrig Dev. + * Copyright 2020-2021 Alexengrig Dev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package dev.alexengrig.metter.demo.includingandexcluding; +import dev.alexengrig.metter.demo.BaseDomainTest; import org.junit.Test; import java.util.Map; @@ -23,36 +24,29 @@ import java.util.function.Function; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -public class IncludedAndExcludedDomainTest { +public class IncludedAndExcludedDomainTest extends BaseDomainTest { @Test public void should_contains_allIncludedGetters() { - Map> getterByField - = new IncludedAndExcludedDomainGetterSupplier().get(); - assertNotNull("Map is null", getterByField); - assertEquals("Map size not equal to 1", 1, getterByField.size()); - assertTrue("Map not contain getter for 'included' field", getterByField.containsKey("included")); - assertFalse("Map contains getter for 'ignored' field", getterByField.containsKey("ignored")); - assertFalse("Map contains getter for 'excluded' field", getterByField.containsKey("excluded")); + Map> getterByField = getGetterMap(new IncludedAndExcludedDomainGetterSupplier()); + assertSize(getterByField, 1); + assertGetterFields(getterByField, "included"); IncludedAndExcludedDomain domain = new IncludedAndExcludedDomain(1, 2, 3); - assertEquals("Getter for 'included' field returns wrong value", - 1, getterByField.get("included").apply(domain)); + assertGetterValue(getterByField, domain, "included", 1); + assertEquals("Ignored field value is incorrect", 2, domain.getIgnored()); + assertEquals("Excluded field value is incorrect", 3, domain.getExcluded()); } @Test public void should_contains_allIncludedSetters() { - Map> setterByField - = new IncludedAndExcludedDomainSetterSupplier().get(); - assertNotNull("Map is null", setterByField); - assertEquals("Map size not equal to 1", 1, setterByField.size()); - assertTrue("Map not contain setter for 'included' field", setterByField.containsKey("included")); - assertFalse("Map contains setter for 'ignored' field", setterByField.containsKey("ignored")); - assertFalse("Map contains setter for 'excluded' field", setterByField.containsKey("excluded")); - IncludedAndExcludedDomain domain = new IncludedAndExcludedDomain(0, 2, 3); - setterByField.get("included").accept(domain, 1); - assertEquals("Setter for 'included' field sets wrong value", 1, domain.getIncluded()); + Map> setterByField = getSetterMap(new IncludedAndExcludedDomainSetterSupplier()); + assertSize(setterByField, 1); + assertSetterFields(setterByField, "included"); + IncludedAndExcludedDomain domain = new IncludedAndExcludedDomain(1, 2, 3); + assertSetterValue(setterByField, domain, "included", 10, IncludedAndExcludedDomain::getIncluded); + domain.setIgnored(20); + assertEquals("Ignored field value is incorrect", 20, domain.getIgnored()); + domain.setExcluded(30); + assertEquals("Excluded field value is incorrect", 30, domain.getExcluded()); } } \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/includingandexcluding/InheritedIncludedAndExcludedDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/includingandexcluding/InheritedIncludedAndExcludedDomainTest.java new file mode 100644 index 0000000..cda9485 --- /dev/null +++ b/demo/src/test/java/dev/alexengrig/metter/demo/includingandexcluding/InheritedIncludedAndExcludedDomainTest.java @@ -0,0 +1,66 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.includingandexcluding; + +import dev.alexengrig.metter.demo.BaseDomainTest; +import org.junit.Test; + +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Function; + +import static org.junit.Assert.assertEquals; + +public class InheritedIncludedAndExcludedDomainTest extends BaseDomainTest { + @Test + public void should_contains_allIncludedGetters() { + Map> getterByField + = getGetterMap(new InheritedIncludedAndExcludedDomainGetterSupplier()); + assertSize(getterByField, 2); + assertGetterFields(getterByField, "included"); + assertGetterFields(getterByField, "alsoIncluded"); + InheritedIncludedAndExcludedDomain domain = + new InheritedIncludedAndExcludedDomain(1, 2, 3, 4, 5, 6); + assertGetterValue(getterByField, domain, "included", 1); + assertGetterValue(getterByField, domain, "alsoIncluded", 4); + assertEquals("Ignored field value is incorrect", 2, domain.getIgnored()); + assertEquals("Excluded field value is incorrect", 3, domain.getExcluded()); + assertEquals("AlsoIgnored field value is incorrect", 5, domain.getAlsoIgnored()); + assertEquals("AlsoExcluded field value is incorrect", 6, domain.getAlsoExcluded()); + } + + @Test + public void should_contains_allIncludedSetters() { + Map> setterByField = + getSetterMap(new InheritedIncludedAndExcludedDomainSetterSupplier()); + assertSize(setterByField, 2); + assertSetterFields(setterByField, "included"); + assertSetterFields(setterByField, "alsoIncluded"); + InheritedIncludedAndExcludedDomain domain = + new InheritedIncludedAndExcludedDomain(1, 2, 3, 4, 5, 6); + assertSetterValue(setterByField, domain, "included", 10, InheritedIncludedAndExcludedDomain::getIncluded); + assertSetterValue(setterByField, domain, "alsoIncluded", 40, InheritedIncludedAndExcludedDomain::getAlsoIncluded); + domain.setIgnored(20); + assertEquals("Ignored field value is incorrect", 20, domain.getIgnored()); + domain.setExcluded(30); + assertEquals("Excluded field value is incorrect", 30, domain.getExcluded()); + domain.setAlsoIgnored(50); + assertEquals("AlsoIgnored field value is incorrect", 50, domain.getAlsoIgnored()); + domain.setAlsoExcluded(60); + assertEquals("AlsoExcluded field value is incorrect", 60, domain.getAlsoExcluded()); + } +} \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/inheritance/FatherDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/inheritance/FatherDomainTest.java new file mode 100644 index 0000000..6aa3089 --- /dev/null +++ b/demo/src/test/java/dev/alexengrig/metter/demo/inheritance/FatherDomainTest.java @@ -0,0 +1,44 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.inheritance; + +import dev.alexengrig.metter.demo.BaseDomainTest; +import org.junit.Test; + +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Function; + +public class FatherDomainTest extends BaseDomainTest { + @Test + public void should_contains_allGettersWithSuper() { + Map> getterByField = getGetterMap(new FatherDomainGetterSupplier()); + assertSize(getterByField, 1); + assertGetterFields(getterByField, "fatherInt"); + FatherDomain domain = new FatherDomain(1); + assertGetterValue(getterByField, domain, "fatherInt", 1); + } + + @Test + public void should_contains_allSettersWithSuper() { + Map> setterByField = getSetterMap(new FatherDomainSetterSupplier()); + assertSize(setterByField, 1); + assertSetterFields(setterByField, "fatherInt"); + FatherDomain domain = new FatherDomain(1); + assertSetterValue(setterByField, domain, "fatherInt", 10, FatherDomain::getFatherInt); + } +} \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/inheritance/GrandsonDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/inheritance/GrandsonDomainTest.java new file mode 100644 index 0000000..c329856 --- /dev/null +++ b/demo/src/test/java/dev/alexengrig/metter/demo/inheritance/GrandsonDomainTest.java @@ -0,0 +1,48 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.inheritance; + +import dev.alexengrig.metter.demo.BaseDomainTest; +import org.junit.Test; + +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Function; + +public class GrandsonDomainTest extends BaseDomainTest { + @Test + public void should_contains_allGettersWithSuper() { + Map> getterByField = getGetterMap(new GrandsonDomainGetterSupplier()); + assertSize(getterByField, 3); + assertGetterFields(getterByField, "fatherInt", "sonInt", "grandsonInt"); + GrandsonDomain domain = new GrandsonDomain(1, 2, 3); + assertGetterValue(getterByField, domain, "fatherInt", 1); + assertGetterValue(getterByField, domain, "sonInt", 2); + assertGetterValue(getterByField, domain, "grandsonInt", 3); + } + + @Test + public void should_contains_allSettersWithSuper() { + Map> setterByField = getSetterMap(new GrandsonDomainSetterSupplier()); + assertSize(setterByField, 3); + assertSetterFields(setterByField, "fatherInt", "sonInt", "grandsonInt"); + GrandsonDomain domain = new GrandsonDomain(1, 2, 3); + assertSetterValue(setterByField, domain, "fatherInt", 10, GrandsonDomain::getFatherInt); + assertSetterValue(setterByField, domain, "sonInt", 20, GrandsonDomain::getSonInt); + assertSetterValue(setterByField, domain, "grandsonInt", 30, GrandsonDomain::getGrandsonInt); + } +} \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/inheritance/SonDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/inheritance/SonDomainTest.java new file mode 100644 index 0000000..8539260 --- /dev/null +++ b/demo/src/test/java/dev/alexengrig/metter/demo/inheritance/SonDomainTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.inheritance; + +import dev.alexengrig.metter.demo.BaseDomainTest; +import org.junit.Test; + +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Function; + +public class SonDomainTest extends BaseDomainTest { + @Test + public void should_contains_allGettersWithSuper() { + Map> getterByField = getGetterMap(new SonDomainGetterSupplier()); + assertSize(getterByField, 2); + assertGetterFields(getterByField, "fatherInt", "sonInt"); + SonDomain domain = new SonDomain(1, 2); + assertGetterValue(getterByField, domain, "fatherInt", 1); + assertGetterValue(getterByField, domain, "sonInt", 2); + } + + @Test + public void should_contains_allSettersWithSuper() { + Map> setterByField = getSetterMap(new SonDomainSetterSupplier()); + assertSize(setterByField, 2); + assertSetterFields(setterByField, "fatherInt", "sonInt"); + SonDomain domain = new SonDomain(1, 2); + assertSetterValue(setterByField, domain, "fatherInt", 10, SonDomain::getFatherInt); + assertSetterValue(setterByField, domain, "sonInt", 20, SonDomain::getSonInt); + } +} \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/lombokdata/InheritedLombokDataDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/lombokdata/InheritedLombokDataDomainTest.java new file mode 100644 index 0000000..d01a5d2 --- /dev/null +++ b/demo/src/test/java/dev/alexengrig/metter/demo/lombokdata/InheritedLombokDataDomainTest.java @@ -0,0 +1,48 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.lombokdata; + +import dev.alexengrig.metter.demo.BaseDomainTest; +import org.junit.Test; + +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Function; + +public class InheritedLombokDataDomainTest extends BaseDomainTest { + @Test + public void should_contains_allGetters() { + Map> getterByField = getGetterMap(new InheritedLombokDataDomainGetterSupplier()); + assertSize(getterByField, 3); + assertGetterFields(getterByField, "integer", "bool", "longer"); + InheritedLombokDataDomain domain = new InheritedLombokDataDomain(1, true, 3); + assertGetterValue(getterByField, domain, "integer", 1); + assertGetterValue(getterByField, domain, "bool", true); + assertGetterValue(getterByField, domain, "longer", 3L); + } + + @Test + public void should_contains_allSetters() { + Map> setterByField = getSetterMap(new InheritedLombokDataDomainSetterSupplier()); + assertSize(setterByField, 3); + assertSetterFields(setterByField, "integer", "bool", "longer"); + InheritedLombokDataDomain domain = new InheritedLombokDataDomain(1, true, 3); + assertSetterValue(setterByField, domain, "integer", 10, InheritedLombokDataDomain::getInteger); + assertSetterValue(setterByField, domain, "bool", false, InheritedLombokDataDomain::isBool); + assertSetterValue(setterByField, domain, "longer", 30L, InheritedLombokDataDomain::getLonger); + } +} \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/lombokdata/LombokDataDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/lombokdata/LombokDataDomainTest.java index c23d272..1c1210e 100644 --- a/demo/src/test/java/dev/alexengrig/metter/demo/lombokdata/LombokDataDomainTest.java +++ b/demo/src/test/java/dev/alexengrig/metter/demo/lombokdata/LombokDataDomainTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 Alexengrig Dev. + * Copyright 2020-2021 Alexengrig Dev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,42 +16,31 @@ package dev.alexengrig.metter.demo.lombokdata; +import dev.alexengrig.metter.demo.BaseDomainTest; import org.junit.Test; import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Function; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -public class LombokDataDomainTest { +public class LombokDataDomainTest extends BaseDomainTest { @Test public void should_contains_allGetters() { - Map> getterByField = new LombokDataDomainGetterSupplier().get(); - assertNotNull("Map is null", getterByField); - assertEquals("Map size not equal to 2", 2, getterByField.size()); - assertTrue("Map not contain getter for 'integer' field", getterByField.containsKey("integer")); - assertTrue("Map not contain getter for 'bool' field", getterByField.containsKey("bool")); + Map> getterByField = getGetterMap(new LombokDataDomainGetterSupplier()); + assertSize(getterByField, 2); + assertGetterFields(getterByField, "integer", "bool"); LombokDataDomain domain = new LombokDataDomain(1, true); - assertEquals("Getter for 'integer' field returns wrong value", - 1, getterByField.get("integer").apply(domain)); - assertEquals("Getter for 'bool' field returns wrong value", - true, getterByField.get("bool").apply(domain)); + assertGetterValue(getterByField, domain, "integer", 1); + assertGetterValue(getterByField, domain, "bool", true); } @Test public void should_contains_allSetters() { - Map> setterByField = new LombokDataDomainSetterSupplier().get(); - assertNotNull("Map is null", setterByField); - assertEquals("Map size not equal to 2", 2, setterByField.size()); - assertTrue("Map not contain setter for 'integer' field", setterByField.containsKey("integer")); - assertTrue("Map not contain setter for 'bool' field", setterByField.containsKey("bool")); - LombokDataDomain domain = new LombokDataDomain(0, false); - setterByField.get("integer").accept(domain, 1); - assertEquals("Setter for 'integer' field sets wrong value", 1, domain.getInteger()); - setterByField.get("bool").accept(domain, true); - assertTrue("Setter for 'bool' field sets wrong value", domain.isBool()); + Map> setterByField = getSetterMap(new LombokDataDomainSetterSupplier()); + assertSize(setterByField, 2); + assertSetterFields(setterByField, "integer", "bool"); + LombokDataDomain domain = new LombokDataDomain(1, true); + assertSetterValue(setterByField, domain, "integer", 10, LombokDataDomain::getInteger); + assertSetterValue(setterByField, domain, "bool", false, LombokDataDomain::isBool); } } \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/lombokgetterandsetter/InheritedLombokGetterAndSetterDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/lombokgetterandsetter/InheritedLombokGetterAndSetterDomainTest.java new file mode 100644 index 0000000..8f7844a --- /dev/null +++ b/demo/src/test/java/dev/alexengrig/metter/demo/lombokgetterandsetter/InheritedLombokGetterAndSetterDomainTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.lombokgetterandsetter; + +import dev.alexengrig.metter.demo.BaseDomainTest; +import org.junit.Test; + +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Function; + +public class InheritedLombokGetterAndSetterDomainTest extends BaseDomainTest { + @Test + public void should_contains_allGetters() { + Map> getterByField + = getGetterMap(new InheritedLombokGetterAndSetterDomainGetterSupplier()); + assertSize(getterByField, 3); + assertGetterFields(getterByField, "integer", "bool", "longer"); + InheritedLombokGetterAndSetterDomain domain = new InheritedLombokGetterAndSetterDomain(1, true, 3L); + assertGetterValue(getterByField, domain, "integer", 1); + assertGetterValue(getterByField, domain, "bool", true); + assertGetterValue(getterByField, domain, "longer", 3L); + } + + @Test + public void should_contains_allSetters() { + Map> setterByField + = getSetterMap(new InheritedLombokGetterAndSetterDomainSetterSupplier()); + assertSize(setterByField, 3); + assertSetterFields(setterByField, "integer", "bool", "longer"); + InheritedLombokGetterAndSetterDomain domain = new InheritedLombokGetterAndSetterDomain(1, true, 3L); + assertSetterValue(setterByField, domain, "integer", 10, InheritedLombokGetterAndSetterDomain::getInteger); + assertSetterValue(setterByField, domain, "bool", false, InheritedLombokGetterAndSetterDomain::isBool); + assertSetterValue(setterByField, domain, "longer", 30L, InheritedLombokGetterAndSetterDomain::getLonger); + } + +} \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/lombokgetterandsetter/LombokGetterAndSetterDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/lombokgetterandsetter/LombokGetterAndSetterDomainTest.java index f3d9e09..911422e 100644 --- a/demo/src/test/java/dev/alexengrig/metter/demo/lombokgetterandsetter/LombokGetterAndSetterDomainTest.java +++ b/demo/src/test/java/dev/alexengrig/metter/demo/lombokgetterandsetter/LombokGetterAndSetterDomainTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 Alexengrig Dev. + * Copyright 2020-2021 Alexengrig Dev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,44 +16,31 @@ package dev.alexengrig.metter.demo.lombokgetterandsetter; +import dev.alexengrig.metter.demo.BaseDomainTest; import org.junit.Test; import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Function; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -public class LombokGetterAndSetterDomainTest { +public class LombokGetterAndSetterDomainTest extends BaseDomainTest { @Test public void should_contains_allGetters() { - Map> getterByField - = new LombokGetterAndSetterDomainGetterSupplier().get(); - assertNotNull("Map is null", getterByField); - assertEquals("Map size not equal to 2", 2, getterByField.size()); - assertTrue("Map not contain getter for 'integer' field", getterByField.containsKey("integer")); - assertTrue("Map not contain getter for 'bool' field", getterByField.containsKey("bool")); + Map> getterByField = getGetterMap(new LombokGetterAndSetterDomainGetterSupplier()); + assertSize(getterByField, 2); + assertGetterFields(getterByField, "integer", "bool"); LombokGetterAndSetterDomain domain = new LombokGetterAndSetterDomain(1, true); - assertEquals("Getter for 'integer' field returns wrong value", - 1, getterByField.get("integer").apply(domain)); - assertEquals("Getter for 'bool' field returns wrong value", - true, getterByField.get("bool").apply(domain)); + assertGetterValue(getterByField, domain, "integer", 1); + assertGetterValue(getterByField, domain, "bool", true); } @Test public void should_contains_allSetters() { - Map> setterByField - = new LombokGetterAndSetterDomainSetterSupplier().get(); - assertNotNull("Map is null", setterByField); - assertEquals("Map size not equal to 2", 2, setterByField.size()); - assertTrue("Map not contain setter for 'integer' field", setterByField.containsKey("integer")); - assertTrue("Map not contain setter for 'bool' field", setterByField.containsKey("bool")); - LombokGetterAndSetterDomain domain = new LombokGetterAndSetterDomain(0, false); - setterByField.get("integer").accept(domain, 1); - assertEquals("Setter for 'integer' field sets wrong value", 1, domain.getInteger()); - setterByField.get("bool").accept(domain, true); - assertTrue("Setter for 'bool' field sets wrong value", domain.isBool()); + Map> setterByField = getSetterMap(new LombokGetterAndSetterDomainSetterSupplier()); + assertSize(setterByField, 2); + assertSetterFields(setterByField, "integer", "bool"); + LombokGetterAndSetterDomain domain = new LombokGetterAndSetterDomain(1, true); + assertSetterValue(setterByField, domain, "integer", 10, LombokGetterAndSetterDomain::getInteger); + assertSetterValue(setterByField, domain, "bool", false, LombokGetterAndSetterDomain::isBool); } } \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/lombokgettersandsetters/InheritedLombokGettersAndSettersDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/lombokgettersandsetters/InheritedLombokGettersAndSettersDomainTest.java new file mode 100644 index 0000000..a63750e --- /dev/null +++ b/demo/src/test/java/dev/alexengrig/metter/demo/lombokgettersandsetters/InheritedLombokGettersAndSettersDomainTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.lombokgettersandsetters; + +import dev.alexengrig.metter.demo.BaseDomainTest; +import org.junit.Test; + +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Function; + +public class InheritedLombokGettersAndSettersDomainTest extends BaseDomainTest { + @Test + public void should_contains_allGetters() { + Map> getterByField + = getGetterMap(new InheritedLombokGettersAndSettersDomainGetterSupplier()); + assertSize(getterByField, 3); + assertGetterFields(getterByField, "integer", "bool", "longer"); + InheritedLombokGettersAndSettersDomain domain = new InheritedLombokGettersAndSettersDomain(1, true, 2, 3); + assertGetterValue(getterByField, domain, "integer", 1); + assertGetterValue(getterByField, domain, "bool", true); + assertGetterValue(getterByField, domain, "longer", 3L); + } + + @Test + public void should_contains_allSetters() { + Map> setterByField + = getSetterMap(new InheritedLombokGettersAndSettersDomainSetterSupplier()); + assertSize(setterByField, 3); + assertSetterFields(setterByField, "integer", "bool", "longer"); + InheritedLombokGettersAndSettersDomain domain = new InheritedLombokGettersAndSettersDomain(1, true, 2, 3); + assertSetterValue(setterByField, domain, "integer", 10, InheritedLombokGettersAndSettersDomain::getInteger); + assertSetterValue(setterByField, domain, "bool", false, InheritedLombokGettersAndSettersDomain::isBool); + assertSetterValue(setterByField, domain, "longer", 30L, InheritedLombokGettersAndSettersDomain::getLonger); + } +} \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/lombokgettersandsetters/LombokGettersAndSettersDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/lombokgettersandsetters/LombokGettersAndSettersDomainTest.java index a68e4ff..4dcd85d 100644 --- a/demo/src/test/java/dev/alexengrig/metter/demo/lombokgettersandsetters/LombokGettersAndSettersDomainTest.java +++ b/demo/src/test/java/dev/alexengrig/metter/demo/lombokgettersandsetters/LombokGettersAndSettersDomainTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 Alexengrig Dev. + * Copyright 2020-2021 Alexengrig Dev. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,47 +16,33 @@ package dev.alexengrig.metter.demo.lombokgettersandsetters; +import dev.alexengrig.metter.demo.BaseDomainTest; import org.junit.Test; import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Function; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -public class LombokGettersAndSettersDomainTest { +public class LombokGettersAndSettersDomainTest extends BaseDomainTest { @Test public void should_contains_allGetters() { Map> getterByField - = new LombokGettersAndSettersDomainGetterSupplier().get(); - assertNotNull("Map is null", getterByField); - assertEquals("Map size not equal to 2", 2, getterByField.size()); - assertTrue("Map not contain getter for 'integer' field", getterByField.containsKey("integer")); - assertTrue("Map not contain getter for 'bool' field", getterByField.containsKey("bool")); - assertFalse("Map contains getter for 'ignored' field", getterByField.containsKey("ignored")); + = getGetterMap(new LombokGettersAndSettersDomainGetterSupplier()); + assertSize(getterByField, 2); + assertGetterFields(getterByField, "integer", "bool"); LombokGettersAndSettersDomain domain = new LombokGettersAndSettersDomain(1, true, 2); - assertEquals("Getter for 'integer' field returns wrong value", - 1, getterByField.get("integer").apply(domain)); - assertEquals("Getter for 'bool' field returns wrong value", - true, getterByField.get("bool").apply(domain)); + assertGetterValue(getterByField, domain, "integer", 1); + assertGetterValue(getterByField, domain, "bool", true); } @Test public void should_contains_allSetters() { Map> setterByField - = new LombokGettersAndSettersDomainSetterSupplier().get(); - assertNotNull("Map is null", setterByField); - assertEquals("Map size not equal to 2", 2, setterByField.size()); - assertTrue("Map not contain setter for 'integer' field", setterByField.containsKey("integer")); - assertTrue("Map not contain setter for 'bool' field", setterByField.containsKey("bool")); - assertFalse("Map contains setter for 'ignored' field", setterByField.containsKey("ignored")); - LombokGettersAndSettersDomain domain = new LombokGettersAndSettersDomain(0, false, 2); - setterByField.get("integer").accept(domain, 1); - assertEquals("Setter for 'integer' field sets wrong value", 1, domain.getInteger()); - setterByField.get("bool").accept(domain, true); - assertTrue("Setter for 'bool' field sets wrong value", domain.isBool()); + = getSetterMap(new LombokGettersAndSettersDomainSetterSupplier()); + assertSize(setterByField, 2); + assertSetterFields(setterByField, "integer", "bool"); + LombokGettersAndSettersDomain domain = new LombokGettersAndSettersDomain(1, true, 2); + assertSetterValue(setterByField, domain, "integer", 10, LombokGettersAndSettersDomain::getInteger); + assertSetterValue(setterByField, domain, "bool", false, LombokGettersAndSettersDomain::isBool); } } \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/privategettersandsetters/InheritedPrivateGettersAndSettersDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/privategettersandsetters/InheritedPrivateGettersAndSettersDomainTest.java new file mode 100644 index 0000000..b8ca888 --- /dev/null +++ b/demo/src/test/java/dev/alexengrig/metter/demo/privategettersandsetters/InheritedPrivateGettersAndSettersDomainTest.java @@ -0,0 +1,40 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.privategettersandsetters; + +import dev.alexengrig.metter.demo.BaseDomainTest; +import org.junit.Test; + +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Function; + +public class InheritedPrivateGettersAndSettersDomainTest extends BaseDomainTest { + @Test + public void should_ignores_privateGetters() { + Map> getterByField + = getGetterMap(new InheritedPrivateGettersAndSettersDomainGetterSupplier()); + assertEmpty(getterByField); + } + + @Test + public void should_ignores_privateSetters() { + Map> setterByField + = getSetterMap(new InheritedPrivateGettersAndSettersDomainSetterSupplier()); + assertEmpty(setterByField); + } +} \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/privategettersandsetters/PrivateGettersAndSettersDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/privategettersandsetters/PrivateGettersAndSettersDomainTest.java index 33fae6e..f6a1dc8 100644 --- a/demo/src/test/java/dev/alexengrig/metter/demo/privategettersandsetters/PrivateGettersAndSettersDomainTest.java +++ b/demo/src/test/java/dev/alexengrig/metter/demo/privategettersandsetters/PrivateGettersAndSettersDomainTest.java @@ -16,26 +16,25 @@ package dev.alexengrig.metter.demo.privategettersandsetters; +import dev.alexengrig.metter.demo.BaseDomainTest; import org.junit.Test; import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Function; -import static org.junit.Assert.assertTrue; - -public class PrivateGettersAndSettersDomainTest { +public class PrivateGettersAndSettersDomainTest extends BaseDomainTest { @Test public void should_ignores_privateGetters() { Map> getterByField - = new PrivateGettersAndSettersDomainGetterSupplier().get(); - assertTrue("Map is not empty", getterByField.isEmpty()); + = getGetterMap(new PrivateGettersAndSettersDomainGetterSupplier()); + assertEmpty(getterByField); } @Test public void should_ignores_privateSetters() { Map> setterByField - = new PrivateGettersAndSettersDomainSetterSupplier().get(); - assertTrue("Map is not empty", setterByField.isEmpty()); + = getSetterMap(new PrivateGettersAndSettersDomainSetterSupplier()); + assertEmpty(setterByField); } } diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/privatelombokgetterandsetter/InheritedPrivateLombokGetterAndSetterDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/privatelombokgetterandsetter/InheritedPrivateLombokGetterAndSetterDomainTest.java new file mode 100644 index 0000000..a17809b --- /dev/null +++ b/demo/src/test/java/dev/alexengrig/metter/demo/privatelombokgetterandsetter/InheritedPrivateLombokGetterAndSetterDomainTest.java @@ -0,0 +1,40 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.privatelombokgetterandsetter; + +import dev.alexengrig.metter.demo.BaseDomainTest; +import org.junit.Test; + +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Function; + +public class InheritedPrivateLombokGetterAndSetterDomainTest extends BaseDomainTest { + @Test + public void should_ignores_privateGetters() { + Map> getterByField + = getGetterMap(new InheritedPrivateLombokGetterAndSetterDomainGetterSupplier()); + assertEmpty(getterByField); + } + + @Test + public void should_ignores_privateSetters() { + Map> setterByField + = getSetterMap(new InheritedPrivateLombokGetterAndSetterDomainSetterSupplier()); + assertEmpty(setterByField); + } +} \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/privatelombokgetterandsetter/PrivateLombokGetterAndSetterDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/privatelombokgetterandsetter/PrivateLombokGetterAndSetterDomainTest.java index d9ad0d6..1a2fcf5 100644 --- a/demo/src/test/java/dev/alexengrig/metter/demo/privatelombokgetterandsetter/PrivateLombokGetterAndSetterDomainTest.java +++ b/demo/src/test/java/dev/alexengrig/metter/demo/privatelombokgetterandsetter/PrivateLombokGetterAndSetterDomainTest.java @@ -16,26 +16,25 @@ package dev.alexengrig.metter.demo.privatelombokgetterandsetter; +import dev.alexengrig.metter.demo.BaseDomainTest; import org.junit.Test; import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Function; -import static org.junit.Assert.assertTrue; - -public class PrivateLombokGetterAndSetterDomainTest { +public class PrivateLombokGetterAndSetterDomainTest extends BaseDomainTest { @Test public void should_ignores_privateGetters() { Map> getterByField - = new PrivateLombokGetterAndSetterDomainGetterSupplier().get(); - assertTrue("Map is not empty", getterByField.isEmpty()); + = getGetterMap(new PrivateLombokGetterAndSetterDomainGetterSupplier()); + assertEmpty(getterByField); } @Test public void should_ignores_privateSetters() { Map> setterByField - = new PrivateLombokGetterAndSetterDomainSetterSupplier().get(); - assertTrue("Map is not empty", setterByField.isEmpty()); + = getSetterMap(new PrivateLombokGetterAndSetterDomainSetterSupplier()); + assertEmpty(setterByField); } } diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/privatelombokgettersandsetters/InheritedPrivateLombokGettersAndSettersDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/privatelombokgettersandsetters/InheritedPrivateLombokGettersAndSettersDomainTest.java new file mode 100644 index 0000000..c6657ee --- /dev/null +++ b/demo/src/test/java/dev/alexengrig/metter/demo/privatelombokgettersandsetters/InheritedPrivateLombokGettersAndSettersDomainTest.java @@ -0,0 +1,40 @@ +/* + * Copyright 2020-2021 Alexengrig Dev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.alexengrig.metter.demo.privatelombokgettersandsetters; + +import dev.alexengrig.metter.demo.BaseDomainTest; +import org.junit.Test; + +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Function; + +public class InheritedPrivateLombokGettersAndSettersDomainTest extends BaseDomainTest { + @Test + public void should_ignores_privateGetters() { + Map> getterByField + = getGetterMap(new InheritedPrivateLombokGettersAndSettersDomainGetterSupplier()); + assertEmpty(getterByField); + } + + @Test + public void should_ignores_privateSetters() { + Map> setterByField + = getSetterMap(new InheritedPrivateLombokGettersAndSettersDomainSetterSupplier()); + assertEmpty(setterByField); + } +} \ No newline at end of file diff --git a/demo/src/test/java/dev/alexengrig/metter/demo/privatelombokgettersandsetters/PrivateLombokGettersAndSettersDomainTest.java b/demo/src/test/java/dev/alexengrig/metter/demo/privatelombokgettersandsetters/PrivateLombokGettersAndSettersDomainTest.java index 7f344b7..c6d5429 100644 --- a/demo/src/test/java/dev/alexengrig/metter/demo/privatelombokgettersandsetters/PrivateLombokGettersAndSettersDomainTest.java +++ b/demo/src/test/java/dev/alexengrig/metter/demo/privatelombokgettersandsetters/PrivateLombokGettersAndSettersDomainTest.java @@ -16,26 +16,25 @@ package dev.alexengrig.metter.demo.privatelombokgettersandsetters; +import dev.alexengrig.metter.demo.BaseDomainTest; import org.junit.Test; import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Function; -import static org.junit.Assert.assertTrue; - -public class PrivateLombokGettersAndSettersDomainTest { +public class PrivateLombokGettersAndSettersDomainTest extends BaseDomainTest { @Test public void should_ignores_privateGetters() { Map> getterByField - = new PrivateLombokGettersAndSettersDomainGetterSupplier().get(); - assertTrue("Map is not empty", getterByField.isEmpty()); + = getGetterMap(new PrivateLombokGettersAndSettersDomainGetterSupplier()); + assertEmpty(getterByField); } @Test public void should_ignores_privateSetters() { Map> setterByField - = new PrivateLombokGettersAndSettersDomainSetterSupplier().get(); - assertTrue("Map is not empty", setterByField.isEmpty()); + = getSetterMap(new PrivateLombokGettersAndSettersDomainSetterSupplier()); + assertEmpty(setterByField); } } \ No newline at end of file diff --git a/pom.xml b/pom.xml index 3cee610..eb43d77 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ ~ you may not use this file except in compliance with the License. ~ You may obtain a copy of the License at ~ - ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by applicable law or agreed to in writing, software ~ distributed under the License is distributed on an "AS IS" BASIS, diff --git a/src/main/java/dev/alexengrig/metter/element/descriptor/ElementDescriptor.java b/src/main/java/dev/alexengrig/metter/element/descriptor/ElementDescriptor.java index ff1ffc9..dea4e4c 100644 --- a/src/main/java/dev/alexengrig/metter/element/descriptor/ElementDescriptor.java +++ b/src/main/java/dev/alexengrig/metter/element/descriptor/ElementDescriptor.java @@ -26,7 +26,7 @@ * * @param type of element * @author Grig Alex - * @version 0.1.1 + * @version 0.2.0 * @since 0.1.1 */ public class ElementDescriptor { @@ -47,6 +47,16 @@ public ElementDescriptor(E element) { this.element = Objects.requireNonNull(element, "Element must not be null"); } + /** + * Returns an element. + * + * @return element + * @since 0.2.0 + */ + public E getElement() { + return element; + } + /** * Check if has an annotation by an annotation type. * diff --git a/src/main/java/dev/alexengrig/metter/element/package-info.java b/src/main/java/dev/alexengrig/metter/element/package-info.java index 257f4ec..7befa8a 100644 --- a/src/main/java/dev/alexengrig/metter/element/package-info.java +++ b/src/main/java/dev/alexengrig/metter/element/package-info.java @@ -18,7 +18,7 @@ * Elements. * * @author Grig Alex - * @version 0.1.1 + * @version 0.2.0 * @since 0.1.0 */ package dev.alexengrig.metter.element; \ No newline at end of file diff --git a/src/main/java/dev/alexengrig/metter/generator/GetterSupplierSourceGenerator.java b/src/main/java/dev/alexengrig/metter/generator/GetterSupplierSourceGenerator.java index 59bf3cb..e6684ff 100644 --- a/src/main/java/dev/alexengrig/metter/generator/GetterSupplierSourceGenerator.java +++ b/src/main/java/dev/alexengrig/metter/generator/GetterSupplierSourceGenerator.java @@ -43,6 +43,16 @@ protected GetterSupplierSourceGenerator(boolean withGeneratedDate) { super(withGeneratedDate); } + /** + * Returns {@code getterByField}. + * + * @return {@code getterByField} + */ + @Override + protected String getMapFieldName() { + return "getterByField"; + } + /** * {@inheritDoc} * @@ -56,4 +66,24 @@ protected GetterSupplierSourceGenerator(boolean withGeneratedDate) { protected String getMapValueType(String domainClassName) { return String.format("java.util.function.Function<%s, java.lang.Object>", domainClassName); } + + /** + * Returns {@code Getters}. + * + * @return {@code Getters} + */ + @Override + protected String getJavaDocTypeNameForClass() { + return "Getters"; + } + + /** + * Returns {@code getter}. + * + * @return {@code getter} + */ + @Override + protected String getJavaDocTypeName() { + return "getter"; + } } diff --git a/src/main/java/dev/alexengrig/metter/generator/MethodSupplierSourceGenerator.java b/src/main/java/dev/alexengrig/metter/generator/MethodSupplierSourceGenerator.java index c07a6ff..e40e4ec 100644 --- a/src/main/java/dev/alexengrig/metter/generator/MethodSupplierSourceGenerator.java +++ b/src/main/java/dev/alexengrig/metter/generator/MethodSupplierSourceGenerator.java @@ -25,7 +25,7 @@ * Base generator source of method supplier. * * @author Grig Alex - * @version 0.1.0 + * @version 0.2.0 * @since 0.1.0 */ public abstract class MethodSupplierSourceGenerator { @@ -67,9 +67,14 @@ protected MethodSupplierSourceGenerator(boolean withGeneratedDate) { public String generate(String className, String domainClassName, Map field2Method) { String packageName = getPackageName(className); String simpleClassName = getSimpleName(className); + String mapFieldName = getMapFieldName(); String mapValueType = getMapValueType(domainClassName); + String javaDocTypeName = getJavaDocTypeName(); return new LineJoiner() .ftIf(packageName != null, "package %s;\n", packageName) + .ln("/**") + .ft(" * %s supplier of {@link %s}.", getJavaDocTypeNameForClass(), domainClassName) + .ln(" */") .ln("@javax.annotation.Generated(") .ftIf(withGeneratedDate, " date = \"%s\",", LocalDateTime.now().toString()) .ft(" value = \"%s\")", getClass().getName()) @@ -80,15 +85,26 @@ public String generate(String className, String domainClassName, Map> {") .ln() + .ln(" /**") + .ft(" * Map, %s function by field name.", javaDocTypeName) + .ln(" */") .ln(" protected final java.util.Map<") .ln(" java.lang.String,") .ft(" %s", mapValueType) - .ln(" > getterByField;") + .ft(" > %s;", mapFieldName) .ln() + .ln(" /**") + .ln(" * Constructs this.") + .ln(" */") .ft(" public %s() {", simpleClassName) - .ln(" this.getterByField = createMap();") + .ft(" this.%s = createMap();", mapFieldName) .ln(" }") .ln() + .ln(" /**") + .ft(" * Creates map, %s function by field name.", javaDocTypeName) + .ln(" *") + .ft(" * @return map, %s function by field name", javaDocTypeName) + .ln(" */") .ln(" protected java.util.Map<") .ln(" java.lang.String,") .ft(" %s", mapValueType) @@ -97,16 +113,21 @@ public String generate(String className, String domainClassName, Map map = new java.util.HashMap<>(%d);", field2Method.size()) - .mp(" map.put(\"%s\",\n %s);", (f, g) -> new Object[]{f, g}, field2Method) + .mp(" map.put(\"%s\",\n %s);", (s, f) -> new Object[]{s, f}, field2Method) .ln(" return map;") .ln(" }") .ln() + .ln(" /**") + .ft(" * Returns map, %s function by field name.", javaDocTypeName) + .ln(" *") + .ft(" * @return map, %s function by field name", javaDocTypeName) + .ln(" */") .ln(" @Override") .ln(" public java.util.Map<") .ln(" java.lang.String,") .ft(" %s", mapValueType) .ln(" > get() {") - .ln(" return getterByField;") + .ft(" return %s;", mapFieldName) .ln(" }") .ln("}") .toString(); @@ -139,6 +160,14 @@ protected String getSimpleName(String className) { return className.substring(lastIndexOfDot + 1); } + /** + * Returns a field name of map. + * + * @return field name of map + * @since 0.2.0 + */ + protected abstract String getMapFieldName(); + /** * Returns a type of map value from a domain class name. * @@ -147,4 +176,20 @@ protected String getSimpleName(String className) { * @since 0.1.0 */ protected abstract String getMapValueType(String domainClassName); + + /** + * Returns a type name for JavaDoc in class. + * + * @return type name for JavaDoc in class + * @since 0.2.0 + */ + protected abstract String getJavaDocTypeNameForClass(); + + /** + * Returns a type name for JavaDoc. + * + * @return type name for JavaDoc + * @since 0.2.0 + */ + protected abstract String getJavaDocTypeName(); } diff --git a/src/main/java/dev/alexengrig/metter/generator/SetterSupplierSourceGenerator.java b/src/main/java/dev/alexengrig/metter/generator/SetterSupplierSourceGenerator.java index 1b998a9..176cd72 100644 --- a/src/main/java/dev/alexengrig/metter/generator/SetterSupplierSourceGenerator.java +++ b/src/main/java/dev/alexengrig/metter/generator/SetterSupplierSourceGenerator.java @@ -20,7 +20,7 @@ * Generator source of setter supplier. * * @author Grig Alex - * @version 0.1.0 + * @version 0.2.0 * @since 0.1.0 */ public class SetterSupplierSourceGenerator extends MethodSupplierSourceGenerator { @@ -43,6 +43,17 @@ protected SetterSupplierSourceGenerator(boolean withGeneratedDate) { super(withGeneratedDate); } + /** + * Returns {@code setterByField}. + * + * @return {@code setterByField} + * @since 0.2.0 + */ + @Override + protected String getMapFieldName() { + return "setterByField"; + } + /** * {@inheritDoc} * @@ -56,4 +67,26 @@ protected SetterSupplierSourceGenerator(boolean withGeneratedDate) { protected String getMapValueType(String domainClassName) { return String.format("java.util.function.BiConsumer<%s, java.lang.Object>", domainClassName); } + + /** + * Returns {@code Setters}. + * + * @return {@code Setters} + * @since 0.2.0 + */ + @Override + protected String getJavaDocTypeNameForClass() { + return "Setters"; + } + + /** + * Returns {@code setter}. + * + * @return {@code setter} + * @since 0.2.0 + */ + @Override + protected String getJavaDocTypeName() { + return "setter"; + } } diff --git a/src/main/java/dev/alexengrig/metter/generator/package-info.java b/src/main/java/dev/alexengrig/metter/generator/package-info.java index 3f8ca0a..fbcf384 100644 --- a/src/main/java/dev/alexengrig/metter/generator/package-info.java +++ b/src/main/java/dev/alexengrig/metter/generator/package-info.java @@ -18,7 +18,7 @@ * Generators. * * @author Grig Alex - * @version 0.1.1 + * @version 0.2.0 * @since 0.1.0 */ package dev.alexengrig.metter.generator; \ No newline at end of file diff --git a/src/main/java/dev/alexengrig/metter/package-info.java b/src/main/java/dev/alexengrig/metter/package-info.java index ae2bd35..34402c6 100644 --- a/src/main/java/dev/alexengrig/metter/package-info.java +++ b/src/main/java/dev/alexengrig/metter/package-info.java @@ -22,7 +22,7 @@ * {@link dev.alexengrig.metter.annotation.SetterSupplier} for generation setter supplier. * * @author Grig Alex - * @version 0.1.1 + * @version 0.2.0 * @since 0.1.0 */ package dev.alexengrig.metter; \ No newline at end of file diff --git a/src/main/java/dev/alexengrig/metter/processor/BaseMethodSupplierProcessor.java b/src/main/java/dev/alexengrig/metter/processor/BaseMethodSupplierProcessor.java index 8d66011..9c328a2 100644 --- a/src/main/java/dev/alexengrig/metter/processor/BaseMethodSupplierProcessor.java +++ b/src/main/java/dev/alexengrig/metter/processor/BaseMethodSupplierProcessor.java @@ -32,13 +32,14 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Base processor of method supplier. * * @param type of annotation * @author Grig Alex - * @version 0.1.1 + * @version 0.2.0 * @since 0.1.0 */ public abstract class BaseMethodSupplierProcessor extends BaseProcessor { @@ -185,14 +186,17 @@ protected Map createField2MethodMap(TypeDescriptor type) { } /** - * Returns fields from a type descriptor. + * Returns fields from a type descriptor with fields of super classes. * * @param type descriptor - * @return fields from {@code type} + * @return fields from {@code type} with fields of super classes * @since 0.1.0 */ protected Set getFields(TypeDescriptor type) { - Set fields = type.getFields(); + Set superTypes = getAllSuperTypes(type); + Set fields = Stream.concat(Stream.of(type), superTypes.stream()) + .flatMap(descriptor -> descriptor.getFields().stream()) + .collect(Collectors.toSet()); Set includedFields = getIncludedFields(type); Set excludedFields = getExcludedFields(type); if (includedFields.isEmpty() && excludedFields.isEmpty()) { diff --git a/src/main/java/dev/alexengrig/metter/processor/BaseProcessor.java b/src/main/java/dev/alexengrig/metter/processor/BaseProcessor.java index ae6e10e..4368a07 100644 --- a/src/main/java/dev/alexengrig/metter/processor/BaseProcessor.java +++ b/src/main/java/dev/alexengrig/metter/processor/BaseProcessor.java @@ -16,17 +16,25 @@ package dev.alexengrig.metter.processor; +import dev.alexengrig.metter.element.descriptor.TypeDescriptor; import dev.alexengrig.metter.util.Exceptions; import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.RoundEnvironment; import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; import javax.lang.model.element.TypeElement; +import javax.lang.model.type.DeclaredType; +import javax.lang.model.type.TypeKind; import javax.tools.Diagnostic; import java.lang.annotation.Annotation; import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Queue; import java.util.Set; +import java.util.stream.Collectors; /** * Base processor. @@ -34,11 +42,18 @@ * @param type of annotation * @param type of element * @author Grig Alex - * @version 0.1.1 + * @version 0.2.0 * @see javax.annotation.processing.AbstractProcessor * @since 0.1.0 */ public abstract class BaseProcessor extends AbstractProcessor { + /** + * {@link Class#getName()} for {@link Object}. + * + * @since 0.2.0 + */ + protected static final String JAVA_LANG_OBJECT_CLASS_NAME = Object.class.getName(); + /** * Annotation class. * @@ -105,6 +120,36 @@ protected void error(String message, Throwable throwable) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message + System.lineSeparator() + stackTrace); } + /** + * Returns all super type descriptors for a type descriptor. + * + * @param typeDescriptor type descriptor + * @return all super type descriptors for {@code typeDescriptor} + * @since 0.2.0 + */ + protected Set getAllSuperTypes(TypeDescriptor typeDescriptor) { + Set target = new HashSet<>(); + Queue queue = new LinkedList<>(); + queue.add(typeDescriptor.getElement()); + while (!queue.isEmpty()) { + TypeElement type = queue.remove(); + Set superTypes = processingEnv.getTypeUtils().directSupertypes(type.asType()) + .stream() + .filter(typeMirror -> typeMirror.getKind() == TypeKind.DECLARED) + .map(DeclaredType.class::cast) + .map(DeclaredType::asElement) + .filter(element -> element.getKind() == ElementKind.CLASS) + .filter(element -> !JAVA_LANG_OBJECT_CLASS_NAME.equals(element.toString())) + .map(TypeElement.class::cast) + .collect(Collectors.toSet()); + if (!superTypes.isEmpty()) { + target.addAll(superTypes.stream().map(TypeDescriptor::new).collect(Collectors.toSet())); + queue.addAll(superTypes); + } + } + return target; + } + /** * Returns an empty set. * diff --git a/src/main/java/dev/alexengrig/metter/processor/package-info.java b/src/main/java/dev/alexengrig/metter/processor/package-info.java index 0c1400b..78226b0 100644 --- a/src/main/java/dev/alexengrig/metter/processor/package-info.java +++ b/src/main/java/dev/alexengrig/metter/processor/package-info.java @@ -18,7 +18,7 @@ * Processors. * * @author Grig Alex - * @version 0.1.1 + * @version 0.2.0 * @since 0.1.0 */ package dev.alexengrig.metter.processor; \ No newline at end of file diff --git a/src/test/java/dev/alexengrig/metter/ElementMocks.java b/src/test/java/dev/alexengrig/metter/ElementMocks.java index 544e9f4..40d6cce 100644 --- a/src/test/java/dev/alexengrig/metter/ElementMocks.java +++ b/src/test/java/dev/alexengrig/metter/ElementMocks.java @@ -44,12 +44,16 @@ public final class ElementMocks { public static Element annotatedElement(Class type) { - Element element = mock(Element.class); + Element element = element(); A annotation = annotationMock(type); when(element.getAnnotation(type)).thenReturn(annotation); return element; } + public static Element element() { + return mock(Element.class); + } + public static VariableElement fieldMock() { VariableElement mock = mock(VariableElement.class); when(mock.getKind()).thenReturn(ElementKind.FIELD); diff --git a/src/test/java/dev/alexengrig/metter/element/descriptor/ElementDescriptorTest.java b/src/test/java/dev/alexengrig/metter/element/descriptor/ElementDescriptorTest.java index 75071a7..1babe2b 100644 --- a/src/test/java/dev/alexengrig/metter/element/descriptor/ElementDescriptorTest.java +++ b/src/test/java/dev/alexengrig/metter/element/descriptor/ElementDescriptorTest.java @@ -22,9 +22,16 @@ import javax.lang.model.element.Element; import java.util.Optional; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; class ElementDescriptorTest { + @Test + void should_return_element() { + Element element = ElementMocks.element(); + ElementDescriptor descriptor = new ElementDescriptor<>(element); + assertSame(element, descriptor.getElement(), "Element is incorrect"); + } @Test void should_return_annotation() { diff --git a/src/test/java/dev/alexengrig/metter/generator/GetterSupplierSourceGeneratorTest.java b/src/test/java/dev/alexengrig/metter/generator/GetterSupplierSourceGeneratorTest.java index 6995bae..14158c0 100644 --- a/src/test/java/dev/alexengrig/metter/generator/GetterSupplierSourceGeneratorTest.java +++ b/src/test/java/dev/alexengrig/metter/generator/GetterSupplierSourceGeneratorTest.java @@ -30,6 +30,9 @@ class GetterSupplierSourceGeneratorTest { static { SNAPSHOT_OF_SOURCE_WITHOUT_PACKAGE = "" + + "/**\n" + + " * Getters supplier of {@link MyDomain}.\n" + + " */\n" + "@javax.annotation.Generated(\n" + " value = \"dev.alexengrig.metter.generator.GetterSupplierSourceGenerator\")\n" + "public class MyClass implements\n" + @@ -39,15 +42,26 @@ class GetterSupplierSourceGeneratorTest { " java.util.function.Function\n" + " >> {\n" + "\n" + + " /**\n" + + " * Map, getter function by field name.\n" + + " */\n" + " protected final java.util.Map<\n" + " java.lang.String,\n" + " java.util.function.Function\n" + " > getterByField;\n" + "\n" + + " /**\n" + + " * Constructs this.\n" + + " */\n" + " public MyClass() {\n" + " this.getterByField = createMap();\n" + " }\n" + "\n" + + " /**\n" + + " * Creates map, getter function by field name.\n" + + " *\n" + + " * @return map, getter function by field name\n" + + " */\n" + " protected java.util.Map<\n" + " java.lang.String,\n" + " java.util.function.Function\n" + @@ -65,6 +79,11 @@ class GetterSupplierSourceGeneratorTest { " return map;\n" + " }\n" + "\n" + + " /**\n" + + " * Returns map, getter function by field name.\n" + + " *\n" + + " * @return map, getter function by field name\n" + + " */\n" + " @Override\n" + " public java.util.Map<\n" + " java.lang.String,\n" + @@ -76,6 +95,9 @@ class GetterSupplierSourceGeneratorTest { SNAPSHOT_OF_SOURCE_WITH_PACKAGE = "" + "package my.company;\n" + "\n" + + "/**\n" + + " * Getters supplier of {@link my.company.MyDomain}.\n" + + " */\n" + "@javax.annotation.Generated(\n" + " value = \"dev.alexengrig.metter.generator.GetterSupplierSourceGenerator\")\n" + "public class MyClass implements\n" + @@ -85,15 +107,26 @@ class GetterSupplierSourceGeneratorTest { " java.util.function.Function\n" + " >> {\n" + "\n" + + " /**\n" + + " * Map, getter function by field name.\n" + + " */\n" + " protected final java.util.Map<\n" + " java.lang.String,\n" + " java.util.function.Function\n" + " > getterByField;\n" + "\n" + + " /**\n" + + " * Constructs this.\n" + + " */\n" + " public MyClass() {\n" + " this.getterByField = createMap();\n" + " }\n" + "\n" + + " /**\n" + + " * Creates map, getter function by field name.\n" + + " *\n" + + " * @return map, getter function by field name\n" + + " */\n" + " protected java.util.Map<\n" + " java.lang.String,\n" + " java.util.function.Function\n" + @@ -111,6 +144,11 @@ class GetterSupplierSourceGeneratorTest { " return map;\n" + " }\n" + "\n" + + " /**\n" + + " * Returns map, getter function by field name.\n" + + " *\n" + + " * @return map, getter function by field name\n" + + " */\n" + " @Override\n" + " public java.util.Map<\n" + " java.lang.String,\n" + diff --git a/src/test/java/dev/alexengrig/metter/generator/MethodSupplierSourceGeneratorTest.java b/src/test/java/dev/alexengrig/metter/generator/MethodSupplierSourceGeneratorTest.java index ed0eda5..8b11073 100644 --- a/src/test/java/dev/alexengrig/metter/generator/MethodSupplierSourceGeneratorTest.java +++ b/src/test/java/dev/alexengrig/metter/generator/MethodSupplierSourceGeneratorTest.java @@ -31,6 +31,9 @@ class MethodSupplierSourceGeneratorTest { static { SNAPSHOT_OF_SOURCE_WITHOUT_PACKAGE = "" + + "/**\n" + + " * Types supplier of {@link MyDomain}.\n" + + " */\n" + "@javax.annotation.Generated(\n" + " value = \"dev.alexengrig.metter.generator.MethodSupplierSourceGeneratorTest$1\")\n" + "public class MyClass implements\n" + @@ -40,15 +43,26 @@ class MethodSupplierSourceGeneratorTest { " Object\n" + " >> {\n" + "\n" + + " /**\n" + + " * Map, type function by field name.\n" + + " */\n" + " protected final java.util.Map<\n" + " java.lang.String,\n" + " Object\n" + - " > getterByField;\n" + + " > typeByField;\n" + "\n" + + " /**\n" + + " * Constructs this.\n" + + " */\n" + " public MyClass() {\n" + - " this.getterByField = createMap();\n" + + " this.typeByField = createMap();\n" + " }\n" + "\n" + + " /**\n" + + " * Creates map, type function by field name.\n" + + " *\n" + + " * @return map, type function by field name\n" + + " */\n" + " protected java.util.Map<\n" + " java.lang.String,\n" + " Object\n" + @@ -62,17 +76,25 @@ class MethodSupplierSourceGeneratorTest { " return map;\n" + " }\n" + "\n" + + " /**\n" + + " * Returns map, type function by field name.\n" + + " *\n" + + " * @return map, type function by field name\n" + + " */\n" + " @Override\n" + " public java.util.Map<\n" + " java.lang.String,\n" + " Object\n" + " > get() {\n" + - " return getterByField;\n" + + " return typeByField;\n" + " }\n" + "}\n"; SNAPSHOT_OF_SOURCE_WITH_PACKAGE = "" + "package my.company;\n" + "\n" + + "/**\n" + + " * Types supplier of {@link my.company.MyDomain}.\n" + + " */\n" + "@javax.annotation.Generated(\n" + " value = \"dev.alexengrig.metter.generator.MethodSupplierSourceGeneratorTest$1\")\n" + "public class MyClass implements\n" + @@ -82,15 +104,26 @@ class MethodSupplierSourceGeneratorTest { " Object\n" + " >> {\n" + "\n" + + " /**\n" + + " * Map, type function by field name.\n" + + " */\n" + " protected final java.util.Map<\n" + " java.lang.String,\n" + " Object\n" + - " > getterByField;\n" + + " > typeByField;\n" + "\n" + + " /**\n" + + " * Constructs this.\n" + + " */\n" + " public MyClass() {\n" + - " this.getterByField = createMap();\n" + + " this.typeByField = createMap();\n" + " }\n" + "\n" + + " /**\n" + + " * Creates map, type function by field name.\n" + + " *\n" + + " * @return map, type function by field name\n" + + " */\n" + " protected java.util.Map<\n" + " java.lang.String,\n" + " Object\n" + @@ -108,31 +141,66 @@ class MethodSupplierSourceGeneratorTest { " return map;\n" + " }\n" + "\n" + + " /**\n" + + " * Returns map, type function by field name.\n" + + " *\n" + + " * @return map, type function by field name\n" + + " */\n" + " @Override\n" + " public java.util.Map<\n" + " java.lang.String,\n" + " Object\n" + " > get() {\n" + - " return getterByField;\n" + + " return typeByField;\n" + " }\n" + "}\n"; } final MethodSupplierSourceGenerator generator = new MethodSupplierSourceGenerator(false) { + @Override + protected String getMapFieldName() { + return "typeByField"; + } + @Override protected String getMapValueType(String domainClassName) { return "Object"; } + + @Override + protected String getJavaDocTypeNameForClass() { + return "Types"; + } + + @Override + protected String getJavaDocTypeName() { + return "type"; + } }; @Test void should_create_instance() { // coverage MethodSupplierSourceGenerator generator = new MethodSupplierSourceGenerator() { + @Override + protected String getMapFieldName() { + return null; + } + @Override protected String getMapValueType(String domainClassName) { return null; } + + @Override + protected String getJavaDocTypeNameForClass() { + return null; + } + + @Override + protected String getJavaDocTypeName() { + return null; + } }; assertNotNull(generator); } diff --git a/src/test/java/dev/alexengrig/metter/generator/SetterSupplierSourceGeneratorTest.java b/src/test/java/dev/alexengrig/metter/generator/SetterSupplierSourceGeneratorTest.java index 9fd4da4..1da614b 100644 --- a/src/test/java/dev/alexengrig/metter/generator/SetterSupplierSourceGeneratorTest.java +++ b/src/test/java/dev/alexengrig/metter/generator/SetterSupplierSourceGeneratorTest.java @@ -30,6 +30,9 @@ class SetterSupplierSourceGeneratorTest { static { SNAPSHOT_OF_SOURCE_WITHOUT_PACKAGE = "" + + "/**\n" + + " * Setters supplier of {@link MyDomain}.\n" + + " */\n" + "@javax.annotation.Generated(\n" + " value = \"dev.alexengrig.metter.generator.SetterSupplierSourceGenerator\")\n" + "public class MyClass implements\n" + @@ -39,15 +42,26 @@ class SetterSupplierSourceGeneratorTest { " java.util.function.BiConsumer\n" + " >> {\n" + "\n" + + " /**\n" + + " * Map, setter function by field name.\n" + + " */\n" + " protected final java.util.Map<\n" + " java.lang.String,\n" + " java.util.function.BiConsumer\n" + - " > getterByField;\n" + + " > setterByField;\n" + "\n" + + " /**\n" + + " * Constructs this.\n" + + " */\n" + " public MyClass() {\n" + - " this.getterByField = createMap();\n" + + " this.setterByField = createMap();\n" + " }\n" + "\n" + + " /**\n" + + " * Creates map, setter function by field name.\n" + + " *\n" + + " * @return map, setter function by field name\n" + + " */\n" + " protected java.util.Map<\n" + " java.lang.String,\n" + " java.util.function.BiConsumer\n" + @@ -65,17 +79,25 @@ class SetterSupplierSourceGeneratorTest { " return map;\n" + " }\n" + "\n" + + " /**\n" + + " * Returns map, setter function by field name.\n" + + " *\n" + + " * @return map, setter function by field name\n" + + " */\n" + " @Override\n" + " public java.util.Map<\n" + " java.lang.String,\n" + " java.util.function.BiConsumer\n" + " > get() {\n" + - " return getterByField;\n" + + " return setterByField;\n" + " }\n" + "}\n"; SNAPSHOT_OF_SOURCE_WITH_PACKAGE = "" + "package my.company;\n" + "\n" + + "/**\n" + + " * Setters supplier of {@link my.company.MyDomain}.\n" + + " */\n" + "@javax.annotation.Generated(\n" + " value = \"dev.alexengrig.metter.generator.SetterSupplierSourceGenerator\")\n" + "public class MyClass implements\n" + @@ -85,15 +107,26 @@ class SetterSupplierSourceGeneratorTest { " java.util.function.BiConsumer\n" + " >> {\n" + "\n" + + " /**\n" + + " * Map, setter function by field name.\n" + + " */\n" + " protected final java.util.Map<\n" + " java.lang.String,\n" + " java.util.function.BiConsumer\n" + - " > getterByField;\n" + + " > setterByField;\n" + "\n" + + " /**\n" + + " * Constructs this.\n" + + " */\n" + " public MyClass() {\n" + - " this.getterByField = createMap();\n" + + " this.setterByField = createMap();\n" + " }\n" + "\n" + + " /**\n" + + " * Creates map, setter function by field name.\n" + + " *\n" + + " * @return map, setter function by field name\n" + + " */\n" + " protected java.util.Map<\n" + " java.lang.String,\n" + " java.util.function.BiConsumer\n" + @@ -111,12 +144,17 @@ class SetterSupplierSourceGeneratorTest { " return map;\n" + " }\n" + "\n" + + " /**\n" + + " * Returns map, setter function by field name.\n" + + " *\n" + + " * @return map, setter function by field name\n" + + " */\n" + " @Override\n" + " public java.util.Map<\n" + " java.lang.String,\n" + " java.util.function.BiConsumer\n" + " > get() {\n" + - " return getterByField;\n" + + " return setterByField;\n" + " }\n" + "}\n"; } diff --git a/src/test/java/dev/alexengrig/metter/processor/BaseMethodSupplierProcessorTest.java b/src/test/java/dev/alexengrig/metter/processor/BaseMethodSupplierProcessorTest.java index 2ebaf8d..be853fd 100644 --- a/src/test/java/dev/alexengrig/metter/processor/BaseMethodSupplierProcessorTest.java +++ b/src/test/java/dev/alexengrig/metter/processor/BaseMethodSupplierProcessorTest.java @@ -29,9 +29,14 @@ import javax.annotation.processing.Messager; import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; import javax.lang.model.element.Name; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; +import javax.lang.model.type.DeclaredType; +import javax.lang.model.type.TypeKind; +import javax.lang.model.type.TypeMirror; +import javax.lang.model.util.Types; import javax.tools.Diagnostic; import javax.tools.JavaFileObject; import java.io.IOException; @@ -48,12 +53,14 @@ import static dev.alexengrig.metter.ElementMocks.nameMock; import static dev.alexengrig.metter.ElementMocks.typeElementMock; import static java.lang.System.lineSeparator; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyMap; +import static org.mockito.ArgumentMatchers.same; import static org.mockito.Mockito.CALLS_REAL_METHODS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -282,10 +289,17 @@ protected String getMethod(FieldDescriptor field) { when(filer.createSourceFile(any())).thenReturn(file); ProcessingEnvironment environment = mock(ProcessingEnvironment.class); when(environment.getFiler()).thenReturn(filer); + + TypeMirror typeMirror = mock(TypeMirror.class); + Types types = mock(Types.class); + when(types.directSupertypes(same(typeMirror))).thenReturn(Collections.emptyList()); + when(environment.getTypeUtils()).thenReturn(types); + VariableElement variableElement = fieldMock("field", String.class); TypeElement typeElement = typeElementMock(String.class); Mockito.>when(typeElement.getEnclosedElements()) .thenReturn(Collections.singletonList(variableElement)); + when(typeElement.asType()).thenReturn(typeMirror); processor.init(environment); processor.process(typeElement); @@ -304,6 +318,18 @@ void should_create_field2MethodMap() { fieldMock("field1"), fieldMock("field2"))); TypeDescriptor typeDescriptor = new TypeDescriptor(typeElement); + + TypeMirror typeMirror = mock(TypeMirror.class); + when(typeElement.asType()).thenReturn(typeMirror); + + Types types = mock(Types.class); + when(types.directSupertypes(same(typeMirror))).thenReturn(Collections.emptyList()); + + ProcessingEnvironment environment = mock(ProcessingEnvironment.class); + when(environment.getTypeUtils()).thenReturn(types); + + processor.init(environment); + Map field2MethodMap = processor.createField2MethodMap(typeDescriptor); assertEquals(2, field2MethodMap.size(), "Size of field-method map is incorrect"); assertTrue(field2MethodMap.containsKey("field1"), "Field-method map has no 'field1'"); @@ -314,21 +340,90 @@ void should_create_field2MethodMap() { void should_return_fields() { BaseMethodSupplierProcessor processor = getMock(); TypeDescriptor typeDescriptor = mock(TypeDescriptor.class); + + TypeMirror typeMirror = mock(TypeMirror.class); + TypeElement typeElement = mock(TypeElement.class); + when(typeElement.asType()).thenReturn(typeMirror); + when(typeDescriptor.getElement()).thenReturn(typeElement); + FieldDescriptor field = new FieldDescriptor(fieldMock("field")); when(typeDescriptor.getFields()).thenReturn(Collections.singleton(field)); + + Types types = mock(Types.class); + when(types.directSupertypes(same(typeMirror))).thenReturn(Collections.emptyList()); + + ProcessingEnvironment environment = mock(ProcessingEnvironment.class); + when(environment.getTypeUtils()).thenReturn(types); + + processor.init(environment); Set fields = processor.getFields(typeDescriptor); assertEquals(1, fields.size(), "Number of fields does not equal to 1"); assertEquals("field", fields.iterator().next().getName(), "Field name does not equal to 'field'"); } + @Test + void should_return_fields_with_superClassFields() { + VariableElement superField = fieldMock("superField"); + + TypeElement superTypeElement = mock(TypeElement.class); + Mockito.>when(superTypeElement.getEnclosedElements()) + .thenReturn(Collections.singletonList(superField)); + when(superTypeElement.getKind()).thenReturn(ElementKind.CLASS); + when(superTypeElement.toString()).thenReturn("NoObject"); + + DeclaredType declaredType = mock(DeclaredType.class); + when(declaredType.asElement()).thenReturn(superTypeElement); + when(declaredType.getKind()).thenReturn(TypeKind.DECLARED); + + TypeMirror typeMirror = mock(TypeMirror.class); + TypeElement typeElement = mock(TypeElement.class); + when(typeElement.asType()).thenReturn(typeMirror); + + Types types = mock(Types.class); + Mockito.>when(types.directSupertypes(same(typeMirror))) + .thenReturn(Collections.singletonList(declaredType)); + + ProcessingEnvironment environment = mock(ProcessingEnvironment.class); + when(environment.getTypeUtils()).thenReturn(types); + + BaseMethodSupplierProcessor processor = getMock(); + + processor.init(environment); + + FieldDescriptor field = new FieldDescriptor(fieldMock("field")); + TypeDescriptor typeDescriptor = mock(TypeDescriptor.class); + when(typeDescriptor.getElement()).thenReturn(typeElement); + when(typeDescriptor.getFields()).thenReturn(Collections.singleton(field)); + + Set fields = processor.getFields(typeDescriptor); + assertEquals(2, fields.size(), "Number of fields does not equal to 2"); + assertArrayEquals(new String[]{"field", "superField"}, + fields.stream().map(FieldDescriptor::getName).sorted().toArray(), "Fields are incorrect"); + } + @Test void should_return_includedFields() { BaseMethodSupplierProcessor processor = getMock(); TypeDescriptor typeDescriptor = mock(TypeDescriptor.class); + + TypeMirror typeMirror = mock(TypeMirror.class); + TypeElement typeElement = mock(TypeElement.class); + when(typeElement.asType()).thenReturn(typeMirror); + when(typeDescriptor.getElement()).thenReturn(typeElement); + FieldDescriptor field = new FieldDescriptor(fieldMock("field")); FieldDescriptor included = new FieldDescriptor(fieldMock("included")); when(typeDescriptor.getFields()).thenReturn(new HashSet<>(Arrays.asList(field, included))); when(processor.getIncludedFields(any())).thenReturn(Collections.singleton("included")); + + Types types = mock(Types.class); + when(types.directSupertypes(same(typeMirror))).thenReturn(Collections.emptyList()); + + ProcessingEnvironment environment = mock(ProcessingEnvironment.class); + when(environment.getTypeUtils()).thenReturn(types); + + processor.init(environment); + Set fields = processor.getFields(typeDescriptor); assertEquals(1, fields.size(), "Number of fields does not equal to 1"); assertEquals("included", fields.iterator().next().getName(), "Field name does not equal to 'included'"); @@ -338,10 +433,25 @@ void should_return_includedFields() { void should_return_notExcludedFields() { BaseMethodSupplierProcessor processor = getMock(); TypeDescriptor typeDescriptor = mock(TypeDescriptor.class); + + TypeMirror typeMirror = mock(TypeMirror.class); + TypeElement typeElement = mock(TypeElement.class); + when(typeElement.asType()).thenReturn(typeMirror); + when(typeDescriptor.getElement()).thenReturn(typeElement); + FieldDescriptor field = new FieldDescriptor(fieldMock("field")); FieldDescriptor excluded = new FieldDescriptor(fieldMock("excluded")); when(typeDescriptor.getFields()).thenReturn(new HashSet<>(Arrays.asList(field, excluded))); when(processor.getExcludedFields(any())).thenReturn(Collections.singleton("excluded")); + + Types types = mock(Types.class); + when(types.directSupertypes(same(typeMirror))).thenReturn(Collections.emptyList()); + + ProcessingEnvironment environment = mock(ProcessingEnvironment.class); + when(environment.getTypeUtils()).thenReturn(types); + + processor.init(environment); + Set fields = processor.getFields(typeDescriptor); assertEquals(1, fields.size(), "Number of fields does not equal to 1"); assertEquals("field", fields.iterator().next().getName(), "Field name does not equal to 'field'"); diff --git a/src/test/java/dev/alexengrig/metter/processor/BaseProcessorTest.java b/src/test/java/dev/alexengrig/metter/processor/BaseProcessorTest.java index ef68034..7b2d73b 100644 --- a/src/test/java/dev/alexengrig/metter/processor/BaseProcessorTest.java +++ b/src/test/java/dev/alexengrig/metter/processor/BaseProcessorTest.java @@ -17,6 +17,7 @@ package dev.alexengrig.metter.processor; import dev.alexengrig.metter.ElementMocks; +import dev.alexengrig.metter.element.descriptor.TypeDescriptor; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -25,34 +26,37 @@ import javax.annotation.processing.RoundEnvironment; import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.TypeElement; +import javax.lang.model.element.VariableElement; +import javax.lang.model.type.DeclaredType; +import javax.lang.model.type.TypeKind; +import javax.lang.model.type.TypeMirror; +import javax.lang.model.util.Types; import javax.tools.Diagnostic; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.Set; +import static dev.alexengrig.metter.ElementMocks.fieldMock; import static java.lang.System.lineSeparator; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.same; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; class BaseProcessorTest { - static final BaseProcessor processor; - static final ProcessingEnvironment environment; - - static { - environment = mock(ProcessingEnvironment.class); - processor = new BaseProcessor(Deprecated.class) { - { - init(environment); - } - + private static BaseProcessor getMock() { + return new BaseProcessor(Deprecated.class) { @Override protected void process(Element annotatedElement) { } @@ -63,6 +67,7 @@ protected void process(Element annotatedElement) { void should_not_process() { RoundEnvironment roundEnvironment = mock(RoundEnvironment.class); when(roundEnvironment.processingOver()).thenReturn(true); + BaseProcessor processor = getMock(); assertFalse(processor.process(Collections.emptySet(), roundEnvironment), "Processor must not process"); } @@ -73,6 +78,7 @@ void should_process() { Element typeElement = ElementMocks.typeElementMock(); Mockito.>when(roundEnvironment.getElementsAnnotatedWith(Deprecated.class)) .thenReturn(Collections.singleton(typeElement)); + BaseProcessor processor = getMock(); assertTrue(processor.process(Collections.emptySet(), roundEnvironment), "Processor must process"); verify(roundEnvironment).getElementsAnnotatedWith(Deprecated.class); } @@ -80,7 +86,10 @@ void should_process() { @Test void should_print_noteMessage() { Messager messager = mock(Messager.class); + ProcessingEnvironment environment = mock(ProcessingEnvironment.class); when(environment.getMessager()).thenReturn(messager); + BaseProcessor processor = getMock(); + processor.init(environment); processor.note("Note message"); verify(messager).printMessage(Diagnostic.Kind.NOTE, "Note message"); } @@ -88,6 +97,7 @@ void should_print_noteMessage() { @Test void should_print_errorMessage() { Messager messager = mock(Messager.class); + ProcessingEnvironment environment = mock(ProcessingEnvironment.class); when(environment.getMessager()).thenReturn(messager); ArrayList messages = new ArrayList<>(); doAnswer(invocation -> { @@ -100,6 +110,8 @@ void should_print_errorMessage() { printWriter.write("java.lang.RuntimeException without stack trace"); return null; }).when(exception).printStackTrace(any(PrintWriter.class)); + BaseProcessor processor = getMock(); + processor.init(environment); processor.error("Error message", exception); verify(messager).printMessage(eq(Diagnostic.Kind.ERROR), any()); assertEquals(1, messages.size(), "Number of messages is not 1"); @@ -108,19 +120,59 @@ void should_print_errorMessage() { "Error message is incorrect"); } + @Test + void should_return_allSuperTypes() { + VariableElement superField = fieldMock("superField"); + + TypeElement superTypeElement = mock(TypeElement.class); + Mockito.>when(superTypeElement.getEnclosedElements()) + .thenReturn(Collections.singletonList(superField)); + when(superTypeElement.getKind()).thenReturn(ElementKind.CLASS); + when(superTypeElement.toString()).thenReturn("NoObject"); + + DeclaredType declaredType = mock(DeclaredType.class); + when(declaredType.asElement()).thenReturn(superTypeElement); + when(declaredType.getKind()).thenReturn(TypeKind.DECLARED); + + TypeMirror typeMirror = mock(TypeMirror.class); + TypeElement typeElement = mock(TypeElement.class); + when(typeElement.asType()).thenReturn(typeMirror); + + Types types = mock(Types.class); + Mockito.>when(types.directSupertypes(same(typeMirror))) + .thenReturn(Collections.singletonList(declaredType)); + + ProcessingEnvironment environment = mock(ProcessingEnvironment.class); + when(environment.getTypeUtils()).thenReturn(types); + + TypeDescriptor typeDescriptor = new TypeDescriptor(typeElement); + + BaseProcessor processor = getMock(); + + processor.init(environment); + + Set superTypes = processor.getAllSuperTypes(typeDescriptor); + + assertEquals(1, superTypes.size(), "Number of super types are incorrect"); + assertSame(superTypeElement, superTypes.iterator().next().getElement(), "Super type element is incorrect"); + } + @Test void should_return_supportedOptions() { + BaseProcessor processor = getMock(); assertTrue(processor.getSupportedOptions().isEmpty(), "Processor has supported options"); } @Test void should_return_supportedVersion() { + BaseProcessor processor = getMock(); assertEquals(SourceVersion.RELEASE_8, processor.getSupportedSourceVersion(), "Supported source version does not equal to 8"); } @Test void should_return_supportedAnnotations() { + BaseProcessor processor = getMock(); assertEquals(Collections.singleton("java.lang.Deprecated"), processor.getSupportedAnnotationTypes(), "Supported annotation types are not equal to 'java.lang.Deprecated'"); }