@ -13,7 +13,7 @@ import static java.nio.charset.StandardCharsets.US_ASCII;
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertNull ;
import static org.junit.Assert.assertNull ;
import static org.junit.Assert.assertSame ;
import static org.junit.Assert.assertSame ;
import static org.junit.Assert.fail ;
import static org.junit.Assert.assertThrows ;
import java.io.ByteArrayOutputStream ;
import java.io.ByteArrayOutputStream ;
import java.io.IOException ;
import java.io.IOException ;
@ -24,18 +24,8 @@ import org.junit.Test;
public class CommitBuilderTest {
public class CommitBuilderTest {
private void assertGpgSignatureStringOutcome ( String signature ,
String expectedOutcome ) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream ( ) ;
CommitBuilder . writeGpgSignatureString ( signature , out ) ;
String formatted_signature = new String ( out . toByteArray ( ) , US_ASCII ) ;
assertEquals ( expectedOutcome , formatted_signature ) ;
}
@Test
public void writeGpgSignatureString_1 ( ) throws Exception {
// @formatter:off
// @formatter:off
String signature = "-----BEGIN PGP SIGNATURE-----\n" +
private static final String SIGNATURE = "-----BEGIN PGP SIGNATURE-----\n" +
"Version: BCPG v1.60\n" +
"Version: BCPG v1.60\n" +
"\n" +
"\n" +
"iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
"iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
@ -46,7 +36,8 @@ public class CommitBuilderTest {
"IQEKkjnA+lhejjK1rv+ulq4kGZJFKGYWYYhRDwFg5PTkzhudhN2SGUq5Wxq1Eg4=\n" +
"IQEKkjnA+lhejjK1rv+ulq4kGZJFKGYWYYhRDwFg5PTkzhudhN2SGUq5Wxq1Eg4=\n" +
"=b9OI\n" +
"=b9OI\n" +
"-----END PGP SIGNATURE-----" ;
"-----END PGP SIGNATURE-----" ;
String expectedOutcome = "-----BEGIN PGP SIGNATURE-----\n" +
private static final String EXPECTED = "-----BEGIN PGP SIGNATURE-----\n" +
" Version: BCPG v1.60\n" +
" Version: BCPG v1.60\n" +
" \n" +
" \n" +
" iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
" iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
@ -58,23 +49,48 @@ public class CommitBuilderTest {
" =b9OI\n" +
" =b9OI\n" +
" -----END PGP SIGNATURE-----" ;
" -----END PGP SIGNATURE-----" ;
// @formatter:on
// @formatter:on
assertGpgSignatureStringOutcome ( signature , expectedOutcome ) ;
private void assertGpgSignatureStringOutcome ( String signature ,
String expectedOutcome ) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream ( ) ;
CommitBuilder . writeGpgSignatureString ( signature , out ) ;
String formatted_signature = new String ( out . toByteArray ( ) , US_ASCII ) ;
assertEquals ( expectedOutcome , formatted_signature ) ;
}
@Test
public void writeGpgSignatureString ( ) throws Exception {
assertGpgSignatureStringOutcome ( SIGNATURE , EXPECTED ) ;
}
@Test
public void writeGpgSignatureStringTrailingLF ( ) throws Exception {
assertGpgSignatureStringOutcome ( SIGNATURE + '\n' , EXPECTED ) ;
}
@Test
public void writeGpgSignatureStringCRLF ( ) throws Exception {
assertGpgSignatureStringOutcome ( SIGNATURE . replaceAll ( "\n" , "\r\n" ) ,
EXPECTED ) ;
}
@Test
public void writeGpgSignatureStringTrailingCRLF ( ) throws Exception {
assertGpgSignatureStringOutcome (
SIGNATURE . replaceAll ( "\n" , "\r\n" ) + "\r\n" , EXPECTED ) ;
}
}
@Test
@Test
public void writeGpgSignatureString_failsForNonAscii ( ) throws Exception {
public void writeGpgSignatureString_failsForNonAscii ( ) throws Exception {
String signature = "Ü Ä" ;
String signature = "Ü Ä" ;
try {
IllegalArgumentException e = assertThrows (
CommitBuilder . writeGpgSignatureString ( signature ,
IllegalArgumentException . class ,
new ByteArrayOutputStream ( ) ) ;
( ) - > CommitBuilder . writeGpgSignatureString ( signature ,
fail ( "Exception expected" ) ;
new ByteArrayOutputStream ( ) ) ) ;
} catch ( IllegalArgumentException e ) {
// good
String message = MessageFormat . format ( JGitText . get ( ) . notASCIIString ,
String message = MessageFormat . format ( JGitText . get ( ) . notASCIIString ,
signature ) ;
signature ) ;
assertEquals ( message , e . getMessage ( ) ) ;
assertEquals ( message , e . getMessage ( ) ) ;
}
}
}
@Test
@Test
public void writeGpgSignatureString_oneLineNotModified ( ) throws Exception {
public void writeGpgSignatureString_oneLineNotModified ( ) throws Exception {