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
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.webmodule;
043:
044: import java.util.Collection;
045: import java.util.HashMap;
046: import java.util.Map;
047: import org.netbeans.junit.MockServices;
048: import org.netbeans.junit.MockServices;
049: import org.netbeans.junit.NbTestCase;
050: import org.netbeans.modules.web.api.webmodule.WebModule;
051: import org.netbeans.modules.web.spi.webmodule.WebModuleFactory;
052: import org.netbeans.modules.web.spi.webmodule.WebModuleProvider;
053: import org.openide.filesystems.FileObject;
054: import org.openide.filesystems.FileUtil;
055: import org.openide.util.Lookup;
056:
057: /**
058: *
059: * @author Pavel Buzek, Andrei Badea
060: */
061: public class WebModuleTest extends NbTestCase {
062:
063: private FileObject datadir;
064:
065: public WebModuleTest(String name) {
066: super (name);
067: }
068:
069: @Override
070: protected void setUp() throws Exception {
071: super .setUp();
072: MockServices.setServices(WebModuleProviderImpl.class);
073: datadir = FileUtil.toFileObject(getDataDir());
074: }
075:
076: @Override
077: protected void tearDown() throws Exception {
078: super .tearDown();
079: MockServices.setServices();
080: }
081:
082: public void testProviders() throws Exception {
083: Collection<? extends WebModuleProvider> providers = Lookup
084: .getDefault().lookupAll(WebModuleProvider.class);
085: assertEquals(
086: "there should be 2 instances - one from web/webapi and one from tests",
087: 2, providers.size());
088: }
089:
090: public void testGetWebModule() throws Exception {
091: FileObject foo = datadir.getFileObject("a.foo");
092: FileObject bar = datadir.getFileObject("b.bar");
093: WebModule wm1 = WebModule.getWebModule(foo);
094: assertNotNull("found web module", wm1);
095: WebModule wm2 = WebModule.getWebModule(bar);
096: assertNull("no web module", wm2);
097: }
098:
099: public static final class WebModuleProviderImpl implements
100: WebModuleProvider {
101:
102: private final Map<FileObject, WebModule> cache = new HashMap<FileObject, WebModule>();
103:
104: public WebModuleProviderImpl() {
105: }
106:
107: public WebModule findWebModule(FileObject file) {
108: if (file.getExt().equals("foo")) {
109: WebModule wm = cache.get(file.getParent());
110: if (wm == null) {
111: wm = WebModuleFactory
112: .createWebModule(new SimpleWebModuleImpl());
113: cache.put(file.getParent(), wm);
114: }
115: return wm;
116: }
117: return null;
118: }
119: }
120: }
|