Skip to content

Commit

Permalink
AVRO-1901: Record named Exception generated bad code
Browse files Browse the repository at this point in the history
  • Loading branch information
radai-rosenblatt authored and nielsbasjes committed Sep 19, 2016
1 parent 27cb9e2 commit be33922
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ Trunk (not yet released)

AVRO-1914: Fix licencing errors reported by rat. (Niels Basjes)

AVRO-1901: Record named "Exception" generated bad code.
(Radai Rosenblatt via Niels Basjes)

Avro 1.8.1 (14 May 2016)

INCOMPATIBLE CHANGES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ OutputFile compileInterface(Protocol protocol) {
return outputFile;
}

//package private for testing purposes
String makePath(String name, String space) {
if (space == null || space.isEmpty()) {
return name + suffix;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public class ${this.mangle($schema.getName())}#if ($schema.isError()) extends or
#end
#end
return record;
} catch (Exception e) {
} catch (java.lang.Exception e) {
throw new org.apache.avro.AvroRuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public void setUp() {
this.src = new File(this.schemaSrcPath);
this.outputDir = AvroTestUtil.tempDirectory(getClass(), "specific-output");
this.outputFile = new File(this.outputDir, "SimpleRecord.java");
if (outputFile.exists() && !outputFile.delete()) {
throw new IllegalStateException("unable to delete " + outputFile);
}
}

@After
Expand Down Expand Up @@ -143,6 +146,7 @@ public void testPublicFieldVisibility() throws IOException {
assertFalse("Line started with a deprecated field declaration: " + line,
line.startsWith("@Deprecated public int value"));
}
reader.close();
}

@Test
Expand All @@ -156,6 +160,7 @@ public void testCreateAllArgsConstructor() throws Exception {
while (!foundAllArgsConstructor && (line = reader.readLine()) != null) {
foundAllArgsConstructor = line.contains("All-args constructor");
}
reader.close();
assertTrue(foundAllArgsConstructor);
}

Expand Down Expand Up @@ -216,6 +221,7 @@ public void testPublicDeprecatedFieldVisibility() throws IOException {
assertFalse("Line started with a public field declaration: " + line,
line.startsWith("public int value"));
}
reader.close();
}

@Test
Expand All @@ -238,6 +244,7 @@ public void testPrivateFieldVisibility() throws IOException {
assertFalse("Line started with a deprecated field declaration: " + line,
line.startsWith("@Deprecated public int value"));
}
reader.close();
}

@Test
Expand All @@ -256,6 +263,7 @@ public void testSettersCreatedByDefault() throws IOException {
foundSetters++;
}
}
reader.close();
assertEquals("Found the wrong number of setters", 1, foundSetters);
}

Expand All @@ -274,6 +282,7 @@ public void testSettersNotCreatedWhenOptionTurnedOff() throws IOException {
assertFalse("No line should include the setter: " + line,
line.startsWith("public void setValue("));
}
reader.close();
}

@Test
Expand All @@ -282,14 +291,20 @@ public void testSettingOutputCharacterEncoding() throws Exception {
// Generated file in default encoding
compiler.compileToDestination(this.src, this.outputDir);
byte[] fileInDefaultEncoding = new byte[(int) this.outputFile.length()];
new FileInputStream(this.outputFile).read(fileInDefaultEncoding);
this.outputFile.delete();
FileInputStream is = new FileInputStream(this.outputFile);
is.read(fileInDefaultEncoding);
is.close(); //close input stream otherwise delete might fail
if (!this.outputFile.delete()) {
throw new IllegalStateException("unable to delete " + this.outputFile); //delete otherwise compiler might not overwrite because src timestamp hasnt changed.
}
// Generate file in another encoding (make sure it has different number of bytes per character)
String differentEncoding = Charset.defaultCharset().equals(Charset.forName("UTF-16")) ? "UTF-32" : "UTF-16";
compiler.setOutputCharacterEncoding(differentEncoding);
compiler.compileToDestination(this.src, this.outputDir);
byte[] fileInDifferentEncoding = new byte[(int) this.outputFile.length()];
new FileInputStream(this.outputFile).read(fileInDifferentEncoding);
is = new FileInputStream(this.outputFile);
is.read(fileInDifferentEncoding);
is.close();
// Compare as bytes
assertThat("Generated file should contain different bytes after setting non-default encoding",
fileInDefaultEncoding, not(equalTo(fileInDifferentEncoding)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public Player build() {
record.last_name = fieldSetFlags()[2] ? this.last_name : (java.lang.String) defaultValue(fields()[2]);
record.position = fieldSetFlags()[3] ? this.position : (java.util.List<avro.examples.baseball.Position>) defaultValue(fields()[3]);
return record;
} catch (Exception e) {
} catch (java.lang.Exception e) {
throw new org.apache.avro.AvroRuntimeException(e);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lang/java/tools/src/test/compiler/output/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public Player build() {
record.last_name = fieldSetFlags()[2] ? this.last_name : (java.lang.CharSequence) defaultValue(fields()[2]);
record.position = fieldSetFlags()[3] ? this.position : (java.util.List<avro.examples.baseball.Position>) defaultValue(fields()[3]);
return record;
} catch (Exception e) {
} catch (java.lang.Exception e) {
throw new org.apache.avro.AvroRuntimeException(e);
}
}
Expand Down
3 changes: 3 additions & 0 deletions share/test/schemas/specialtypes.avdl
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,7 @@ protocol LetsBreakIt {
string nl;
}

record Exception {
string whatever;
}
}

0 comments on commit be33922

Please sign in to comment.