Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
8cccbcf4
authored
Jun 22, 2020
by
ibaker
Committed by
Andrew Lewis
Jun 23, 2020
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Migrate DefaultHttpDataSourceTest from Mockito to MockWebServer
PiperOrigin-RevId: 317636681
parent
0ff917ad
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
100 deletions
constants.gradle
library/core/build.gradle
library/core/src/test/java/com/google/android/exoplayer2/upstream/DefaultHttpDataSourceTest.java
constants.gradle
View file @
8cccbcf4
...
@@ -23,6 +23,7 @@ project.ext {
...
@@ -23,6 +23,7 @@ project.ext {
junitVersion
=
'4.13-rc-2'
junitVersion
=
'4.13-rc-2'
guavaVersion
=
'27.1-android'
guavaVersion
=
'27.1-android'
mockitoVersion
=
'2.25.0'
mockitoVersion
=
'2.25.0'
mockWebServerVersion
=
'3.12.0'
robolectricVersion
=
'4.4-SNAPSHOT'
robolectricVersion
=
'4.4-SNAPSHOT'
checkerframeworkVersion
=
'2.5.0'
checkerframeworkVersion
=
'2.5.0'
jsr305Version
=
'3.0.2'
jsr305Version
=
'3.0.2'
...
...
library/core/build.gradle
View file @
8cccbcf4
...
@@ -66,6 +66,7 @@ dependencies {
...
@@ -66,6 +66,7 @@ dependencies {
exclude
module:
modulePrefix
.
substring
(
1
)
+
'library-core'
exclude
module:
modulePrefix
.
substring
(
1
)
+
'library-core'
}
}
testImplementation
'com.google.guava:guava:'
+
guavaVersion
testImplementation
'com.google.guava:guava:'
+
guavaVersion
testImplementation
'com.squareup.okhttp3:mockwebserver:'
+
mockWebServerVersion
testImplementation
'org.robolectric:robolectric:'
+
robolectricVersion
testImplementation
'org.robolectric:robolectric:'
+
robolectricVersion
testImplementation
project
(
modulePrefix
+
'testutils'
)
testImplementation
project
(
modulePrefix
+
'testutils'
)
}
}
...
...
library/core/src/test/java/com/google/android/exoplayer2/upstream/DefaultHttpDataSourceTest.java
View file @
8cccbcf4
...
@@ -17,23 +17,19 @@
...
@@ -17,23 +17,19 @@
package
com
.
google
.
android
.
exoplayer2
.
upstream
;
package
com
.
google
.
android
.
exoplayer2
.
upstream
;
import
static
com
.
google
.
common
.
truth
.
Truth
.
assertThat
;
import
static
com
.
google
.
common
.
truth
.
Truth
.
assertThat
;
import
static
java
.
util
.
concurrent
.
TimeUnit
.
SECONDS
;
import
static
org
.
junit
.
Assert
.
assertThrows
;
import
static
org
.
junit
.
Assert
.
assertThrows
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
Mockito
.
doReturn
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
androidx.test.ext.junit.runners.AndroidJUnit4
;
import
androidx.test.ext.junit.runners.AndroidJUnit4
;
import
com.google.android.exoplayer2.testutil.TestUtil
;
import
com.google.android.exoplayer2.testutil.TestUtil
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.net.HttpURLConnection
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Map
;
import
okhttp3.Headers
;
import
okhttp3.mockwebserver.MockResponse
;
import
okhttp3.mockwebserver.MockWebServer
;
import
okio.Buffer
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runner.RunWith
;
import
org.mockito.ArgumentMatchers
;
import
org.mockito.Mockito
;
/** Unit tests for {@link DefaultHttpDataSource}. */
/** Unit tests for {@link DefaultHttpDataSource}. */
@RunWith
(
AndroidJUnit4
.
class
)
@RunWith
(
AndroidJUnit4
.
class
)
...
@@ -45,86 +41,84 @@ public class DefaultHttpDataSourceTest {
...
@@ -45,86 +41,84 @@ public class DefaultHttpDataSourceTest {
* table below. Values wrapped in '*' are the ones that should be set in the connection request.
* table below. Values wrapped in '*' are the ones that should be set in the connection request.
*
*
* <pre>{@code
* <pre>{@code
* +---------------
--------+
---+-----+-----+-----+-----+-----+
* +---------------
+-----+--
---+-----+-----+-----+-----+-----+
* | | Header Key |
* | | Header Key |
* +---------------
--------+
---+-----+-----+-----+-----+-----+
* +---------------
+-----+--
---+-----+-----+-----+-----+-----+
* |
Location | 0 | 1 | 2 | 3 | 4 | 5
|
* |
Location | 0 | 1 | 2 | 3 | 4 | 5 | 6
|
* +---------------
--------+
---+-----+-----+-----+-----+-----+
* +---------------
+-----+--
---+-----+-----+-----+-----+-----+
* |
Default |*Y*| Y | Y |
| | |
* |
Constructor | *Y* | Y | Y | | Y
| | |
* |
DefaultHttpDataSource | | *Y* | Y | Y
| *Y* | |
* |
Setter | | *Y* | Y | Y |
| *Y* | |
* | DataSpec
| |
| *Y* | *Y* | | *Y* |
* | DataSpec
| | | *Y*
| *Y* | *Y* | | *Y* |
* +---------------
--------+
---+-----+-----+-----+-----+-----+
* +---------------
+-----+--
---+-----+-----+-----+-----+-----+
* }</pre>
* }</pre>
*/
*/
@Test
@Test
public
void
open_withSpecifiedRequestParameters_usesCorrectParameters
()
throws
IOException
{
public
void
open_withSpecifiedRequestParameters_usesCorrectParameters
()
throws
Exception
{
String
defaultParameter
=
"Default"
;
MockWebServer
mockWebServer
=
new
MockWebServer
();
String
dataSourceInstanceParameter
=
"DefaultHttpDataSource"
;
mockWebServer
.
enqueue
(
new
MockResponse
());
String
dataSpecParameter
=
"Dataspec"
;
String
propertyFromConstructor
=
"fromConstructor"
;
HttpDataSource
.
RequestProperties
defaultParameters
=
new
HttpDataSource
.
RequestProperties
();
HttpDataSource
.
RequestProperties
constructorProperties
=
new
HttpDataSource
.
RequestProperties
();
defaultParameters
.
set
(
"0"
,
defaultParameter
);
constructorProperties
.
set
(
"0"
,
propertyFromConstructor
);
defaultParameters
.
set
(
"1"
,
defaultParameter
);
constructorProperties
.
set
(
"1"
,
propertyFromConstructor
);
defaultParameters
.
set
(
"2"
,
defaultParameter
);
constructorProperties
.
set
(
"2"
,
propertyFromConstructor
);
constructorProperties
.
set
(
"4"
,
propertyFromConstructor
);
DefaultHttpDataSource
defaultHttpDataSource
=
DefaultHttpDataSource
dataSource
=
Mockito
.
spy
(
new
DefaultHttpDataSource
(
new
DefaultHttpDataSource
(
/* userAgent= */
"testAgent"
,
/* userAgent= */
"testAgent"
,
/* connectTimeoutMillis= */
1000
,
/* connectTimeoutMillis= */
1000
,
/* readTimeoutMillis= */
1000
,
/* readTimeoutMillis= */
1000
,
/* allowCrossProtocolRedirects= */
false
,
/* allowCrossProtocolRedirects= */
false
,
defaultParameters
)
);
constructorProperties
);
Map
<
String
,
String
>
sentRequestProperties
=
new
HashMap
<>();
String
propertyFromSetter
=
"fromSetter"
;
HttpURLConnection
mockHttpUrlConnection
=
make200MockHttpUrlConnection
(
sentRequestProperties
);
dataSource
.
setRequestProperty
(
"1"
,
propertyFromSetter
);
doReturn
(
mockHttpUrlConnection
).
when
(
defaultHttpDataSource
).
openConnection
(
any
());
dataSource
.
setRequestProperty
(
"2"
,
propertyFromSetter
);
dataSource
.
setRequestProperty
(
"3"
,
propertyFromSetter
);
defaultHttpDataSource
.
setRequestProperty
(
"1"
,
dataSourceInstanceParameter
);
dataSource
.
setRequestProperty
(
"5"
,
propertyFromSetter
);
defaultHttpDataSource
.
setRequestProperty
(
"2"
,
dataSourceInstanceParameter
);
defaultHttpDataSource
.
setRequestProperty
(
"3"
,
dataSourceInstanceParameter
);
defaultHttpDataSource
.
setRequestProperty
(
"4"
,
dataSourceInstanceParameter
);
String
propertyFromDataSpec
=
"fromDataSpec"
;
Map
<
String
,
String
>
dataSpecRequestProperties
=
new
HashMap
<>();
Map
<
String
,
String
>
dataSpecRequestProperties
=
new
HashMap
<>();
dataSpecRequestProperties
.
put
(
"2"
,
dataSpecParameter
);
dataSpecRequestProperties
.
put
(
"2"
,
propertyFromDataSpec
);
dataSpecRequestProperties
.
put
(
"3"
,
dataSpecParameter
);
dataSpecRequestProperties
.
put
(
"3"
,
propertyFromDataSpec
);
dataSpecRequestProperties
.
put
(
"
5"
,
dataSpecParameter
);
dataSpecRequestProperties
.
put
(
"
4"
,
propertyFromDataSpec
);
dataSpecRequestProperties
.
put
(
"6"
,
propertyFromDataSpec
);
DataSpec
dataSpec
=
DataSpec
dataSpec
=
new
DataSpec
.
Builder
()
new
DataSpec
.
Builder
()
.
setUri
(
"http://www.google.com"
)
.
setUri
(
mockWebServer
.
url
(
"/test-path"
).
toString
())
.
setHttpBody
(
new
byte
[]
{
0
,
0
,
0
,
0
})
.
setLength
(
1
)
.
setKey
(
"key"
)
.
setHttpRequestHeaders
(
dataSpecRequestProperties
)
.
setHttpRequestHeaders
(
dataSpecRequestProperties
)
.
build
();
.
build
();
d
efaultHttpD
ataSource
.
open
(
dataSpec
);
dataSource
.
open
(
dataSpec
);
assertThat
(
sentRequestProperties
.
get
(
"0"
)).
isEqualTo
(
defaultParameter
);
Headers
headers
=
mockWebServer
.
takeRequest
(
10
,
SECONDS
).
getHeaders
();
assertThat
(
sentRequestProperties
.
get
(
"1"
)).
isEqualTo
(
dataSourceInstanceParameter
);
assertThat
(
headers
.
get
(
"0"
)).
isEqualTo
(
propertyFromConstructor
);
assertThat
(
sentRequestProperties
.
get
(
"2"
)).
isEqualTo
(
dataSpecParameter
);
assertThat
(
headers
.
get
(
"1"
)).
isEqualTo
(
propertyFromSetter
);
assertThat
(
sentRequestProperties
.
get
(
"3"
)).
isEqualTo
(
dataSpecParameter
);
assertThat
(
headers
.
get
(
"2"
)).
isEqualTo
(
propertyFromDataSpec
);
assertThat
(
sentRequestProperties
.
get
(
"4"
)).
isEqualTo
(
dataSourceInstanceParameter
);
assertThat
(
headers
.
get
(
"3"
)).
isEqualTo
(
propertyFromDataSpec
);
assertThat
(
sentRequestProperties
.
get
(
"5"
)).
isEqualTo
(
dataSpecParameter
);
assertThat
(
headers
.
get
(
"4"
)).
isEqualTo
(
propertyFromDataSpec
);
assertThat
(
headers
.
get
(
"5"
)).
isEqualTo
(
propertyFromSetter
);
assertThat
(
headers
.
get
(
"6"
)).
isEqualTo
(
propertyFromDataSpec
);
}
}
@Test
@Test
public
void
open_invalidResponseCode
()
throws
Exception
{
public
void
open_invalidResponseCode
()
throws
Exception
{
DefaultHttpDataSource
defaultHttpDataSource
=
DefaultHttpDataSource
defaultHttpDataSource
=
Mockito
.
spy
(
new
DefaultHttpDataSource
(
new
DefaultHttpDataSource
(
/* userAgent= */
"testAgent"
,
/* userAgent= */
"testAgent"
,
/* connectTimeoutMillis= */
1000
,
/* connectTimeoutMillis= */
1000
,
/* readTimeoutMillis= */
1000
,
/* readTimeoutMillis= */
1000
,
/* allowCrossProtocolRedirects= */
false
,
/* allowCrossProtocolRedirects= */
false
,
/* defaultRequestProperties= */
null
)
);
/* defaultRequestProperties= */
null
);
HttpURLConnection
mockHttpUrlConnection
=
MockWebServer
mockWebServer
=
new
MockWebServer
();
make404MockHttpUrlConnection
(
/* responseData= */
TestUtil
.
createByteArray
(
1
,
2
,
3
));
mockWebServer
.
enqueue
(
doReturn
(
mockHttpUrlConnection
).
when
(
defaultHttpDataSource
).
openConnection
(
any
());
new
MockResponse
()
.
setResponseCode
(
404
)
.
setBody
(
new
Buffer
().
write
(
TestUtil
.
createByteArray
(
1
,
2
,
3
))));
DataSpec
dataSpec
=
new
DataSpec
.
Builder
().
setUri
(
"http://www.google.com"
).
build
();
DataSpec
dataSpec
=
new
DataSpec
.
Builder
().
setUri
(
mockWebServer
.
url
(
"/test-path"
).
toString
()).
build
();
HttpDataSource
.
InvalidResponseCodeException
exception
=
HttpDataSource
.
InvalidResponseCodeException
exception
=
assertThrows
(
assertThrows
(
...
@@ -134,45 +128,4 @@ public class DefaultHttpDataSourceTest {
...
@@ -134,45 +128,4 @@ public class DefaultHttpDataSourceTest {
assertThat
(
exception
.
responseCode
).
isEqualTo
(
404
);
assertThat
(
exception
.
responseCode
).
isEqualTo
(
404
);
assertThat
(
exception
.
responseBody
).
isEqualTo
(
TestUtil
.
createByteArray
(
1
,
2
,
3
));
assertThat
(
exception
.
responseBody
).
isEqualTo
(
TestUtil
.
createByteArray
(
1
,
2
,
3
));
}
}
/**
* Creates a mock {@link HttpURLConnection} that stores all request parameters inside {@code
* requestProperties}.
*/
private
static
HttpURLConnection
make200MockHttpUrlConnection
(
Map
<
String
,
String
>
requestProperties
)
throws
IOException
{
HttpURLConnection
mockHttpUrlConnection
=
Mockito
.
mock
(
HttpURLConnection
.
class
);
when
(
mockHttpUrlConnection
.
usingProxy
()).
thenReturn
(
false
);
when
(
mockHttpUrlConnection
.
getInputStream
())
.
thenReturn
(
new
ByteArrayInputStream
(
new
byte
[
128
]));
when
(
mockHttpUrlConnection
.
getOutputStream
()).
thenReturn
(
new
ByteArrayOutputStream
());
when
(
mockHttpUrlConnection
.
getResponseCode
()).
thenReturn
(
200
);
when
(
mockHttpUrlConnection
.
getResponseMessage
()).
thenReturn
(
"OK"
);
Mockito
.
doAnswer
(
(
invocation
)
->
{
String
key
=
invocation
.
getArgument
(
0
);
String
value
=
invocation
.
getArgument
(
1
);
requestProperties
.
put
(
key
,
value
);
return
null
;
})
.
when
(
mockHttpUrlConnection
)
.
setRequestProperty
(
ArgumentMatchers
.
anyString
(),
ArgumentMatchers
.
anyString
());
return
mockHttpUrlConnection
;
}
/** Creates a mock {@link HttpURLConnection} that returns a 404 response code. */
private
static
HttpURLConnection
make404MockHttpUrlConnection
(
byte
[]
responseData
)
throws
IOException
{
HttpURLConnection
mockHttpUrlConnection
=
Mockito
.
mock
(
HttpURLConnection
.
class
);
when
(
mockHttpUrlConnection
.
getOutputStream
()).
thenReturn
(
new
ByteArrayOutputStream
());
when
(
mockHttpUrlConnection
.
getErrorStream
()).
thenReturn
(
new
ByteArrayInputStream
(
responseData
));
when
(
mockHttpUrlConnection
.
getResponseCode
()).
thenReturn
(
404
);
when
(
mockHttpUrlConnection
.
getResponseMessage
()).
thenReturn
(
"NOT FOUND"
);
return
mockHttpUrlConnection
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment