/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 
*
* Copyright 2012 Oracle and/or its affiliates.  All rights reserved. 
*
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
* Other names may be trademarks of their respective owners. 
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License").  You may not use this file except in compliance with the
* License.  You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP.  See the License for the
* specific language governing permissions and limitations under the
* License.  When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the GPL Version 2 section of the License file that
* accompanied this code.  If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license."  If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above. 
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder. 
*
* Contributor(s):
*
* Portions Copyrighted 2012 Sun Microsystems, Inc.
*/
package org.openide.loaders;
import java.io. ByteArrayInputStream;
import java.io. OutputStream;
import javax.swing.text. StyledDocument;
import org.netbeans.junit. NbTestCase;
import org.openide.cookies. EditorCookie;
import org.openide.cookies. SaveCookie;
import org.openide.filesystems. FileSystem;
import org.openide.filesystems. FileUtil;
/**
* Test that editor contains the up-to-date content. 
*/
public class UpToDateEditorContentTest extends NbTestCase {
private String[] fsstruct = new String[] {
"Dir/SubDir/A.txt",
};
private FileSystem lfs;
private DataObject do1;
public UpToDateEditorContentTest(String name) {
super(name);
}
@Override
protected void setUp() throws Exception {
clearWorkDir();
lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), fsstruct);
do1 = DataObject.find(lfs.findResource("/Dir/SubDir/A.txt"));
}
@Override
protected void tearDown() throws Exception {
TestUtilHid.destroyLocalFileSystem(getName());
}
/**
* Steps:
* - have local changes in the file
* - save the file
* - copy another content to the file using streams
* - check content of the file
*/
public void testUpToDateContent() throws Exception {
StyledDocument document = do1.getLookup().lookup(EditorCookie.class).openDocument();
assertFalse("File should not be modified", do1.isModified());
// modify document
document.insertString(0, "old content", null);
assertTrue("File should be modified", do1.isModified());
// save document
do1.getLookup().lookup(SaveCookie.class).save();
// XXX thread sleep does not help in test?! 
//Thread.sleep(2000);
assertFalse("File should not be modified", do1.isModified());
// copy content
final String newContent = "new content";
OutputStream outputStream = do1.getPrimaryFile().getOutputStream();
FileUtil.copy(new ByteArrayInputStream(newContent.getBytes()), outputStream);
outputStream.close();
// check content
String content = document.getText(0, document.getLength());
assertEquals("New content should be present", newContent, content);
}
}