Coverage Report - com.jcabi.s3.mock.MkBucket
 
Classes in this File Line Coverage Branch Coverage Complexity
MkBucket
58%
10/17
8%
1/12
1
MkBucket$1
100%
2/2
N/A
1
MkBucket$AjcClosure1
0%
0/1
N/A
1
MkBucket$AjcClosure11
100%
1/1
N/A
1
MkBucket$AjcClosure13
0%
0/1
N/A
1
MkBucket$AjcClosure3
0%
0/1
N/A
1
MkBucket$AjcClosure5
100%
1/1
N/A
1
MkBucket$AjcClosure7
0%
0/1
N/A
1
MkBucket$AjcClosure9
0%
0/1
N/A
1
 
 1  2
 /**
 2  
  * Copyright (c) 2012-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.s3.mock;
 31  
 
 32  
 import com.google.common.base.Function;
 33  
 import com.google.common.collect.Iterables;
 34  
 import com.jcabi.aspects.Immutable;
 35  
 import com.jcabi.aspects.Loggable;
 36  
 import com.jcabi.s3.Bucket;
 37  
 import com.jcabi.s3.Ocket;
 38  
 import com.jcabi.s3.Region;
 39  
 import java.io.File;
 40  
 import lombok.EqualsAndHashCode;
 41  
 import org.apache.commons.io.FileUtils;
 42  
 import org.apache.commons.io.FilenameUtils;
 43  
 import org.apache.commons.io.filefilter.TrueFileFilter;
 44  
 
 45  
 /**
 46  
  * Mock/fake bucket.
 47  
  *
 48  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 49  
  * @version $Id: e9835e9fc1a2b735edb7cf229478a114f71d23b1 $
 50  
  * @since 0.6
 51  
  */
 52  0
 @Immutable
 53  5
 @EqualsAndHashCode(of = "bkt")
 54  
 @Loggable(Loggable.DEBUG)
 55  
 public final class MkBucket implements Bucket {
 56  
 
 57  
     /**
 58  
      * My name.
 59  
      */
 60  
     private final transient String bkt;
 61  
 
 62  
     /**
 63  
      * Directory we're working in.
 64  
      */
 65  
     private final transient String dir;
 66  
 
 67  
     /**
 68  
      * Ctor.
 69  
      * @param file Directory to keep files in
 70  
      * @param name Name of the bucket
 71  
      */
 72  5
     public MkBucket(final File file, final String name) {
 73  5
         this.dir = file.getAbsolutePath();
 74  5
         this.bkt = name;
 75  5
     }
 76  
 
 77  
     @Override
 78  
     public Region region() {
 79  0
         return new MkRegion(new File(this.dir));
 80  
     }
 81  
 
 82  
     @Override
 83  
     public String name() {
 84  0
         return this.bkt;
 85  
     }
 86  
 
 87  
     @Override
 88  
     public Ocket ocket(final String key) {
 89  26
         return new MkOcket(new File(this.dir), this.bkt, key);
 90  
     }
 91  
 
 92  
     @Override
 93  
     public boolean exists() {
 94  0
         return true;
 95  
     }
 96  
 
 97  
     @Override
 98  
     public void remove(final String key) {
 99  0
         new File(this.home(), key).delete();
 100  0
     }
 101  
 
 102  
     @Override
 103  
     public Iterable<String> list(final String pfx) {
 104  4
         final File home = this.home();
 105  2
         return Iterables.transform(
 106  
             FileUtils.listFiles(
 107  
                 new File(home, pfx),
 108  
                 TrueFileFilter.INSTANCE,
 109  
                 TrueFileFilter.INSTANCE
 110  
             ),
 111  26
             new Function<File, String>() {
 112  
                 @Override
 113  
                 public String apply(final File file) {
 114  24
                     return FilenameUtils.separatorsToUnix(
 115  
                         file.getAbsolutePath().substring(
 116  
                             home.getAbsolutePath().length() + 1
 117  
                         )
 118  
                     );
 119  
                 }
 120  
             }
 121  
         );
 122  
     }
 123  
 
 124  
     @Override
 125  
     public int compareTo(final Bucket bucket) {
 126  0
         return this.bkt.compareTo(bucket.name());
 127  
     }
 128  
 
 129  
     /**
 130  
      * Get my file.
 131  
      * @return File
 132  
      */
 133  
     private File home() {
 134  2
         return new File(new File(this.dir), this.bkt);
 135  
     }
 136  
 
 137  
 }