Search code examples
javasqloracle-databasedbunit

DbUnit NoSuchTableException - Workaround for long table names in Oracle


I'm working on creating a test suite that runs on multiple databases using dbunit xml. Unfortunately, yesterday I discovered that some table names in our schema are over 30 characters, and are truncated for Oracle. For example, a table named unusually_long_table_name_error in mysql is named unusually_long_table_name_erro in Oracle. This means that my dbunit file contains lines like <unusually_long_table_name_error col1="value1" col2="value2 />. These lines throw a NoSuchTableException when running the tests in Oracle.

Is there a programmatic workaround for this? I'd really like to avoid generating special xml files for Oracle. I looked into a custom MetadataHandler but it returns lots of java.sql datatypes that I don't know how to intercept/spoof. I could read the xml myself, truncate each table name to 30 characters, write that out to a temp file or StringBufferInputStream and then use that as input to my DataSetBuilder, but that seems like a whole lot of steps to accomplish very little. Maybe there's some ninja Oracle trick with synonyms or stored procedures or goodness-know-what-else that could help me. Is one of these ideas clearly better than the others? Is there some other approach that would blow me away with its simplicity and elegance? Thanks!


Solution

  • In light of the lack of answers, I ended up going with my own suggested approach, which

    1. Reads the .xml file
    2. Regex's out the table name
    3. Truncates the table name if it's over 30 characters
    4. Appends the (potentially modified) line to a StringBuilder
    5. Feeds that StringBuilder into a ByteArrayInputStream, suitable for passing into a DataSetBuilder

    public InputStream oracleWorkaroundStream(String fileName) throws IOException
    {
      String ls = System.getProperty("line.separator");
    
      // This pattern isolates the table name from the rest of the line
      Pattern pattern = Pattern.compile("(\\s*<)(\\w+)(.*/>)");
    
      FileInputStream fis = new FileInputStream(fileName);
      // Use a StringBuidler for better performance over repeated concatenation
      StringBuilder sb = new StringBuilder(fis.available()*2);
    
      InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
      BufferedReader buff = new BufferedReader(isr);
      while (buff.ready())
      {
        // Read a line from the source xml file
        String line = buff.readLine();
        Matcher matcher = pattern.matcher(line);
    
        // See if the line contains a table name
        if (matcher.matches())
        {
          String tableName = matcher.group(2);
          if (tableName.length() > 30)
          {
            tableName = tableName.substring(0, 30);
          }
    
          // Append the (potentially modified) line
          sb.append(matcher.group(1));
          sb.append(tableName);
          sb.append(matcher.group(3));
        }
        else
        {
          // Some lines don't have tables names (<dataset>, <?xml?>, etc.)
          sb.append(line);
        }
        sb.append(ls);
      }
    
      return new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
    }
    

    EDIT: Swtiched to StringBuilder from repeated String concatenation, which gives a huge performance boost