<commit_before>package org.fedorahosted.flies.client.config;

import java.io.File;
import java.net.URL;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import junit.framework.TestCase;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.FileConfiguration;
import org.apache.commons.configuration.HierarchicalINIConfiguration;
import org.apache.commons.configuration.SystemConfiguration;
import org.fedorahosted.flies.rest.GenerateSchema;

// FIXME make this a real test (ie add assertions)
public class TestFliesConfig extends TestCase
{
   JAXBContext jc = JAXBContext.newInstance(FliesConfig.class);
   Unmarshaller unmarshaller = jc.createUnmarshaller();
   Marshaller marshaller = jc.createMarshaller();
   File fliesProjectXml = new File(System.getProperty("user.dir"), "target/flies.xml");
   File fliesUserFile = new File(System.getProperty("user.dir"), "target/.config/flies.ini");

   public TestFliesConfig() throws Exception
   {
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
   }

   public void testGenerateSchema() throws Exception
   {
      GenerateSchema.generateSchemaToStdout(jc);
   }

   public void testWriteReadProject() throws Exception
   {
      writeProject();
      readProject();
   }

   void writeProject() throws Exception
   {
      FliesConfig config = new FliesConfig();
      config.getDocSets().add(new DocSet());
      config.getDocSets().add(new DocSet());
      marshaller.marshal(config, fliesProjectXml);
   }

   void readProject() throws JAXBException
   {
      FliesConfig config = (FliesConfig) unmarshaller.unmarshal(fliesProjectXml);
      // System.out.println(config);
   }

   public void testWriteReadUser() throws Exception
   {
      writeUser();
      readUser();
   }

   void writeUser() throws Exception
   {
      FileConfiguration config = new HierarchicalINIConfiguration(fliesUserFile);
      config.setProperty("flies.url", new URL("http://flies.example.com/"));
      config.setProperty("flies.username", "admin");
      config.setProperty("flies.key", "<KEY>");
      config.setProperty("flies.debug", false);
      config.setProperty("flies.errors", true);

      config.save();
   }

   void readUser() throws Exception
   {
      CompositeConfiguration config = new CompositeConfiguration();
      config.addConfiguration(new SystemConfiguration());
      config.addConfiguration(new HierarchicalINIConfiguration(fliesUserFile));
      String user = config.getString("flies.user");
      System.out.println(user);
      boolean debug = config.getBoolean("flies.debug");
      System.out.println(debug);<commit_msg>Added simple tests for FliesConfig persistence<commit_after>package org.fedorahosted.flies.client.config;

import java.io.File;
import java.net.URL;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import junit.framework.TestCase;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.FileConfiguration;
import org.apache.commons.configuration.HierarchicalINIConfiguration;
import org.apache.commons.configuration.SystemConfiguration;

// FIXME make this a real test (ie add assertions)
public class TestFliesConfig extends TestCase
{
   JAXBContext jc = JAXBContext.newInstance(FliesConfig.class);
   Unmarshaller unmarshaller = jc.createUnmarshaller();
   Marshaller marshaller = jc.createMarshaller();
   File fliesProjectXml = new File(System.getProperty("user.dir"), "target/flies.xml");
   File fliesUserFile = new File(System.getProperty("user.dir"), "target/.config/flies.ini");

   public TestFliesConfig() throws Exception
   {
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
   }

   public void testGenerateSchema() throws Exception
   {
      // GenerateSchema.generateSchemaToStdout(jc);
   }

   public void testWriteReadProject() throws Exception
   {
      writeProject();
      readProject();
   }

   void writeProject() throws Exception
   {
      FliesConfig config = new FliesConfig();
      config.getDocSets().add(new DocSet());
      config.getDocSets().add(new DocSet());
      config.setUrl(new URL("http://example.com"));
      config.setProject("project");
      config.setProjectVersion("version");
      marshaller.marshal(config, fliesProjectXml);
   }

   void readProject() throws Exception
   {
      FliesConfig config = (FliesConfig) unmarshaller.unmarshal(fliesProjectXml);
      assertFalse(config.getDocSets().isEmpty());
      assertEquals(new URL("http://example.com"), config.getUrl());
      assertEquals("project", config.getProject());
      assertEquals("version", config.getProjectVersion());
   }

   public void testWriteReadUser() throws Exception
   {
      writeUser();
      readUser();
   }

   void writeUser() throws Exception
   {
      FileConfiguration config = new HierarchicalINIConfiguration(fliesUserFile);
      config.setProperty("flies.url", new URL("http://flies.example.com/"));
      config.setProperty("flies.username", "admin");
      config.setProperty("flies.key", "<KEY>");
      config.setProperty("flies.debug", false);
      config.setProperty("flies.errors", true);

      config.save();
   }

   void readUser() throws Exception
   {
      CompositeConfiguration config = new CompositeConfiguration();
      config.addConfiguration(new SystemConfiguration());
      config.addConfiguration(new HierarchicalINIConfiguration(fliesUserFile));
      String user = config.getString("flies.username");
      assertEquals("admin", user);
      boolean debug = config.getBoolean("flies.debug");
      assertFalse(debug);
      boolean errors = config.getBoolean("flies.errors");
      assertTrue(errors);
{'fraction_non_alphanumeric': 0.11666382398089715, 'fraction_numerical': 0.0, 'mean_word_length': 4.217081850533808, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 9}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6208392', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1623, 'n_words': 312}