diff --git a/tests/plugins/org.polarsys.capella.test.migration.ju/model/ImagePath InRichTextAttribute2/.project b/tests/plugins/org.polarsys.capella.test.migration.ju/model/ImagePath InRichTextAttribute2/.project new file mode 100644 index 0000000000..156ce58ebe --- /dev/null +++ b/tests/plugins/org.polarsys.capella.test.migration.ju/model/ImagePath InRichTextAttribute2/.project @@ -0,0 +1,12 @@ + + + ImagePath InRichTextAttribute + + + + + + + org.polarsys.capella.project.nature + + diff --git a/tests/plugins/org.polarsys.capella.test.migration.ju/model/ImagePath InRichTextAttribute2/ImagePath InRichTextTextAttribute2.afm b/tests/plugins/org.polarsys.capella.test.migration.ju/model/ImagePath InRichTextAttribute2/ImagePath InRichTextTextAttribute2.afm new file mode 100644 index 0000000000..276cc92dfe --- /dev/null +++ b/tests/plugins/org.polarsys.capella.test.migration.ju/model/ImagePath InRichTextAttribute2/ImagePath InRichTextTextAttribute2.afm @@ -0,0 +1,4 @@ + + + + diff --git a/tests/plugins/org.polarsys.capella.test.migration.ju/model/ImagePath InRichTextAttribute2/ImagePath InRichTextTextAttribute2.aird b/tests/plugins/org.polarsys.capella.test.migration.ju/model/ImagePath InRichTextAttribute2/ImagePath InRichTextTextAttribute2.aird new file mode 100644 index 0000000000..a49b044ee4 --- /dev/null +++ b/tests/plugins/org.polarsys.capella.test.migration.ju/model/ImagePath InRichTextAttribute2/ImagePath InRichTextTextAttribute2.aird @@ -0,0 +1,49 @@ + + + + ImagePath%20InRichTextTextAttribute2.afm + ImagePath%20InRichTextTextAttribute2.capella + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/plugins/org.polarsys.capella.test.migration.ju/model/ImagePath InRichTextAttribute2/ImagePath InRichTextTextAttribute2.capella b/tests/plugins/org.polarsys.capella.test.migration.ju/model/ImagePath InRichTextAttribute2/ImagePath InRichTextTextAttribute2.capella new file mode 100644 index 0000000000..0c471a349c --- /dev/null +++ b/tests/plugins/org.polarsys.capella.test.migration.ju/model/ImagePath InRichTextAttribute2/ImagePath InRichTextTextAttribute2.capella @@ -0,0 +1,274 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/plugins/org.polarsys.capella.test.migration.ju/model/ImagePath InRichTextAttribute2/capella.png b/tests/plugins/org.polarsys.capella.test.migration.ju/model/ImagePath InRichTextAttribute2/capella.png new file mode 100644 index 0000000000..de9ad99919 Binary files /dev/null and b/tests/plugins/org.polarsys.capella.test.migration.ju/model/ImagePath InRichTextAttribute2/capella.png differ diff --git a/tests/plugins/org.polarsys.capella.test.migration.ju/src/org/polarsys/capella/test/migration/ju/testcases/basic/ImagePathInRichTextAttributeMigrationTest.java b/tests/plugins/org.polarsys.capella.test.migration.ju/src/org/polarsys/capella/test/migration/ju/testcases/basic/ImagePathInRichTextAttributeMigrationTest.java index e6e12ef11c..b60a0af777 100644 --- a/tests/plugins/org.polarsys.capella.test.migration.ju/src/org/polarsys/capella/test/migration/ju/testcases/basic/ImagePathInRichTextAttributeMigrationTest.java +++ b/tests/plugins/org.polarsys.capella.test.migration.ju/src/org/polarsys/capella/test/migration/ju/testcases/basic/ImagePathInRichTextAttributeMigrationTest.java @@ -29,6 +29,7 @@ import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Platform; +import org.eclipse.emf.common.util.URI; import org.eclipse.emf.transaction.RecordingCommand; import org.eclipse.sirius.business.api.image.ImageManager; import org.eclipse.sirius.business.api.session.Session; @@ -49,26 +50,24 @@ public class ImagePathInRichTextAttributeMigrationTest extends BasicTestCase { private static final String CAPELLA_PNG = "capella.png"; - private static final String SOURCE_MODEL = "ImagePath InRichTextAttribute"; + private static final String SOURCE_MODEL1 = "ImagePath InRichTextAttribute"; + + private static final String SOURCE_MODEL2 = "ImagePath InRichTextAttribute2"; String HTML_IMAGE_PATH_PATTERN_HTTP = ""; //$NON-NLS-1$ - String HTML_IMAGE_PATH_PATTERN_BASE64 = ""; //$NON-NLS-1$ - String HTML_IMAGE_PATH_PATTERN_COPIED = ""; //$NON-NLS-1$ - - String HTML_MIGRATED_NOT_EXISTING_IMAGE_RELATIVE_PATH = SOURCE_MODEL + "/RELATIVE_IMAGE_NOT_FOUND.png"; //$NON-NLS-1$ String HTML_NOT_MIGRATED_NOT_EXISTING_IMAGE_RELATIVE_PATH = "FOLDEROROTHERPROJECT/RELATIVE_IMAGE_NOT_FOUND.png"; //$NON-NLS-1$ - String HTML_MIGRATED_EXISTING_IMAGE_RELATIVE_PATH_PATTERN = ""; - String HTML_NOT_MIGRATED_NOT_EXISTING_IMAGE_RELATIVE_PATH_PATTERN = ""; + + String HTML_NOT_MIGRATED_NOT_EXISTING_IMAGE_RELATIVE_PATH_PATTERN = ""; String HTML_IMAGE_INVALID_ABSOLUTE_PATH = "C:\\INVALID\\PATH\\IMAGE.png"; //$NON-NLS-1$ private static final String OA_ELEMENT_ID = "b302cb2c-9ebb-4a79-a1cb-2cf8e46fe51b"; - private IProject sourceModelProject; + private IProject sourceModelProject1; + + private IProject sourceModelProject2; private ILogListener logListener; @@ -76,14 +75,16 @@ public class ImagePathInRichTextAttributeMigrationTest extends BasicTestCase { @Override public List getRequiredTestModels() { - return Arrays.asList(SOURCE_MODEL); + return Arrays.asList(SOURCE_MODEL1, SOURCE_MODEL2); } @Override protected void setUp() throws Exception { super.setUp(); - sourceModelProject = IResourceHelpers.getEclipseProjectInWorkspace(SOURCE_MODEL); + sourceModelProject1 = IResourceHelpers.getEclipseProjectInWorkspace(SOURCE_MODEL1); + sourceModelProject2 = IResourceHelpers.getEclipseProjectInWorkspace(SOURCE_MODEL2); + sourceModelProject2.getFolder(ImageManager.IMAGE_FOLDER_NAME.toUpperCase()).create(true, true, null); logListener = new ILogListener() { @Override @@ -96,15 +97,25 @@ public void logging(IStatus status, String plugin) { @Override public void test() throws Exception { - if (sourceModelProject.exists()) { - assertTrue("Bad test data: there should be no images folder", - Arrays.asList(sourceModelProject.members()).stream().filter(IFolder.class::isInstance).count() == 0); + migrateProject(sourceModelProject1, ImageManager.IMAGE_FOLDER_NAME); + migrateProject(sourceModelProject2, ImageManager.IMAGE_FOLDER_NAME.toUpperCase()); + } + + private void migrateProject(IProject project, String expectedFolderName) throws CoreException, Exception { + if (project.exists()) { + if (project == sourceModelProject1) { + assertTrue("Bad test data: there should be no images folder", + Arrays.asList(project.members()).stream().filter(IFolder.class::isInstance).count() == 0); + } else if (project == sourceModelProject2) { + assertTrue("Bad test data: there should be an images folder", + Arrays.asList(project.members()).stream().filter(IFolder.class::isInstance).count() == 1); + } // migrate the project - migrateAndCheckFirstTime(); + migrateAndCheckFirstTime(project, expectedFolderName); // change the absolute path so that the absolute path references the existing image dinosaur in the project. - Session session = getSessionForTestModel(SOURCE_MODEL); + Session session = getSessionForTestModel(project.getName()); session.getTransactionalEditingDomain().getCommandStack() .execute(new RecordingCommand(session.getTransactionalEditingDomain()) { @@ -115,7 +126,7 @@ protected void doExecute() { OperationalActivity oa = context.getSemanticElement(OA_ELEMENT_ID); try { newDescription = oa.getDescription().replace("file:/" + HTML_IMAGE_INVALID_ABSOLUTE_PATH, - sourceModelProject.getFile(CAPELLA_PNG).getLocationURI().toURL().toExternalForm()); + project.getFile(CAPELLA_PNG).getLocationURI().toURL().toExternalForm()); } catch (MalformedURLException e) { } oa.setDescription(newDescription); @@ -126,22 +137,21 @@ protected void doExecute() { session.close(new NullProgressMonitor()); // migrate a second time to check the absolute path differently and that the expected did not change - migrateAndCheckSecondTime(); - + migrateAndCheckSecondTime(project, expectedFolderName); } } - private void migrateAndCheckFirstTime() throws Exception { + private void migrateAndCheckFirstTime(IProject project, String expectedFolderName) throws Exception { statuses.clear(); - MigrationHelper.migrateProject(sourceModelProject); + MigrationHelper.migrateProject(project); - Session session = getSessionForTestModel(SOURCE_MODEL); + Session session = getSessionForTestModel(project.getName()); SessionContext context = new SessionContext(session); OperationalActivity oa = context.getSemanticElement(OA_ELEMENT_ID); // check the base64 string migration - checkBase64StringMigration(session, oa); + checkBase64StringMigration(session, oa, project, expectedFolderName); // check the absolute path migration checkInvalidAbsolutePathMigration(session, oa); @@ -150,31 +160,31 @@ private void migrateAndCheckFirstTime() throws Exception { checkPathWithHttp(session, oa); // check the project path migration - checkProjectRelativePathMigration(oa); + checkProjectRelativePathMigration(oa, project); - checkLogsAfterFirstMigration(); + checkLogsAfterFirstMigration(project); } - private void migrateAndCheckSecondTime() throws Exception { + private void migrateAndCheckSecondTime(IProject project, String expectedFolderName) throws Exception { statuses.clear(); - MigrationHelper.migrateProject(sourceModelProject); + MigrationHelper.migrateProject(project); - Session session = getSessionForTestModel(SOURCE_MODEL); + Session session = getSessionForTestModel(project.getName()); SessionContext context = new SessionContext(session); OperationalActivity oa = context.getSemanticElement(OA_ELEMENT_ID); // check the base64 string migration - checkBase64StringMigration(session, oa); + checkBase64StringMigration(session, oa, project, expectedFolderName); // check the absolute path migration - checkValidAbsolutePathMigration(session, oa); + checkValidAbsolutePathMigration(session, oa, project, expectedFolderName); // check the path beginning with https checkPathWithHttp(session, oa); // check the project path migration - checkProjectRelativePathMigration(oa); + checkProjectRelativePathMigration(oa, project); // check that the migration log only not found images checkLogsAfterSecondMigration(); @@ -192,12 +202,12 @@ private void checkPathWithHttp(Session session, OperationalActivity oa) { * When the image with the absolute path is found, the base64 should be converted into a image in the images folder in * the project. */ - private void checkBase64StringMigration(Session session, OperationalActivity oa) throws CoreException { + private void checkBase64StringMigration(Session session, OperationalActivity oa, IProject project, String expectedFolderName) throws CoreException { // check that an image has been created in the images folder - List folders = Arrays.asList(sourceModelProject.members()).stream().filter(IFolder.class::isInstance) + List folders = Arrays.asList(project.members()).stream().filter(IFolder.class::isInstance) .map(IFolder.class::cast).collect(Collectors.toList()); assertEquals(1, folders.size()); - assertEquals("Bad imagefolder name", ImageManager.IMAGE_FOLDER_NAME, folders.get(0).getName()); + assertEquals("Bad imagefolder name", expectedFolderName, folders.get(0).getName()); List imageNames = List.of(folders.get(0).members()).stream()// .map(IResource::getName)// .filter(name -> { @@ -212,12 +222,12 @@ private void checkBase64StringMigration(Session session, OperationalActivity oa) 2, imageNames.size()); // check that the OA description has been properly migrated - Pattern pattern = Pattern.compile(HTML_IMAGE_PATH_PATTERN_BASE64); + Pattern pattern = Pattern.compile(getHTMLImagePathPatternBase64(project.getName(), expectedFolderName)); Matcher matcher = pattern.matcher(oa.getDescription()); - assertTrue("The path to the image has not been found with pattern " + HTML_IMAGE_PATH_PATTERN_BASE64, + assertTrue("The path to the image has not been found with pattern " + getHTMLImagePathPatternBase64(project.getName(), expectedFolderName), matcher.find()); assertEquals(imageNames.get(0), matcher.group(1)); - assertFalse("There should not be any other match for the pattern " + HTML_IMAGE_PATH_PATTERN_BASE64, + assertFalse("There should not be any other match for the pattern " + getHTMLImagePathPatternBase64(project.getName(), expectedFolderName), matcher.find()); } @@ -228,30 +238,31 @@ private void checkInvalidAbsolutePathMigration(Session session, final Operationa oa.getDescription().contains(HTML_IMAGE_INVALID_ABSOLUTE_PATH)); } - private void checkValidAbsolutePathMigration(Session session, final OperationalActivity oa) throws CoreException { + private void checkValidAbsolutePathMigration(Session session, final OperationalActivity oa, IProject project, String expectedFolderName) throws CoreException { SessionContext context = new SessionContext(session); // Valid test OperationalActivity oa2 = context.getSemanticElement(OA_ELEMENT_ID); - String expectedPath = SOURCE_MODEL + "/" + ImageManager.IMAGE_FOLDER_NAME + "/" + CAPELLA_PNG; - Pattern pattern = Pattern.compile(HTML_IMAGE_PATH_PATTERN_COPIED); + String expectedPath = project.getName() + "/" + expectedFolderName + "/" + CAPELLA_PNG; + Pattern pattern = Pattern.compile(getHTMLImagePathPatternCopied(project.getName(), expectedFolderName)); Matcher matcher = pattern.matcher(oa2.getDescription()); - assertTrue("file:/xxxx/junit-workspace/ImagePath%20InRichTextAttribute/capella.png should have been migrated into " + String encodedNameProject = URI.encodeFragment(project.getName(), true); + assertTrue("file:/xxxx/junit-workspace/" + encodedNameProject + "/capella.png should have been migrated into " + expectedPath, matcher.find()); // Check the copied file - long nbImageFiles = Arrays.asList(sourceModelProject.getFolder(ImageManager.IMAGE_FOLDER_NAME).members()).stream() + long nbImageFiles = Arrays.asList(project.getFolder(expectedFolderName).members()).stream() .filter(IFile.class::isInstance).map(IFile.class::cast).filter(file -> { return "png".equals(file.getFileExtension()); }).count(); assertEquals("Bad number of images in the images folder", 3, nbImageFiles); } - private void checkProjectRelativePathMigration(OperationalActivity oa) { - Pattern pattern = Pattern.compile(HTML_MIGRATED_EXISTING_IMAGE_RELATIVE_PATH_PATTERN); + private void checkProjectRelativePathMigration(OperationalActivity oa, IProject project) { + Pattern pattern = Pattern.compile(getHTMLMigratedExistingImageRelativePathPattern(project.getName())); Matcher matcher = pattern.matcher(oa.getDescription()); assertTrue("The project relative path migration failed. The regex" - + HTML_MIGRATED_EXISTING_IMAGE_RELATIVE_PATH_PATTERN + + getHTMLMigratedExistingImageRelativePathPattern(project.getName()) + " should have been found. The image path should be migrated from a project relative path to a workspace relative path.", matcher.find()); @@ -263,7 +274,7 @@ private void checkProjectRelativePathMigration(OperationalActivity oa) { matcher.find()); } - private void checkLogsAfterFirstMigration() { + private void checkLogsAfterFirstMigration(IProject project) { synchronizationWithUIThread(); List warnings = statuses.stream().filter(s -> s.getSeverity() == IStatus.WARNING) .collect(Collectors.toList()); @@ -273,7 +284,7 @@ private void checkLogsAfterFirstMigration() { assertTrue("There should be a log that warns that a not migrated relative path has not found", warnings.get(1).getMessage().contains(HTML_NOT_MIGRATED_NOT_EXISTING_IMAGE_RELATIVE_PATH)); assertTrue("There should be a log that warns that an relative path has been migrated but the image is not found", - warnings.get(2).getMessage().contains(HTML_MIGRATED_NOT_EXISTING_IMAGE_RELATIVE_PATH)); + warnings.get(2).getMessage().contains(getHTMLMigratedNotExistingImageRelativePath(project.getName()))); } private void checkLogsAfterSecondMigration() { @@ -284,6 +295,22 @@ private void checkLogsAfterSecondMigration() { assertTrue("There should be a log that warns that a not migrated relative path has not found", warnings.get(0).getMessage().contains(HTML_NOT_MIGRATED_NOT_EXISTING_IMAGE_RELATIVE_PATH)); } + + private String getHTMLImagePathPatternBase64(String projectName, String folderName) { + return ""; + } + + private String getHTMLImagePathPatternCopied(String projectName, String folderName) { + return ""; + } + + private String getHTMLMigratedNotExistingImageRelativePath(String projectName) { + return projectName + "/RELATIVE_IMAGE_NOT_FOUND.png"; + } + + private String getHTMLMigratedExistingImageRelativePathPattern(String projectName) { + return ""; + } /** * Wait the end of the asynchronous calls waiting in UI thread.