View Javadoc
1   /**
2    * Copyright (c) 2013-2022, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.github.mock;
31  
32  import java.io.IOException;
33  import java.util.concurrent.ExecutorService;
34  import java.util.concurrent.Executors;
35  import java.util.concurrent.Future;
36  import java.util.concurrent.TimeUnit;
37  import java.util.concurrent.TimeoutException;
38  import org.hamcrest.MatcherAssert;
39  import org.hamcrest.Matchers;
40  import org.junit.Test;
41  import org.xembly.Directives;
42  
43  /**
44   * Test case for {@link MkStorage}.
45   * @author Yegor Bugayenko (yegor256@gmail.com)
46   * @version $Id: b0a83e3eb3f9ffb7e61948eeaa3941966c230be1 $
47   * @checkstyle MultipleStringLiteralsCheck (200 lines)
48   */
49  @SuppressWarnings("PMD.DoNotUseThreads")
50  public final class MkStorageTest {
51  
52      /**
53       * MkStorage can text and write.
54       * @throws Exception If some problem inside
55       */
56      @Test
57      public void readsAndWrites() throws Exception {
58          final MkStorage storage = new MkStorage.InFile();
59          storage.lock();
60          try {
61              storage.apply(
62                  new Directives().xpath("/github").add("test")
63                      .set("hello, world")
64              );
65              MatcherAssert.assertThat(
66                  storage.xml().xpath("/github/test/text()").get(0),
67                  Matchers.endsWith(", world")
68              );
69          } finally {
70              storage.unlock();
71          }
72      }
73  
74      /**
75       * MkStorage can lock and unlock files.
76       * @throws Exception If some problem inside
77       */
78      @Test
79      public void locksAndUnlocks() throws Exception {
80          final MkStorage storage = new MkStorage.Synced(new MkStorage.InFile());
81          final ExecutorService executor = Executors.newSingleThreadExecutor();
82          final Runnable second = new Runnable() {
83              @Override
84              public void run() {
85                  try {
86                      storage.lock();
87                  } catch (final IOException ex) {
88                      throw new IllegalStateException(ex);
89                  }
90              }
91          };
92          storage.lock();
93          Future<?> future = executor.submit(second);
94          try {
95              future.get(1L, TimeUnit.SECONDS);
96              MatcherAssert.assertThat("timeout SHOULD happen", false);
97          } catch (final TimeoutException ex) {
98              future.cancel(true);
99          } finally {
100             storage.unlock();
101         }
102         future = executor.submit(second);
103         try {
104             future.get(1L, TimeUnit.SECONDS);
105         } catch (final TimeoutException ex) {
106             MatcherAssert.assertThat("timeout SHOULD NOT happen", false);
107             future.cancel(true);
108         }
109         executor.shutdown();
110     }
111 
112 }