Skip to content

Commit 857f51a

Browse files
committed
Simplify o.e.ui.tests.harness
Make use of new Java and Eclipse APIs.
1 parent b0d7509 commit 857f51a

File tree

4 files changed

+25
-82
lines changed

4 files changed

+25
-82
lines changed

tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/DialogCheck.java

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package org.eclipse.ui.tests.harness.util;
1616

1717
import static org.junit.Assert.assertNotNull;
18-
import static org.junit.Assert.assertTrue;
18+
import static org.junit.Assert.fail;
1919

2020
import org.eclipse.jface.dialogs.Dialog;
2121
import org.eclipse.jface.dialogs.IDialogConstants;
@@ -66,7 +66,7 @@ public static void assertDialog(Dialog dialog) {
6666
getShell();
6767
}
6868
if (_verifyDialog.open(dialog) == IDialogConstants.NO_ID) {
69-
assertTrue(_verifyDialog.getFailureText(), false);
69+
fail(_verifyDialog.getFailureText());
7070
}
7171
}
7272

@@ -117,30 +117,28 @@ public static Shell getShell() {
117117
private static void verifyCompositeText(Composite composite) {
118118
Control children[] = composite.getChildren();
119119
for (Control child : children) {
120-
if (child instanceof TabFolder) {
121-
TabFolder folder = (TabFolder) child;
120+
if (child instanceof TabFolder folder) {
122121
int numPages = folder.getItemCount();
123122
for (int j = 0; j < numPages; j++) {
124123
folder.setSelection(j);
125124
}
126-
} else if (child instanceof CTabFolder) {
127-
CTabFolder folder = (CTabFolder) child;
125+
} else if (child instanceof CTabFolder folder) {
128126
int numPages = folder.getItemCount();
129127
for (int j = 0; j < numPages; j++) {
130128
folder.setSelection(j);
131129
}
132130
}
133-
else if (child instanceof Button) {
131+
else if (child instanceof Button b) {
134132
//verify the text if the child is a button
135-
verifyButtonText((Button) child);
133+
verifyButtonText(b);
136134
}
137-
else if (child instanceof Label) {
135+
else if (child instanceof Label l) {
138136
//child is not a button, maybe a label
139-
verifyLabelText((Label) child);
137+
verifyLabelText(l);
140138
}
141-
else if (child instanceof Composite) {
139+
else if (child instanceof Composite comp) {
142140
//child is not a label, make a recursive call if it is a composite
143-
verifyCompositeText((Composite) child);
141+
verifyCompositeText(comp);
144142
}
145143
}
146144
}
@@ -158,7 +156,7 @@ private static void verifyButtonText(Button button) {
158156
//if (size.y/preferred.y) == X, then label spans X lines, so divide
159157
//the calculated value of preferred.x by X
160158
if (preferred.y * size.y > 0) {
161-
preferred.y /= countLines(button.getText()); //check for '\n\'
159+
preferred.y /= button.getText().lines().count(); // check for '\n\'
162160
if (size.y / preferred.y > 1) {
163161
preferred.x /= (size.y / preferred.y);
164162
}
@@ -170,7 +168,7 @@ private static void verifyButtonText(Button button) {
170168
if (preferred.x > size.x) {
171169
//close the dialog
172170
button.getShell().dispose();
173-
assertTrue(message, false);
171+
fail(message);
174172
}
175173
}
176174

@@ -191,7 +189,7 @@ private static void verifyLabelText(Label label) {
191189
//if (size.y/preferred.y) == X, then label spans X lines, so divide
192190
//the calculated value of preferred.x by X
193191
if (preferred.y * size.y > 0) {
194-
preferred.y /= countLines(label.getText());
192+
preferred.y /= label.getText().lines().count();
195193
if (size.y / preferred.y > 1) {
196194
preferred.x /= (size.y / preferred.y);
197195
}
@@ -202,24 +200,8 @@ private static void verifyLabelText(Label label) {
202200
if (preferred.x > size.x) {
203201
//close the dialog
204202
label.getShell().dispose();
205-
assertTrue(message, false);
203+
fail(message);
206204
}
207205
}
208206

209-
/*
210-
* Counts the number of lines in a given String.
211-
* For example, if a string contains one (1) newline character,
212-
* a value of two (2) would be returned.
213-
* @param text The string to look through.
214-
* @return int the number of lines in text.
215-
*/
216-
private static int countLines(String text) {
217-
int newLines = 1;
218-
for (int i = 0; i < text.length(); i++) {
219-
if (text.charAt(i) == '\n') {
220-
newLines++;
221-
}
222-
}
223-
return newLines;
224-
}
225207
}

tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/FileTool.java

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2017 IBM Corporation and others.
2+
* Copyright (c) 2000, 2024 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -18,13 +18,10 @@
1818
import java.io.File;
1919
import java.io.FileInputStream;
2020
import java.io.FileOutputStream;
21-
import java.io.FileReader;
22-
import java.io.FileWriter;
2321
import java.io.IOException;
2422
import java.io.InputStream;
2523
import java.io.OutputStream;
2624
import java.io.Reader;
27-
import java.io.Writer;
2825
import java.net.URL;
2926
import java.util.Enumeration;
3027
import java.util.zip.ZipEntry;
@@ -37,10 +34,6 @@
3734

3835
public class FileTool {
3936

40-
/**
41-
* A buffer.
42-
*/
43-
private static byte[] buffer = new byte[8192];
4437
/**
4538
* Unzips the given zip file to the given destination directory
4639
* extracting only those entries the pass through the given
@@ -64,7 +57,7 @@ public static void unzip(ZipFile zipFile, File dstDir) throws IOException {
6457
File file = new File(dstDir, changeSeparator(entryName, '/', File.separatorChar));
6558
file.getParentFile().mkdirs();
6659
try (InputStream src = zipFile.getInputStream(entry); OutputStream dst= new FileOutputStream(file)){
67-
transferData(src, dst);
60+
src.transferTo(dst);
6861
}
6962
}
7063
} finally {
@@ -99,24 +92,7 @@ public static String changeSeparator(String path, char oldSeparator, char newSep
9992
public static void transferData(File source, File destination) throws IOException {
10093
destination.getParentFile().mkdirs();
10194
try (InputStream is = new FileInputStream(source); OutputStream os = new FileOutputStream(destination)) {
102-
transferData(is, os);
103-
}
104-
}
105-
/**
106-
* Copies all bytes in the given source stream to
107-
* the given destination stream. Neither streams
108-
* are closed.
109-
*
110-
* @param source the given source stream
111-
* @param destination the given destination stream
112-
*/
113-
public static void transferData(InputStream source, OutputStream destination) throws IOException {
114-
int bytesRead = 0;
115-
while(bytesRead != -1){
116-
bytesRead = source.read(buffer, 0, buffer.length);
117-
if(bytesRead != -1){
118-
destination.write(buffer, 0, bytesRead);
119-
}
95+
is.transferTo(os);
12096
}
12197
}
12298

@@ -151,12 +127,6 @@ public static File getFileInPlugin(Plugin plugin, IPath path) {
151127
}
152128
}
153129

154-
public static StringBuilder readToBuilder(String fileName) throws IOException {
155-
try (FileReader reader = new FileReader(fileName)) {
156-
return readToBuilder(reader);
157-
}
158-
}
159-
160130
public static StringBuilder readToBuilder(Reader reader) throws IOException {
161131
StringBuilder s = new StringBuilder();
162132
try {
@@ -175,9 +145,4 @@ public static StringBuilder readToBuilder(Reader reader) throws IOException {
175145
return s;
176146
}
177147

178-
public static void writeFromBuilder(String fileName, StringBuilder content) throws IOException {
179-
try (Writer writer = new FileWriter(fileName)) {
180-
writer.write(content.toString());
181-
}
182-
}
183148
}

tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/FileUtil.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2010 IBM Corporation and others.
2+
* Copyright (c) 2000, 2024 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -13,9 +13,6 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.tests.harness.util;
1515

16-
import java.io.ByteArrayInputStream;
17-
import java.io.InputStream;
18-
1916
import org.eclipse.core.resources.IContainer;
2017
import org.eclipse.core.resources.IFile;
2118
import org.eclipse.core.resources.IFolder;
@@ -81,8 +78,8 @@ public static void createFolder(IFolder folder, boolean force, boolean local, IP
8178
throws CoreException {
8279
if (!folder.exists()) {
8380
IContainer parent = folder.getParent();
84-
if (parent instanceof IFolder) {
85-
createFolder((IFolder) parent, force, local, null);
81+
if (parent instanceof IFolder f) {
82+
createFolder(f, force, local, null);
8683
}
8784
folder.create(force, local, monitor);
8885
}
@@ -98,9 +95,7 @@ public static void createFolder(IFolder folder, boolean force, boolean local, IP
9895
public static IFile createFile(String name, IProject proj) throws CoreException {
9996
IFile file = proj.getFile(name);
10097
if (!file.exists()) {
101-
String str = " ";
102-
InputStream in = new ByteArrayInputStream(str.getBytes());
103-
file.create(in, true, null);
98+
file.create(" ".getBytes(), IResource.FORCE, null);
10499
}
105100
return file;
106101
}

tests/org.eclipse.ui.tests.navigator/src/org/eclipse/ui/tests/navigator/GoBackForwardsTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2020 IBM Corporation and others.
2+
* Copyright (c) 2000, 2024 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -14,6 +14,8 @@
1414
package org.eclipse.ui.tests.navigator;
1515

1616
import java.io.IOException;
17+
import java.nio.file.Files;
18+
import java.nio.file.Paths;
1719

1820
import org.eclipse.core.resources.IFile;
1921
import org.eclipse.core.resources.IProject;
@@ -31,7 +33,6 @@
3133
import org.eclipse.ui.intro.IIntroPart;
3234
import org.eclipse.ui.part.FileEditorInput;
3335
import org.eclipse.ui.tests.harness.util.EditorTestHelper;
34-
import org.eclipse.ui.tests.harness.util.FileTool;
3536
import org.eclipse.ui.tests.harness.util.FileUtil;
3637
import org.eclipse.ui.tests.harness.util.UITestCase;
3738
import org.eclipse.ui.texteditor.AbstractTextEditor;
@@ -70,7 +71,7 @@ public void doSetUp() {
7071
file = FileUtil.createFile(FILE_NAME, project);
7172
StringBuilder stringBuilder = new StringBuilder();
7273
stringBuilder.append(FILE_CONTENTS);
73-
FileTool.writeFromBuilder(file.getLocation().toOSString(), stringBuilder);
74+
Files.writeString(Paths.get(file.getLocation().toOSString()), stringBuilder);
7475
project.refreshLocal(IResource.DEPTH_INFINITE, null);
7576
} catch (CoreException e) {
7677
fail("Should not throw an exception");

0 commit comments

Comments
 (0)