-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create base class for serialization tests #754
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ae8a1b
Create base class for serialization tests
mziccard 967c5c5
Add tests for restorable classes. Fix serialization issues
mziccard 034432a
Remove serialization test for ApplicationDefaultAuthCredentials
mziccard ce59793
Add restorableObjects method to BaseSerializationTest
mziccard 5c4e288
Make NoAuthCredentials constructor private
mziccard 19e0c6e
Add IamPolicy to SerializationTest
mziccard 83ddb08
Add equals and hashCode to exceptions. Add exceptions to serializatio…
mziccard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.gcloud; | ||
|
||
import static com.google.common.base.MoreObjects.firstNonNull; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotSame; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.io.Serializable; | ||
|
||
/** | ||
* Base class for serialization tests. To use this class in your tests override the | ||
* {@code serializableObjects()} method to return all objects that must be serializable. Also | ||
* override {@code restorableObjects()} method to return all restorable objects whose state must be | ||
* tested for proper serialization. Both methods can return {@code null} if no such object needs to | ||
* be tested. | ||
*/ | ||
public abstract class BaseSerializationTest { | ||
|
||
/** | ||
* Returns all objects for which correct serialization must be tested. | ||
*/ | ||
protected abstract Serializable[] serializableObjects(); | ||
|
||
/** | ||
* Returns all restorable objects whose state must be tested for proper serialization. | ||
*/ | ||
protected abstract Restorable<?>[] restorableObjects(); | ||
|
||
@Test | ||
public void testSerializableObjects() throws Exception { | ||
for (Serializable obj : firstNonNull(serializableObjects(), new Serializable[0])) { | ||
Object copy = serializeAndDeserialize(obj); | ||
assertEquals(obj, obj); | ||
assertEquals(obj, copy); | ||
assertEquals(obj.hashCode(), copy.hashCode()); | ||
assertEquals(obj.toString(), copy.toString()); | ||
assertNotSame(obj, copy); | ||
assertEquals(copy, copy); | ||
} | ||
} | ||
|
||
@Test | ||
public void testRestorableObjects() throws Exception { | ||
for (Restorable restorable : firstNonNull(restorableObjects(), new Restorable[0])) { | ||
RestorableState<?> state = restorable.capture(); | ||
RestorableState<?> deserializedState = serializeAndDeserialize(state); | ||
assertEquals(state, deserializedState); | ||
assertEquals(state.hashCode(), deserializedState.hashCode()); | ||
assertEquals(state.toString(), deserializedState.toString()); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T> T serializeAndDeserialize(T obj) throws IOException, ClassNotFoundException { | ||
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); | ||
try (ObjectOutputStream output = new ObjectOutputStream(bytes)) { | ||
output.writeObject(obj); | ||
} | ||
try (ObjectInputStream input = | ||
new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) { | ||
return (T) input.readObject(); | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.gcloud; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
|
||
public class SerializationTest extends BaseSerializationTest { | ||
|
||
private static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.defaultInstance(); | ||
private static final Identity IDENTITY = Identity.allAuthenticatedUsers(); | ||
private static final PageImpl<String> PAGE = | ||
new PageImpl<>(null, "cursor", ImmutableList.of("string1", "string2")); | ||
private static final RetryParams RETRY_PARAMS = RetryParams.defaultInstance(); | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
private static final String JSON_KEY = "{\n" | ||
+ " \"private_key_id\": \"somekeyid\",\n" | ||
+ " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS" | ||
+ "kAgEAAoIBAQC+K2hSuFpAdrJI\\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg" | ||
+ "aR\\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\nQP/9dJfIkIDJ9Fw9N4" | ||
+ "Bhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nknddadwkwewcVxHFhcZJO+XWf6ofLUXpRwiTZakGMn8EE1uVa2" | ||
+ "LgczOjwWHGi99MFjxSer5m9\\n1tCa3/KEGKiS/YL71JvjwX3mb+cewlkcmweBKZHM2JPTk0ZednFSpVZMtycjkbLa" | ||
+ "\\ndYOS8V85AgMBewECggEBAKksaldajfDZDV6nGqbFjMiizAKJolr/M3OQw16K6o3/\\n0S31xIe3sSlgW0+UbYlF" | ||
+ "4U8KifhManD1apVSC3csafaspP4RZUHFhtBywLO9pR5c\\nr6S5aLp+gPWFyIp1pfXbWGvc5VY/v9x7ya1VEa6rXvL" | ||
+ "sKupSeWAW4tMj3eo/64ge\\nsdaceaLYw52KeBYiT6+vpsnYrEkAHO1fF/LavbLLOFJmFTMxmsNaG0tuiJHgjshB\\" | ||
+ "n82DpMCbXG9YcCgI/DbzuIjsdj2JC1cascSP//3PmefWysucBQe7Jryb6NQtASmnv\\nCdDw/0jmZTEjpe4S1lxfHp" | ||
+ "lAhHFtdgYTvyYtaLZiVVkCgYEA8eVpof2rceecw/I6\\n5ng1q3Hl2usdWV/4mZMvR0fOemacLLfocX6IYxT1zA1FF" | ||
+ "JlbXSRsJMf/Qq39mOR2\\nSpW+hr4jCoHeRVYLgsbggtrevGmILAlNoqCMpGZ6vDmJpq6ECV9olliDvpPgWOP+\\nm" | ||
+ "YPDreFBGxWvQrADNbRt2dmGsrsCgYEAyUHqB2wvJHFqdmeBsaacewzV8x9WgmeX\\ngUIi9REwXlGDW0Mz50dxpxcK" | ||
+ "CAYn65+7TCnY5O/jmL0VRxU1J2mSWyWTo1C+17L0\\n3fUqjxL1pkefwecxwecvC+gFFYdJ4CQ/MHHXU81Lwl1iWdF" | ||
+ "Cd2UoGddYaOF+KNeM\\nHC7cmqra+JsCgYEAlUNywzq8nUg7282E+uICfCB0LfwejuymR93CtsFgb7cRd6ak\\nECR" | ||
+ "8FGfCpH8ruWJINllbQfcHVCX47ndLZwqv3oVFKh6pAS/vVI4dpOepP8++7y1u\\ncoOvtreXCX6XqfrWDtKIvv0vjl" | ||
+ "HBhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nkndj5uNl5SiuVxHFhcZJO+XWf6ofLUregtevZakGMn8EE1uVa" | ||
+ "2AY7eafmoU/nZPT\\n00YB0TBATdCbn/nBSuKDESkhSg9s2GEKQZG5hBmL5uCMfo09z3SfxZIhJdlerreP\\nJ7gSi" | ||
+ "dI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\\nEfeFCoOX75MxKwXs6xgrw4W//AYG" | ||
+ "GUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\\nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk" | ||
+ "XyRDW4IG1Oa2p\\nrALStNBx5Y9t0/LQnFI4w3aG\\n-----END PRIVATE KEY-----\\n\",\n" | ||
+ " \"client_email\": \"[email protected]\",\n" | ||
+ " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n" | ||
+ " \"type\": \"service_account\"\n" | ||
+ "}"; | ||
|
||
@Override | ||
protected Serializable[] serializableObjects() { | ||
return new Serializable[]{EXCEPTION_HANDLER, IDENTITY, PAGE, RETRY_PARAMS}; | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
} | ||
|
||
@Override | ||
protected Restorable<?>[] restorableObjects() { | ||
try { | ||
return new Restorable<?>[]{AuthCredentials.createForAppEngine(), AuthCredentials.noAuth(), | ||
AuthCredentials.createForJson(new ByteArrayInputStream(JSON_KEY.getBytes()))}; | ||
} catch (IOException ex) { | ||
// never reached | ||
throw new RuntimeException(ex); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.