View Javadoc
1   /*
2    * Copyright 2022 Adrian Herscu
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package dev.aherscu.qa.testing.example.scenarios.tutorial1;
18  
19  import static java.lang.Math.*;
20  
21  import org.testng.annotations.*;
22  
23  import edu.umd.cs.findbugs.annotations.*;
24  import lombok.*;
25  import lombok.extern.slf4j.*;
26  
27  @Slf4j
28  public class PlainTestNg {
29      @Test
30      public void _1_shouldSucceed() {
31          assert true; // "assert" requires -ea vm flag
32      }
33  
34      @Test(dataProvider = "_2_notTruthTable")
35      public void _2_checkNot(
36          final boolean argument,
37          final boolean result) {
38          log.debug("checking {} is not {}", argument, result);
39          assert argument != result;
40      }
41  
42      @Test(dataProvider = "_3_fullAdderTruthTable")
43      public void _3_checkFullAdder(final FAT v) {
44          assert ((v.a || v.b) == v.sum)
45              && ((v.a && v.b) == v.carry);
46      }
47  
48      @Test(dataProvider = "_4_sinusTruthTable")
49      public void _4_checkSinus(final double angle, final double sinus) {
50          assert sin(angle) == sinus;
51      }
52  
53      @SuppressFBWarnings(
54          value = "UPM_UNCALLED_PRIVATE_METHOD",
55          justification = "called by testng framework")
56      @DataProvider(parallel = false) // parallel is not always good magic
57      private Object[][] _2_notTruthTable() {
58          return new Object[][] {
59              { false, true },
60              { true, false }
61          };
62      }
63  
64      @SuppressFBWarnings(
65          value = "UPM_UNCALLED_PRIVATE_METHOD",
66          justification = "called by testng framework")
67      @DataProvider
68      private Object[][] _3_fullAdderTruthTable() {
69          return new Object[][] {
70              // @formatter:off
71              {FAT.builder().a(false).b(false).sum(false).carry(false).build()},
72              {FAT.builder().a(false).b(true) .sum(true) .carry(false).build()},
73              {FAT.builder().a(true) .b(false).sum(true) .carry(false).build()},
74              {FAT.builder().a(true) .b(true) .sum(true) .carry(true) .build()},
75              // @formatter:on
76          };
77      }
78  
79      @SuppressFBWarnings(
80          value = "UPM_UNCALLED_PRIVATE_METHOD",
81          justification = "called by testng framework")
82      @DataProvider
83      private Object[][] _4_sinusTruthTable() {
84          return new Object[][] {
85              // @formatter:off
86              // there are toooooooo much possibilities...
87              { 0,         0 },
88              // { PI,        0 }, // fails because precision loss
89              { PI / 2,    1 },
90              { 1.5 * PI, -1 },
91              // { 2 * PI,    0 }, // fails because precision loss
92              // @formatter:on
93          };
94      }
95  
96      @Builder // lombok generates a builder
97      @ToString // lombok generates a nice toString method
98      static class FAT { // Full-Adder Truth
99          final boolean a, b, sum, carry;
100     }
101 }