#88504: alternative to MockServices supporting individual instances in Lookup.default.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/openide.util/test/unit/src/org/openide/util/test/MockLookup.java Wed Apr 25 02:14:40 2007 +0000
1.3 @@ -0,0 +1,83 @@
1.4 +/*
1.5 + * The contents of this file are subject to the terms of the Common Development
1.6 + * and Distribution License (the License). You may not use this file except in
1.7 + * compliance with the License.
1.8 + *
1.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
1.10 + * or http://www.netbeans.org/cddl.txt.
1.11 + *
1.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
1.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
1.14 + * If applicable, add the following below the CDDL Header, with the fields
1.15 + * enclosed by brackets [] replaced by your own identifying information:
1.16 + * "Portions Copyrighted [year] [name of copyright owner]"
1.17 + *
1.18 + * The Original Software is NetBeans. The Initial Developer of the Original
1.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
1.20 + * Microsystems, Inc. All Rights Reserved.
1.21 + */
1.22 +
1.23 +package org.openide.util.test;
1.24 +
1.25 +import java.lang.reflect.Field;
1.26 +import static junit.framework.Assert.*;
1.27 +import org.openide.util.Lookup;
1.28 +import org.openide.util.lookup.Lookups;
1.29 +import org.openide.util.lookup.ProxyLookup;
1.30 +
1.31 +/**
1.32 + * Mock implementation of system default lookup suitable for use in unit tests.
1.33 + * The initial value just contains classpath services.
1.34 + */
1.35 +public class MockLookup extends ProxyLookup {
1.36 +
1.37 + private static MockLookup DEFAULT;
1.38 + private static boolean making = false;
1.39 +
1.40 + static {
1.41 + making = true;
1.42 + try {
1.43 + System.setProperty("org.openide.util.Lookup", MockLookup.class.getName());
1.44 + if (Lookup.getDefault().getClass() != MockLookup.class) {
1.45 + // Someone else initialized lookup first. Try to force our way.
1.46 + Field defaultLookup = Lookup.class.getDeclaredField("defaultLookup");
1.47 + defaultLookup.setAccessible(true);
1.48 + defaultLookup.set(null, null);
1.49 + }
1.50 + assertEquals(MockLookup.class, Lookup.getDefault().getClass());
1.51 + } catch (Exception x) {
1.52 + throw new ExceptionInInitializerError(x);
1.53 + } finally {
1.54 + making = false;
1.55 + }
1.56 + }
1.57 +
1.58 + /** Do not call this directly! */
1.59 + public MockLookup() {
1.60 + assertTrue(making);
1.61 + assertNull(DEFAULT);
1.62 + DEFAULT = this;
1.63 + setInstances();
1.64 + }
1.65 +
1.66 + /**
1.67 + * Sets the global default lookup with zero or more delegate lookups.
1.68 + * Caution: if you don't include Lookups.metaInfServices, you may have trouble,
1.69 + * e.g. {@link #makeScratchDir} will not work.
1.70 + * Most of the time you should use {@link #setInstances} instead.
1.71 + */
1.72 + public static void setLookup(Lookup... lookups) {
1.73 + DEFAULT.setLookups(lookups);
1.74 + }
1.75 +
1.76 + /**
1.77 + * Sets the global default lookup with some fixed instances.
1.78 + * Will also include (at a lower priority) a {@link ClassLoader},
1.79 + * and services found from <code>META-INF/services/*</code> in the classpath.
1.80 + */
1.81 + public static void setInstances(Object... instances) {
1.82 + ClassLoader l = MockLookup.class.getClassLoader();
1.83 + setLookup(Lookups.fixed(instances), Lookups.metaInfServices(l), Lookups.singleton(l));
1.84 + }
1.85 +
1.86 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/openide.util/test/unit/src/org/openide/util/test/MockLookupTest.java Wed Apr 25 02:14:40 2007 +0000
2.3 @@ -0,0 +1,44 @@
2.4 +/*
2.5 + * The contents of this file are subject to the terms of the Common Development
2.6 + * and Distribution License (the License). You may not use this file except in
2.7 + * compliance with the License.
2.8 + *
2.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
2.10 + * or http://www.netbeans.org/cddl.txt.
2.11 + *
2.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
2.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
2.14 + * If applicable, add the following below the CDDL Header, with the fields
2.15 + * enclosed by brackets [] replaced by your own identifying information:
2.16 + * "Portions Copyrighted [year] [name of copyright owner]"
2.17 + *
2.18 + * The Original Software is NetBeans. The Initial Developer of the Original
2.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
2.20 + * Microsystems, Inc. All Rights Reserved.
2.21 + */
2.22 +
2.23 +package org.openide.util.test;
2.24 +
2.25 +import junit.framework.TestCase;
2.26 +import org.openide.util.Lookup;
2.27 +
2.28 +public class MockLookupTest extends TestCase {
2.29 +
2.30 + // XXX test:
2.31 + // setLookup with one or more lookup args does not use M-I/s
2.32 + // still works if another Lookup.getDefault was set before
2.33 +
2.34 + public MockLookupTest(String n) {
2.35 + super(n);
2.36 + }
2.37 +
2.38 + public void testSetLookup() throws Exception {
2.39 + MockLookup.setInstances("hello");
2.40 + assertEquals("initial lookup works", "hello", Lookup.getDefault().lookup(String.class));
2.41 + MockLookup.setInstances("goodbye");
2.42 + assertEquals("modified lookup works", "goodbye", Lookup.getDefault().lookup(String.class));
2.43 + MockLookup.setInstances();
2.44 + assertEquals("cleared lookup works", null, Lookup.getDefault().lookup(String.class));
2.45 + }
2.46 +
2.47 +}