1 /******************************************************************************* 2 * Copyright (c) 2008 Symbian Software Systems, IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * Andrew Ferguson (Symbian) - Initial implementation - [api] enable document setup participants to customize behaviour based on resource being opened - https://bugs.eclipse.org/bugs/show_bug.cgi?id=208881 13 *******************************************************************************/ 14 package org.eclipse.core.filebuffers.tests; 15 16 import static org.junit.Assert.assertEquals; 17 18 import java.util.Arrays; 19 import java.util.HashSet; 20 import java.util.Set; 21 22 import org.junit.Test; 23 24 import org.eclipse.core.runtime.Path; 25 26 import org.eclipse.core.filebuffers.IDocumentSetupParticipantExtension; 27 import org.eclipse.core.filebuffers.ITextFileBufferManager; 28 import org.eclipse.core.filebuffers.LocationKind; 29 import org.eclipse.core.filebuffers.tests.MockDocumentSetupParticipants.TestDSP1; 30 import org.eclipse.core.filebuffers.tests.MockDocumentSetupParticipants.TestDSP2; 31 import org.eclipse.core.filebuffers.tests.MockDocumentSetupParticipants.TestDSP3; 32 import org.eclipse.core.filebuffers.tests.MockDocumentSetupParticipants.TestDSP4; 33 import org.eclipse.core.filebuffers.tests.MockDocumentSetupParticipants.TestDSP5; 34 import org.eclipse.core.filebuffers.tests.MockDocumentSetupParticipants.TestDSP6; 35 36 import org.eclipse.jface.text.IDocument; 37 38 39 /** 40 * @since 3.4 41 */ 42 public abstract class AbstractFileBufferDocCreationTests { 43 protected ITextFileBufferManager fManager; 44 45 @Test testCreateDocumentPartipants_FileExt()46 public void testCreateDocumentPartipants_FileExt() { 47 assertParticipantsInvoked("anything.111foo", new Class[] {TestDSP1.class, TestDSP3.class}); 48 } 49 50 @Test testCreateDocumentPartipants_Name()51 public void testCreateDocumentPartipants_Name() { 52 assertParticipantsInvoked("111fooname", new Class[] {TestDSP2.class, TestDSP3.class}); 53 } 54 55 @Test testCreateDocumentPartipants_FileExt_Name()56 public void testCreateDocumentPartipants_FileExt_Name() { 57 assertParticipantsInvoked("111fooname.111foo", new Class[] {TestDSP1.class, TestDSP2.class, TestDSP3.class}); 58 } 59 60 @Test testCreateDocumentPartipants_FileExt_Extension()61 public void testCreateDocumentPartipants_FileExt_Extension() { 62 assertParticipantsInvoked("anything.222foo", new Class[] {TestDSP4.class, TestDSP6.class}); 63 } 64 65 @Test testCreateDocumentPartipants_Name_Extension()66 public void testCreateDocumentPartipants_Name_Extension() { 67 assertParticipantsInvoked("222fooname", new Class[] {TestDSP5.class, TestDSP6.class}); 68 } 69 70 @Test testCreateDocumentPartipants_FileExt_Name_Extension()71 public void testCreateDocumentPartipants_FileExt_Name_Extension() { 72 assertParticipantsInvoked("222fooname.222foo", new Class[] {TestDSP4.class, TestDSP5.class, TestDSP6.class}); 73 } 74 75 @Test testDocumentSetupParticipantExtension_1()76 public void testDocumentSetupParticipantExtension_1() { 77 assertDocumentContent("emanoof333/p/", "/p/333fooname", LocationKind.IFILE); 78 assertDocumentContent("oof333.emanoof333/p/", "/p/333fooname.333foo", LocationKind.IFILE); 79 assertDocumentContent("oof333.gnihtyna/p/", "/p/anything.333foo", LocationKind.IFILE); 80 81 assertDocumentContent("", "333fooname", LocationKind.LOCATION); 82 assertDocumentContent("", "333fooname.333foo", LocationKind.LOCATION); 83 assertDocumentContent("", "anything.333foo", LocationKind.LOCATION); 84 85 assertDocumentContent("", "333fooname", LocationKind.NORMALIZE); 86 assertDocumentContent("", "333fooname.333foo", LocationKind.NORMALIZE); 87 assertDocumentContent("", "anything.333foo", LocationKind.NORMALIZE); 88 } 89 90 @Test testDocumentSetupParticipantExtension_2()91 public void testDocumentSetupParticipantExtension_2() { 92 assertDocumentContent("", "/p/444fooname", LocationKind.IFILE); 93 assertDocumentContent("", "/p/444fooname.444foo", LocationKind.IFILE); 94 assertDocumentContent("", "/p/anything.444foo", LocationKind.IFILE); 95 96 assertDocumentContent("emanoof444", "444fooname", LocationKind.LOCATION); 97 assertDocumentContent("oof444.emanoof444", "444fooname.444foo", LocationKind.LOCATION); 98 assertDocumentContent("oof444.gnihtyna", "anything.444foo", LocationKind.LOCATION); 99 100 assertDocumentContent("", "444fooname", LocationKind.NORMALIZE); 101 assertDocumentContent("", "444fooname.444foo", LocationKind.NORMALIZE); 102 assertDocumentContent("", "anything.444foo", LocationKind.NORMALIZE); 103 } 104 105 @Test testDocumentSetupParticipantExtension_3()106 public void testDocumentSetupParticipantExtension_3() { 107 assertDocumentContent("", "/p/555fooname", LocationKind.IFILE); 108 assertDocumentContent("", "/p/555fooname.555foo", LocationKind.IFILE); 109 assertDocumentContent("", "/p/anything.555foo", LocationKind.IFILE); 110 111 assertDocumentContent("", "555fooname", LocationKind.LOCATION); 112 assertDocumentContent("", "555fooname.555foo", LocationKind.LOCATION); 113 assertDocumentContent("", "anything.555foo", LocationKind.LOCATION); 114 115 assertDocumentContent("emanoof555", "555fooname", LocationKind.NORMALIZE); 116 assertDocumentContent("oof555.emanoof555", "555fooname.555foo", LocationKind.NORMALIZE); 117 assertDocumentContent("oof555.gnihtyna", "anything.555foo", LocationKind.NORMALIZE); 118 } 119 120 /* Utilities */ 121 assertParticipantsInvoked(String path, Class<?>[] expectedDSPsArray)122 private void assertParticipantsInvoked(String path, Class<?>[] expectedDSPsArray) { 123 LocationKind[] lks= getSupportLocationKinds(); 124 for (LocationKind lk : lks) { 125 IDocument document = fManager.createEmptyDocument(new Path(path), lk); 126 String content= document.get(); 127 Set<String> expectedDSPs= new HashSet<>(Arrays.asList(toString(expectedDSPsArray))); 128 Set<String> actualDSPs= new HashSet<>(Arrays.asList(content.split("\n"))); 129 assertEquals(expectedDSPs, actualDSPs); 130 } 131 } 132 getSupportLocationKinds()133 abstract protected LocationKind[] getSupportLocationKinds(); 134 assertDocumentContent(String expectedContent, String path, LocationKind locKind)135 protected void assertDocumentContent(String expectedContent, String path, LocationKind locKind) { 136 assertEquals(expectedContent, fManager.createEmptyDocument(new Path(path), locKind).get()); 137 } 138 toString(Class<?>[] clss)139 private static String[] toString(Class<?>[] clss) { 140 String[] result= new String[clss.length]; 141 for(int i=0; i<result.length; i++) { 142 String s= null; 143 if(clss[i]!=null) { 144 s= clss[i].toString(); 145 if(IDocumentSetupParticipantExtension.class.isAssignableFrom(clss[i])) { 146 s+= "%%EXTENSION"; 147 } 148 } 149 result[i]= s; 150 } 151 return result; 152 } 153 } 154