diff --git a/tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/DialogCheck.java b/tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/DialogCheck.java index ee8e19180c6..1166a64b47a 100644 --- a/tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/DialogCheck.java +++ b/tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/DialogCheck.java @@ -15,7 +15,7 @@ package org.eclipse.ui.tests.harness.util; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogConstants; @@ -66,7 +66,7 @@ public static void assertDialog(Dialog dialog) { getShell(); } if (_verifyDialog.open(dialog) == IDialogConstants.NO_ID) { - assertTrue(_verifyDialog.getFailureText(), false); + fail(_verifyDialog.getFailureText()); } } @@ -117,30 +117,28 @@ public static Shell getShell() { private static void verifyCompositeText(Composite composite) { Control children[] = composite.getChildren(); for (Control child : children) { - if (child instanceof TabFolder) { - TabFolder folder = (TabFolder) child; + if (child instanceof TabFolder folder) { int numPages = folder.getItemCount(); for (int j = 0; j < numPages; j++) { folder.setSelection(j); } - } else if (child instanceof CTabFolder) { - CTabFolder folder = (CTabFolder) child; + } else if (child instanceof CTabFolder folder) { int numPages = folder.getItemCount(); for (int j = 0; j < numPages; j++) { folder.setSelection(j); } } - else if (child instanceof Button) { + else if (child instanceof Button b) { //verify the text if the child is a button - verifyButtonText((Button) child); + verifyButtonText(b); } - else if (child instanceof Label) { + else if (child instanceof Label l) { //child is not a button, maybe a label - verifyLabelText((Label) child); + verifyLabelText(l); } - else if (child instanceof Composite) { + else if (child instanceof Composite comp) { //child is not a label, make a recursive call if it is a composite - verifyCompositeText((Composite) child); + verifyCompositeText(comp); } } } @@ -158,7 +156,7 @@ private static void verifyButtonText(Button button) { //if (size.y/preferred.y) == X, then label spans X lines, so divide //the calculated value of preferred.x by X if (preferred.y * size.y > 0) { - preferred.y /= countLines(button.getText()); //check for '\n\' + preferred.y /= button.getText().lines().count(); // check for '\n\' if (size.y / preferred.y > 1) { preferred.x /= (size.y / preferred.y); } @@ -170,7 +168,7 @@ private static void verifyButtonText(Button button) { if (preferred.x > size.x) { //close the dialog button.getShell().dispose(); - assertTrue(message, false); + fail(message); } } @@ -191,7 +189,7 @@ private static void verifyLabelText(Label label) { //if (size.y/preferred.y) == X, then label spans X lines, so divide //the calculated value of preferred.x by X if (preferred.y * size.y > 0) { - preferred.y /= countLines(label.getText()); + preferred.y /= label.getText().lines().count(); if (size.y / preferred.y > 1) { preferred.x /= (size.y / preferred.y); } @@ -202,24 +200,8 @@ private static void verifyLabelText(Label label) { if (preferred.x > size.x) { //close the dialog label.getShell().dispose(); - assertTrue(message, false); + fail(message); } } - /* - * Counts the number of lines in a given String. - * For example, if a string contains one (1) newline character, - * a value of two (2) would be returned. - * @param text The string to look through. - * @return int the number of lines in text. - */ - private static int countLines(String text) { - int newLines = 1; - for (int i = 0; i < text.length(); i++) { - if (text.charAt(i) == '\n') { - newLines++; - } - } - return newLines; - } } diff --git a/tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/FileTool.java b/tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/FileTool.java index d5f94ffabda..a5c01a16879 100644 --- a/tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/FileTool.java +++ b/tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/FileTool.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2017 IBM Corporation and others. + * Copyright (c) 2000, 2024 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -18,13 +18,10 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.FileReader; -import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; -import java.io.Writer; import java.net.URL; import java.util.Enumeration; import java.util.zip.ZipEntry; @@ -37,10 +34,6 @@ public class FileTool { - /** - * A buffer. - */ - private static byte[] buffer = new byte[8192]; /** * Unzips the given zip file to the given destination directory * extracting only those entries the pass through the given @@ -64,7 +57,7 @@ public static void unzip(ZipFile zipFile, File dstDir) throws IOException { File file = new File(dstDir, changeSeparator(entryName, '/', File.separatorChar)); file.getParentFile().mkdirs(); try (InputStream src = zipFile.getInputStream(entry); OutputStream dst= new FileOutputStream(file)){ - transferData(src, dst); + src.transferTo(dst); } } } finally { @@ -99,24 +92,7 @@ public static String changeSeparator(String path, char oldSeparator, char newSep public static void transferData(File source, File destination) throws IOException { destination.getParentFile().mkdirs(); try (InputStream is = new FileInputStream(source); OutputStream os = new FileOutputStream(destination)) { - transferData(is, os); - } - } - /** - * Copies all bytes in the given source stream to - * the given destination stream. Neither streams - * are closed. - * - * @param source the given source stream - * @param destination the given destination stream - */ - public static void transferData(InputStream source, OutputStream destination) throws IOException { - int bytesRead = 0; - while(bytesRead != -1){ - bytesRead = source.read(buffer, 0, buffer.length); - if(bytesRead != -1){ - destination.write(buffer, 0, bytesRead); - } + is.transferTo(os); } } @@ -151,12 +127,6 @@ public static File getFileInPlugin(Plugin plugin, IPath path) { } } - public static StringBuilder readToBuilder(String fileName) throws IOException { - try (FileReader reader = new FileReader(fileName)) { - return readToBuilder(reader); - } - } - public static StringBuilder readToBuilder(Reader reader) throws IOException { StringBuilder s = new StringBuilder(); try { @@ -175,9 +145,4 @@ public static StringBuilder readToBuilder(Reader reader) throws IOException { return s; } - public static void writeFromBuilder(String fileName, StringBuilder content) throws IOException { - try (Writer writer = new FileWriter(fileName)) { - writer.write(content.toString()); - } - } } diff --git a/tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/FileUtil.java b/tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/FileUtil.java index 8c80473cd6d..015a1c51fc0 100644 --- a/tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/FileUtil.java +++ b/tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/FileUtil.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2010 IBM Corporation and others. + * Copyright (c) 2000, 2024 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -13,9 +13,6 @@ *******************************************************************************/ package org.eclipse.ui.tests.harness.util; -import java.io.ByteArrayInputStream; -import java.io.InputStream; - import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; @@ -81,8 +78,8 @@ public static void createFolder(IFolder folder, boolean force, boolean local, IP throws CoreException { if (!folder.exists()) { IContainer parent = folder.getParent(); - if (parent instanceof IFolder) { - createFolder((IFolder) parent, force, local, null); + if (parent instanceof IFolder f) { + createFolder(f, force, local, null); } folder.create(force, local, monitor); } @@ -98,9 +95,7 @@ public static void createFolder(IFolder folder, boolean force, boolean local, IP public static IFile createFile(String name, IProject proj) throws CoreException { IFile file = proj.getFile(name); if (!file.exists()) { - String str = " "; - InputStream in = new ByteArrayInputStream(str.getBytes()); - file.create(in, true, null); + file.create(" ".getBytes(), IResource.FORCE, null); } return file; } diff --git a/tests/org.eclipse.ui.tests.navigator/src/org/eclipse/ui/tests/navigator/GoBackForwardsTest.java b/tests/org.eclipse.ui.tests.navigator/src/org/eclipse/ui/tests/navigator/GoBackForwardsTest.java index 53625dbba5d..1b248ded62f 100644 --- a/tests/org.eclipse.ui.tests.navigator/src/org/eclipse/ui/tests/navigator/GoBackForwardsTest.java +++ b/tests/org.eclipse.ui.tests.navigator/src/org/eclipse/ui/tests/navigator/GoBackForwardsTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2020 IBM Corporation and others. + * Copyright (c) 2000, 2024 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -14,6 +14,8 @@ package org.eclipse.ui.tests.navigator; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; @@ -31,7 +33,6 @@ import org.eclipse.ui.intro.IIntroPart; import org.eclipse.ui.part.FileEditorInput; import org.eclipse.ui.tests.harness.util.EditorTestHelper; -import org.eclipse.ui.tests.harness.util.FileTool; import org.eclipse.ui.tests.harness.util.FileUtil; import org.eclipse.ui.tests.harness.util.UITestCase; import org.eclipse.ui.texteditor.AbstractTextEditor; @@ -70,7 +71,7 @@ public void doSetUp() { file = FileUtil.createFile(FILE_NAME, project); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(FILE_CONTENTS); - FileTool.writeFromBuilder(file.getLocation().toOSString(), stringBuilder); + Files.writeString(Paths.get(file.getLocation().toOSString()), stringBuilder); project.refreshLocal(IResource.DEPTH_INFINITE, null); } catch (CoreException e) { fail("Should not throw an exception");