13
13
*******************************************************************************/
14
14
package org .eclipse .core .tests .resources .regression ;
15
15
16
+ import static org .junit .Assert .assertThrows ;
17
+
16
18
import java .io .ByteArrayInputStream ;
19
+ import java .io .IOException ;
17
20
import java .io .InputStream ;
18
- import org .eclipse .core .resources .*;
21
+ import org .eclipse .core .resources .IFile ;
22
+ import org .eclipse .core .resources .IFolder ;
23
+ import org .eclipse .core .resources .IProject ;
24
+ import org .eclipse .core .resources .IResource ;
19
25
import org .eclipse .core .runtime .CoreException ;
20
26
import org .eclipse .core .runtime .Platform .OS ;
21
27
import org .eclipse .core .tests .resources .ResourceTest ;
30
36
*/
31
37
public class Bug_025457 extends ResourceTest {
32
38
33
- public void testFile () {
39
+ public void testFile () throws Exception {
34
40
//this test only works on windows
35
41
if (!OS .isWindows ()) {
36
42
return ;
@@ -43,39 +49,22 @@ public void testFile() {
43
49
ensureExistsInWorkspace (sourceFile , content );
44
50
45
51
//open a stream in the source to cause the rename to fail
46
- InputStream stream = null ;
47
- try {
48
- try {
49
- stream = sourceFile .getContents ();
50
- } catch (CoreException e ) {
51
- fail ("0.99" , e );
52
- }
52
+ try (InputStream stream = sourceFile .getContents ()) {
53
53
//try to rename the file (should fail)
54
- try {
55
- sourceFile .move (destFile .getFullPath (), IResource .NONE , getMonitor ());
56
- fail ("1.99" );
57
- } catch (CoreException e1 ) {
58
- //should fail
59
- }
60
- } finally {
61
- assertClose (stream );
54
+ assertThrows (CoreException .class ,
55
+ () -> sourceFile .move (destFile .getFullPath (), IResource .NONE , getMonitor ()));
62
56
}
63
57
//ensure source still exists and has same content
64
58
assertTrue ("2.0" , source .exists ());
65
59
assertTrue ("2.1" , sourceFile .exists ());
66
- try {
67
- stream = sourceFile .getContents ();
60
+ try (InputStream stream = sourceFile .getContents ()) {
68
61
assertTrue ("2.2" , compareContent (stream , new ByteArrayInputStream (content .getBytes ())));
69
- } catch (CoreException e ) {
70
- fail ("3.99" , e );
71
- } finally {
72
- assertClose (stream );
73
62
}
74
63
//ensure destination file does not exist
75
64
assertTrue ("2.3" , !destFile .exists ());
76
65
}
77
66
78
- public void testFolder () {
67
+ public void testFolder () throws IOException , CoreException {
79
68
//this test only works on windows
80
69
//native code must also be present so move can detect the case change
81
70
if (!OS .isWindows () || !isReadOnlySupported ()) {
@@ -91,20 +80,10 @@ public void testFolder() {
91
80
ensureExistsInWorkspace (sourceFile , true );
92
81
93
82
//open a stream in the source to cause the rename to fail
94
- InputStream stream = null ;
95
- try {
96
- try {
97
- stream = sourceFile .getContents ();
98
- } catch (CoreException e ) {
99
- fail ("0.99" , e );
100
- }
83
+ try (InputStream stream = sourceFile .getContents ()) {
101
84
//try to rename the project (should fail)
102
- try {
103
- sourceFolder .move (destFolder .getFullPath (), IResource .NONE , getMonitor ());
104
- fail ("1.99" );
105
- } catch (CoreException e1 ) {
106
- //should fail
107
- }
85
+ assertThrows (CoreException .class ,
86
+ () -> sourceFolder .move (destFolder .getFullPath (), IResource .NONE , getMonitor ()));
108
87
//ensure source still exists
109
88
assertTrue ("2.0" , source .exists ());
110
89
assertTrue ("2.1" , sourceFolder .exists ());
@@ -113,13 +92,10 @@ public void testFolder() {
113
92
//ensure destination does not exist
114
93
assertTrue ("2.3" , !destFolder .exists ());
115
94
assertTrue ("2.4" , !destFile .exists ());
116
-
117
- } finally {
118
- assertClose (stream );
119
95
}
120
96
}
121
97
122
- public void testProject () {
98
+ public void testProject () throws IOException , CoreException {
123
99
//this test only works on windows
124
100
if (!OS .isWindows ()) {
125
101
return ;
@@ -132,30 +108,18 @@ public void testProject() {
132
108
ensureExistsInWorkspace (sourceFile , true );
133
109
134
110
//open a stream in the source to cause the rename to fail
135
- InputStream stream = null ;
136
- try {
137
- try {
138
- stream = sourceFile .getContents ();
139
- } catch (CoreException e ) {
140
- fail ("1.99" , e );
141
- }
111
+ try (InputStream stream = sourceFile .getContents ()) {
142
112
//try to rename the project (should fail)
143
- try {
144
- source .move (destination .getFullPath (), IResource .NONE , getMonitor ());
145
- fail ("1.99" );
146
- } catch (CoreException e1 ) {
147
- //should fail
148
- }
113
+ assertThrows (CoreException .class ,
114
+ () -> source .move (destination .getFullPath (), IResource .NONE , getMonitor ()));
115
+
149
116
//ensure source does not exist
150
117
assertTrue ("2.0" , !source .exists ());
151
118
assertTrue ("2.1" , !sourceFile .exists ());
152
119
153
120
//ensure destination does not exist
154
121
assertTrue ("2.2" , destination .exists ());
155
122
assertTrue ("2.3" , destFile .exists ());
156
-
157
- } finally {
158
- assertClose (stream );
159
123
}
160
124
}
161
125
}
0 commit comments