001/* 002 * Copyright 2005,2009 Ivan SZKIBA 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ini4j; 017 018import org.ini4j.sample.Dwarfs; 019 020import org.ini4j.test.DwarfsData; 021import org.ini4j.test.Helper; 022 023import static org.junit.Assert.assertArrayEquals; 024import static org.junit.Assert.assertNotNull; 025import static org.junit.Assert.assertSame; 026import static org.junit.Assert.fail; 027 028import org.junit.Test; 029 030import java.io.ByteArrayOutputStream; 031import java.io.File; 032import java.io.FileInputStream; 033import java.io.FileNotFoundException; 034import java.io.FileOutputStream; 035import java.io.IOException; 036import java.io.InputStream; 037import java.io.InputStreamReader; 038import java.io.StringReader; 039 040public class RegTest extends Ini4jCase 041{ 042 private static final String DWARFS_PATH = Helper.DWARFS_REG_PATH + "\\dwarfs\\"; 043 044 @Test public void proba() throws Exception 045 { 046 } 047 048 @Test public void testDwarfs() throws Exception 049 { 050 Reg reg = Helper.loadDwarfsReg(); 051 Dwarfs dwarfs = reg.as(Dwarfs.class, DWARFS_PATH); 052 053 assertNotNull(dwarfs); 054 Helper.assertEquals(DwarfsData.dwarfs, dwarfs); 055 } 056 057 @Test public void testInvalidFileFormatException() throws Exception 058 { 059 try 060 { 061 new Reg(Helper.getResourceReader(Helper.DWARFS_INI)); 062 missing(InvalidFileFormatException.class); 063 } 064 catch (InvalidFileFormatException x) 065 { 066 // 067 } 068 } 069 070 @Test public void testIsWindwos() 071 { 072 assertEquals(isWindows(), Reg.isWindows()); 073 } 074 075 @Test public void testLoad() throws Exception 076 { 077 Reg r1 = new Reg(new InputStreamReader(Helper.getResourceStream(Helper.DWARFS_REG), "UnicodeLittle")); 078 Reg r2 = new Reg(Helper.getResourceStream(Helper.DWARFS_REG)); 079 Reg r3 = new Reg(Helper.getResourceURL(Helper.DWARFS_REG)); 080 File f = Helper.getSourceFile(Helper.DWARFS_REG); 081 Reg r4 = new Reg(f); 082 Reg r5 = new Reg(); 083 084 r5.setFile(f); 085 r5.load(); 086 Helper.assertEquals(DwarfsData.dwarfs, r1.as(Dwarfs.class, DWARFS_PATH)); 087 Helper.assertEquals(DwarfsData.dwarfs, r2.as(Dwarfs.class, DWARFS_PATH)); 088 Helper.assertEquals(DwarfsData.dwarfs, r3.as(Dwarfs.class, DWARFS_PATH)); 089 Helper.assertEquals(DwarfsData.dwarfs, r4.as(Dwarfs.class, DWARFS_PATH)); 090 Helper.assertEquals(DwarfsData.dwarfs, r5.as(Dwarfs.class, DWARFS_PATH)); 091 assertSame(f, r4.getFile()); 092 } 093 094 @Test public void testLoadFileNotFoundException() throws Exception 095 { 096 Reg reg = new Reg(); 097 098 try 099 { 100 reg.load(); 101 missing(FileNotFoundException.class); 102 } 103 catch (FileNotFoundException x) 104 { 105 // 106 } 107 } 108 109 @Test public void testLoadSave() throws Exception 110 { 111 Reg reg = new Reg(Helper.getResourceURL(Helper.TEST_REG)); 112 113 checkLoadSave(Helper.TEST_REG, reg); 114 } 115 116 @Test public void testMissingVersion() throws Exception 117 { 118 try 119 { 120 new Reg(new StringReader("\r\n\r\n[section]\r\n\"option\"=\"value\"")); 121 missing(InvalidFileFormatException.class); 122 } 123 catch (InvalidFileFormatException x) 124 { 125 // 126 } 127 } 128 129 @Test public void testNonWindwosExec() throws Exception 130 { 131 if (isSkip(isWindows(), "testNonWindwosExec")) 132 { 133 return; 134 } 135 136 Reg reg = new Reg(); 137 138 reg.exec(new String[] { "/bin/true" }); 139 try 140 { 141 reg.exec(new String[] { "/bin/ls", "no such file" }); 142 fail("IOException expected"); 143 } 144 catch (IOException x) 145 { 146 assert true; 147 } 148 } 149 150 @Test public void testReadException() throws Exception 151 { 152 if (!isWindows()) 153 { 154 try 155 { 156 new Reg(Reg.Hive.HKEY_CURRENT_USER.toString()); 157 fail("missing UnsupportedOperationException"); 158 } 159 catch (UnsupportedOperationException x) 160 { 161 assert true; 162 } 163 } 164 else 165 { 166 try 167 { 168 new Reg("no such key"); 169 fail("missing IOException"); 170 } 171 catch (IOException x) 172 { 173 assert true; 174 } 175 } 176 } 177 178 @Test public void testReadWrite() throws Exception 179 { 180 if (isSkip(!isWindows(), "testReadWrite")) 181 { 182 return; 183 } 184 185 Reg reg = Helper.loadDwarfsReg(); 186 187 reg.write(); 188 Reg dup = new Reg(Helper.DWARFS_REG_PATH); 189 190 Helper.assertEquals(reg.get(Helper.DWARFS_REG_PATH), dup.get(Helper.DWARFS_REG_PATH)); 191 Dwarfs dwarfs = dup.as(Dwarfs.class, DWARFS_PATH); 192 193 assertNotNull(dwarfs); 194 Helper.assertEquals(DwarfsData.dwarfs, dwarfs); 195 } 196 197 @Test public void testStore() throws Exception 198 { 199 Reg reg = Helper.loadDwarfsReg(); 200 File tmp = File.createTempFile(Reg.TMP_PREFIX, Reg.DEFAULT_SUFFIX); 201 202 tmp.deleteOnExit(); 203 reg.setFile(tmp); 204 reg.store(); 205 reg = new Reg(tmp); 206 Helper.assertEquals(DwarfsData.dwarfs, reg.as(Dwarfs.class, DWARFS_PATH)); 207 tmp.delete(); 208 } 209 210 @Test public void testStoreFileNotFoundException() throws Exception 211 { 212 try 213 { 214 new Reg().store(); 215 missing(FileNotFoundException.class); 216 } 217 catch (FileNotFoundException x) 218 { 219 // 220 } 221 } 222 223 @Test public void testUnsupportedOperatingSystem() throws Exception 224 { 225 if (isSkip(isWindows(), "testUnsupportedOperatingSystem")) 226 { 227 return; 228 } 229 230 Reg reg = new Reg(); 231 232 try 233 { 234 reg.read(Helper.DWARFS_REG_PATH); 235 fail("UnsupportedOperationException expected"); 236 } 237 catch (UnsupportedOperationException x) 238 { 239 assert true; 240 } 241 242 try 243 { 244 reg.write(); 245 fail("UnsupportedOperationException expected"); 246 } 247 catch (UnsupportedOperationException x) 248 { 249 assert true; 250 } 251 } 252 253 private boolean isSkip(boolean flag, String testName) 254 { 255 if (!flag) 256 { 257 System.out.println("Skipping " + getClass().getName() + '#' + testName); 258 } 259 260 return flag; 261 } 262 263 private boolean isWindows() 264 { 265 String family = System.getProperty("os.family"); 266 267 return (family != null) && family.equals("windows"); 268 } 269 270 private void checkLoadSave(String path, Reg reg) throws Exception 271 { 272 File tmp = File.createTempFile(Reg.TMP_PREFIX, Reg.DEFAULT_SUFFIX); 273 274 tmp.deleteOnExit(); 275 reg.store(new FileOutputStream(tmp)); 276 assertArrayEquals(read(Helper.getResourceStream(path)), read(new FileInputStream(tmp))); 277 } 278 279 private byte[] read(InputStream input) throws Exception 280 { 281 ByteArrayOutputStream out = new ByteArrayOutputStream(); 282 byte[] buff = new byte[81912]; 283 int n; 284 285 while ((n = input.read(buff)) >= 0) 286 { 287 out.write(buff, 0, n); 288 } 289 290 return out.toByteArray(); 291 } 292}