From 8319e8ab7d0618ecb6b84ba04e8ade705b267d1b Mon Sep 17 00:00:00 2001 From: goflutterjava <95280282+goflutterjava@users.noreply.github.com> Date: Tue, 4 Jan 2022 14:05:18 +0800 Subject: [PATCH] Enable CheckStyle Plugin in Pulsar Test Mocks (#13581) --- testmocks/pom.xml | 18 +++++++++ .../client/PulsarMockBookKeeper.java | 22 +++++----- .../client/PulsarMockLedgerHandle.java | 40 +++++++++---------- .../client/PulsarMockReadHandle.java | 3 +- .../bookkeeper/client/package-info.java | 19 +++++++++ .../zookeeper/MockZooKeeperSession.java | 6 +-- .../org/apache/zookeeper/package-info.java | 19 +++++++++ 7 files changed, 91 insertions(+), 36 deletions(-) create mode 100644 testmocks/src/main/java/org/apache/bookkeeper/client/package-info.java create mode 100644 testmocks/src/main/java/org/apache/zookeeper/package-info.java diff --git a/testmocks/pom.xml b/testmocks/pom.xml index 2dea92fa863a5..c7e2100702283 100644 --- a/testmocks/pom.xml +++ b/testmocks/pom.xml @@ -65,4 +65,22 @@ + + + + org.apache.maven.plugins + maven-checkstyle-plugin + + + checkstyle + verify + + check + + + + + + + diff --git a/testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockBookKeeper.java b/testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockBookKeeper.java index f83e1c73a06cf..50df4b6b0df0f 100644 --- a/testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockBookKeeper.java +++ b/testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockBookKeeper.java @@ -75,8 +75,8 @@ public ClientConfiguration getConf() { return super.getConf(); } - Map ledgers = new ConcurrentHashMap<>(); - AtomicLong sequence = new AtomicLong(3); + final Map ledgers = new ConcurrentHashMap<>(); + final AtomicLong sequence = new AtomicLong(3); CompletableFuture defaultResponse = CompletableFuture.completedFuture(null); private static final List ensemble = Collections.unmodifiableList(Lists.newArrayList( @@ -88,8 +88,8 @@ public static Collection getMockEnsemble() { return ensemble; } - Queue addEntryDelaysMillis = new ConcurrentLinkedQueue<>(); - List> failures = new ArrayList<>(); + final Queue addEntryDelaysMillis = new ConcurrentLinkedQueue<>(); + final List> failures = new ArrayList<>(); public PulsarMockBookKeeper(OrderedExecutor orderedExecutor) throws Exception { this.orderedExecutor = orderedExecutor; @@ -120,7 +120,8 @@ public void asyncCreateLedger(int ensSize, int writeQuorumSize, int ackQuorumSiz try { long id = sequence.getAndIncrement(); log.info("Creating ledger {}", id); - PulsarMockLedgerHandle lh = new PulsarMockLedgerHandle(PulsarMockBookKeeper.this, id, digestType, passwd); + PulsarMockLedgerHandle lh = + new PulsarMockLedgerHandle(PulsarMockBookKeeper.this, id, digestType, passwd); ledgers.put(id, lh); return FutureUtils.value(lh); } catch (Throwable t) { @@ -303,7 +304,7 @@ void checkProgrammedFail() throws BKException, InterruptedException { getProgrammedFailure().get(); } catch (ExecutionException ee) { if (ee.getCause() instanceof BKException) { - throw (BKException)ee.getCause(); + throw (BKException) ee.getCause(); } else { throw new BKException.BKUnexpectedConditionException(); } @@ -331,7 +332,7 @@ public void failAfter(int steps, int rc) { private int emptyLedgerAfter = -1; /** - * After N times, make a ledger to appear to be empty + * After N times, make a ledger to appear to be empty. */ public synchronized void returnEmptyLedgerAfter(int steps) { emptyLedgerAfter = steps; @@ -373,12 +374,12 @@ public CompletableFuture>> getWritableBookies() { @Override public CompletableFuture>> getAllBookies() { - return CompletableFuture.completedFuture(new Versioned>(new HashSet<>(ensemble), new LongVersion(0))); + return CompletableFuture.completedFuture(new Versioned<>(new HashSet<>(ensemble), new LongVersion(0))); } @Override public CompletableFuture>> getReadOnlyBookies() { - return CompletableFuture.completedFuture(new Versioned>(new HashSet<>(), new LongVersion(0))); + return CompletableFuture.completedFuture(new Versioned<>(new HashSet<>(), new LongVersion(0))); } @Override @@ -404,7 +405,8 @@ public void unwatchReadOnlyBookies(RegistrationListener listener) { private final MetadataClientDriver metadataClientDriver = new MetadataClientDriver() { @Override - public MetadataClientDriver initialize(ClientConfiguration conf, ScheduledExecutorService scheduler, StatsLogger statsLogger, Optional ctx) throws MetadataException { + public MetadataClientDriver initialize(ClientConfiguration conf, ScheduledExecutorService scheduler, + StatsLogger statsLogger, Optional ctx) throws MetadataException { return this; } diff --git a/testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockLedgerHandle.java b/testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockLedgerHandle.java index 197d13a3c8393..7378a6f91063d 100644 --- a/testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockLedgerHandle.java +++ b/testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockLedgerHandle.java @@ -19,10 +19,8 @@ package org.apache.bookkeeper.client; import com.google.common.collect.Lists; - import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; - import java.security.GeneralSecurityException; import java.util.ArrayDeque; import java.util.ArrayList; @@ -31,7 +29,6 @@ import java.util.List; import java.util.Queue; import java.util.concurrent.CompletableFuture; - import org.apache.bookkeeper.client.AsyncCallback.AddCallback; import org.apache.bookkeeper.client.AsyncCallback.CloseCallback; import org.apache.bookkeeper.client.AsyncCallback.ReadCallback; @@ -78,23 +75,23 @@ public PulsarMockLedgerHandle(PulsarMockBookKeeper bk, long id, @Override public void asyncClose(CloseCallback cb, Object ctx) { bk.getProgrammedFailure().thenComposeAsync((res) -> { - fenced = true; - - Versioned current = getVersionedLedgerMetadata(); - Versioned newMetadata = new Versioned<>( - LedgerMetadataBuilder.from(current.getValue()) - .withClosedState().withLastEntryId(getLastAddConfirmed()) - .withLength(getLength()).build(), - new LongVersion(((LongVersion)current.getVersion()).getLongVersion() + 1)); - setLedgerMetadata(current, newMetadata); - return FutureUtils.value(null); - }, bk.executor).whenCompleteAsync((res, exception) -> { - if (exception != null) { - cb.closeComplete(PulsarMockBookKeeper.getExceptionCode(exception), null, ctx); - } else { - cb.closeComplete(BKException.Code.OK, this, ctx); - } - }, bk.executor); + fenced = true; + + Versioned current = getVersionedLedgerMetadata(); + Versioned newMetadata = new Versioned<>( + LedgerMetadataBuilder.from(current.getValue()) + .withClosedState().withLastEntryId(getLastAddConfirmed()) + .withLength(getLength()).build(), + new LongVersion(((LongVersion) current.getVersion()).getLongVersion() + 1)); + setLedgerMetadata(current, newMetadata); + return FutureUtils.value(null); + }, bk.executor).whenCompleteAsync((res, exception) -> { + if (exception != null) { + cb.closeComplete(PulsarMockBookKeeper.getExceptionCode(exception), null, ctx); + } else { + cb.closeComplete(BKException.Code.OK, this, ctx); + } + }, bk.executor); } @Override @@ -128,7 +125,8 @@ public LedgerEntry nextElement() { return FutureUtils.value(entries); }).whenCompleteAsync((res, exception) -> { if (exception != null) { - cb.readComplete(PulsarMockBookKeeper.getExceptionCode(exception), PulsarMockLedgerHandle.this, null, ctx); + cb.readComplete(PulsarMockBookKeeper.getExceptionCode(exception), + PulsarMockLedgerHandle.this, null, ctx); } else { cb.readComplete(BKException.Code.OK, PulsarMockLedgerHandle.this, res, ctx); } diff --git a/testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockReadHandle.java b/testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockReadHandle.java index 745857389a3df..51dedde5b54c7 100644 --- a/testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockReadHandle.java +++ b/testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockReadHandle.java @@ -41,7 +41,8 @@ class PulsarMockReadHandle implements ReadHandle { private final LedgerMetadata metadata; private final List entries; - PulsarMockReadHandle(PulsarMockBookKeeper bk, long ledgerId, LedgerMetadata metadata, List entries) { + PulsarMockReadHandle(PulsarMockBookKeeper bk, long ledgerId, LedgerMetadata metadata, + List entries) { this.bk = bk; this.ledgerId = ledgerId; this.metadata = metadata; diff --git a/testmocks/src/main/java/org/apache/bookkeeper/client/package-info.java b/testmocks/src/main/java/org/apache/bookkeeper/client/package-info.java new file mode 100644 index 0000000000000..878b83520d01e --- /dev/null +++ b/testmocks/src/main/java/org/apache/bookkeeper/client/package-info.java @@ -0,0 +1,19 @@ +/** + * 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 + * + * 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 org.apache.bookkeeper.client; diff --git a/testmocks/src/main/java/org/apache/zookeeper/MockZooKeeperSession.java b/testmocks/src/main/java/org/apache/zookeeper/MockZooKeeperSession.java index a33d4483c89f3..de1fbdc1dcb55 100644 --- a/testmocks/src/main/java/org/apache/zookeeper/MockZooKeeperSession.java +++ b/testmocks/src/main/java/org/apache/zookeeper/MockZooKeeperSession.java @@ -34,7 +34,7 @@ import org.objenesis.instantiator.ObjectInstantiator; /** - * mock zookeeper with different session based on {@link MockZooKeeper} + * mock zookeeper with different session based on {@link MockZooKeeper}. */ public class MockZooKeeperSession extends ZooKeeper { @@ -250,8 +250,6 @@ public long getSessionId() { @Override public String toString() { - return "MockZooKeeperSession{" + - "sessionId=" + sessionId + - '}'; + return "MockZooKeeperSession{" + "sessionId=" + sessionId + '}'; } } diff --git a/testmocks/src/main/java/org/apache/zookeeper/package-info.java b/testmocks/src/main/java/org/apache/zookeeper/package-info.java new file mode 100644 index 0000000000000..8c313f51d3a1c --- /dev/null +++ b/testmocks/src/main/java/org/apache/zookeeper/package-info.java @@ -0,0 +1,19 @@ +/** + * 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 + * + * 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 org.apache.zookeeper;