1 /** 2 * Copyright (c) 2013-2023, 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.util.concurrent.ExecutorService; 33 import java.util.concurrent.Executors; 34 import java.util.concurrent.Future; 35 import java.util.concurrent.TimeUnit; 36 import java.util.concurrent.TimeoutException; 37 import org.hamcrest.MatcherAssert; 38 import org.hamcrest.Matchers; 39 import org.junit.Test; 40 import org.xembly.Directives; 41 42 /** 43 * Test case for {@link MkStorage}. 44 * @author Yegor Bugayenko (yegor256@gmail.com) 45 * @version $Id: 68af2fcec4e7af74a427af60ba2f9a2d299ce841 $ 46 * @checkstyle MultipleStringLiteralsCheck (200 lines) 47 */ 48 @SuppressWarnings("PMD.DoNotUseThreads") 49 public final class MkStorageTest { 50 51 /** 52 * MkStorage can text and write. 53 * @throws Exception If some problem inside 54 */ 55 @Test 56 public void readsAndWrites() throws Exception { 57 final MkStorage storage = new MkStorage.InFile(); 58 storage.lock(); 59 try { 60 storage.apply( 61 new Directives().xpath("/github").add("test") 62 .set("hello, world") 63 ); 64 MatcherAssert.assertThat( 65 storage.xml().xpath("/github/test/text()").get(0), 66 Matchers.endsWith(", world") 67 ); 68 } finally { 69 storage.unlock(); 70 } 71 } 72 73 /** 74 * MkStorage can lock and unlock files. 75 * @throws Exception If some problem inside 76 */ 77 @Test 78 public void locksAndUnlocks() throws Exception { 79 final MkStorage storage = new MkStorage.Synced(new MkStorage.InFile()); 80 final ExecutorService executor = Executors.newSingleThreadExecutor(); 81 final Runnable second = () -> storage.lock(); 82 storage.lock(); 83 Future<?> future = executor.submit(second); 84 try { 85 future.get(1L, TimeUnit.SECONDS); 86 MatcherAssert.assertThat("timeout SHOULD happen", false); 87 } catch (final TimeoutException ex) { 88 future.cancel(true); 89 } finally { 90 storage.unlock(); 91 } 92 future = executor.submit(second); 93 try { 94 future.get(1L, TimeUnit.SECONDS); 95 } catch (final TimeoutException ex) { 96 MatcherAssert.assertThat("timeout SHOULD NOT happen", false); 97 future.cancel(true); 98 } 99 executor.shutdown(); 100 } 101 102 }