From 74bc0fc87005bb2971da0957c287729f6806d384 Mon Sep 17 00:00:00 2001
From: Yannick Lecaillez <yannick.lecaillez@forgerock.com>
Date: Mon, 18 May 2015 13:51:11 +0000
Subject: [PATCH] Code cleanup
---
opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/ControlsTestCase.java | 6
opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/StateTest.java | 8
opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/ID2CountTest.java | 7
opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/EntryIDSetTest.java | 66 ++++++------
opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/Utils.java | 6 +
opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/PluggableBackendImplTestCase.java | 26 +---
opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/TestDnKeyFormat.java | 160 +++++++++++++++----------------
7 files changed, 135 insertions(+), 144 deletions(-)
diff --git a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/ControlsTestCase.java b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/ControlsTestCase.java
index 52d2db4..deedfd4 100644
--- a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/ControlsTestCase.java
+++ b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/ControlsTestCase.java
@@ -598,7 +598,7 @@
private ArrayList<DN> getDNs(final LinkedList<SearchResultEntry> entries)
{
- final ArrayList<DN> results = new ArrayList<DN>();
+ final ArrayList<DN> results = new ArrayList<>();
for (final Entry e : entries)
{
results.add(e.getName());
@@ -608,7 +608,7 @@
private List<DN> getDNs(List<Integer> expectedOrder) throws Exception
{
- List<DN> dns = new ArrayList<DN>(expectedOrder.size());
+ List<DN> dns = new ArrayList<>(expectedOrder.size());
for (int i : expectedOrder)
{
dns.add(USERS[i].toDN());
@@ -739,7 +739,7 @@
private Entry toEntry() throws Exception
{
- final List<String> ldif = new ArrayList<String>();
+ final List<String> ldif = new ArrayList<>();
ldif.add("dn: " + toDN());
ldif.add("objectClass: top");
ldif.add("objectClass: person");
diff --git a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/EntryIDSetTest.java b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/EntryIDSetTest.java
index a02bffe..2f3d64f 100644
--- a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/EntryIDSetTest.java
+++ b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/EntryIDSetTest.java
@@ -50,25 +50,25 @@
@Test
public void testDefinedAdd()
{
- EntryIDSet set = newDefinedSet(6, 8, 10, 12);
+ final EntryIDSet set = newDefinedSet(6, 8, 10, 12);
- assertThat(set.add(new EntryID(4))).isTrue();
- assertIdsEquals(set, 4L, 6, 8, 10, 12);
+ assertThat(set.add(id(4))).isTrue();
+ assertIdsEquals(set, 4, 6, 8, 10, 12);
- assertThat(set.add(new EntryID(14L))).isTrue();
- assertIdsEquals(set, 4L, 6, 8, 10, 12, 14L);
+ assertThat(set.add(id(14))).isTrue();
+ assertIdsEquals(set, 4, 6, 8, 10, 12, 14);
- assertThat(set.add(new EntryID(11))).isTrue();
- assertIdsEquals(set, 4L, 6, 8, 10, 11, 12, 14L);
+ assertThat(set.add(id(11))).isTrue();
+ assertIdsEquals(set, 4, 6, 8, 10, 11, 12, 14);
- assertThat(set.add(new EntryID(10))).isFalse();
- assertIdsEquals(set, 4L, 6, 8, 10, 11, 12, 14);
+ assertThat(set.add(id(10))).isFalse();
+ assertIdsEquals(set, 4, 6, 8, 10, 11, 12, 14);
}
@Test
public void testDefinedAddAll()
{
- EntryIDSet set = newDefinedSet(10, 12);
+ final EntryIDSet set = newDefinedSet(10, 12);
// Add nothing
set.addAll(newDefinedSet());
@@ -98,25 +98,25 @@
@Test
public void testDefinedRemove()
{
- EntryIDSet set = newDefinedSet(4, 6, 8, 10, 12, 14);
+ final EntryIDSet set = newDefinedSet(4, 6, 8, 10, 12, 14);
- assertThat(set.remove(new EntryID(4))).isTrue();
+ assertThat(set.remove(id(4))).isTrue();
assertIdsEquals(set, 6, 8, 10, 12, 14);
- assertThat(set.remove(new EntryID(14))).isTrue();
+ assertThat(set.remove(id(14))).isTrue();
assertIdsEquals(set, 6, 8, 10, 12);
- assertThat(set.remove(new EntryID(10))).isTrue();
+ assertThat(set.remove(id(10))).isTrue();
assertIdsEquals(set, 6, 8, 12);
- assertThat(set.remove(new EntryID(10))).isFalse();
+ assertThat(set.remove(id(10))).isFalse();
assertIdsEquals(set, 6, 8, 12);
}
@Test
public void testDefinedRemoveAll()
{
- EntryIDSet set = newDefinedSet(1, 2, 4, 6, 8, 9, 10, 12, 13, 14, 16, 18, 20, 21);
+ final EntryIDSet set = newDefinedSet(1, 2, 4, 6, 8, 9, 10, 12, 13, 14, 16, 18, 20, 21);
// Remove nothing
set.removeAll(newDefinedSet());
@@ -142,33 +142,33 @@
@Test
public void testDefinedContain()
{
- EntryIDSet set = newDefinedSet(4, 6, 8, 10, 12, 14);
+ final EntryIDSet set = newDefinedSet(4, 6, 8, 10, 12, 14);
- assertThat(set.contains(new EntryID(2))).isFalse();
- assertThat(set.contains(new EntryID(4))).isTrue();
+ assertThat(set.contains(id(2))).isFalse();
+ assertThat(set.contains(id(4))).isTrue();
- assertThat(set.contains(new EntryID(9))).isFalse();
- assertThat(set.contains(new EntryID(10))).isTrue();
+ assertThat(set.contains(id(9))).isFalse();
+ assertThat(set.contains(id(10))).isTrue();
- assertThat(set.contains(new EntryID(14))).isTrue();
- assertThat(set.contains(new EntryID(16))).isFalse();
+ assertThat(set.contains(id(14))).isTrue();
+ assertThat(set.contains(id(16))).isFalse();
}
@Test
public void testDefinedIterator()
{
- assertIdsEquals(newDefinedSet(4, 6, 8, 10, 12).iterator(), 4L, 6L, 8L, 10L, 12L);
+ assertIdsEquals(newDefinedSet(4, 6, 8, 10, 12).iterator(), 4, 6, 8, 10, 12);
}
@Test
public void testDefinedIteratorWithBegin()
{
- EntryIDSet set = newDefinedSet(4, 6, 8, 10, 12);
+ final EntryIDSet set = newDefinedSet(4, 6, 8, 10, 12);
- assertIdsEquals(set.iterator(new EntryID(4)), 4L, 6L, 8L, 10L, 12L);
- assertIdsEquals(set.iterator(new EntryID(8)), 8L, 10L, 12L);
- assertIdsEquals(set.iterator(new EntryID(12)), 12L);
- assertIdsEquals(set.iterator(new EntryID(13)), 4L, 6L, 8L, 10L, 12L);
+ assertIdsEquals(set.iterator(id(4)), 4, 6, 8, 10, 12);
+ assertIdsEquals(set.iterator(id(8)), 8, 10, 12);
+ assertIdsEquals(set.iterator(id(12)), 12);
+ assertIdsEquals(set.iterator(id(13)), 4, 6, 8, 10, 12);
}
@Test(dataProvider = "codecs")
@@ -208,7 +208,7 @@
public void testUndefinedAddDoesNothing()
{
final EntryIDSet undefined = newUndefinedSet();
- assertThat(undefined.add(new EntryID(4))).isTrue();
+ assertThat(undefined.add(id(4))).isTrue();
assertThat(undefined.size()).isEqualTo(Long.MAX_VALUE);
}
@@ -228,7 +228,7 @@
public void testUndefinedRemoveDoesNothing()
{
final EntryIDSet undefined = newUndefinedSet();
- assertThat(undefined.remove(new EntryID(4))).isTrue();
+ assertThat(undefined.remove(id(4))).isTrue();
assertThat(undefined.size()).isEqualTo(Long.MAX_VALUE);
}
@@ -243,7 +243,7 @@
@Test
public void testUndefinedContain()
{
- assertThat(newUndefinedSet().contains(new EntryID(4))).isTrue();
+ assertThat(newUndefinedSet().contains(id(4))).isTrue();
}
@Test
@@ -255,7 +255,7 @@
@Test
public void testUndefinedIteratorWithBegin()
{
- assertThat(newUndefinedSet().iterator(new EntryID(8)).hasNext()).isFalse();
+ assertThat(newUndefinedSet().iterator(id(8)).hasNext()).isFalse();
}
@Test
diff --git a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/ID2CountTest.java b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/ID2CountTest.java
index 827ea14..7165394 100644
--- a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/ID2CountTest.java
+++ b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/ID2CountTest.java
@@ -35,7 +35,6 @@
import java.util.concurrent.TimeUnit;
import org.forgerock.opendj.config.server.ConfigException;
-import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.util.promise.NeverThrowsException;
import org.forgerock.util.promise.PromiseImpl;
import org.opends.server.DirectoryServerTestCase;
@@ -87,9 +86,9 @@
when(serverContext.getDiskSpaceMonitor()).thenReturn(mock(DiskSpaceMonitor.class));
storage = new PersistItStorage(createBackendCfg(), serverContext);
- org.opends.server.backends.pluggable.spi.Importer importer = storage.startImport();
- importer.createTree(id2CountTreeName);
- importer.close();
+ try(final org.opends.server.backends.pluggable.spi.Importer importer = storage.startImport()) {
+ importer.createTree(id2CountTreeName);
+ }
storage.open();
diff --git a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/PluggableBackendImplTestCase.java b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/PluggableBackendImplTestCase.java
index d8d04c9..61c3dc6 100644
--- a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/PluggableBackendImplTestCase.java
+++ b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/PluggableBackendImplTestCase.java
@@ -750,7 +750,7 @@
private List<Entry> getDbEntries(List<Entry> entries) throws DirectoryException
{
- List<Entry> result = new ArrayList<Entry>(entries.size());
+ List<Entry> result = new ArrayList<>(entries.size());
for (Entry currentEntry : entries)
{
Entry dbEntry = backend.getEntry(currentEntry.getName());
@@ -840,16 +840,13 @@
ByteArrayOutputStream rejectedEntries = new ByteArrayOutputStream();
- LDIFImportConfig importConf = new LDIFImportConfig(templateFile);
- importConf.setInvokeImportPlugins(true);
- importConf.setClearBackend(true);
- importConf.writeRejectedEntries(rejectedEntries);
- try {
+ try (final LDIFImportConfig importConf = new LDIFImportConfig(templateFile))
+ {
+ importConf.setInvokeImportPlugins(true);
+ importConf.setClearBackend(true);
+ importConf.writeRejectedEntries(rejectedEntries);
backend.importLDIF(importConf, DirectoryServer.getInstance().getServerContext());
}
- finally {
- importConf.close();
- }
assertEquals(rejectedEntries.size(), 0, "No entries should be rejected");
backend.openBackend();
@@ -887,17 +884,12 @@
assertTrue(backend.supports(BackendOperation.LDIF_EXPORT), "Export not supported");
ByteArrayOutputStream ldifData = new ByteArrayOutputStream();
- LDIFExportConfig exportConfig = new LDIFExportConfig(ldifData);
- exportConfig.setIncludeOperationalAttributes(true);
- exportConfig.setIncludeVirtualAttributes(true);
- try
+ try (final LDIFExportConfig exportConfig = new LDIFExportConfig(ldifData))
{
+ exportConfig.setIncludeOperationalAttributes(true);
+ exportConfig.setIncludeVirtualAttributes(true);
backend.exportLDIF(exportConfig);
}
- finally
- {
- exportConfig.close();
- }
String ldifString = ldifData.toString();
assertEquals(ldifString.contains(testBaseDN.toString()), true, "Export without rootDN");
diff --git a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/StateTest.java b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/StateTest.java
index 345194e..935ee4e 100644
--- a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/StateTest.java
+++ b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/StateTest.java
@@ -86,10 +86,10 @@
when(serverContext.getDiskSpaceMonitor()).thenReturn(mock(DiskSpaceMonitor.class));
storage = new PersistItStorage(createBackendCfg(), serverContext);
- org.opends.server.backends.pluggable.spi.Importer importer = storage.startImport();
- importer.createTree(stateTreeName);
- importer.close();
-
+ try(final org.opends.server.backends.pluggable.spi.Importer importer = storage.startImport()) {
+ importer.createTree(stateTreeName);
+ }
+
storage.open();
state = new State(stateTreeName);
diff --git a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/TestDnKeyFormat.java b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/TestDnKeyFormat.java
index 86d474b..75456ef 100644
--- a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/TestDnKeyFormat.java
+++ b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/TestDnKeyFormat.java
@@ -361,61 +361,60 @@
// Convert the test LDIF string to a byte array
byte[] originalLDIFBytes = StaticUtils.getBytes(ldifString);
- LDIFReader reader = new LDIFReader(new LDIFImportConfig(
- new ByteArrayInputStream(originalLDIFBytes)));
+ try (final LDIFReader reader = new LDIFReader(new LDIFImportConfig(new ByteArrayInputStream(originalLDIFBytes))))
+ {
+ Entry entryBefore, entryAfter;
+ while ((entryBefore = reader.readEntry(false)) != null) {
+ ByteString bytes = ID2Entry.entryToDatabase(entryBefore,
+ new DataConfig(false, false, null));
- Entry entryBefore, entryAfter;
- while ((entryBefore = reader.readEntry(false)) != null) {
- ByteString bytes = ID2Entry.entryToDatabase(entryBefore,
- new DataConfig(false, false, null));
+ entryAfter = ID2Entry.entryFromDatabase(bytes,
+ DirectoryServer.getDefaultCompressedSchema());
- entryAfter = ID2Entry.entryFromDatabase(bytes,
- DirectoryServer.getDefaultCompressedSchema());
+ // check DN and number of attributes
+ assertEquals(entryBefore.getAttributes().size(), entryAfter
+ .getAttributes().size());
- // check DN and number of attributes
- assertEquals(entryBefore.getAttributes().size(), entryAfter
- .getAttributes().size());
+ assertEquals(entryBefore.getName(), entryAfter.getName());
- assertEquals(entryBefore.getName(), entryAfter.getName());
-
- // check the object classes were not changed
- for (String ocBefore : entryBefore.getObjectClasses().values()) {
- ObjectClass objectClass = DirectoryServer.getObjectClass(ocBefore
- .toLowerCase());
- if (objectClass == null) {
- objectClass = DirectoryServer.getDefaultObjectClass(ocBefore);
- }
- String ocAfter = entryAfter.getObjectClasses().get(objectClass);
-
- assertEquals(ocBefore, ocAfter);
- }
-
- // check the user attributes were not changed
- for (AttributeType attrType : entryBefore.getUserAttributes()
- .keySet()) {
- List<Attribute> listBefore = entryBefore.getAttribute(attrType);
- List<Attribute> listAfter = entryAfter.getAttribute(attrType);
-
- assertNotNull(listAfter);
- assertEquals(listBefore.size(), listAfter.size());
-
- for (Attribute attrBefore : listBefore) {
- boolean found = false;
-
- for (Attribute attrAfter : listAfter) {
- if (attrAfter.optionsEqual(attrBefore.getOptions())) {
- // Found the corresponding attribute
-
- assertEquals(attrBefore, attrAfter);
- found = true;
- }
+ // check the object classes were not changed
+ for (String ocBefore : entryBefore.getObjectClasses().values()) {
+ ObjectClass objectClass = DirectoryServer.getObjectClass(ocBefore
+ .toLowerCase());
+ if (objectClass == null) {
+ objectClass = DirectoryServer.getDefaultObjectClass(ocBefore);
}
+ String ocAfter = entryAfter.getObjectClasses().get(objectClass);
- assertTrue(found);
+ assertEquals(ocBefore, ocAfter);
+ }
+
+ // check the user attributes were not changed
+ for (AttributeType attrType : entryBefore.getUserAttributes()
+ .keySet()) {
+ List<Attribute> listBefore = entryBefore.getAttribute(attrType);
+ List<Attribute> listAfter = entryAfter.getAttribute(attrType);
+
+ assertNotNull(listAfter);
+ assertEquals(listBefore.size(), listAfter.size());
+
+ for (Attribute attrBefore : listBefore) {
+ boolean found = false;
+
+ for (Attribute attrAfter : listAfter) {
+ if (attrAfter.optionsEqual(attrBefore.getOptions())) {
+ // Found the corresponding attribute
+
+ assertEquals(attrBefore, attrAfter);
+ found = true;
+ }
+ }
+
+ assertTrue(found);
+ }
}
}
}
- reader.close();
}
/**
@@ -431,18 +430,17 @@
// Convert the test LDIF string to a byte array
byte[] originalLDIFBytes = StaticUtils.getBytes(ldifString);
- LDIFReader reader = new LDIFReader(new LDIFImportConfig(
- new ByteArrayInputStream(originalLDIFBytes)));
+ try (final LDIFReader reader = new LDIFReader(new LDIFImportConfig(new ByteArrayInputStream(originalLDIFBytes))))
+ {
+ Entry entryBefore, entryAfterV1;
+ while ((entryBefore = reader.readEntry(false)) != null) {
+ ByteStringBuilder bsb = new ByteStringBuilder();
+ encodeV1(entryBefore, bsb);
+ entryAfterV1 = Entry.decode(bsb.asReader());
- Entry entryBefore, entryAfterV1;
- while ((entryBefore = reader.readEntry(false)) != null) {
- ByteStringBuilder bsb = new ByteStringBuilder();
- encodeV1(entryBefore, bsb);
- entryAfterV1 = Entry.decode(bsb.asReader());
-
- assertEquals(entryBefore, entryAfterV1);
+ assertEquals(entryBefore, entryAfterV1);
+ }
}
- reader.close();
}
/**
@@ -480,21 +478,20 @@
// Convert the test LDIF string to a byte array
byte[] originalLDIFBytes = StaticUtils.getBytes(ldifString);
- LDIFReader reader = new LDIFReader(new LDIFImportConfig(
- new ByteArrayInputStream(originalLDIFBytes)));
-
- Entry entryBefore, entryAfterV2;
- while ((entryBefore = reader.readEntry(false)) != null) {
- ByteStringBuilder bsb = new ByteStringBuilder();
- encodeV2(entryBefore, bsb, config);
- entryAfterV2 = Entry.decode(bsb.asReader());
- if (config.excludeDN())
- {
- entryAfterV2.setDN(entryBefore.getName());
+ try (final LDIFReader reader = new LDIFReader(new LDIFImportConfig(new ByteArrayInputStream(originalLDIFBytes))))
+ {
+ Entry entryBefore, entryAfterV2;
+ while ((entryBefore = reader.readEntry(false)) != null) {
+ ByteStringBuilder bsb = new ByteStringBuilder();
+ encodeV2(entryBefore, bsb, config);
+ entryAfterV2 = Entry.decode(bsb.asReader());
+ if (config.excludeDN())
+ {
+ entryAfterV2.setDN(entryBefore.getName());
+ }
+ assertEquals(entryBefore, entryAfterV2);
}
- assertEquals(entryBefore, entryAfterV2);
}
- reader.close();
}
/**
@@ -511,21 +508,20 @@
// Convert the test LDIF string to a byte array
byte[] originalLDIFBytes = StaticUtils.getBytes(ldifString);
- LDIFReader reader = new LDIFReader(new LDIFImportConfig(
- new ByteArrayInputStream(originalLDIFBytes)));
-
- Entry entryBefore, entryAfterV3;
- while ((entryBefore = reader.readEntry(false)) != null) {
- ByteStringBuilder bsb = new ByteStringBuilder();
- entryBefore.encode(bsb, config);
- entryAfterV3 = Entry.decode(bsb.asReader());
- if (config.excludeDN())
- {
- entryAfterV3.setDN(entryBefore.getName());
+ try (final LDIFReader reader = new LDIFReader(new LDIFImportConfig(new ByteArrayInputStream(originalLDIFBytes))))
+ {
+ Entry entryBefore, entryAfterV3;
+ while ((entryBefore = reader.readEntry(false)) != null) {
+ ByteStringBuilder bsb = new ByteStringBuilder();
+ entryBefore.encode(bsb, config);
+ entryAfterV3 = Entry.decode(bsb.asReader());
+ if (config.excludeDN())
+ {
+ entryAfterV3.setDN(entryBefore.getName());
+ }
+ assertEquals(entryBefore, entryAfterV3);
}
- assertEquals(entryBefore, entryAfterV3);
}
- reader.close();
}
@DataProvider
diff --git a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/Utils.java b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/Utils.java
index 0ea72da..a939ef9 100644
--- a/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/Utils.java
+++ b/opendj-sdk/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/Utils.java
@@ -51,8 +51,12 @@
assertIdsEquals(actual.iterator(), expected);
}
+ public static EntryID id(long id) {
+ return new EntryID(id);
+ }
+
private static List<EntryID> asList(long... array) {
- List<EntryID> list = new ArrayList<EntryID>(array.length);
+ List<EntryID> list = new ArrayList<>(array.length);
for(long l : array) {
list.add(new EntryID(l));
}
--
Gitblit v1.10.0