1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.storage.file;
44
45 import static org.junit.Assert.assertArrayEquals;
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertTrue;
48
49 import java.io.ByteArrayOutputStream;
50 import java.io.File;
51 import java.io.FileOutputStream;
52 import java.io.IOException;
53
54 import org.eclipse.jgit.errors.ConfigInvalidException;
55 import org.eclipse.jgit.util.FS;
56 import org.eclipse.jgit.util.FileUtils;
57 import org.eclipse.jgit.util.IO;
58 import org.junit.After;
59 import org.junit.Before;
60 import org.junit.Test;
61
62 public class FileBasedConfigTest {
63
64 private static final String USER = "user";
65
66 private static final String NAME = "name";
67
68 private static final String ALICE = "Alice";
69
70 private static final String BOB = "Bob";
71
72 private static final String CONTENT1 = "[" + USER + "]\n\t" + NAME + " = "
73 + ALICE + "\n";
74
75 private static final String CONTENT2 = "[" + USER + "]\n\t" + NAME + " = "
76 + BOB + "\n";
77
78 private File trash;
79
80 @Before
81 public void setUp() throws Exception {
82 trash = File.createTempFile("tmp_", "");
83 trash.delete();
84 assertTrue("mkdir " + trash, trash.mkdir());
85 }
86
87 @After
88 public void tearDown() throws Exception {
89 FileUtils.delete(trash, FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
90 }
91
92 @Test
93 public void testSystemEncoding() throws IOException, ConfigInvalidException {
94 final File file = createFile(CONTENT1.getBytes());
95 final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
96 config.load();
97 assertEquals(ALICE, config.getString(USER, null, NAME));
98
99 config.setString(USER, null, NAME, BOB);
100 config.save();
101 assertArrayEquals(CONTENT2.getBytes(), IO.readFully(file));
102 }
103
104 @Test
105 public void testUTF8withoutBOM() throws IOException, ConfigInvalidException {
106 final File file = createFile(CONTENT1.getBytes("UTF-8"));
107 final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
108 config.load();
109 assertEquals(ALICE, config.getString(USER, null, NAME));
110
111 config.setString(USER, null, NAME, BOB);
112 config.save();
113 assertArrayEquals(CONTENT2.getBytes(), IO.readFully(file));
114 }
115
116 @Test
117 public void testUTF8withBOM() throws IOException, ConfigInvalidException {
118 final ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
119 bos1.write(0xEF);
120 bos1.write(0xBB);
121 bos1.write(0xBF);
122 bos1.write(CONTENT1.getBytes("UTF-8"));
123
124 final File file = createFile(bos1.toByteArray());
125 final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
126 config.load();
127 assertEquals(ALICE, config.getString(USER, null, NAME));
128
129 config.setString(USER, null, NAME, BOB);
130 config.save();
131
132 final ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
133 bos2.write(0xEF);
134 bos2.write(0xBB);
135 bos2.write(0xBF);
136 bos2.write(CONTENT2.getBytes("UTF-8"));
137 assertArrayEquals(bos2.toByteArray(), IO.readFully(file));
138 }
139
140 @Test
141 public void testLeadingWhitespaces() throws IOException, ConfigInvalidException {
142 final ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
143 bos1.write(" \n\t".getBytes());
144 bos1.write(CONTENT1.getBytes());
145
146 final File file = createFile(bos1.toByteArray());
147 final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
148 config.load();
149 assertEquals(ALICE, config.getString(USER, null, NAME));
150
151 config.setString(USER, null, NAME, BOB);
152 config.save();
153
154 final ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
155 bos2.write(" \n\t".getBytes());
156 bos2.write(CONTENT2.getBytes());
157 assertArrayEquals(bos2.toByteArray(), IO.readFully(file));
158 }
159
160 private File createFile(byte[] content) throws IOException {
161 trash.mkdirs();
162 File f = File.createTempFile(getClass().getName(), null, trash);
163 FileOutputStream os = new FileOutputStream(f, true);
164 try {
165 os.write(content);
166 } finally {
167 os.close();
168 }
169 return f;
170 }
171 }