• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / smartsprites / 555

21 May 2026 03:26PM UTC coverage: 87.799%. Remained the same
555

push

github

hazendaz
[tests] Fix tests that were looking at size of original license before spdx change

557 of 670 branches covered (83.13%)

Branch coverage included in aggregate %.

1350 of 1502 relevant lines covered (89.88%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/org/carrot2/labs/smartsprites/ant/SmartSpritesTask.java
1
/*
2
 * SPDX-License-Identifier: BSD-3-Clause
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2021-2026 Hazendaz
6
 * Copyright (C) 2007-2009, Stanisław Osiński.
7
 */
8
package org.carrot2.labs.smartsprites.ant;
9

10
import java.io.File;
11
import java.io.IOException;
12
import java.util.ArrayList;
13
import java.util.Iterator;
14
import java.util.List;
15

16
import org.apache.tools.ant.BuildException;
17
import org.apache.tools.ant.Task;
18
import org.apache.tools.ant.types.FileSet;
19
import org.apache.tools.ant.types.Resource;
20
import org.carrot2.labs.smartsprites.SmartSpritesParameters;
21
import org.carrot2.labs.smartsprites.SmartSpritesParameters.PngDepth;
22
import org.carrot2.labs.smartsprites.SpriteBuilder;
23
import org.carrot2.labs.smartsprites.message.Message;
24
import org.carrot2.labs.smartsprites.message.Message.MessageLevel;
25
import org.carrot2.labs.smartsprites.message.MessageLog;
26
import org.carrot2.labs.smartsprites.message.MessageSink;
27
import org.carrot2.util.EnumUtils;
28

29
/**
30
 * Ant task for calling SmartSprites processing.
31
 */
32
public class SmartSpritesTask extends Task {
×
33

34
    /** The root dir. */
35
    private String rootDir;
36

37
    /** The output dir. */
38
    private String outputDir;
39

40
    /** The document root dir. */
41
    private String documentRootDir;
42

43
    /** The log level. */
44
    private MessageLevel logLevel;
45

46
    /** The fail on level. */
47
    private MessageLevel failOnLevel = MessageLevel.ERROR;
×
48

49
    /** The css file suffix. */
50
    private String cssFileSuffix = SmartSpritesParameters.DEFAULT_CSS_FILE_SUFFIX;
×
51

52
    /** The css file encoding. */
53
    private String cssFileEncoding = SmartSpritesParameters.DEFAULT_CSS_FILE_ENCODING;
×
54

55
    /** The sprite png depth. */
56
    private PngDepth spritePngDepth = SmartSpritesParameters.DEFAULT_SPRITE_PNG_DEPTH;
×
57

58
    /** The mark sprite images. */
59
    private boolean markSpriteImages = SmartSpritesParameters.DEFAULT_MARK_SPRITE_IMAGES;
×
60

61
    /** The css files. */
62
    private List<String> cssFiles = new ArrayList<>();
×
63

64
    /**
65
     * Sets the root dir.
66
     *
67
     * @param dir
68
     *            the new root dir
69
     */
70
    public void setRootDir(File dir) {
71
        this.rootDir = dir.getPath();
×
72
    }
×
73

74
    /**
75
     * Sets the output dir.
76
     *
77
     * @param outputDir
78
     *            the new output dir
79
     */
80
    public void setOutputDir(File outputDir) {
81
        this.outputDir = outputDir.getPath();
×
82
    }
×
83

84
    /**
85
     * Sets the document root dir.
86
     *
87
     * @param documentRootDir
88
     *            the new document root dir
89
     */
90
    public void setDocumentRootDir(File documentRootDir) {
91
        this.documentRootDir = documentRootDir.getPath();
×
92
    }
×
93

94
    /**
95
     * Sets the log level.
96
     *
97
     * @param logLevel
98
     *            the new log level
99
     */
100
    public void setLogLevel(String logLevel) {
101
        this.logLevel = getLogLevelFromString(logLevel, MessageLevel.INFO);
×
102
    }
×
103

104
    /**
105
     * Sets the fail on level.
106
     *
107
     * @param failOnLevel
108
     *            the new fail on level
109
     */
110
    public void setFailOnLevel(String failOnLevel) {
111
        this.failOnLevel = getLogLevelFromString(failOnLevel, MessageLevel.ERROR);
×
112
    }
×
113

114
    /**
115
     * Gets the log level from string.
116
     *
117
     * @param logLevel
118
     *            the log level
119
     * @param defaultLevel
120
     *            the default level
121
     *
122
     * @return the log level from string
123
     */
124
    private MessageLevel getLogLevelFromString(String logLevel, MessageLevel defaultLevel) {
125
        try {
126
            return MessageLevel.valueOf(logLevel);
×
127
        } catch (Exception e) {
×
128
            return defaultLevel;
×
129
        }
130
    }
131

132
    /**
133
     * Sets the css file encoding.
134
     *
135
     * @param cssFileEncoding
136
     *            the new css file encoding
137
     */
138
    public void setCssFileEncoding(String cssFileEncoding) {
139
        this.cssFileEncoding = cssFileEncoding;
×
140
    }
×
141

142
    /**
143
     * Sets the css file suffix.
144
     *
145
     * @param cssFileSuffix
146
     *            the new css file suffix
147
     */
148
    public void setCssFileSuffix(String cssFileSuffix) {
149
        this.cssFileSuffix = cssFileSuffix;
×
150
    }
×
151

152
    /**
153
     * Sets the sprite png depth.
154
     *
155
     * @param spritePngDepthString
156
     *            the new sprite png depth
157
     */
158
    public void setSpritePngDepth(String spritePngDepthString) {
159
        this.spritePngDepth = EnumUtils.valueOf(spritePngDepthString, PngDepth.class,
×
160
                SmartSpritesParameters.DEFAULT_SPRITE_PNG_DEPTH);
161
    }
×
162

163
    /**
164
     * Sets the mark sprite images.
165
     *
166
     * @param markSpriteImages
167
     *            the new mark sprite images
168
     */
169
    public void setMarkSpriteImages(boolean markSpriteImages) {
170
        this.markSpriteImages = markSpriteImages;
×
171
    }
×
172

173
    @Override
174
    public void execute() {
175
        final SmartSpritesParameters parameters = new SmartSpritesParameters(rootDir, cssFiles, outputDir,
×
176
                documentRootDir, logLevel, cssFileSuffix, spritePngDepth, cssFileEncoding, markSpriteImages);
177

178
        final FailureDetectorMessageSink failureDetectorMessageSink = new FailureDetectorMessageSink();
×
179
        MessageLog log = new MessageLog(new AntLogMessageSink(), failureDetectorMessageSink);
×
180

181
        if (parameters.validate(log)) {
×
182
            try {
183
                new SpriteBuilder(parameters, log).buildSprites();
×
184
            } catch (IOException e) {
×
185
                throw new BuildException(e);
×
186
            }
×
187
        }
188

189
        if (failureDetectorMessageSink.shouldFail) {
×
190
            throw new BuildException(failureDetectorMessageSink.failureLevel.name() + " messages found");
×
191
        }
192
    }
×
193

194
    /**
195
     * The Class AntLogMessageSink.
196
     */
197
    private class AntLogMessageSink implements MessageSink {
×
198
        @Override
199
        public void add(Message message) {
200
            if (MessageLevel.COMPARATOR.compare(message.level, logLevel) >= 0) {
×
201
                log(message.toString());
×
202
            }
203
        }
×
204
    }
205

206
    /**
207
     * The Class FailureDetectorMessageSink.
208
     */
209
    private class FailureDetectorMessageSink implements MessageSink {
×
210

211
        /** The should fail. */
212
        boolean shouldFail = false;
×
213

214
        /** The failure level. */
215
        MessageLevel failureLevel = null;
×
216

217
        @Override
218
        public void add(Message message) {
219
            if (failOnLevel != null && MessageLevel.COMPARATOR.compare(message.level, failOnLevel) >= 0
×
220
                    && message.level != MessageLevel.STATUS) {
221
                failureLevel = message.level;
×
222
                shouldFail = true;
×
223
            }
224
        }
×
225
    }
226

227
    /**
228
     * Adds the configured fileset.
229
     *
230
     * @param fileset
231
     *            the fileset
232
     */
233
    public void addConfiguredFileset(FileSet fileset) {
234
        for (Iterator<Resource> it = fileset.iterator(); it.hasNext();) {
×
235
            cssFiles.add(it.next().getName());
×
236
        }
237
    }
×
238
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc