-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
AVRO-3660: [JAVA] Use data generator with RandomData #2526
Merged
opwvhk
merged 1 commit into
apache:master
from
opwvhk:AVRO-3660-Use-data-generator-with-RandomData
Sep 27, 2023
+261
−23
Merged
Changes from all commits
Commits
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
224 changes: 224 additions & 0 deletions
224
lang/java/avro/src/test/java/org/apache/avro/util/TestRandomData.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,224 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* https://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 org.apache.avro.util; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.Objects; | ||
import java.util.Random; | ||
|
||
import org.apache.avro.Schema; | ||
import org.apache.avro.file.DataFileReader; | ||
import org.apache.avro.file.DataFileWriter; | ||
import org.apache.avro.generic.GenericData; | ||
import org.apache.avro.reflect.ReflectData; | ||
import org.apache.avro.specific.SpecificData; | ||
import org.apache.avro.specific.SpecificRecordBase; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class TestRandomData { | ||
private long seed; | ||
|
||
private int count; | ||
|
||
private File file; | ||
private GenericData genericData; | ||
private SpecificData specificData; | ||
private Schema specificSchema; | ||
private ReflectData reflectData; | ||
private Schema reflectedSchema; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
file = Files.createTempFile("randomData", ".avro").toFile(); | ||
seed = System.currentTimeMillis(); | ||
count = new Random().nextInt(50) + 75; | ||
|
||
genericData = GenericData.get(); | ||
specificData = SpecificData.get(); | ||
specificSchema = specificData.getSchema(SpecificTestRecord.class); | ||
reflectData = ReflectData.get(); | ||
reflectedSchema = reflectData.getSchema(ReflectTestRecord.class); | ||
} | ||
|
||
@Test | ||
public void testRandomDataFromGenericToGeneric() throws IOException { | ||
checkWrite(genericData, TEST_SCHEMA); | ||
checkRead(genericData, TEST_SCHEMA); | ||
} | ||
|
||
@Test | ||
public void testRandomDataFromGenericToSpecific() throws IOException { | ||
checkWrite(genericData, TEST_SCHEMA); | ||
checkRead(specificData, specificSchema); | ||
} | ||
|
||
@Test | ||
public void testRandomDataFromGenericToReflected() throws IOException { | ||
checkWrite(genericData, TEST_SCHEMA); | ||
checkRead(reflectData, reflectedSchema); | ||
} | ||
|
||
@Test | ||
public void testRandomDataFromSpecificToGeneric() throws IOException { | ||
checkWrite(specificData, specificSchema); | ||
checkRead(genericData, TEST_SCHEMA); | ||
} | ||
|
||
@Test | ||
public void testRandomDataFromSpecificToSpecific() throws IOException { | ||
checkWrite(specificData, specificSchema); | ||
checkRead(specificData, specificSchema); | ||
} | ||
|
||
@Test | ||
public void testRandomDataFromSpecificToReflected() throws IOException { | ||
checkWrite(specificData, specificSchema); | ||
checkRead(reflectData, reflectedSchema); | ||
} | ||
|
||
@Test | ||
public void testRandomDataFromReflectedToGeneric() throws IOException { | ||
checkWrite(reflectData, reflectedSchema); | ||
checkRead(genericData, TEST_SCHEMA); | ||
} | ||
|
||
@Test | ||
public void testRandomDataFromReflectedToSpecific() throws IOException { | ||
checkWrite(reflectData, reflectedSchema); | ||
checkRead(specificData, specificSchema); | ||
} | ||
|
||
@Test | ||
public void testRandomDataFromReflectedToReflected() throws IOException { | ||
checkWrite(reflectData, reflectedSchema); | ||
checkRead(reflectData, reflectedSchema); | ||
} | ||
|
||
private void checkWrite(GenericData genericData, Schema schema) throws IOException { | ||
// noinspection unchecked | ||
try (DataFileWriter<Object> writer = new DataFileWriter<Object>(genericData.createDatumWriter(schema))) { | ||
writer.create(schema, file); | ||
for (Object datum : new RandomData(genericData, schema, this.count, seed)) { | ||
writer.append(datum); | ||
} | ||
} | ||
} | ||
|
||
private void checkRead(GenericData genericData, Schema schema) throws IOException { | ||
// noinspection unchecked | ||
try (DataFileReader<Object> reader = new DataFileReader<Object>(file, genericData.createDatumReader(schema))) { | ||
for (Object expected : new RandomData(genericData, schema, this.count, seed)) { | ||
assertEquals(expected, reader.next()); | ||
} | ||
} | ||
} | ||
|
||
/* | ||
* Test classes: they implement the same schema, but one is a SpecificRecord and | ||
* the other uses a reflected schema. | ||
*/ | ||
|
||
public static final String TEST_SCHEMA_JSON = "{\"type\":\"record\",\"name\":\"Record\",\"fields\":[{\"name\":\"x\",\"type\":\"int\"},{\"name\":\"y\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}}]}"; | ||
|
||
public static final Schema TEST_SCHEMA = new Schema.Parser().parse(TEST_SCHEMA_JSON); | ||
|
||
public static class SpecificTestRecord extends SpecificRecordBase { | ||
public static final Schema SCHEMA$ = new Schema.Parser().parse(TEST_SCHEMA_JSON.replace("\"name\":\"Record\"", | ||
"\"name\":\"" + SpecificTestRecord.class.getCanonicalName() + "\"")); | ||
private int x; | ||
private String y; | ||
|
||
@Override | ||
public Schema getSchema() { | ||
return SCHEMA$; | ||
} | ||
|
||
@Override | ||
public void put(int i, Object v) { | ||
switch (i) { | ||
case 0: | ||
x = (Integer) v; | ||
break; | ||
case 1: | ||
y = (String) v; | ||
break; | ||
default: | ||
throw new RuntimeException(); | ||
} | ||
} | ||
|
||
@Override | ||
public Object get(int i) { | ||
switch (i) { | ||
case 0: | ||
return x; | ||
case 1: | ||
return y; | ||
} | ||
throw new RuntimeException(); | ||
} | ||
} | ||
|
||
public static class ReflectTestRecord { | ||
private int x; | ||
private String y; | ||
|
||
public int getX() { | ||
return x; | ||
} | ||
|
||
public void setX(int x) { | ||
this.x = x; | ||
} | ||
|
||
public String getY() { | ||
return y; | ||
} | ||
|
||
public void setY(String y) { | ||
this.y = y; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
ReflectTestRecord that = (ReflectTestRecord) o; | ||
return x == that.x && Objects.equals(y, that.y); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(x, y); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("{\"x\": %d, \"y\": \"%s\"}", x, y); | ||
} | ||
} | ||
} |
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.
Check warning
Code scanning / CodeQL
Random used only once