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.apisupport.project.suite;
043:
044: import java.io.File;
045: import java.io.FileNotFoundException;
046: import java.io.FileOutputStream;
047: import java.io.IOException;
048: import java.io.InputStream;
049: import java.io.OutputStream;
050: import java.net.MalformedURLException;
051: import java.util.Collections;
052: import java.util.Set;
053: import org.netbeans.api.project.ProjectManager;
054: import org.netbeans.modules.apisupport.project.TestBase;
055: import org.netbeans.modules.apisupport.project.ui.customizer.SuitePropertiesTest;
056: import org.netbeans.modules.apisupport.project.universe.NbPlatform;
057: import org.openide.filesystems.FileObject;
058: import org.openide.filesystems.FileUtil;
059:
060: /**
061: *
062: * @author Radek Matous
063: */
064: public class BrandingSupportTest extends TestBase {
065: private BrandingSupport instance = null;
066:
067: public BrandingSupportTest(String testName) {
068: super (testName);
069: }
070:
071: protected void setUp() throws Exception {
072: super .setUp();
073: File suiteDir = new File(getWorkDir(), "testSuite");
074: SuiteProjectGenerator.createSuiteProject(suiteDir,
075: NbPlatform.PLATFORM_ID_DEFAULT);
076: FileObject fo = FileUtil.toFileObject(suiteDir);
077: SuiteProject suitePrj = (SuiteProject) ProjectManager
078: .getDefault().findProject(fo);
079: assertNotNull(suitePrj);
080:
081: instance = BrandingSupport.getInstance(SuitePropertiesTest
082: .getSuiteProperties(suitePrj));
083:
084: }
085:
086: public void testBranding1() throws IOException {
087: assertFalse(instance.getBrandingRoot().exists());
088: implOfBundleKeyTest("org.netbeans.core.startup",
089: "org/netbeans/core/startup/Bundle.properties",
090: Collections.singleton("CTL_About_Title"), "About");
091: }
092:
093: public void testBranding2() throws IOException {
094: assertFalse(instance.getBrandingRoot().exists());
095: implOfBundleKeyTest("org.netbeans.core.startup", null,
096: Collections.singleton("CTL_About_Title"), "About");
097: }
098:
099: public void testBranding3() throws IOException {
100: assertFalse(instance.getBrandingRoot().exists());
101: implOfBundleKeyTest("org.netbeans.core",
102: "org/netbeans/core/ui/Bundle.properties", Collections
103: .singleton("LBL_SwingBrowserDescription"),
104: "Simple HTML Browser based on a Swing component");
105: }
106:
107: public void testBranding4() throws IOException {
108: assertFalse(instance.getBrandingRoot().exists());
109: implOfBundleKeyTest("org.netbeans.core.windows",
110: "org/netbeans/core/windows/view/ui/Bundle.properties",
111: Collections.singleton("CTL_MainWindow_Title"),
112: "NetBeans Platform {0}");
113: }
114:
115: public void testBrandingFile() throws IOException {
116: assertFalse(instance.getBrandingRoot().exists());
117: assertNotNull(instance.getBrandedFiles());
118: assertEquals(0, instance.getBrandedFiles().size());
119: BrandingSupport.BrandedFile bFile = instance.getBrandedFile(
120: "org.netbeans.core.startup",
121: "org/netbeans/core/startup/splash.gif");
122:
123: BrandingSupport.BrandedFile bFile2 = instance.getBrandedFile(
124: "org.netbeans.core.startup",
125: "org/netbeans/core/startup/splash.gif");
126:
127: assertEquals(bFile2, bFile);
128: assertEquals(bFile2.getBrandingSource(), bFile
129: .getBrandingSource());
130: assertFalse(bFile.isModified());
131:
132: assertNotNull(bFile);
133: assertEquals(0, instance.getBrandedFiles().size());
134: assertFalse(instance.isBranded(bFile));
135: instance.brandFile(bFile);
136: assertFalse(bFile.isModified());
137:
138: assertFalse(instance.isBranded(bFile));
139: assertEquals(0, instance.getBrandedFiles().size());
140:
141: File newSource = createNewSource(bFile);
142: assertEquals(0, instance.getBrandedFiles().size());
143:
144: bFile.setBrandingSource(newSource);
145: assertTrue(bFile.isModified());
146:
147: assertEquals(0, instance.getBrandedFiles().size());
148: instance.brandFile(bFile);
149: assertFalse(bFile.isModified());
150:
151: assertEquals(1, instance.getBrandedFiles().size());
152: assertTrue(instance.isBranded(bFile));
153: assertEquals(bFile2, bFile);
154: assertFalse(bFile2.getBrandingSource().equals(
155: bFile.getBrandingSource()));
156:
157: }
158:
159: private File createNewSource(final BrandingSupport.BrandedFile bFile)
160: throws MalformedURLException, FileNotFoundException,
161: IOException {
162: OutputStream os = null;
163: InputStream is = null;
164: File newSource = new File(getWorkDir(), "newSource.gif");
165:
166: try {
167:
168: os = new FileOutputStream(newSource);
169: is = bFile.getBrandingSource().openStream();
170: FileUtil.copy(is, os);
171: } finally {
172: if (is != null) {
173: is.close();
174: }
175: if (os != null) {
176: os.close();
177: }
178: }
179: return newSource;
180: }
181:
182: private void implOfBundleKeyTest(final String moduleCodeNameBase,
183: final String bundleEntry, final Set<String> keys,
184: String expectedValue) throws IOException {
185: Set<BrandingSupport.BundleKey> bKeys;
186: if (bundleEntry != null) {
187: bKeys = instance.getBundleKeys(moduleCodeNameBase,
188: bundleEntry, keys);
189: } else {
190: bKeys = instance.getLocalizingBundleKeys(
191: moduleCodeNameBase, keys);
192: }
193:
194: assertNotNull(bKeys);
195: assertEquals(1, bKeys.size());
196:
197: BrandingSupport.BundleKey bKey = bKeys.iterator().next();
198: assertFalse(instance.isBranded(bKey));
199: assertFalse(instance.isBranded(bKey.getModuleEntry()));
200: assertFalse(instance.getBrandingRoot().exists());
201: assertFalse(instance.getModuleEntryDirectory(
202: bKey.getModuleEntry()).exists());
203: assertNotNull(instance.getBrandedBundleKeys());
204: assertFalse(instance.getBrandedBundleKeys().contains(bKey));
205: assertEquals(expectedValue, bKey.getValue());
206:
207: instance.brandBundleKeys(bKeys);
208: assertFalse(instance.isBranded(bKey));
209: assertFalse(instance.isBranded(bKey.getModuleEntry()));
210: assertFalse(instance.getBrandingRoot().exists());
211: assertFalse(instance.getModuleEntryDirectory(
212: bKey.getModuleEntry()).exists());
213: assertNotNull(instance.getBrandedBundleKeys());
214: assertFalse(instance.getBrandedBundleKeys().contains(bKey));
215: assertEquals(expectedValue, bKey.getValue());
216: assertFalse(bKey.isModified());
217:
218: bKey.setValue("brandedValue");
219: assertTrue(bKey.isModified());
220: instance.brandBundleKeys(bKeys);
221: assertFalse(bKey.isModified());
222:
223: assertTrue(instance.isBranded(bKey));
224: assertTrue(instance.isBranded(bKey.getModuleEntry()));
225: assertTrue(instance.getBrandingRoot().exists());
226: assertTrue(instance.getModuleEntryDirectory(
227: bKey.getModuleEntry()).exists());
228: assertNotNull(instance.getBrandedBundleKeys());
229: assertTrue(instance.getBrandedBundleKeys().contains(bKey));
230: assertEquals("brandedValue", bKey.getValue());
231:
232: }
233:
234: }
|