View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.maven;
6   
7   import com.qulice.spi.ResourceValidator;
8   import com.qulice.spi.Violation;
9   import java.io.File;
10  import java.util.Collection;
11  import java.util.Collections;
12  import java.util.concurrent.CountDownLatch;
13  import java.util.concurrent.atomic.AtomicInteger;
14  
15  /**
16   * A test fake {@link ResourceValidator} that signals through a latch
17   * the moment its {@code validate} call begins, then blocks the calling
18   * thread indefinitely on {@code Thread.sleep}.
19   *
20   * <p>Used by tests that exercise the timeout/interruption behaviour of
21   * {@code CheckMojo}: tests use {@link #await()} to wait until validation
22   * has actually entered the validator before driving the timeout, and
23   * {@link #count()} to confirm the validator was invoked exactly once.</p>
24   *
25   * @since 0.27.0
26   */
27  final class BlockedValidator implements ResourceValidator {
28  
29      /**
30       * Method calls counter.
31       */
32      private final AtomicInteger cnt;
33  
34      /**
35       * Latch to signal when validation starts.
36       */
37      private final CountDownLatch latch;
38  
39      BlockedValidator() {
40          this.cnt = new AtomicInteger(0);
41          this.latch = new CountDownLatch(1);
42      }
43  
44      @Override
45      public Collection<Violation> validate(final Collection<File> ignore) {
46          this.cnt.incrementAndGet();
47          this.latch.countDown();
48          try {
49              Thread.sleep(Long.MAX_VALUE);
50          } catch (final InterruptedException ex) {
51              Thread.currentThread().interrupt();
52          }
53          return Collections.emptyList();
54      }
55  
56      @Override
57      public String name() {
58          return "blocked forever";
59      }
60  
61      int count() {
62          return this.cnt.get();
63      }
64  
65      void await() {
66          try {
67              this.latch.await();
68          } catch (final InterruptedException ex) {
69              Thread.currentThread().interrupt();
70          }
71      }
72  }