Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FkDecks |
|
| 1.0;1 | ||||
FkDecks$1 |
|
| 1.0;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.google.common.base.Function; | |
33 | import com.google.common.collect.Iterables; | |
34 | import com.google.common.io.Files; | |
35 | import com.jcabi.aspects.Immutable; | |
36 | import com.thindeck.api.Deck; | |
37 | import com.thindeck.api.Decks; | |
38 | import java.io.File; | |
39 | import java.io.IOException; | |
40 | import lombok.EqualsAndHashCode; | |
41 | import lombok.ToString; | |
42 | import org.apache.commons.io.FileUtils; | |
43 | import org.apache.commons.io.filefilter.TrueFileFilter; | |
44 | import org.xembly.Directives; | |
45 | ||
46 | /** | |
47 | * Mock of {@link com.thindeck.api.Decks}. | |
48 | * | |
49 | * @author Yegor Bugayenko (yegor@teamed.io) | |
50 | * @version $Id$ | |
51 | * @since 0.4 | |
52 | */ | |
53 | @Immutable | |
54 | 0 | @ToString |
55 | 0 | @EqualsAndHashCode |
56 | public final class FkDecks implements Decks { | |
57 | ||
58 | /** | |
59 | * Dir path. | |
60 | */ | |
61 | private final transient String path; | |
62 | ||
63 | /** | |
64 | * Ctor. | |
65 | * @throws IOException If fails | |
66 | */ | |
67 | public FkDecks() throws IOException { | |
68 | 0 | this(FkDecks.temp()); |
69 | 0 | } |
70 | ||
71 | /** | |
72 | * Ctor. | |
73 | * @param file File to use for XML | |
74 | */ | |
75 | 7 | public FkDecks(final File file) { |
76 | 7 | this.path = file.getAbsolutePath(); |
77 | 7 | } |
78 | ||
79 | @Override | |
80 | public Deck get(final String name) { | |
81 | 0 | return new FkDeck(new File(this.path, name)); |
82 | } | |
83 | ||
84 | @Override | |
85 | public void add(final String name) throws IOException { | |
86 | 6 | final File file = new File(this.path, name); |
87 | 6 | FileUtils.write(file, "<deck/>"); |
88 | 6 | new Deck.Smart(new FkDeck(file)).update( |
89 | new Directives().xpath("/deck").attr( | |
90 | "name", String.format("test/%s", name) | |
91 | ) | |
92 | ); | |
93 | 0 | } |
94 | ||
95 | @Override | |
96 | public void delete(final String name) { | |
97 | 0 | new File(this.path, name).delete(); |
98 | 0 | } |
99 | ||
100 | @Override | |
101 | public Iterable<Deck> iterate() { | |
102 | 1 | return Iterables.transform( |
103 | FileUtils.listFiles( | |
104 | new File(this.path), | |
105 | TrueFileFilter.INSTANCE, | |
106 | TrueFileFilter.INSTANCE | |
107 | ), | |
108 | 1 | new Function<File, Deck>() { |
109 | @Override | |
110 | public Deck apply(final File input) { | |
111 | 0 | return new FkDeck(input); |
112 | } | |
113 | } | |
114 | ); | |
115 | } | |
116 | ||
117 | /** | |
118 | * Create temp dir. | |
119 | * @return Temp dir | |
120 | * @throws IOException If fails | |
121 | */ | |
122 | private static File temp() throws IOException { | |
123 | 0 | final File file = Files.createTempDir(); |
124 | 0 | FileUtils.forceDeleteOnExit(file); |
125 | 0 | return file; |
126 | } | |
127 | } |