Skip to content

Commit 754e9f3

Browse files
committed
fix try with resources statements
Signed-off-by: Alex Knop <[email protected]>
1 parent 30d972a commit 754e9f3

File tree

2 files changed

+43
-34
lines changed

2 files changed

+43
-34
lines changed

app/src/androidTest/java/com/owncloud/android/ScreenshotsIT.java

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,18 @@ public static void beforeScreenshot() {
5252

5353
@Test
5454
public void gridViewScreenshot() {
55-
ActivityScenario.launch(FileDisplayActivity.class);
55+
try (ActivityScenario<FileDisplayActivity> ignored = ActivityScenario.launch(FileDisplayActivity.class)) {
5656

57-
onView(anyOf(withText(R.string.action_switch_grid_view), withId(R.id.switch_grid_view_button))).perform(click());
57+
onView(anyOf(withText(R.string.action_switch_grid_view), withId(R.id.switch_grid_view_button))).perform(click());
5858

59-
shortSleep();
59+
shortSleep();
6060

61-
Screengrab.screenshot("01_gridView");
61+
Screengrab.screenshot("01_gridView");
6262

63-
onView(anyOf(withText(R.string.action_switch_list_view), withId(R.id.switch_grid_view_button))).perform(click());
63+
onView(anyOf(withText(R.string.action_switch_list_view), withId(R.id.switch_grid_view_button))).perform(click());
6464

65-
Assert.assertTrue(true); // if we reach this, everything is ok
65+
Assert.assertTrue(true); // if we reach this, everything is ok
66+
}
6667
}
6768

6869
@Test
@@ -77,61 +78,66 @@ public void listViewScreenshot() {
7778
assertTrue(result.isSuccess());
7879
}
7980

80-
ActivityScenario.launch(FileDisplayActivity.class);
81+
try (ActivityScenario<FileDisplayActivity> ignored = ActivityScenario.launch(FileDisplayActivity.class)) {
8182

82-
// go into work folder
83-
onView(withId(R.id.list_root)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
83+
// go into work folder
84+
onView(withId(R.id.list_root)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
8485

85-
Screengrab.screenshot("02_listView");
86+
Screengrab.screenshot("02_listView");
8687

87-
Assert.assertTrue(true); // if we reach this, everything is ok
88+
Assert.assertTrue(true); // if we reach this, everything is ok
89+
}
8890
}
8991

9092
@Test
9193
public void drawerScreenshot() {
92-
ActivityScenario.launch(FileDisplayActivity.class);
94+
try (ActivityScenario<FileDisplayActivity> ignored = ActivityScenario.launch(FileDisplayActivity.class)) {
9395

94-
onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
96+
onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
9597

96-
Screengrab.screenshot("03_drawer");
98+
Screengrab.screenshot("03_drawer");
9799

98-
onView(withId(R.id.drawer_layout)).perform(DrawerActions.close());
100+
onView(withId(R.id.drawer_layout)).perform(DrawerActions.close());
99101

100-
Assert.assertTrue(true); // if we reach this, everything is ok
102+
Assert.assertTrue(true); // if we reach this, everything is ok
103+
}
101104
}
102105

103106
@Test
104107
public void multipleAccountsScreenshot() {
105-
ActivityScenario.launch(FileDisplayActivity.class);
108+
try (ActivityScenario<FileDisplayActivity> ignored = ActivityScenario.launch(FileDisplayActivity.class)) {
106109

107-
onView(withId(R.id.switch_account_button)).perform(click());
110+
onView(withId(R.id.switch_account_button)).perform(click());
108111

109-
Screengrab.screenshot("04_accounts");
112+
Screengrab.screenshot("04_accounts");
110113

111-
pressBack();
114+
pressBack();
112115

113-
Assert.assertTrue(true); // if we reach this, everything is ok
116+
Assert.assertTrue(true); // if we reach this, everything is ok
117+
}
114118
}
115119

116120
@Test
117121
public void autoUploadScreenshot() {
118-
ActivityScenario.launch(SyncedFoldersActivity.class);
122+
try (ActivityScenario<SyncedFoldersActivity> ignored = ActivityScenario.launch(SyncedFoldersActivity.class)) {
119123

120-
Screengrab.screenshot("05_autoUpload");
124+
Screengrab.screenshot("05_autoUpload");
121125

122-
Assert.assertTrue(true); // if we reach this, everything is ok
126+
Assert.assertTrue(true); // if we reach this, everything is ok
127+
}
123128
}
124129

125130
@Test
126131
public void davdroidScreenshot() {
127-
ActivityScenario.launch(SettingsActivity.class);
132+
try (ActivityScenario<SettingsActivity> ignored = ActivityScenario.launch(SettingsActivity.class)) {
128133

129-
onData(PreferenceMatchers.withTitle(R.string.prefs_category_more)).perform(ViewActions.scrollTo());
134+
onData(PreferenceMatchers.withTitle(R.string.prefs_category_more)).perform(ViewActions.scrollTo());
130135

131-
shortSleep();
136+
shortSleep();
132137

133-
Screengrab.screenshot("06_davdroid");
138+
Screengrab.screenshot("06_davdroid");
134139

135-
Assert.assertTrue(true); // if we reach this, everything is ok
140+
Assert.assertTrue(true); // if we reach this, everything is ok
141+
}
136142
}
137143
}

app/src/main/java/com/owncloud/android/operations/UploadFileOperation.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -982,8 +982,8 @@ private RemoteOperationResult normalUpload(OwnCloudClient client) {
982982
final Long creationTimestamp = FileUtil.getCreationTimestamp(originalFile);
983983

984984
// Initialize channel and fileLock in try-with-resources
985-
try (
986-
FileChannel channel = new RandomAccessFile(mFile.getStoragePath(), "rw").getChannel();
985+
try (RandomAccessFile randomAccessFile = new RandomAccessFile(mFile.getStoragePath(), "rw");
986+
FileChannel channel = randomAccessFile.getChannel();
987987
FileLock fileLock = channel.tryLock()
988988
) {
989989
if (fileLock == null) {
@@ -998,7 +998,8 @@ private RemoteOperationResult normalUpload(OwnCloudClient client) {
998998
if (result.isSuccess()) {
999999
if (temporalFile.length() == originalFile.length()) {
10001000
// Acquire lock on temporary file
1001-
try (FileChannel tempChannel = new RandomAccessFile(temporalFile.getAbsolutePath(), "rw").getChannel();
1001+
try (RandomAccessFile randomAccessFile = new RandomAccessFile(temporalFile.getAbsolutePath(), "rw");
1002+
FileChannel tempChannel = randomAccessFile.getChannel();
10021003
FileLock tempFileLock = tempChannel.tryLock()) {
10031004
if (tempFileLock != null) {
10041005
// Use the temporary channel for the upload
@@ -1546,8 +1547,10 @@ private void move(File sourceFile, File targetFile) throws IOException {
15461547
// try to copy and then delete
15471548
targetFile.createNewFile();
15481549
try (
1549-
FileChannel inChannel = new FileInputStream(sourceFile).getChannel();
1550-
FileChannel outChannel = new FileOutputStream(targetFile).getChannel()
1550+
FileInputStream fis = new FileInputStream(sourceFile);
1551+
FileOutputStream fos = new FileOutputStream(targetFile);
1552+
FileChannel inChannel = fis.getChannel();
1553+
FileChannel outChannel = fos.getChannel()
15511554
) {
15521555
inChannel.transferTo(0, inChannel.size(), outChannel);
15531556
sourceFile.delete();

0 commit comments

Comments
 (0)