Lines 10-22
Link Here
|
10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.compare.tests; |
11 |
package org.eclipse.compare.tests; |
12 |
|
12 |
|
13 |
import java.io.BufferedInputStream; |
|
|
14 |
import java.io.BufferedReader; |
13 |
import java.io.BufferedReader; |
15 |
import java.io.ByteArrayInputStream; |
14 |
import java.io.ByteArrayInputStream; |
16 |
import java.io.File; |
15 |
import java.io.File; |
17 |
import java.io.FileFilter; |
16 |
import java.io.FileFilter; |
18 |
import java.io.FileInputStream; |
17 |
import java.io.FileInputStream; |
19 |
import java.io.FileNotFoundException; |
|
|
20 |
import java.io.IOException; |
18 |
import java.io.IOException; |
21 |
import java.io.InputStream; |
19 |
import java.io.InputStream; |
22 |
import java.io.InputStreamReader; |
20 |
import java.io.InputStreamReader; |
Lines 38-44
Link Here
|
38 |
import junit.framework.AssertionFailedError; |
36 |
import junit.framework.AssertionFailedError; |
39 |
import junit.framework.TestCase; |
37 |
import junit.framework.TestCase; |
40 |
|
38 |
|
41 |
import org.eclipse.compare.internal.Utilities; |
|
|
42 |
import org.eclipse.compare.internal.core.patch.FileDiff; |
39 |
import org.eclipse.compare.internal.core.patch.FileDiff; |
43 |
import org.eclipse.compare.internal.core.patch.FileDiffResult; |
40 |
import org.eclipse.compare.internal.core.patch.FileDiffResult; |
44 |
import org.eclipse.compare.internal.core.patch.LineReader; |
41 |
import org.eclipse.compare.internal.core.patch.LineReader; |
Lines 49-56
Link Here
|
49 |
import org.eclipse.compare.patch.IHunk; |
46 |
import org.eclipse.compare.patch.IHunk; |
50 |
import org.eclipse.compare.patch.IHunkFilter; |
47 |
import org.eclipse.compare.patch.IHunkFilter; |
51 |
import org.eclipse.compare.patch.PatchConfiguration; |
48 |
import org.eclipse.compare.patch.PatchConfiguration; |
|
|
49 |
import org.eclipse.compare.tests.PatchUtils.JarEntryStorage; |
50 |
import org.eclipse.compare.tests.PatchUtils.PatchTestConfiguration; |
51 |
import org.eclipse.compare.tests.PatchUtils.StringStorage; |
52 |
import org.eclipse.core.resources.IStorage; |
52 |
import org.eclipse.core.resources.IStorage; |
53 |
import org.eclipse.core.resources.ResourcesPlugin; |
|
|
54 |
import org.eclipse.core.runtime.CoreException; |
53 |
import org.eclipse.core.runtime.CoreException; |
55 |
import org.eclipse.core.runtime.FileLocator; |
54 |
import org.eclipse.core.runtime.FileLocator; |
56 |
import org.eclipse.core.runtime.IPath; |
55 |
import org.eclipse.core.runtime.IPath; |
Lines 59-161
Link Here
|
59 |
import org.eclipse.core.runtime.Path; |
58 |
import org.eclipse.core.runtime.Path; |
60 |
import org.eclipse.core.runtime.Platform; |
59 |
import org.eclipse.core.runtime.Platform; |
61 |
import org.eclipse.core.runtime.Status; |
60 |
import org.eclipse.core.runtime.Status; |
62 |
import org.osgi.framework.Bundle; |
|
|
63 |
|
61 |
|
64 |
public class PatchTest extends TestCase { |
62 |
public class PatchTest extends TestCase { |
65 |
|
63 |
|
66 |
private static final String PATCHDATA = "patchdata"; |
|
|
67 |
private static final String PATCH_CONFIGURATION = "patchConfiguration.properties"; |
64 |
private static final String PATCH_CONFIGURATION = "patchConfiguration.properties"; |
68 |
|
65 |
|
69 |
Properties defaultPatchProperties; |
66 |
Properties defaultPatchProperties; |
70 |
|
67 |
|
71 |
class StringStorage implements IStorage { |
|
|
72 |
String fileName; |
73 |
public StringStorage(String old) { |
74 |
fileName = old; |
75 |
} |
76 |
public Object getAdapter(Class adapter) { |
77 |
return null; |
78 |
} |
79 |
public boolean isReadOnly() { |
80 |
return false; |
81 |
} |
82 |
public String getName() { |
83 |
return fileName; |
84 |
} |
85 |
public IPath getFullPath() { |
86 |
return null; |
87 |
} |
88 |
public InputStream getContents() throws CoreException { |
89 |
return new BufferedInputStream(asInputStream(fileName)); |
90 |
} |
91 |
} |
92 |
|
93 |
class FileStorage implements IStorage { |
94 |
File file; |
95 |
public FileStorage(File file) { |
96 |
this.file = file; |
97 |
} |
98 |
public InputStream getContents() throws CoreException { |
99 |
try { |
100 |
return new FileInputStream(file); |
101 |
} catch (FileNotFoundException e) { |
102 |
// ignore, should never happen |
103 |
} |
104 |
return null; |
105 |
} |
106 |
public IPath getFullPath() { |
107 |
return new Path(file.getAbsolutePath()); |
108 |
} |
109 |
public String getName() { |
110 |
return file.getName(); |
111 |
} |
112 |
public boolean isReadOnly() { |
113 |
return true; |
114 |
} |
115 |
public Object getAdapter(Class adapter) { |
116 |
return null; |
117 |
} |
118 |
} |
119 |
|
120 |
class JarEntryStorage implements IStorage { |
121 |
JarEntry jarEntry; |
122 |
JarFile jarFile; |
123 |
public JarEntryStorage(JarEntry jarEntry, JarFile jarFile) { |
124 |
this.jarEntry = jarEntry; |
125 |
this.jarFile = jarFile; |
126 |
} |
127 |
public InputStream getContents() throws CoreException { |
128 |
try { |
129 |
return jarFile.getInputStream(jarEntry); |
130 |
} catch (IOException e) { |
131 |
// ignore, should never happen |
132 |
} |
133 |
return null; |
134 |
} |
135 |
public IPath getFullPath() { |
136 |
return new Path(jarFile.getName()); |
137 |
} |
138 |
public String getName() { |
139 |
return jarEntry.getName(); |
140 |
} |
141 |
public boolean isReadOnly() { |
142 |
return true; |
143 |
} |
144 |
public Object getAdapter(Class adapter) { |
145 |
return null; |
146 |
} |
147 |
} |
148 |
|
149 |
class PatchTestConfiguration { |
150 |
String subfolderName; |
151 |
PatchConfiguration pc; |
152 |
String patchFileName; |
153 |
String[] originalFileNames; |
154 |
String[] expectedFileNames; |
155 |
String[] actualFileNames; |
156 |
// TODO: getters, setters |
157 |
} |
158 |
|
159 |
public PatchTest(String name) { |
68 |
public PatchTest(String name) { |
160 |
super(name); |
69 |
super(name); |
161 |
defaultPatchProperties = new Properties(); |
70 |
defaultPatchProperties = new Properties(); |
Lines 217-227
Link Here
|
217 |
|
126 |
|
218 |
InputStream actual = result.getPatchedContents(); |
127 |
InputStream actual = result.getPatchedContents(); |
219 |
|
128 |
|
220 |
LineReader lr = new LineReader(getReader("exp_hunkFilter.txt")); |
129 |
LineReader lr = new LineReader(PatchUtils.getReader("exp_hunkFilter.txt")); |
221 |
List inLines = lr.readLines(); |
130 |
List inLines = lr.readLines(); |
222 |
String expected = LineReader.createString(false, inLines); |
131 |
String expected = LineReader.createString(false, inLines); |
223 |
|
132 |
|
224 |
assertEquals(expected, asString(actual)); |
133 |
assertEquals(expected, PatchUtils.asString(actual)); |
225 |
} |
134 |
} |
226 |
|
135 |
|
227 |
public void testContext3PatchWithHeader() throws CoreException, IOException { |
136 |
public void testContext3PatchWithHeader() throws CoreException, IOException { |
Lines 298-304
Link Here
|
298 |
private List failures = new ArrayList(); |
207 |
private List failures = new ArrayList(); |
299 |
|
208 |
|
300 |
public void testPatchdataSubfolders() throws IOException, CoreException { |
209 |
public void testPatchdataSubfolders() throws IOException, CoreException { |
301 |
URL patchdataUrl = new URL(getBundle().getEntry("/"), new Path(PATCHDATA).toString()); |
210 |
URL patchdataUrl = new URL(PatchUtils.getBundle().getEntry("/"), |
|
|
211 |
new Path(PatchUtils.PATCHDATA).toString()); |
302 |
patchdataUrl = FileLocator.resolve(patchdataUrl); |
212 |
patchdataUrl = FileLocator.resolve(patchdataUrl); |
303 |
|
213 |
|
304 |
Map map = null; |
214 |
Map map = null; |
Lines 321-327
Link Here
|
321 |
PatchConfiguration pc = ptc.pc; |
231 |
PatchConfiguration pc = ptc.pc; |
322 |
|
232 |
|
323 |
// create a message to distinguish tests from different subfolders |
233 |
// create a message to distinguish tests from different subfolders |
324 |
String msg = "Test for subfolder [" + PATCHDATA + "/" + sf + "] failed."; |
234 |
String msg = "Test for subfolder [" + PatchUtils.PATCHDATA + "/" |
|
|
235 |
+ sf + "] failed."; |
325 |
|
236 |
|
326 |
try { |
237 |
try { |
327 |
// test with expected result |
238 |
// test with expected result |
Lines 339-346
Link Here
|
339 |
continue; // continue with a next subfolder |
250 |
continue; // continue with a next subfolder |
340 |
} |
251 |
} |
341 |
failures.add(new AssertionFailedError( |
252 |
failures.add(new AssertionFailedError( |
342 |
"\npatchWorkspace should fail for folder [" + PATCHDATA |
253 |
"\npatchWorkspace should fail for folder [" |
343 |
+ "/" + sf + "].")); |
254 |
+ PatchUtils.PATCHDATA + "/" + sf + "].")); |
344 |
} |
255 |
} |
345 |
} |
256 |
} |
346 |
|
257 |
|
Lines 391-397
Link Here
|
391 |
while (entries.hasMoreElements()) { |
302 |
while (entries.hasMoreElements()) { |
392 |
JarEntry entry = (JarEntry) entries.nextElement(); |
303 |
JarEntry entry = (JarEntry) entries.nextElement(); |
393 |
String entryName = entry.getName(); |
304 |
String entryName = entry.getName(); |
394 |
if (entryName.endsWith("/" + PATCHDATA + "/")) { |
305 |
if (entryName.endsWith("/" + PatchUtils.PATCHDATA + "/")) { |
395 |
patchdataName = entryName; |
306 |
patchdataName = entryName; |
396 |
break; |
307 |
break; |
397 |
} |
308 |
} |
Lines 512-543
Link Here
|
512 |
result.put(subfolderName, tpc); |
423 |
result.put(subfolderName, tpc); |
513 |
} |
424 |
} |
514 |
|
425 |
|
515 |
|
|
|
516 |
// Test changing |
517 |
private BufferedReader getReader(String name) { |
518 |
InputStream resourceAsStream = asInputStream(name); |
519 |
InputStreamReader reader2= new InputStreamReader(resourceAsStream); |
520 |
return new BufferedReader(reader2); |
521 |
} |
522 |
|
523 |
private InputStream asInputStream(String name) { |
524 |
IPath path= new Path(PATCHDATA).append(name); |
525 |
try { |
526 |
URL url= new URL(getBundle().getEntry("/"), path.toString()); |
527 |
return url.openStream(); |
528 |
} catch (IOException e) { |
529 |
fail("Failed while reading " + name); |
530 |
return null; // never reached |
531 |
} |
532 |
} |
533 |
|
534 |
private void patch(final String old, String patch, String expt) throws CoreException, IOException { |
426 |
private void patch(final String old, String patch, String expt) throws CoreException, IOException { |
535 |
patcherPatch(old, patch, expt); |
427 |
patcherPatch(old, patch, expt); |
536 |
filePatch(old, patch, expt); |
428 |
filePatch(old, patch, expt); |
537 |
} |
429 |
} |
538 |
|
430 |
|
539 |
private void filePatch(final String old, String patch, String expt) throws CoreException, IOException { |
431 |
private void filePatch(final String old, String patch, String expt) throws CoreException, IOException { |
540 |
LineReader lr= new LineReader(getReader(expt)); |
432 |
LineReader lr= new LineReader(PatchUtils.getReader(expt)); |
541 |
List inLines= lr.readLines(); |
433 |
List inLines= lr.readLines(); |
542 |
String expected = LineReader.createString(false, inLines); |
434 |
String expected = LineReader.createString(false, inLines); |
543 |
|
435 |
|
Lines 549-569
Link Here
|
549 |
assertTrue(result.hasMatches()); |
441 |
assertTrue(result.hasMatches()); |
550 |
assertFalse(result.hasRejects()); |
442 |
assertFalse(result.hasRejects()); |
551 |
InputStream actualStream = result.getPatchedContents(); |
443 |
InputStream actualStream = result.getPatchedContents(); |
552 |
String actual = asString(actualStream); |
444 |
String actual = PatchUtils.asString(actualStream); |
553 |
assertEquals(expected, actual); |
445 |
assertEquals(expected, actual); |
554 |
} |
446 |
} |
555 |
|
447 |
|
556 |
private String asString(InputStream exptStream) throws IOException { |
|
|
557 |
return Utilities.readString(exptStream, ResourcesPlugin.getEncoding()); |
558 |
} |
559 |
|
560 |
private void patcherPatch(String old, String patch, String expt) { |
448 |
private void patcherPatch(String old, String patch, String expt) { |
561 |
LineReader lr= new LineReader(getReader(old)); |
449 |
LineReader lr= new LineReader(PatchUtils.getReader(old)); |
562 |
List inLines= lr.readLines(); |
450 |
List inLines= lr.readLines(); |
563 |
|
451 |
|
564 |
WorkspacePatcher patcher= new WorkspacePatcher(); |
452 |
WorkspacePatcher patcher= new WorkspacePatcher(); |
565 |
try { |
453 |
try { |
566 |
patcher.parse(getReader(patch)); |
454 |
patcher.parse(PatchUtils.getReader(patch)); |
567 |
} catch (IOException e) { |
455 |
} catch (IOException e) { |
568 |
e.printStackTrace(); |
456 |
e.printStackTrace(); |
569 |
} |
457 |
} |
Lines 574-580
Link Here
|
574 |
FileDiffResult diffResult = patcher.getDiffResult(diffs[0]); |
462 |
FileDiffResult diffResult = patcher.getDiffResult(diffs[0]); |
575 |
diffResult.patch(inLines, null); |
463 |
diffResult.patch(inLines, null); |
576 |
|
464 |
|
577 |
LineReader expectedContents= new LineReader(getReader(expt)); |
465 |
LineReader expectedContents= new LineReader(PatchUtils.getReader(expt)); |
578 |
List expectedLines= expectedContents.readLines(); |
466 |
List expectedLines= expectedContents.readLines(); |
579 |
|
467 |
|
580 |
Object[] expected= expectedLines.toArray(); |
468 |
Object[] expected= expectedLines.toArray(); |
Lines 618-624
Link Here
|
618 |
patcher.getConfiguration().setFuzz(patchConfiguration.getFuzz()); |
506 |
patcher.getConfiguration().setFuzz(patchConfiguration.getFuzz()); |
619 |
patcher.getConfiguration().setIgnoreWhitespace(patchConfiguration.isIgnoreWhitespace()); |
507 |
patcher.getConfiguration().setIgnoreWhitespace(patchConfiguration.isIgnoreWhitespace()); |
620 |
patcher.getConfiguration().setPrefixSegmentStripCount(patchConfiguration.getPrefixSegmentStripCount()); |
508 |
patcher.getConfiguration().setPrefixSegmentStripCount(patchConfiguration.getPrefixSegmentStripCount()); |
621 |
patcher.parse(getReader(patch)); |
509 |
patcher.parse(PatchUtils.getReader(patch)); |
622 |
patcher.setReversed(patchConfiguration.isReversed()); |
510 |
patcher.setReversed(patchConfiguration.isReversed()); |
623 |
} catch (IOException e) { |
511 |
} catch (IOException e) { |
624 |
e.printStackTrace(); |
512 |
e.printStackTrace(); |
Lines 630-642
Link Here
|
630 |
//Iterate through all of the original files, apply the diffs that belong to the file and compare |
518 |
//Iterate through all of the original files, apply the diffs that belong to the file and compare |
631 |
//with the corresponding outcome file |
519 |
//with the corresponding outcome file |
632 |
for (int i = 0; i < originalFiles.length; i++) { |
520 |
for (int i = 0; i < originalFiles.length; i++) { |
633 |
LineReader lr= new LineReader(getReader(originalFiles[i])); |
521 |
LineReader lr= new LineReader(PatchUtils.getReader(originalFiles[i])); |
634 |
List inLines= lr.readLines(); |
522 |
List inLines= lr.readLines(); |
635 |
|
523 |
|
636 |
FileDiffResult diffResult = patcher.getDiffResult(diffs[i]); |
524 |
FileDiffResult diffResult = patcher.getDiffResult(diffs[i]); |
637 |
diffResult.patch(inLines, null); |
525 |
diffResult.patch(inLines, null); |
638 |
|
526 |
|
639 |
LineReader expectedContents= new LineReader(getReader(expectedOutcomeFiles[i])); |
527 |
LineReader expectedContents= new LineReader(PatchUtils.getReader(expectedOutcomeFiles[i])); |
640 |
List expectedLines= expectedContents.readLines(); |
528 |
List expectedLines= expectedContents.readLines(); |
641 |
|
529 |
|
642 |
Object[] expected= expectedLines.toArray(); |
530 |
Object[] expected= expectedLines.toArray(); |
Lines 652-658
Link Here
|
652 |
} |
540 |
} |
653 |
} |
541 |
} |
654 |
|
542 |
|
655 |
private Bundle getBundle() { |
|
|
656 |
return CompareTestPlugin.getDefault().getBundle(); |
657 |
} |
658 |
} |
543 |
} |