View Javadoc
1   /*
2    * Copyright (C) 2012, Marc Strapetz
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 }