Lines 10-16
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; |
Lines 36-54
Link Here
|
36 |
|
35 |
|
37 |
import junit.framework.Assert; |
36 |
import junit.framework.Assert; |
38 |
import junit.framework.AssertionFailedError; |
37 |
import junit.framework.AssertionFailedError; |
39 |
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; |
45 |
import org.eclipse.compare.internal.patch.WorkspacePatcher; |
42 |
import org.eclipse.compare.internal.patch.WorkspacePatcher; |
46 |
import org.eclipse.compare.patch.ApplyPatchOperation; |
43 |
import org.eclipse.compare.patch.ApplyPatchOperation; |
47 |
import org.eclipse.compare.patch.IFilePatch; |
44 |
import org.eclipse.compare.patch.IFilePatch; |
48 |
import org.eclipse.compare.patch.IFilePatchResult; |
|
|
49 |
import org.eclipse.compare.patch.PatchConfiguration; |
45 |
import org.eclipse.compare.patch.PatchConfiguration; |
50 |
import org.eclipse.core.resources.IStorage; |
46 |
import org.eclipse.core.resources.IStorage; |
51 |
import org.eclipse.core.resources.ResourcesPlugin; |
|
|
52 |
import org.eclipse.core.runtime.CoreException; |
47 |
import org.eclipse.core.runtime.CoreException; |
53 |
import org.eclipse.core.runtime.FileLocator; |
48 |
import org.eclipse.core.runtime.FileLocator; |
54 |
import org.eclipse.core.runtime.IPath; |
49 |
import org.eclipse.core.runtime.IPath; |
Lines 56-92
Link Here
|
56 |
import org.eclipse.core.runtime.Path; |
51 |
import org.eclipse.core.runtime.Path; |
57 |
import org.eclipse.core.runtime.Platform; |
52 |
import org.eclipse.core.runtime.Platform; |
58 |
import org.eclipse.core.runtime.Status; |
53 |
import org.eclipse.core.runtime.Status; |
59 |
import org.osgi.framework.Bundle; |
|
|
60 |
|
61 |
public class PatchTest extends TestCase { |
62 |
|
54 |
|
|
|
55 |
public class PatchTest extends AbstractPatchTest { |
56 |
|
63 |
private static final String PATCHDATA = "patchdata"; |
57 |
private static final String PATCHDATA = "patchdata"; |
64 |
private static final String PATCH_CONFIGURATION = "patchConfiguration.properties"; |
58 |
private static final String PATCH_CONFIGURATION = "patchConfiguration.properties"; |
65 |
|
59 |
|
66 |
Properties defaultPatchProperties; |
60 |
Properties defaultPatchProperties; |
67 |
|
61 |
|
68 |
class StringStorage implements IStorage { |
|
|
69 |
String fileName; |
70 |
public StringStorage(String old) { |
71 |
fileName = old; |
72 |
} |
73 |
public Object getAdapter(Class adapter) { |
74 |
return null; |
75 |
} |
76 |
public boolean isReadOnly() { |
77 |
return false; |
78 |
} |
79 |
public String getName() { |
80 |
return fileName; |
81 |
} |
82 |
public IPath getFullPath() { |
83 |
return null; |
84 |
} |
85 |
public InputStream getContents() throws CoreException { |
86 |
return new BufferedInputStream(asInputStream(fileName)); |
87 |
} |
88 |
} |
89 |
|
90 |
class FileStorage implements IStorage { |
62 |
class FileStorage implements IStorage { |
91 |
File file; |
63 |
File file; |
92 |
public FileStorage(File file) { |
64 |
public FileStorage(File file) { |
Lines 161-167
Link Here
|
161 |
defaultPatchProperties.setProperty("expectedResultFile", "exp_context.txt"); |
133 |
defaultPatchProperties.setProperty("expectedResultFile", "exp_context.txt"); |
162 |
defaultPatchProperties.setProperty("fuzzFactor", "-1"); |
134 |
defaultPatchProperties.setProperty("fuzzFactor", "-1"); |
163 |
} |
135 |
} |
164 |
|
136 |
|
|
|
137 |
protected String getWorkingFolder() { |
138 |
return PATCHDATA; |
139 |
} |
140 |
|
165 |
protected void setUp() throws Exception { |
141 |
protected void setUp() throws Exception { |
166 |
// empty |
142 |
// empty |
167 |
} |
143 |
} |
Lines 479-557
Link Here
|
479 |
} |
455 |
} |
480 |
|
456 |
|
481 |
|
457 |
|
482 |
// Test changing |
|
|
483 |
private BufferedReader getReader(String name) { |
484 |
InputStream resourceAsStream = asInputStream(name); |
485 |
InputStreamReader reader2= new InputStreamReader(resourceAsStream); |
486 |
return new BufferedReader(reader2); |
487 |
} |
488 |
|
489 |
private InputStream asInputStream(String name) { |
490 |
IPath path= new Path(PATCHDATA).append(name); |
491 |
try { |
492 |
URL url= new URL(getBundle().getEntry("/"), path.toString()); |
493 |
return url.openStream(); |
494 |
} catch (IOException e) { |
495 |
fail("Failed while reading " + name); |
496 |
return null; // never reached |
497 |
} |
498 |
} |
499 |
|
500 |
private void patch(final String old, String patch, String expt) throws CoreException, IOException { |
501 |
patcherPatch(old, patch, expt); |
502 |
filePatch(old, patch, expt); |
503 |
} |
504 |
|
505 |
private void filePatch(final String old, String patch, String expt) throws CoreException, IOException { |
506 |
LineReader lr= new LineReader(getReader(expt)); |
507 |
List inLines= lr.readLines(); |
508 |
String expected = LineReader.createString(false, inLines); |
509 |
|
510 |
IStorage oldStorage = new StringStorage(old); |
511 |
IStorage patchStorage = new StringStorage(patch); |
512 |
IFilePatch[] patches = ApplyPatchOperation.parsePatch(patchStorage); |
513 |
assertTrue(patches.length == 1); |
514 |
IFilePatchResult result = patches[0].apply(oldStorage, new PatchConfiguration(), null); |
515 |
assertTrue(result.hasMatches()); |
516 |
assertFalse(result.hasRejects()); |
517 |
InputStream actualStream = result.getPatchedContents(); |
518 |
String actual = asString(actualStream); |
519 |
assertEquals(expected, actual); |
520 |
} |
521 |
|
522 |
private String asString(InputStream exptStream) throws IOException { |
523 |
return Utilities.readString(exptStream, ResourcesPlugin.getEncoding()); |
524 |
} |
525 |
|
526 |
private void patcherPatch(String old, String patch, String expt) { |
527 |
LineReader lr= new LineReader(getReader(old)); |
528 |
List inLines= lr.readLines(); |
529 |
|
530 |
WorkspacePatcher patcher= new WorkspacePatcher(); |
531 |
try { |
532 |
patcher.parse(getReader(patch)); |
533 |
} catch (IOException e) { |
534 |
e.printStackTrace(); |
535 |
} |
536 |
|
537 |
FileDiff[] diffs= patcher.getDiffs(); |
538 |
Assert.assertEquals(diffs.length, 1); |
539 |
|
540 |
FileDiffResult diffResult = patcher.getDiffResult(diffs[0]); |
541 |
diffResult.patch(inLines, null); |
542 |
|
543 |
LineReader expectedContents= new LineReader(getReader(expt)); |
544 |
List expectedLines= expectedContents.readLines(); |
545 |
|
546 |
Object[] expected= expectedLines.toArray(); |
547 |
Object[] result= inLines.toArray(); |
548 |
|
549 |
Assert.assertEquals(expected.length, result.length); |
550 |
|
551 |
for (int i= 0; i < expected.length; i++) |
552 |
Assert.assertEquals(expected[i], result[i]); |
553 |
} |
554 |
|
555 |
private void patchWorkspace(String[] originalFiles, String patch, |
458 |
private void patchWorkspace(String[] originalFiles, String patch, |
556 |
String[] expectedOutcomeFiles, boolean reverse, |
459 |
String[] expectedOutcomeFiles, boolean reverse, |
557 |
int fuzzFactor) { |
460 |
int fuzzFactor) { |
Lines 616-624
Link Here
|
616 |
for (int j= 0; j < expected.length; j++) |
519 |
for (int j= 0; j < expected.length; j++) |
617 |
Assert.assertEquals(msg, expected[j], result[j]); |
520 |
Assert.assertEquals(msg, expected[j], result[j]); |
618 |
} |
521 |
} |
619 |
} |
|
|
620 |
|
621 |
private Bundle getBundle() { |
622 |
return CompareTestPlugin.getDefault().getBundle(); |
623 |
} |
522 |
} |
624 |
} |
523 |
} |