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: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028:
029: package org.netbeans.modules.web.jsf.refactoring;
030:
031: import java.io.ByteArrayInputStream;
032: import java.io.ByteArrayOutputStream;
033: import java.io.File;
034: import java.io.FileWriter;
035: import java.io.InputStream;
036: import java.io.InputStreamReader;
037: import java.io.Reader;
038: import java.io.StringWriter;
039: import java.io.Writer;
040: import java.nio.charset.Charset;
041: import java.util.LinkedList;
042: import java.util.List;
043: import javax.swing.text.Position.Bias;
044: import junit.framework.TestCase;
045: import org.netbeans.api.queries.FileEncodingQuery;
046: import org.netbeans.junit.MockServices;
047: import org.netbeans.junit.NbTestCase;
048: import org.netbeans.modules.web.jsf.editor.JSFEditorUtilities;
049: import org.netbeans.modules.web.jsf.refactoring.Modifications.Difference;
050: import org.netbeans.modules.xml.api.XmlFileEncodingQueryImpl;
051: import org.netbeans.spi.queries.FileEncodingQueryImplementation;
052: import org.openide.filesystems.FileObject;
053: import org.openide.filesystems.FileUtil;
054: import org.openide.loaders.DataObject;
055: import org.openide.text.CloneableEditorSupport;
056: import org.openide.text.PositionRef;
057:
058: /**
059: *
060: * @author Petr Pisl
061: */
062: public class ModificationsTest extends NbTestCase {
063:
064: public ModificationsTest(String testName) {
065: super (testName);
066: }
067:
068: @Override
069: protected void setUp() throws Exception {
070: super .setUp();
071: MockServices.setServices(MockXMLFileEncodingQuery.class);
072: }
073:
074: @Override
075: protected void tearDown() throws Exception {
076: super .tearDown();
077: }
078:
079: public void testInputStream() throws Exception {
080: File file = new File(getDataDir(), "faces-config1.xml");
081: FileObject fileObject = FileUtil.toFileObject(file);
082: CloneableEditorSupport editor = JSFEditorUtilities
083: .findCloneableEditorSupport(DataObject.find(fileObject));
084: Modifications modifications = new Modifications();
085: Modifications.Difference difference = new Modifications.Difference(
086: Modifications.Difference.Kind.CHANGE, editor
087: .createPositionRef(549, Bias.Forward), editor
088: .createPositionRef(549 + 21, Bias.Backward),
089: "org.ncl.backing.Login", "org.ncl.forward.Login", "");
090: File outFile = new File(getWorkDir(), "faces-modification.xml");
091: FileWriter fWriter = new FileWriter(outFile);
092: modifications.addDifference(fileObject, difference);
093: List<Difference> differences = new LinkedList();
094: differences.add(difference);
095: modifications.commit(fileObject, differences, fWriter);
096: fWriter.close();
097: assertFile(outFile, getGoldenFile("gold-modifications.xml"));
098:
099: }
100:
101: public static class MockXMLFileEncodingQuery extends
102: FileEncodingQueryImplementation {
103:
104: @Override
105: public Charset getEncoding(FileObject file) {
106: return XmlFileEncodingQueryImpl.singleton().getEncoding(
107: file);
108: }
109:
110: }
111: }
|