001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.util.system;
038:
039: import junit.framework.*;
040: import org.netbeans.installer.utils.SystemUtils;
041: import org.netbeans.installer.utils.system.WindowsNativeUtils;
042: import org.netbeans.installer.utils.system.windows.*;
043:
044: /**
045: *
046: * @author dlm198383
047: */
048: public class WindowsRegistryTest extends TestCase {
049: private static WindowsRegistry registry = null;
050: private SystemUtils su = null;
051: private String sep = "\\";
052: private int HKLM = WindowsRegistry.HKEY_LOCAL_MACHINE;
053: private int HKCU = WindowsRegistry.HKEY_CURRENT_USER;
054: private int HKCC = WindowsRegistry.HKEY_CURRENT_CONFIG;
055: private int HKCR = WindowsRegistry.HKEY_CLASSES_ROOT;
056: private int HKU = WindowsRegistry.HKEY_USERS;
057: private String sw = "Software";
058: String subkey = "WindowsRegistryTest";
059: String valueName = "Name";
060: String stringValue = "Value";
061:
062: public WindowsRegistryTest(String testName) {
063: super (testName);
064: }
065:
066: /*
067: protected void setUp() throws Exception {
068: init();
069: }
070:
071: protected void tearDown() throws Exception {
072: }
073:
074: private void init() {
075: if(su==null) {
076: su = SystemUtils.getInstance();
077: if(su instanceof WindowsNativeUtils) {
078: registry = ((WindowsNativeUtils)su).getWindowsRegistry();
079: }
080:
081: }
082: }
083: public void testInitialize() {
084: init();
085: }
086:
087:
088: public void testNullKeysAndValues() {
089: if(!registry.isKeyExists(HKCU, "")) {
090: fail();
091: return;
092: }
093: if(registry.isValueExists(HKCU, null, null)) {
094: fail();
095: return;
096: }
097: if(!registry.isKeyEmpty(HKCU, null)) {
098: fail();
099: return;
100: }
101: if(registry.getStringValue(HKCU, null,null)!=null) {
102: fail();
103: return;
104: }
105: if(registry.getBinaryValue(HKCU, null,null)!=null) {
106: fail();
107: return;
108: }
109: if(registry.getMultiStringValue(HKCU, null,null)!=null) {
110: fail();
111: return;
112: }
113: if(registry.get32BitValue(HKCU, null,null)!=-1) {
114: fail();
115: return;
116: }
117: if(registry.isKeyExists(HKCU,subkey)) {
118: if(!registry.deleteKey(HKCU,subkey)) {
119: fail();
120: }
121: }
122: if(!registry.createKey(HKCU,null,subkey)) {
123: fail();
124: }
125: if(registry.deleteKey(HKCU,subkey,null)) {
126: fail();
127: }
128: if(registry.createKey(HKCU,null,subkey)) {
129: fail();
130: }
131: if(registry.deleteKey(HKCU,null,null)) {
132: fail();
133: }
134: if(registry.createKey(HKCU,null,subkey)) {
135: fail();
136: }
137: if(registry.deleteKey(HKCU,null)) {
138: fail();
139: }
140: if(!registry.deleteKey(HKCU,null,subkey)) {
141: fail();
142: }
143: return;
144: }
145:
146: public void testIsKeyExists() {
147:
148: String keys = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
149: if(registry.isKeyExists(HKCU, keys)) {
150: return;
151: }
152: fail();
153: }
154:
155:
156: public void testIsValueExists() {
157: if(!registry.isValueExists(HKCU,
158: "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
159: "User Agent")) {
160: fail();
161: }
162:
163: }
164:
165: public void testIsKeyEmpty() {
166: if(!registry.isKeyEmpty(
167: HKCU,
168: "Software\\Microsoft\\Windows")) {
169: fail();
170: }
171: }
172:
173:
174: public void testAddRemoveStringValue() {
175:
176: if(registry.isKeyExists(HKCU,sw + sep + subkey)) {
177: if(!registry.deleteKey(HKCU,sw,subkey)) {
178: fail("Can`t delete key");
179: }
180: }
181:
182: if(!registry.createKey(HKCU,sw,subkey)) {
183: fail("Can`t create key");
184: }
185:
186:
187: if(registry.isValueExists(HKCU,sw + sep + subkey,valueName)) {
188: if(!registry.deleteValue(HKCU,sw + sep + subkey, valueName)) {
189: fail("Can`t delete value");
190: }
191: }
192:
193: if(!registry.setStringValue(HKCU,sw + sep + subkey, valueName, stringValue, false)) {
194: fail("Can`t set string value");
195: }
196:
197: String getV = registry.getStringValue(HKCU,sw + sep + subkey, valueName, false);
198:
199: if(getV == null) {
200: fail("getStringValue return null");
201: }
202:
203: if(!getV.equals(stringValue)) {
204: fail("\n\ngetStringValue returned \"" + getV + "\"!=\""+stringValue + "\"\n");
205: }
206:
207: if(!registry.setStringValue(HKCU,sw + sep + subkey, valueName, stringValue, false)) {
208: fail("Can`t set string value");
209: }
210:
211: String env = "USERPROFILE";
212: String value = System.getenv(env);
213: if(value==null) {
214: env = "TEMP";
215: value = System.getenv(env);
216: }
217: if(value==null) {
218: env = "SYSTEMROOT";
219: value = System.getenv(env);
220: }
221: if(value!=null) {
222: if(!registry.setStringValue(HKCU,sw + sep + subkey, valueName, "%" + env +"%", true)) {
223: fail("Can`t set string value");
224: }
225: getV = registry.getStringValue(HKCU,sw + sep + subkey, valueName, true);
226:
227: if(getV == null) {
228: fail("getStringValue return null");
229: }
230:
231: if(!getV.equals(value)) {
232: fail("\n\ngetStringValue returned \"" + getV + "\"!=\""+value + "\"\n");
233: }
234: }
235:
236: if(!registry.deleteValue(HKCU,sw + sep + subkey, valueName)) {
237: fail();
238: }
239: if(!registry.deleteKey(HKCU,sw,subkey)) {
240: fail("Can`t delete key");
241: }
242: }
243:
244: public void testAddRemoveKey() {
245:
246: if(registry.isKeyExists(HKCU,sw + sep + subkey)) {
247: if(!registry.deleteKey(HKCU,sw, subkey)) {
248: fail("Can`t delete key");
249: }
250: }
251:
252: if(!registry.createKey(HKCU,sw,subkey)) {
253: fail("Can`t create key");
254: }
255:
256: if(!registry.deleteKey(HKCU,sw, subkey)) {
257: fail("Can`t delete key");
258: }
259: }
260: public void testAddRemoveBinaryValue() {
261: if(registry.isKeyExists(HKCU,sw + sep + subkey)) {
262: if(!registry.deleteKey(HKCU,sw,subkey)) {
263: fail("Can`t delete key");
264: }
265: }
266:
267: if(!registry.createKey(HKCU,sw,subkey)) {
268: fail("Can`t create key");
269: }
270:
271:
272: if(registry.isValueExists(HKCU,sw + sep + subkey,valueName)) {
273: if(!registry.deleteValue(HKCU,sw + sep + subkey, valueName)) {
274: fail("Can`t delete value");
275: }
276: }
277: byte [] data = new byte [] {0,Byte.MIN_VALUE,Byte.MIN_VALUE/2, Byte.MAX_VALUE, Byte.MAX_VALUE/2};
278:
279: if(!registry.setBinaryValue(HKCU,sw + sep + subkey, valueName, data)) {
280: fail("Can`t set string value");
281: }
282: byte [] getV = registry.getBinaryValue(HKCU,sw + sep + subkey, valueName);
283: if(getV == null) {
284: fail("getBinaryValue return null");
285: }
286:
287: if(getV.length!=data.length) {
288: fail("getBinaryValue return unexpected length of data");
289: }
290: for(int i=0;i<getV.length;i++) {
291: if(getV[i]!=data[i]) {
292: fail("\n\ngetBinaryValue returned \"" + getV[i] +
293: "\"!=\"" + data[i] + "\"\n");
294: }
295: }
296:
297: byte [] data2 = new byte [] {Byte.MIN_VALUE,Byte.MAX_VALUE,0};
298:
299: if(!registry.setBinaryValue(HKCU,sw + sep + subkey, valueName, data2)) {
300: fail("Can`t set string value");
301: }
302:
303: getV = registry.getBinaryValue(HKCU,sw + sep + subkey, valueName);
304:
305: if(getV == null) {
306: fail("getBinaryValue return null");
307: }
308:
309: if(getV.length!=data2.length) {
310: fail("getBinaryValue return unexpected length of data");
311: }
312: for(int i=0;i<getV.length;i++) {
313: if(getV[i]!=data2[i]) {
314: fail("\n\ngetBinaryValue returned \"" + getV[i] +
315: "\"!=\"" + data2[i] + "\"\n");
316: }
317: }
318:
319: if(!registry.deleteValue(HKCU,sw + sep + subkey, valueName)) {
320: fail();
321: }
322: if(!registry.deleteKey(HKCU,sw,subkey)) {
323: fail("Can`t delete key");
324: }
325: }
326: public void testAddRemoveMultiStringValue() {
327: if(registry.isKeyExists(HKCU,sw + sep + subkey)) {
328: if(!registry.deleteKey(HKCU,sw,subkey)) {
329: fail("Can`t delete key");
330: }
331: }
332:
333: if(!registry.createKey(HKCU,sw,subkey)) {
334: fail("Can`t create key");
335: }
336:
337:
338: if(registry.isValueExists(HKCU,sw + sep + subkey,valueName)) {
339: if(!registry.deleteValue(HKCU,sw + sep + subkey, valueName)) {
340: fail("Can`t delete value");
341: }
342: }
343: String [] data = new String [] {"String1","String2","String3","String4"};
344:
345: if(!registry.setMultiStringValue(HKCU,sw + sep + subkey, valueName, data)) {
346: fail("Can`t set multi string value");
347: }
348: String [] getV = registry.getMultiStringValue(HKCU,sw + sep + subkey, valueName);
349: if(getV == null) {
350: fail("getMultiStringValue return null");
351: }
352:
353: if(getV.length!=data.length) {
354: fail("getMultiStringValue return unexpected length of data");
355: }
356: for(int i=0;i<getV.length;i++) {
357: if(!getV[i].equals(data[i])) {
358: fail("\n\ngetBinaryValue returned \"" + getV[i] +
359: "\"!=\"" + data[i] + "\"\n");
360: }
361: }
362:
363: String [] data2 = new String [] {"String1","String2","String3","String4","String5"};
364:
365: if(!registry.setMultiStringValue(HKCU,sw + sep + subkey, valueName, data2)) {
366: fail("Can`t set string value");
367: }
368:
369: getV = registry.getMultiStringValue(HKCU,sw + sep + subkey, valueName);
370:
371: if(getV == null) {
372: fail("getBinaryValue return null");
373: }
374:
375: if(getV.length!=data2.length) {
376: fail("getMultiStringValue return unexpected length of data");
377: }
378: for(int i=0;i<getV.length;i++) {
379: if(!getV[i].equals(data2[i])) {
380: fail("\n\ngetMultiStringValue returned \"" + getV[i] +
381: "\"!=\"" + data2[i] + "\"\n");
382: }
383: }
384:
385: if(!registry.deleteValue(HKCU,sw + sep + subkey, valueName)) {
386: fail();
387: }
388: if(!registry.deleteKey(HKCU,sw,subkey)) {
389: fail("Can`t delete key");
390: }
391: }
392: public void testAddRemove32BitValue() {
393: if(registry.isKeyExists(HKCU,sw + sep + subkey)) {
394: if(!registry.deleteKey(HKCU,sw,subkey)) {
395: fail("Can`t delete key");
396: }
397: }
398:
399: if(!registry.createKey(HKCU,sw,subkey)) {
400: fail("Can`t create key");
401: }
402:
403:
404: if(registry.isValueExists(HKCU,sw + sep + subkey,valueName)) {
405: if(!registry.deleteValue(HKCU,sw + sep + subkey, valueName)) {
406: fail("Can`t delete value");
407: }
408: }
409: int data = Integer.MAX_VALUE;
410:
411: if(!registry.set32BitValue(HKCU,sw + sep + subkey, valueName, data)) {
412: fail("Can`t set integer value");
413: }
414: int getV = registry.get32BitValue(HKCU,sw + sep + subkey, valueName);
415: if(getV == -1) {
416: fail("get32BitValue return null");
417: }
418:
419: if(getV!=data) {
420: fail("\n\nget32BitValue returned \"" + getV +
421: "\"!=\"" + data + "\"\n");
422: }
423:
424: int data2 = Integer.MIN_VALUE;
425:
426: if(!registry.set32BitValue(HKCU,sw + sep + subkey, valueName, data2)) {
427: fail("Can`t set integer value");
428: }
429: getV = registry.get32BitValue(HKCU,sw + sep + subkey, valueName);
430: if(getV == -1) {
431: fail("get32BitValue return null");
432: }
433:
434: if(getV!=data2) {
435: fail("\n\nget32BitValue returned \"" + getV +
436: "\"!=\"" + data2 + "\"\n");
437: }
438:
439: if(!registry.deleteValue(HKCU,sw + sep + subkey, valueName)) {
440: fail();
441: }
442: if(!registry.deleteKey(HKCU,sw,subkey)) {
443: fail("Can`t delete key");
444: }
445: }
446: public void testGetValueNames () {
447: if(registry.isKeyExists(HKCU,sw + sep + subkey)) {
448: if(!registry.deleteKey(HKCU,sw,subkey)) {
449: fail("Can`t delete key");
450: }
451: }
452:
453: if(!registry.createKey(HKCU,sw,subkey)) {
454: fail("Can`t create key");
455: }
456:
457: String [] names = { "Super Duper Name",
458: "\\u1047\\u1085\\u1072\\u1081\\u32\\u1080\\u32\\u1083" + //NOI18N
459: "\\u1102\\u1073\\u1080\\u32\\u1074\\u1077\\u1083" + //NOI18N
460: "\\u1080\\u1082\\u1080\\u1081\\u32\\u1080\\u32\\u1084" + //NOI18N
461: "\\u1086\\u1075\\u1091\\u1095\\u1080\\u1081\\u46\\u46\\u46"}; //NOI18N
462: for(int i=0;i<names.length;i++) {
463: if(!registry.setStringValue(HKCU,sw + sep + subkey, names[i],"Value")) {
464: fail("Can`t set string value with name: " + names[i]);
465: }
466: }
467: String [] namesV = registry.getValueNames(HKCU,sw + sep + subkey);
468: if(namesV == null) {
469: fail("getValueNames returned unexpected null");
470: }
471: if(namesV.length != names.length) {
472: fail("getValueNames returned unexpected length");
473: }
474: for(int i=0;i<names.length;i++) {
475: if(!names[i].equals(namesV[i])) {
476: fail("getValueNames returned unexpected name. \nWas: " + namesV[i] +
477: "\nExpected: " + names[i]);
478: }
479: }
480:
481: if(!registry.deleteKey(HKCU,sw,subkey)) {
482: fail("Can`t delete key");
483: }
484: }*/
485: public void testNone() {
486:
487: }
488: }
|