KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > spi > project > support > rake > RakeBasedTestUtilTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.ruby.spi.project.support.rake;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.io.StringReader JavaDoc;
28 import java.util.Properties JavaDoc;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.spi.queries.CollocationQueryImplementation;
31
32 /**
33  * Test functionality of RakeBasedTestUtil itself.
34  * @author Jesse Glick
35  */

36 public class RakeBasedTestUtilTest extends NbTestCase {
37
38     static {
39         RakeBasedTestUtilTest.class.getClassLoader().setDefaultAssertionStatus(true);
40     }
41
42     /**
43      * Create a new test suite.
44      * @param name the suite name
45      */

46     public RakeBasedTestUtilTest(String JavaDoc name) {
47         super(name);
48     }
49
50     /**
51      * Check that reported text diffs are actually accurate.
52      * @throws Exception in case of an unexpected error
53      */

54     public void testCountTextDiffs() throws Exception JavaDoc {
55         String JavaDoc f1 =
56             "one\n" +
57             "two\n" +
58             "three\n" +
59             "four\n" +
60             "five\n" +
61             "six\n" +
62             "seven\n" +
63             "eight\n" +
64             "nine\n" +
65             "ten\n";
66         String JavaDoc f2 =
67             "one\n" +
68             // two deleted
69
// three deleted
70
"four\n" +
71             "four #2\n" + // added
72
"four #3\n" + // added
73
"five\n" +
74             "six six six\n" + // modified
75
"sevvin'!\n" + // modified
76
"eight\n" +
77             "najn\n" + // modified
78
"ten\n" +
79             "ten #2\n"; // added
80
int[] count = RakeBasedTestUtil.countTextDiffs(new StringReader JavaDoc(f1), new StringReader JavaDoc(f2));
81         assertEquals("should have three entries", 3, count.length);
82         assertEquals("three lines modified", 3, count[0]);
83         assertEquals("three lines added", 3, count[1]);
84         assertEquals("two lines deleted", 2, count[2]);
85     }
86     
87     public void testTestCollocationQueryImplementation() throws Exception JavaDoc {
88         File JavaDoc root = new File JavaDoc(System.getProperty("java.io.tmpdir"));
89         assertTrue("using absolute root " + root, root.isAbsolute());
90         CollocationQueryImplementation cqi = RakeBasedTestUtil.testCollocationQueryImplementation(root);
91         File JavaDoc f1 = new File JavaDoc(root, "f1");
92         File JavaDoc f2 = new File JavaDoc(root, "f2");
93         File JavaDoc d1f1 = new File JavaDoc(new File JavaDoc(root, "d1"), "f1");
94         File JavaDoc d2f1 = new File JavaDoc(new File JavaDoc(root, "d2"), "f1");
95         File JavaDoc s = new File JavaDoc(root, "separate");
96         File JavaDoc s1 = new File JavaDoc(s, "s1");
97         File JavaDoc s2 = new File JavaDoc(s, "s2");
98         File JavaDoc t = new File JavaDoc(root, "transient");
99         File JavaDoc t1 = new File JavaDoc(t, "t1");
100         File JavaDoc t2 = new File JavaDoc(t, "t2");
101         assertTrue("f1 & f2 collocated", cqi.areCollocated(f1, f2));
102         assertTrue("f1 & f2 collocated (reverse)", cqi.areCollocated(f2, f1));
103         assertTrue("d1f1 & d2f1 collocated", cqi.areCollocated(d1f1, d2f1));
104         assertTrue("s1 & s2 collocated", cqi.areCollocated(s1, s2));
105         assertTrue("s & s1 collocated", cqi.areCollocated(s, s1));
106         assertFalse("t1 & t2 not collocated", cqi.areCollocated(t1, t2));
107         assertFalse("f1 & t1 not collocated", cqi.areCollocated(f1, t1));
108         assertFalse("f1 & s1 not collocated", cqi.areCollocated(f1, s1));
109         assertFalse("s1 & t1 not collocated", cqi.areCollocated(s1, t1));
110         assertEquals("right root for f1", root, cqi.findRoot(f1));
111         assertEquals("right root for f2", root, cqi.findRoot(f2));
112         assertEquals("right root for d1f1", root, cqi.findRoot(d1f1));
113         assertEquals("right root for d2f1", root, cqi.findRoot(d2f1));
114         assertEquals("right root for s", s, cqi.findRoot(s));
115         assertEquals("right root for s1", s, cqi.findRoot(s1));
116         assertEquals("right root for s2", s, cqi.findRoot(s2));
117         assertEquals("right root for t", null, cqi.findRoot(t));
118         assertEquals("right root for t1", null, cqi.findRoot(t1));
119         assertEquals("right root for t2", null, cqi.findRoot(t2));
120     }
121     
122     public void testReplaceInFile() throws Exception JavaDoc {
123         clearWorkDir();
124         File JavaDoc workdir = getWorkDir();
125         File JavaDoc props = new File JavaDoc(workdir, "test.properties");
126         Properties JavaDoc p = new Properties JavaDoc();
127         p.setProperty("key1", "val1");
128         p.setProperty("key2", "val2");
129         OutputStream JavaDoc os = new FileOutputStream JavaDoc(props);
130         try {
131             p.store(os, null);
132         } finally {
133             os.close();
134         }
135         assertEquals("two replacements", 2, RakeBasedTestUtil.replaceInFile(props, "val", "value"));
136         p.clear();
137         InputStream JavaDoc is = new FileInputStream JavaDoc(props);
138         try {
139             p.load(is);
140         } finally {
141             is.close();
142         }
143         assertEquals("correct key1", "value1", p.getProperty("key1"));
144         assertEquals("correct key2", "value2", p.getProperty("key2"));
145     }
146     
147 }
148
Popular Tags