Skip to content

Commit df87db8

Browse files
committed
Fix custom open and close token not applied to FilteredFile.
1 parent ab3424c commit df87db8

File tree

8 files changed

+127
-15
lines changed

8 files changed

+127
-15
lines changed

src/main/java/org/vafer/jdeb/ControlBuilder.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ void buildControl(BinaryPackageControlFile packageControlFile, File[] controlFil
124124

125125
if (CONFIGURATION_FILENAMES.contains(file.getName()) || MAINTAINER_SCRIPTS.contains(file.getName())) {
126126

127-
FilteredFile configurationFile = new FilteredFile(new FileInputStream(file), resolver, encoding);
128-
configurationFile.setOpenToken(openReplaceToken);
129-
configurationFile.setCloseToken(closeReplaceToken);
127+
FilteredFile configurationFile = new FilteredFile(new FileInputStream(file), resolver, encoding, openReplaceToken, closeReplaceToken);
130128
addControlEntry(file.getName(), configurationFile.toString(), outputStream);
131129

132130
} else {
@@ -177,7 +175,7 @@ private String createPackageConffilesFile(final List<String> conffiles) {
177175

178176
@Deprecated
179177
public BinaryPackageControlFile createPackageControlFile(File file, BigInteger pDataSize) throws IOException, ParseException {
180-
return createPackageControlFile(file, pDataSize, Charset.defaultCharset());
178+
return createPackageControlFile(file, pDataSize, Charset.defaultCharset(), "[[", "]]");
181179
}
182180

183181
/**
@@ -191,8 +189,8 @@ public BinaryPackageControlFile createPackageControlFile(File file, BigInteger p
191189
* @param pDataSize the size of the installed package
192190
* @param encoding the encoding used to read the files
193191
*/
194-
public BinaryPackageControlFile createPackageControlFile(File file, BigInteger pDataSize, Charset encoding) throws IOException, ParseException {
195-
FilteredFile controlFile = new FilteredFile(new FileInputStream(file), resolver, encoding);
192+
public BinaryPackageControlFile createPackageControlFile(File file, BigInteger pDataSize, Charset encoding, String openToken, String closeToken) throws IOException, ParseException {
193+
FilteredFile controlFile = new FilteredFile(new FileInputStream(file), resolver, encoding, openToken, closeToken);
196194
BinaryPackageControlFile packageControlFile = new BinaryPackageControlFile(controlFile.toString());
197195

198196
if (packageControlFile.get("Distribution") == null) {

src/main/java/org/vafer/jdeb/DebMaker.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.vafer.jdeb.debian.BinaryPackageControlFile;
3535
import org.vafer.jdeb.debian.ChangesFile;
3636
import org.vafer.jdeb.signing.PGPSigner;
37+
import org.vafer.jdeb.utils.FilteredFile;
3738
import org.vafer.jdeb.utils.PGPSignatureOutputStream;
3839
import org.vafer.jdeb.utils.Utils;
3940
import org.vafer.jdeb.utils.VariableResolver;
@@ -144,8 +145,8 @@ public class DebMaker {
144145
private Long outputTimestampMs;
145146

146147
private VariableResolver variableResolver;
147-
private String openReplaceToken;
148-
private String closeReplaceToken;
148+
private String openReplaceToken = FilteredFile.DEFAULT_OPEN_TOKEN;
149+
private String closeReplaceToken = FilteredFile.DEFAULT_CLOSE_TOKEN;
149150

150151
private final Collection<DataProducer> dataProducers = new ArrayList<>();
151152

@@ -531,7 +532,7 @@ public BinaryPackageControlFile createSignedDeb(Compression compression, final P
531532

532533
console.debug("Building control");
533534
ControlBuilder controlBuilder = new ControlBuilder(console, variableResolver, openReplaceToken, closeReplaceToken, outputTimestampMs);
534-
BinaryPackageControlFile packageControlFile = controlBuilder.createPackageControlFile(new File(control, "control"), size, encoding);
535+
BinaryPackageControlFile packageControlFile = controlBuilder.createPackageControlFile(new File(control, "control"), size, encoding, openReplaceToken, closeReplaceToken);
535536
if (packageControlFile.get("Package") == null) {
536537
packageControlFile.set("Package", packageName);
537538
}

src/main/java/org/vafer/jdeb/utils/FilteredFile.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,34 @@
2626

2727
public class FilteredFile {
2828

29-
private String openToken = "[[";
30-
private String closeToken = "]]";
29+
public static final String DEFAULT_OPEN_TOKEN = "[[";
30+
public static final String DEFAULT_CLOSE_TOKEN = "]]";
31+
32+
private String openToken;
33+
private String closeToken;
3134
private List<String> lines = new ArrayList<>();
3235

3336
@Deprecated
3437
public FilteredFile(InputStream in, VariableResolver resolver) throws IOException {
35-
parse(in, resolver, Charset.defaultCharset());
36-
}
38+
this(in, resolver, Charset.defaultCharset());
39+
}
3740

3841
public FilteredFile(InputStream in, VariableResolver resolver, Charset encoding) throws IOException {
39-
parse(in, resolver, encoding);
42+
this(in, resolver, encoding, DEFAULT_OPEN_TOKEN, DEFAULT_CLOSE_TOKEN);
43+
}
44+
45+
public FilteredFile(InputStream in, VariableResolver resolver, Charset encoding, String openToken, String closeToken) throws IOException {
46+
this.openToken = openToken;
47+
this.closeToken = closeToken;
48+
parse(in, resolver, encoding);
4049
}
4150

51+
@Deprecated
4252
public void setOpenToken(String token) {
4353
openToken = token;
4454
}
4555

56+
@Deprecated
4657
public void setCloseToken(String token) {
4758
closeToken = token;
4859
}

src/test/java/org/vafer/jdeb/DebMakerTestCase.java

+72-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public void visit(TarArchiveEntry entry, byte[] content) throws IOException {
214214
if (entry.getName().equals("./control")) {
215215
try {
216216
ControlFile controlFile = new BinaryPackageControlFile(org.apache.commons.io.IOUtils.toString(new ByteArrayInputStream(content), StandardCharsets.UTF_8));
217-
assertEquals("the encoding is wrong", controlFile.get("Maintainer"), "ジョン Doe <john@doe.org>");
217+
assertEquals("the encoding is wrong", "ジョン Doe <john@doe.org>", controlFile.get("Maintainer"));
218218
} catch(Exception e) {
219219
throw new IOException(e);
220220
}
@@ -256,6 +256,77 @@ else if (entry.getName().equals("./text.txt")) {
256256
});
257257
}
258258

259+
@Test
260+
public void testCreationCustomToken() throws Exception {
261+
DataProducer[] data = prepareData();
262+
File deb = File.createTempFile("jdeb", ".deb");
263+
264+
File conffile = new File(getClass().getResource("deb/data.tgz").toURI());
265+
266+
Map<String, String> map = new HashMap<>();
267+
map.put("name", "actualName");
268+
map.put("version", "actualVersion");
269+
map.put("maintainer", "John Doe <john@doe.org>");
270+
MapVariableResolver variableResolver = new MapVariableResolver(map);
271+
272+
Data conffile1 = new Data();
273+
conffile1.setType("file");
274+
conffile1.setSrc(conffile);
275+
conffile1.setDst("/absolute/path/to/configuration");
276+
conffile1.setConffile(true);
277+
Data conffile2 = new Data();
278+
conffile2.setType("file");
279+
conffile2.setSrc(conffile);
280+
conffile2.setConffile(true);
281+
282+
Mapper mapper = new Mapper();
283+
FieldUtils.writeField(mapper, "type", "perm", true);
284+
FieldUtils.writeField(mapper, "prefix", "/absolute/prefix", true);
285+
FieldUtils.writeField(conffile2, "mapper", mapper, true);
286+
287+
DebMaker maker =
288+
new DebMaker(new NullConsole(), Arrays.asList(data), Arrays.<DataProducer>asList(conffile1, conffile2));
289+
maker.setEncoding(StandardCharsets.UTF_8);
290+
maker.setControl(new File(getClass().getResource("deb/controlcustomtoken").toURI()));
291+
maker.setDeb(deb);
292+
maker.setResolver(variableResolver);
293+
maker.setOpenReplaceToken("{[{");
294+
maker.setCloseReplaceToken("}]}");
295+
296+
BinaryPackageControlFile packageControlFile = maker.createDeb(Compression.GZIP);
297+
298+
assertTrue(packageControlFile.isValid());
299+
300+
final Map<String, TarArchiveEntry> filesInDeb = new HashMap<>();
301+
302+
final Set<String> actualConffileContent = new HashSet<>();
303+
304+
ArchiveWalker.walkControl(deb, new ArchiveVisitor<TarArchiveEntry>() {
305+
@Override
306+
public void visit(TarArchiveEntry entry, byte[] content) throws IOException {
307+
if (entry.getName().equals("./control")) {
308+
try {
309+
ControlFile controlFile = new BinaryPackageControlFile(org.apache.commons.io.IOUtils.toString(new ByteArrayInputStream(content), StandardCharsets.UTF_8));
310+
assertEquals("variable substitution failed", "John Doe <john@doe.org>", controlFile.get("Maintainer"));
311+
} catch(Exception e) {
312+
throw new IOException(e);
313+
}
314+
}
315+
else if (entry.getName().equals("./postinst") || entry.getName().equals("./prerm")) {
316+
try {
317+
for(String line : org.apache.commons.io.IOUtils.readLines(new ByteArrayInputStream(content), StandardCharsets.UTF_8)) {
318+
if(line.startsWith("# P")) {
319+
assertTrue("variable substitution failed", line.endsWith("actualName actualVersion"));
320+
}
321+
}
322+
} catch(Exception e) {
323+
throw new IOException(e);
324+
}
325+
}
326+
}
327+
});
328+
}
329+
259330
@Test
260331
public void testControlFilesPermissions() throws Exception {
261332
File deb = new File("target/test-classes/test-control.deb");

src/test/java/org/vafer/jdeb/utils/FilteredFileTestCase.java

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ public void testTokenSubstitution() throws Exception {
5252
assertEquals("#!/bin/sh\ncat jdebcustom1 \necho 'custom2'\n", actual);
5353
}
5454

55+
@Test
56+
public void testCustomTokenSubstitution() throws Exception {
57+
InputStream in = new ReaderInputStream(new StringReader("#!/bin/sh\ncat {[{artifactId}]}{[{myProperty1}]} \necho '{[{myProperty2}]}'\n"));
58+
59+
FilteredFile placeHolder = new FilteredFile(in, variableResolver, StandardCharsets.UTF_8, "{[{", "}]}");
60+
61+
String actual = placeHolder.toString();
62+
assertEquals("#!/bin/sh\ncat jdebcustom1 \necho 'custom2'\n", actual);
63+
}
64+
5565
@Test
5666
public void testTokenSubstitutionWithinOpenCloseTokens() throws Exception {
5767
HashMap<String, String> table = new HashMap<String, String>() {{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Package: test
2+
Version: 1.0.1
3+
Section: misc
4+
Priority: optional
5+
Architecture: i386
6+
Depends: some-package
7+
Maintainer: {[{maintainer}]}
8+
Distribution: development
9+
Description: revision @REVISION@, test package
10+
This is a sample package control file.
11+
.
12+
Use for testing purposes only.
13+
XB-UserDefinedField: This is a user defined field.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
#
3+
# Post installation script for {[{name}]} {[{version}]}
4+
#
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
#
3+
# Pre removal script for {[{name}]} {[{version}]}
4+
#

0 commit comments

Comments
 (0)