static final int MDB_CURSOR_FULL = -30_787; static void checkRc(final int rc) {
  switch (rc) {
    case MDB_SUCCESS:
      return;
    case Dbi.BadDbiException.MDB_BAD_DBI:
      throw new Dbi.BadDbiException();
    case BadReaderLockException.MDB_BAD_RSLOT:
      throw new BadReaderLockException();
    case BadException.MDB_BAD_TXN:
      throw new BadException();
    case Dbi.BadValueSizeException.MDB_BAD_VALSIZE:
      throw new Dbi.BadValueSizeException();
    case LmdbNativeException.PageCorruptedException.MDB_CORRUPTED:
      throw new LmdbNativeException.PageCorruptedException();
    case Cursor.FullException.MDB_CURSOR_FULL:
      throw new Cursor.FullException();
    case Dbi.DbFullException.MDB_DBS_FULL:
      throw new Dbi.DbFullException();
    case Dbi.IncompatibleException.MDB_INCOMPATIBLE:
      throw new Dbi.IncompatibleException();
    case Env.FileInvalidException.MDB_INVALID:
      throw new Env.FileInvalidException();
    case Dbi.KeyExistsException.MDB_KEYEXIST:
      throw new Dbi.KeyExistsException();
    case Env.MapFullException.MDB_MAP_FULL:
      throw new Env.MapFullException();
    case Dbi.MapResizedException.MDB_MAP_RESIZED:
      throw new Dbi.MapResizedException();
    case Dbi.KeyNotFoundException.MDB_NOTFOUND:
      throw new Dbi.KeyNotFoundException();
    case LmdbNativeException.PageFullException.MDB_PAGE_FULL:
      throw new LmdbNativeException.PageFullException();
    case LmdbNativeException.PageNotFoundException.MDB_PAGE_NOTFOUND:
      throw new LmdbNativeException.PageNotFoundException();
    case LmdbNativeException.PanicException.MDB_PANIC:
      throw new LmdbNativeException.PanicException();
    case Env.ReadersFullException.MDB_READERS_FULL:
      throw new Env.ReadersFullException();
    case LmdbNativeException.TlsFullException.MDB_TLS_FULL:
      throw new LmdbNativeException.TlsFullException();
    case TxFullException.MDB_TXN_FULL:
      throw new TxFullException();
    case Env.VersionMismatchException.MDB_VERSION_MISMATCH:
      throw new Env.VersionMismatchException();
    default:
      break;
  }

  final Constant constant = CONSTANTS.getConstant(rc);
  if (constant == null) {
    throw new IllegalArgumentException("Unknown result code " + rc);
  }
  final String msg = constant.name() + " " + constant.toString();
  throw new LmdbNativeException.ConstantDerivedException(rc, msg);
} static void invokePrivateConstructor(final Class<?> clazz) {
  try {
    final Constructor<?> c = clazz.getDeclaredConstructor();
    c.setAccessible(true);
    c.newInstance();
  } catch (final NoSuchMethodException | InstantiationException
                     | IllegalAccessException | IllegalArgumentException
                     | InvocationTargetException e) {
    throw new LmdbException("Private construction failed", e);
  }
} public static final class FullException extends LmdbNativeException {

  static final int MDB_CURSOR_FULL = -30_787;
  private static final long serialVersionUID = 1L;

  FullException() {
    super(MDB_CURSOR_FULL, "Cursor stack too deep - internal error");
  }
} public static final class BadDbiException extends LmdbNativeException {

  static final int MDB_BAD_DBI = -30_780;
  private static final long serialVersionUID = 1L;

  BadDbiException() {
    super(MDB_BAD_DBI, "The specified DBI was changed unexpectedly");
  }
} public static final class BadValueSizeException extends LmdbNativeException {

  static final int MDB_BAD_VALSIZE = -30_781;
  private static final long serialVersionUID = 1L;

  BadValueSizeException() {
    super(MDB_BAD_VALSIZE,
          "Unsupported size of key/DB name/data, or wrong DUPFIXED size");
  }
} public static final class DbFullException extends LmdbNativeException {

  static final int MDB_DBS_FULL = -30_791;
  private static final long serialVersionUID = 1L;

  DbFullException() {
    super(MDB_DBS_FULL, "Environment maxdbs reached");
  }
} public static final class IncompatibleException extends LmdbNativeException {

  static final int MDB_INCOMPATIBLE = -30_784;
  private static final long serialVersionUID = 1L;

  IncompatibleException() {
    super(MDB_INCOMPATIBLE,
          "Operation and DB incompatible, or DB type changed");
  }
} public static final class KeyExistsException extends LmdbNativeException {

  static final int MDB_KEYEXIST = -30_799;
  private static final long serialVersionUID = 1L;

  KeyExistsException() {
    super(MDB_KEYEXIST, "key/data pair already exists");
  }
} public static final class KeyNotFoundException extends LmdbNativeException {

  static final int MDB_NOTFOUND = -30_798;
  private static final long serialVersionUID = 1L;

  KeyNotFoundException() {
    super(MDB_NOTFOUND, "key/data pair not found (EOF)");
  }
} public static final class MapResizedException extends LmdbNativeException {

  static final int MDB_MAP_RESIZED = -30_785;
  private static final long serialVersionUID = 1L;

  MapResizedException() {
    super(MDB_MAP_RESIZED, "Database contents grew beyond environment mapsize");
  }
} public static final class FileInvalidException extends LmdbNativeException {

  static final int MDB_INVALID = -30_793;
  private static final long serialVersionUID = 1L;

  FileInvalidException() {
    super(MDB_INVALID, "File is not a valid LMDB file");
  }
} public static final class MapFullException extends LmdbNativeException {

  static final int MDB_MAP_FULL = -30_792;
  private static final long serialVersionUID = 1L;

  MapFullException() {
    super(MDB_MAP_FULL, "Environment mapsize reached");
  }
} public static final class ReadersFullException extends LmdbNativeException {

  static final int MDB_READERS_FULL = -30_790;
  private static final long serialVersionUID = 1L;

  ReadersFullException() {
    super(MDB_READERS_FULL, "Environment maxreaders reached");
  }
} public static final class VersionMismatchException extends LmdbNativeException {

  static final int MDB_VERSION_MISMATCH = -30_794;
  private static final long serialVersionUID = 1L;

  VersionMismatchException() {
    super(MDB_VERSION_MISMATCH, "Environment version mismatch");
  }
} public static final class ConstantDerivedException extends LmdbNativeException {

  private static final long serialVersionUID = 1L;

  ConstantDerivedException(final int rc, final String message) {
    super(rc, "Platform constant error code: " + message);
  }
} public static final class PageCorruptedException extends LmdbNativeException {

  static final int MDB_CORRUPTED = -30_796;
  private static final long serialVersionUID = 1L;

  PageCorruptedException() {
    super(MDB_CORRUPTED, "located page was wrong type");
  }
} public static final class PageFullException extends LmdbNativeException {

  static final int MDB_PAGE_FULL = -30_786;
  private static final long serialVersionUID = 1L;

  PageFullException() {
    super(MDB_PAGE_FULL, "Page has not enough space - internal error");
  }
} public static final class PageNotFoundException extends LmdbNativeException {

  static final int MDB_PAGE_NOTFOUND = -30_797;
  private static final long serialVersionUID = 1L;

  PageNotFoundException() {
    super(MDB_PAGE_NOTFOUND,
          "Requested page not found - this usually indicates corruption");
  }
} public static final class PanicException extends LmdbNativeException {

  static final int MDB_PANIC = -30_795;
  private static final long serialVersionUID = 1L;

  PanicException() {
    super(MDB_PANIC,
          "Update of meta page failed or environment had fatal error");
  }
} public static final class TlsFullException extends LmdbNativeException {

  static final int MDB_TLS_FULL = -30_789;
  private static final long serialVersionUID = 1L;

  TlsFullException() {
    super(MDB_TLS_FULL, "Too many TLS keys in use - Windows only");
  }
} public static final class BadException extends LmdbNativeException {

  static final int MDB_BAD_TXN = -30_782;
  private static final long serialVersionUID = 1L;

  BadException() {
    super(MDB_BAD_TXN, "Transaction must abort, has a child, or is invalid");
  }
} public static final class BadReaderLockException extends LmdbNativeException {

  static final int MDB_BAD_RSLOT = -30_783;
  private static final long serialVersionUID = 1L;

  BadReaderLockException() {
    super(MDB_BAD_RSLOT, "Invalid reuse of reader locktable slot");
  }
} public static final class TxFullException extends LmdbNativeException {

  static final int MDB_TXN_FULL = -30_788;
  private static final long serialVersionUID = 1L;

  TxFullException() {
    super(MDB_TXN_FULL, "Transaction has too many dirty pages");
  }
}
import static java.lang.Integer.MAX_VALUE;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.fail;
import static org.lmdbjava.Cursor.FullException.MDB_CURSOR_FULL;
import static org.lmdbjava.ResultCodeMapper.checkRc;
import static org.lmdbjava.TestUtils.invokePrivateConstructor;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import org.lmdbjava.Cursor.FullException;
import org.lmdbjava.Dbi.BadDbiException;
import org.lmdbjava.Dbi.BadValueSizeException;
import org.lmdbjava.Dbi.DbFullException;
import org.lmdbjava.Dbi.IncompatibleException;
import org.lmdbjava.Dbi.KeyExistsException;
import org.lmdbjava.Dbi.KeyNotFoundException;
import org.lmdbjava.Dbi.MapResizedException;
import org.lmdbjava.Env.FileInvalidException;
import org.lmdbjava.Env.MapFullException;
import org.lmdbjava.Env.ReadersFullException;
import org.lmdbjava.Env.VersionMismatchException;
import org.lmdbjava.LmdbNativeException.ConstantDerivedException;
import org.lmdbjava.LmdbNativeException.PageCorruptedException;
import org.lmdbjava.LmdbNativeException.PageFullException;
import org.lmdbjava.LmdbNativeException.PageNotFoundException;
import org.lmdbjava.LmdbNativeException.PanicException;
import org.lmdbjava.LmdbNativeException.TlsFullException;
import org.lmdbjava.Txn.BadException;
import org.lmdbjava.Txn.BadReaderLockException;
import org.lmdbjava.Txn.TxFullException;
/*-
 * #%L
 * LmdbJava
 * %%
 * Copyright (C) 2016 - 2021 The LmdbJava Open Source Project
 * %%
 * Licensed 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.
 * #L%
 */

package org.lmdbjava;




/**
 * Test {@link ResultCodeMapper} and {@link LmdbException}.
 */
public final class ResultCodeMapperTest {

  private static final Set<LmdbNativeException> EXCEPTIONS = new HashSet<>();
  private static final Set<Integer> RESULT_CODES = new HashSet<>();

  static {
    // separate collection instances used to simplify duplicate RC detection
    EXCEPTIONS.add(new BadDbiException());
    EXCEPTIONS.add(new BadReaderLockException());
    EXCEPTIONS.add(new BadException());
    EXCEPTIONS.add(new BadValueSizeException());