Coverage Report - com.thindeck.fakes.FkDeck
 
Classes in this File Line Coverage Branch Coverage Complexity
FkDeck
57%
11/19
0%
0/4
1
 
 1  2
 /**
 2  
  * Copyright (c) 2014-2015, Thindeck.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 thindeck.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.thindeck.fakes;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.log.Logger;
 34  
 import com.jcabi.xml.StrictXML;
 35  
 import com.jcabi.xml.XML;
 36  
 import com.jcabi.xml.XMLDocument;
 37  
 import com.thindeck.api.Agent;
 38  
 import com.thindeck.api.Deck;
 39  
 import com.thindeck.api.Events;
 40  
 import java.io.File;
 41  
 import java.io.IOException;
 42  
 import lombok.EqualsAndHashCode;
 43  
 import lombok.ToString;
 44  
 import org.apache.commons.io.FileUtils;
 45  
 import org.apache.commons.io.FilenameUtils;
 46  
 import org.apache.commons.lang3.CharEncoding;
 47  
 import org.xembly.Xembler;
 48  
 
 49  
 /**
 50  
  * Mock of {@link com.thindeck.api.Deck}.
 51  
  *
 52  
  * @author Yegor Bugayenko (yegor@teamed.io)
 53  
  * @version $Id$
 54  
  * @since 0.4
 55  
  */
 56  
 @Immutable
 57  0
 @ToString
 58  0
 @EqualsAndHashCode
 59  
 public final class FkDeck implements Deck {
 60  
 
 61  
     /**
 62  
      * File path.
 63  
      */
 64  
     private final transient String path;
 65  
 
 66  
     /**
 67  
      * Ctor.
 68  
      * @throws IOException If fails
 69  
      */
 70  
     public FkDeck() throws IOException {
 71  1
         this(FkDeck.temp());
 72  1
     }
 73  
 
 74  
     /**
 75  
      * Ctor.
 76  
      * @param file File to use for XML
 77  
      */
 78  7
     public FkDeck(final File file) {
 79  7
         this.path = file.getAbsolutePath();
 80  7
     }
 81  
 
 82  
     @Override
 83  
     public String name() {
 84  0
         return FilenameUtils.getBaseName(this.path);
 85  
     }
 86  
 
 87  
     @Override
 88  
     public void exec(final Agent agent) throws IOException {
 89  7
         final XML before = new StrictXML(
 90  
             Deck.UPGRADE.transform(
 91  
                 new XMLDocument(new File(this.path))
 92  
             ),
 93  
             Deck.SCHEMA
 94  
         );
 95  0
         final XML after = new XMLDocument(
 96  
             new Xembler(agent.exec(before)).applyQuietly(before.node())
 97  
         );
 98  0
         FileUtils.write(
 99  
             new File(this.path),
 100  
             new StrictXML(
 101  
                 after,
 102  
                 Deck.SCHEMA
 103  
             ).toString(),
 104  
             CharEncoding.UTF_8
 105  
         );
 106  0
         Logger.info(
 107  
             this, "deck saved to %s (%d bytes):\n%s", this.path,
 108  
             new File(this.path).length(), after
 109  
         );
 110  0
     }
 111  
 
 112  
     @Override
 113  
     public Events events() {
 114  0
         return new FkEvents();
 115  
     }
 116  
 
 117  
     /**
 118  
      * Create temp file.
 119  
      * @return Temp file with XML
 120  
      * @throws IOException If fails
 121  
      */
 122  
     private static File temp() throws IOException {
 123  1
         final File file = File.createTempFile("thindeck-", ".xml");
 124  1
         FileUtils.write(file, "<deck/>");
 125  1
         FileUtils.forceDeleteOnExit(file);
 126  1
         return file;
 127  
     }
 128  
 }