Coverage Report - com.jcabi.github.mock.MkStorage
 
Classes in this File Line Coverage Branch Coverage Complexity
MkStorage
N/A
N/A
1.176
MkStorage$InFile
80%
17/21
20%
2/10
1.176
MkStorage$InFile$AjcClosure1
100%
1/1
N/A
1.176
MkStorage$InFile$AjcClosure3
100%
1/1
N/A
1.176
MkStorage$InFile$AjcClosure5
100%
1/1
N/A
1.176
MkStorage$InFile$AjcClosure7
100%
1/1
N/A
1.176
MkStorage$Synced
92%
13/14
18%
3/16
1.176
MkStorage$Synced$AjcClosure1
100%
1/1
N/A
1.176
MkStorage$Synced$AjcClosure3
100%
1/1
N/A
1.176
MkStorage$Synced$AjcClosure5
100%
1/1
N/A
1.176
MkStorage$Synced$AjcClosure7
100%
1/1
N/A
1.176
 
 1  2806
 /**
 2  
  * Copyright (c) 2013-2015, 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 com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.xml.XML;
 35  
 import com.jcabi.xml.XMLDocument;
 36  
 import java.io.File;
 37  
 import java.io.IOException;
 38  
 import javax.validation.constraints.NotNull;
 39  
 import lombok.EqualsAndHashCode;
 40  
 import org.apache.commons.io.Charsets;
 41  
 import org.apache.commons.io.FileUtils;
 42  
 import org.xembly.Directive;
 43  
 import org.xembly.Xembler;
 44  
 
 45  
 /**
 46  
  * Storage of Github data.
 47  
  *
 48  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 49  
  * @version $Id$
 50  
  * @since 0.5
 51  
  * @checkstyle MultipleStringLiteralsCheck (200 lines)
 52  
  */
 53  
 @Immutable
 54  
 @SuppressWarnings("PMD.TooManyMethods")
 55  
 public interface MkStorage {
 56  
 
 57  
     /**
 58  
      * Get full XML.
 59  
      * @return XML
 60  
      * @throws IOException If there is any I/O problem, or if the current
 61  
      *  storage is locked by another thread.
 62  
      */
 63  
     @NotNull(message = "xml is never NULL")
 64  
     XML xml() throws IOException;
 65  
 
 66  
     /**
 67  
      * Update XML with this directives.
 68  
      * @param dirs Directives
 69  
      * @throws IOException If there is any I/O problem, or if the current
 70  
      *  storage is locked by another thread.
 71  
      */
 72  
     void apply(
 73  
         @NotNull(message = "dirs can't be NULL") Iterable<Directive> dirs
 74  
     ) throws IOException;
 75  
 
 76  
     /**
 77  
      * Locks storage to the current thread.
 78  
      *
 79  
      * <p>If the lock is available, grant it
 80  
      * to the calling thread and block all operations from other threads.
 81  
      * If not available, wait for the holder of the lock to release it with
 82  
      * {@link #unlock()} before any other operations can be performed.
 83  
      *
 84  
      * <p>Locking behavior is reentrant, which means a thread can invoke
 85  
      * {@link #lock()} multiple times, where a hold count is maintained.
 86  
      * @throws IOException If there is any I/O problem
 87  
      */
 88  
     void lock() throws IOException;
 89  
 
 90  
     /**
 91  
      * Unlock storage.
 92  
      *
 93  
      * <p>Locking behavior is reentrant, thus if the thread invoked
 94  
      * {@link #lock()} multiple times, the hold count is decremented. If the
 95  
      * hold count reaches 0, the lock is released.
 96  
      *
 97  
      * <p>If the current thread does not hold the lock, an
 98  
      * {@link IllegalMonitorStateException} will be thrown.
 99  
      * @throws IOException If there is any I/O problem
 100  
      */
 101  
     void unlock() throws IOException;
 102  
 
 103  
     /**
 104  
      * In file.
 105  
      */
 106  
     @Immutable
 107  3
     @EqualsAndHashCode(of = "name")
 108  
     @Loggable(Loggable.DEBUG)
 109  
     final class InFile implements MkStorage {
 110  
         /**
 111  
          * File name.
 112  
          */
 113  
         private final transient String name;
 114  
         /**
 115  
          * Public ctor.
 116  
          * @throws IOException If there is any I/O problem
 117  
          */
 118  
         public InFile() throws IOException {
 119  273
             this(File.createTempFile("jcabi-github", ".xml"));
 120  273
             new File(this.name).deleteOnExit();
 121  273
         }
 122  
         /**
 123  
          * Public ctor.
 124  
          * @param file File to use
 125  
          * @throws IOException If there is any I/O problem
 126  
          */
 127  
         public InFile(
 128  
             @NotNull(message = "file can't be NULL") final File file
 129  273
         ) throws IOException {
 130  273
             FileUtils.write(file, "<github/>");
 131  273
             this.name = file.getAbsolutePath();
 132  273
         }
 133  
         @Override
 134  
         public String toString() {
 135  
             try {
 136  0
                 return this.xml().toString();
 137  0
             } catch (final IOException ex) {
 138  0
                 throw new IllegalStateException(ex);
 139  
             }
 140  
         }
 141  
         @Override
 142  
         @NotNull(message = "XML is never NULL")
 143  
         public XML xml() throws IOException {
 144  4464
             synchronized (this.name) {
 145  2232
                 return new XMLDocument(
 146  
                     FileUtils.readFileToString(
 147  
                         new File(this.name), Charsets.UTF_8
 148  
                     )
 149  
                 );
 150  0
             }
 151  
         }
 152  
         @Override
 153  
         public void apply(
 154  
             @NotNull(message = "dirs cannot be NULL")
 155  
             final Iterable<Directive> dirs
 156  
         ) throws IOException {
 157  2980
             synchronized (this.name) {
 158  1490
                 FileUtils.write(
 159  
                     new File(this.name),
 160  
                     new XMLDocument(
 161  
                         new Xembler(dirs).applyQuietly(this.xml().node())
 162  
                     ).toString(),
 163  
                     Charsets.UTF_8
 164  
                 );
 165  1490
             }
 166  1490
         }
 167  
         @Override
 168  
         public void lock() {
 169  
             // nothing
 170  22
         }
 171  
         @Override
 172  
         public void unlock() {
 173  
             // nothing
 174  22
         }
 175  
     }
 176  
 
 177  
     /**
 178  
      * Syncronized.
 179  
      */
 180  
     @Immutable
 181  23
     @EqualsAndHashCode(of = { "origin", "lock" })
 182  
     @Loggable(Loggable.DEBUG)
 183  
     final class Synced implements MkStorage {
 184  
         /**
 185  
          * Original storage.
 186  
          */
 187  
         private final transient MkStorage origin;
 188  
         /**
 189  
          * Lock object.
 190  
          */
 191  225
         private final transient ImmutableReentrantLock lock =
 192  
             new ImmutableReentrantLock();
 193  
         /**
 194  
          * Public ctor.
 195  
          * @param storage Original
 196  
          */
 197  225
         public Synced(final MkStorage storage) {
 198  225
             this.origin = storage;
 199  225
         }
 200  
         @Override
 201  
         public String toString() {
 202  0
             return this.origin.toString();
 203  
         }
 204  
         @Override
 205  
         public XML xml() throws IOException {
 206  1420
             return this.origin.xml();
 207  
         }
 208  
         @Override
 209  
         public void apply(final Iterable<Directive> dirs) throws IOException {
 210  2806
             this.origin.apply(dirs);
 211  1403
         }
 212  
         @Override
 213  
         public void lock() {
 214  606
             this.lock.lock();
 215  303
         }
 216  
         @Override
 217  
         public void unlock() {
 218  602
             this.lock.unlock();
 219  301
         }
 220  
     }
 221  
 
 222  
 }