1 19 20 package org.netbeans.modules.ruby.spi.project.support.rake; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.io.StringReader ; 28 import java.util.Properties ; 29 import org.netbeans.junit.NbTestCase; 30 import org.netbeans.spi.queries.CollocationQueryImplementation; 31 32 36 public class RakeBasedTestUtilTest extends NbTestCase { 37 38 static { 39 RakeBasedTestUtilTest.class.getClassLoader().setDefaultAssertionStatus(true); 40 } 41 42 46 public RakeBasedTestUtilTest(String name) { 47 super(name); 48 } 49 50 54 public void testCountTextDiffs() throws Exception { 55 String 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 f2 = 67 "one\n" + 68 "four\n" + 71 "four #2\n" + "four #3\n" + "five\n" + 74 "six six six\n" + "sevvin'!\n" + "eight\n" + 77 "najn\n" + "ten\n" + 79 "ten #2\n"; int[] count = RakeBasedTestUtil.countTextDiffs(new StringReader (f1), new StringReader (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 { 88 File root = new File (System.getProperty("java.io.tmpdir")); 89 assertTrue("using absolute root " + root, root.isAbsolute()); 90 CollocationQueryImplementation cqi = RakeBasedTestUtil.testCollocationQueryImplementation(root); 91 File f1 = new File (root, "f1"); 92 File f2 = new File (root, "f2"); 93 File d1f1 = new File (new File (root, "d1"), "f1"); 94 File d2f1 = new File (new File (root, "d2"), "f1"); 95 File s = new File (root, "separate"); 96 File s1 = new File (s, "s1"); 97 File s2 = new File (s, "s2"); 98 File t = new File (root, "transient"); 99 File t1 = new File (t, "t1"); 100 File t2 = new File (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 { 123 clearWorkDir(); 124 File workdir = getWorkDir(); 125 File props = new File (workdir, "test.properties"); 126 Properties p = new Properties (); 127 p.setProperty("key1", "val1"); 128 p.setProperty("key2", "val2"); 129 OutputStream os = new FileOutputStream (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 is = new FileInputStream (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 |