run Android Studio default code formatter with Ctrl-Alt-L

This commit is contained in:
Hans-Christoph Steiner 2021-02-09 17:16:13 +01:00
parent 3cb6cc747b
commit a81140be47
2 changed files with 29 additions and 43 deletions

View File

@ -34,7 +34,6 @@ import org.fdroid.fdroid.Utils;
import org.fdroid.fdroid.data.Schema.RepoTable.Cols; import org.fdroid.fdroid.data.Schema.RepoTable.Cols;
import org.fdroid.fdroid.net.TreeUriDownloader; import org.fdroid.fdroid.net.TreeUriDownloader;
import java.lang.reflect.Array;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
@ -270,11 +269,10 @@ public class Repo extends ValueObject {
return tempName; return tempName;
} }
public String getFileUrl(String... pathElements) public String getFileUrl(String... pathElements) {
{
/* Each String in pathElements might contain a /, should keep these as path elements */ /* Each String in pathElements might contain a /, should keep these as path elements */
List<String> elements = new ArrayList(); List<String> elements = new ArrayList();
for (String element: pathElements) { for (String element : pathElements) {
for (String elementPart : element.split("/")) { for (String elementPart : element.split("/")) {
elements.add(elementPart); elements.add(elementPart);
} }
@ -291,21 +289,20 @@ public class Repo extends ValueObject {
*/ */
if (address.startsWith("content://")) { if (address.startsWith("content://")) {
StringBuilder result = new StringBuilder(address); StringBuilder result = new StringBuilder(address);
for (String element: elements) { for (String element : elements) {
result.append(TreeUriDownloader.ESCAPED_SLASH); result.append(TreeUriDownloader.ESCAPED_SLASH);
result.append(element); result.append(element);
} }
return result.toString(); return result.toString();
} else { // Normal URL } else { // Normal URL
Uri.Builder result = Uri.parse(address).buildUpon(); Uri.Builder result = Uri.parse(address).buildUpon();
for (String element: elements) { for (String element : elements) {
result.appendPath((element)); result.appendPath(element);
} }
return result.build().toString(); return result.build().toString();
} }
} }
private static int toInt(Integer value) { private static int toInt(Integer value) {
if (value == null) { if (value == null) {
return 0; return 0;

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2018-2021 Senecto Limited * Copyright (C) 2021 Angus Gratton
* Copyright (C) 2018 Senecto Limited
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -15,35 +16,26 @@
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
package org.fdroid.fdroid; package org.fdroid.fdroid;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import androidx.test.core.app.ApplicationProvider;
import org.fdroid.fdroid.data.Apk; import org.fdroid.fdroid.data.Apk;
import org.fdroid.fdroid.data.ApkProvider;
import org.fdroid.fdroid.data.App;
import org.fdroid.fdroid.data.FDroidProviderTest; import org.fdroid.fdroid.data.FDroidProviderTest;
import org.fdroid.fdroid.data.Repo; import org.fdroid.fdroid.data.Repo;
import org.fdroid.fdroid.data.Schema;
import org.fdroid.fdroid.mock.MockApk; import org.fdroid.fdroid.mock.MockApk;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import static org.fdroid.fdroid.data.Schema.*;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class RepoUrlsTest extends FDroidProviderTest { public class RepoUrlsTest extends FDroidProviderTest {
private static final String TAG = "RepoUrlsTest"; public static final String TAG = "RepoUrlsTest";
/** Private class describing a repository URL we're going to test, and /**
* Private class describing a repository URL we're going to test, and
* the file pattern for any files within that URL. * the file pattern for any files within that URL.
*/ */
private static class TestRepo { private static class TestRepo {
@ -52,8 +44,7 @@ public class RepoUrlsTest extends FDroidProviderTest {
// String format pattern for generating file URLs, should contain a single %s for the filename // String format pattern for generating file URLs, should contain a single %s for the filename
public String fileUrlPattern; public String fileUrlPattern;
public TestRepo(String repoUrl, String fileUrlPattern) TestRepo(String repoUrl, String fileUrlPattern) {
{
this.repoUrl = repoUrl; this.repoUrl = repoUrl;
this.fileUrlPattern = fileUrlPattern; this.fileUrlPattern = fileUrlPattern;
} }
@ -61,7 +52,7 @@ public class RepoUrlsTest extends FDroidProviderTest {
private static final String APK_NAME = "test-v1.apk"; private static final String APK_NAME = "test-v1.apk";
private static final TestRepo REPOS[] = { private static final TestRepo[] REPOS = {
new TestRepo( new TestRepo(
"https://microg.org/fdroid/repo", "https://microg.org/fdroid/repo",
"https://microg.org/fdroid/repo/%s"), "https://microg.org/fdroid/repo/%s"),
@ -100,20 +91,21 @@ public class RepoUrlsTest extends FDroidProviderTest {
Preferences.setupForTests(context); Preferences.setupForTests(context);
} }
interface getFileFromRepo { interface GetFileFromRepo {
String get(TestRepo tr); String get(TestRepo tr);
} }
/** Utility test function - go through the list of test repos, /**
* using the useOfRepo interface to instantiate a repo from the URL * Utility test function - go through the list of test repos,
* and return a file of some kind (Apk, index, etc.) and check that * using the useOfRepo interface to instantiate a repo from the URL
* it matches the test repo's expected URL format. * and return a file of some kind (Apk, index, etc.) and check that
* @param fileName File that 'useOfRepo' will return in the repo, when called * it matches the test repo's expected URL format.
*
* @param fileName File that 'useOfRepo' will return in the repo, when called
* @param useOfRepo Instance of the function that uses the repo to build a file URL * @param useOfRepo Instance of the function that uses the repo to build a file URL
*/ */
private void testReposWithFile(String fileName, getFileFromRepo useOfRepo) private void testReposWithFile(String fileName, GetFileFromRepo useOfRepo) {
{ for (TestRepo tr : REPOS) {
for(TestRepo tr: REPOS) {
String expectedUrl = String.format(tr.fileUrlPattern, fileName); String expectedUrl = String.format(tr.fileUrlPattern, fileName);
System.out.println("Testing URL " + expectedUrl); System.out.println("Testing URL " + expectedUrl);
String actualUrl = useOfRepo.get(tr); String actualUrl = useOfRepo.get(tr);
@ -122,9 +114,8 @@ public class RepoUrlsTest extends FDroidProviderTest {
} }
@Test @Test
public void testIndexUrls() public void testIndexUrls() {
{ testReposWithFile(IndexUpdater.SIGNED_FILE_NAME, new GetFileFromRepo() {
testReposWithFile(IndexUpdater.SIGNED_FILE_NAME, new getFileFromRepo() {
@Override @Override
public String get(TestRepo tr) { public String get(TestRepo tr) {
Repo repo = new Repo(); Repo repo = new Repo();
@ -136,9 +127,8 @@ public class RepoUrlsTest extends FDroidProviderTest {
} }
@Test @Test
public void testIndexV1Urls() public void testIndexV1Urls() {
{ testReposWithFile(IndexV1Updater.SIGNED_FILE_NAME, new GetFileFromRepo() {
testReposWithFile(IndexV1Updater.SIGNED_FILE_NAME, new getFileFromRepo() {
@Override @Override
public String get(TestRepo tr) { public String get(TestRepo tr) {
Repo repo = new Repo(); Repo repo = new Repo();
@ -150,9 +140,8 @@ public class RepoUrlsTest extends FDroidProviderTest {
} }
@Test @Test
public void testApkUrls() public void testApkUrls() {
{ testReposWithFile(APK_NAME, new GetFileFromRepo() {
testReposWithFile(APK_NAME, new getFileFromRepo() {
@Override @Override
public String get(TestRepo tr) { public String get(TestRepo tr) {
Apk apk = new MockApk(APK_NAME, 1, tr.repoUrl, APK_NAME); Apk apk = new MockApk(APK_NAME, 1, tr.repoUrl, APK_NAME);