Skip to content

Commit

Permalink
Change Test annotation with exceptions to assertThatThrownBy
Browse files Browse the repository at this point in the history
  • Loading branch information
Damans227 authored and ebyhr committed Oct 13, 2021
1 parent 62bd60f commit 6f62a18
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ public void testResumeFromPreviousPosition()
}
}

@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "No more buffers already set")
@Test
public void testUseUndeclaredBufferAfterFinalBuffersSet()
{
ArbitraryOutputBuffer buffer = createArbitraryBuffer(
Expand All @@ -475,7 +475,9 @@ public void testUseUndeclaredBufferAfterFinalBuffersSet()
assertFalse(buffer.isFinished());

// get a page from a buffer that was not declared, which will fail
buffer.get(SECOND, 0L, sizeOfPages(1));
assertThatThrownBy(() -> buffer.get(SECOND, 0L, sizeOfPages(1)))
.isInstanceOf(IllegalStateException.class)
.hasMessage("No more buffers already set");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ public void testGetBeforeCreate()
assertBufferResultEquals(TYPES, getFuture(future, NO_WAIT), bufferResult(0, createPage(33)));
}

@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = ".*does not contain.*\\[0]")
@Test
public void testSetFinalBuffersWihtoutDeclaringUsedBuffer()
{
BroadcastOutputBuffer buffer = createBroadcastBuffer(createInitialEmptyOutputBuffers(BROADCAST), sizeOfPages(10));
Expand All @@ -518,10 +518,12 @@ public void testSetFinalBuffersWihtoutDeclaringUsedBuffer()
buffer.abort(FIRST);

// set final buffers to a set that does not contain the buffer, which will fail
buffer.setOutputBuffers(createInitialEmptyOutputBuffers(BROADCAST).withNoMoreBufferIds());
assertThatThrownBy(() -> buffer.setOutputBuffers(createInitialEmptyOutputBuffers(BROADCAST).withNoMoreBufferIds()))
.isInstanceOf(IllegalStateException.class)
.hasMessageMatching(".*does not contain.*\\[0]");
}

@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "No more buffers already set")
@Test
public void testUseUndeclaredBufferAfterFinalBuffersSet()
{
BroadcastOutputBuffer buffer = createBroadcastBuffer(
Expand All @@ -532,7 +534,9 @@ public void testUseUndeclaredBufferAfterFinalBuffersSet()
assertFalse(buffer.isFinished());

// get a page from a buffer that was not declared, which will fail
buffer.get(SECOND, 0L, sizeOfPages(1));
assertThatThrownBy(() -> buffer.get(SECOND, 0L, sizeOfPages(1)))
.isInstanceOf(IllegalStateException.class)
.hasMessage("No more buffers already set");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,13 @@ public class TestAccessControlManager
private static final String USER_NAME = "user_name";
private static final QueryId queryId = new QueryId("query_id");

@Test(expectedExceptions = TrinoException.class, expectedExceptionsMessageRegExp = "Trino server is still initializing")
@Test
public void testInitializing()
{
AccessControlManager accessControlManager = createAccessControlManager(createTestTransactionManager());
accessControlManager.checkCanSetUser(Optional.empty(), "foo");
assertThatThrownBy(() -> accessControlManager.checkCanSetUser(Optional.empty(), "foo"))
.isInstanceOf(TrinoException.class)
.hasMessage("Trino server is still initializing");
}

@Test
Expand Down Expand Up @@ -169,7 +171,7 @@ public void testNoCatalogAccessControl()
});
}

@Test(expectedExceptions = TrinoException.class, expectedExceptionsMessageRegExp = "Access Denied: Cannot select from columns \\[column\\] in table or view schema.table")
@Test
public void testDenyCatalogAccessControl()
{
CatalogManager catalogManager = new CatalogManager();
Expand All @@ -183,10 +185,12 @@ public void testDenyCatalogAccessControl()
CatalogName catalogName = registerBogusConnector(catalogManager, transactionManager, accessControlManager, "catalog");
accessControlManager.addCatalogAccessControl(catalogName, new DenyConnectorAccessControl());

transaction(transactionManager, accessControlManager)
assertThatThrownBy(() -> transaction(transactionManager, accessControlManager)
.execute(transactionId -> {
accessControlManager.checkCanSelectFromColumns(context(transactionId), new QualifiedObjectName("catalog", "schema", "table"), ImmutableSet.of("column"));
});
}))
.isInstanceOf(TrinoException.class)
.hasMessageMatching("Access Denied: Cannot select from columns \\[column\\] in table or view schema.table");
}

@Test
Expand Down Expand Up @@ -253,7 +257,7 @@ private static SecurityContext context(TransactionId transactionId)
return new SecurityContext(transactionId, identity, queryId);
}

@Test(expectedExceptions = TrinoException.class, expectedExceptionsMessageRegExp = "Access Denied: Cannot select from table secured_catalog.schema.table")
@Test
public void testDenySystemAccessControl()
{
CatalogManager catalogManager = new CatalogManager();
Expand All @@ -267,10 +271,12 @@ public void testDenySystemAccessControl()
registerBogusConnector(catalogManager, transactionManager, accessControlManager, "connector");
accessControlManager.addCatalogAccessControl(new CatalogName("connector"), new DenyConnectorAccessControl());

transaction(transactionManager, accessControlManager)
assertThatThrownBy(() -> transaction(transactionManager, accessControlManager)
.execute(transactionId -> {
accessControlManager.checkCanSelectFromColumns(context(transactionId), new QualifiedObjectName("secured_catalog", "schema", "table"), ImmutableSet.of("column"));
});
}))
.isInstanceOf(TrinoException.class)
.hasMessageMatching("Access Denied: Cannot select from table secured_catalog.schema.table");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void testClientCapabilities()
assertEquals(context2.getClientCapabilities(), ImmutableSet.of());
}

@Test(expectedExceptions = TrinoException.class)
@Test
public void testInvalidTimeZone()
{
MultivaluedMap<String, String> headers = new GuavaMultivaluedMap<>(ImmutableListMultimap.<String, String>builder()
Expand All @@ -141,7 +141,9 @@ public void testInvalidTimeZone()
.build());
SessionContext context = SESSION_CONTEXT_FACTORY.createSessionContext(headers, Optional.empty(), Optional.of("remoteAddress"), Optional.empty());
QuerySessionSupplier sessionSupplier = createSessionSupplier(new SqlEnvironmentConfig());
sessionSupplier.createSession(new QueryId("test_query_id"), context);
assertThatThrownBy(() -> sessionSupplier.createSession(new QueryId("test_query_id"), context))
.isInstanceOf(TrinoException.class)
.hasMessage("Time zone not supported: unknown_timezone");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.testng.annotations.Test;

import static io.airlift.units.DataSize.Unit.MEGABYTE;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.testng.Assert.assertEquals;

@Test(singleThreaded = true)
Expand Down Expand Up @@ -57,18 +58,22 @@ public void testSpillSpaceTracker()
assertEquals(spillSpaceTracker.getCurrentBytes(), 0);
}

@Test(expectedExceptions = ExceededSpillLimitException.class)
@Test
public void testSpillOutOfSpace()
{
assertEquals(spillSpaceTracker.getCurrentBytes(), 0);
spillSpaceTracker.reserve(MAX_DATA_SIZE.toBytes() + 1);
assertThatThrownBy(() -> spillSpaceTracker.reserve(MAX_DATA_SIZE.toBytes() + 1))
.isInstanceOf(ExceededSpillLimitException.class)
.hasMessageMatching("Query exceeded local spill limit of.*");
}

@Test(expectedExceptions = IllegalArgumentException.class)
@Test
public void testFreeToMuch()
{
assertEquals(spillSpaceTracker.getCurrentBytes(), 0);
spillSpaceTracker.reserve(1000);
spillSpaceTracker.free(1001);
assertThatThrownBy(() -> spillSpaceTracker.free(1001))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("tried to free more disk space than is reserved");
}
}

0 comments on commit 6f62a18

Please sign in to comment.