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.spi.project.support.ant;
043:
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.FileOutputStream;
047: import java.io.InputStream;
048: import java.io.OutputStream;
049: import java.io.StringReader;
050: import java.util.Properties;
051: import org.netbeans.junit.NbTestCase;
052: import org.netbeans.spi.queries.CollocationQueryImplementation;
053:
054: /**
055: * Test functionality of AntBasedTestUtil itself.
056: * @author Jesse Glick
057: */
058: public class AntBasedTestUtilTest extends NbTestCase {
059:
060: static {
061: AntBasedTestUtilTest.class.getClassLoader()
062: .setDefaultAssertionStatus(true);
063: }
064:
065: /**
066: * Create a new test suite.
067: * @param name the suite name
068: */
069: public AntBasedTestUtilTest(String name) {
070: super (name);
071: }
072:
073: /**
074: * Check that reported text diffs are actually accurate.
075: * @throws Exception in case of an unexpected error
076: */
077: public void testCountTextDiffs() throws Exception {
078: String f1 = "one\n" + "two\n" + "three\n" + "four\n" + "five\n"
079: + "six\n" + "seven\n" + "eight\n" + "nine\n" + "ten\n";
080: String f2 = "one\n" +
081: // two deleted
082: // three deleted
083: "four\n" + "four #2\n" + // added
084: "four #3\n" + // added
085: "five\n" + "six six six\n" + // modified
086: "sevvin'!\n" + // modified
087: "eight\n" + "najn\n" + // modified
088: "ten\n" + "ten #2\n"; // added
089: int[] count = AntBasedTestUtil.countTextDiffs(new StringReader(
090: f1), new StringReader(f2));
091: assertEquals("should have three entries", 3, count.length);
092: assertEquals("three lines modified", 3, count[0]);
093: assertEquals("three lines added", 3, count[1]);
094: assertEquals("two lines deleted", 2, count[2]);
095: }
096:
097: public void testTestCollocationQueryImplementation()
098: throws Exception {
099: File root = new File(System.getProperty("java.io.tmpdir"));
100: assertTrue("using absolute root " + root, root.isAbsolute());
101: CollocationQueryImplementation cqi = AntBasedTestUtil
102: .testCollocationQueryImplementation(root);
103: File f1 = new File(root, "f1");
104: File f2 = new File(root, "f2");
105: File d1f1 = new File(new File(root, "d1"), "f1");
106: File d2f1 = new File(new File(root, "d2"), "f1");
107: File s = new File(root, "separate");
108: File s1 = new File(s, "s1");
109: File s2 = new File(s, "s2");
110: File t = new File(root, "transient");
111: File t1 = new File(t, "t1");
112: File t2 = new File(t, "t2");
113: assertTrue("f1 & f2 collocated", cqi.areCollocated(f1, f2));
114: assertTrue("f1 & f2 collocated (reverse)", cqi.areCollocated(
115: f2, f1));
116: assertTrue("d1f1 & d2f1 collocated", cqi.areCollocated(d1f1,
117: d2f1));
118: assertTrue("s1 & s2 collocated", cqi.areCollocated(s1, s2));
119: assertTrue("s & s1 collocated", cqi.areCollocated(s, s1));
120: assertFalse("t1 & t2 not collocated", cqi.areCollocated(t1, t2));
121: assertFalse("f1 & t1 not collocated", cqi.areCollocated(f1, t1));
122: assertFalse("f1 & s1 not collocated", cqi.areCollocated(f1, s1));
123: assertFalse("s1 & t1 not collocated", cqi.areCollocated(s1, t1));
124: assertEquals("right root for f1", root, cqi.findRoot(f1));
125: assertEquals("right root for f2", root, cqi.findRoot(f2));
126: assertEquals("right root for d1f1", root, cqi.findRoot(d1f1));
127: assertEquals("right root for d2f1", root, cqi.findRoot(d2f1));
128: assertEquals("right root for s", s, cqi.findRoot(s));
129: assertEquals("right root for s1", s, cqi.findRoot(s1));
130: assertEquals("right root for s2", s, cqi.findRoot(s2));
131: assertEquals("right root for t", null, cqi.findRoot(t));
132: assertEquals("right root for t1", null, cqi.findRoot(t1));
133: assertEquals("right root for t2", null, cqi.findRoot(t2));
134: }
135:
136: public void testReplaceInFile() throws Exception {
137: clearWorkDir();
138: File workdir = getWorkDir();
139: File props = new File(workdir, "test.properties");
140: Properties p = new Properties();
141: p.setProperty("key1", "val1");
142: p.setProperty("key2", "val2");
143: OutputStream os = new FileOutputStream(props);
144: try {
145: p.store(os, null);
146: } finally {
147: os.close();
148: }
149: assertEquals("two replacements", 2, AntBasedTestUtil
150: .replaceInFile(props, "val", "value"));
151: p.clear();
152: InputStream is = new FileInputStream(props);
153: try {
154: p.load(is);
155: } finally {
156: is.close();
157: }
158: assertEquals("correct key1", "value1", p.getProperty("key1"));
159: assertEquals("correct key2", "value2", p.getProperty("key2"));
160: }
161:
162: }
|