@Test public void findStreamingFile() throws Exception { FileStreamStateHandler rwd = new FileStreamStateHandler(); File testFilesDir = new File(samplesDir, "/multiple-logs/"); File[] testFiles = testFilesDir.listFiles((FilenameFilter) new WildcardFileFilter("orders*")); FileAccessState newFAS = new FileAccessState(); int count = 0; File fileToSearchFor = null; int lineLastRead = 0; File fileWritten = null; for (File testFile : testFiles) { count++; FileReader in; LineNumberReader reader; Long fileCRC = rwd.getFileCrc(testFile); if (count == 2) { newFAS.currentFileCrc = fileCRC; fileToSearchFor = testFile; } in = new FileReader(testFile); reader = new LineNumberReader(in); reader.setLineNumber(0); String line = reader.readLine(); int count2 = 0; while (line != null) { count2++; Checksum crcLine = new CRC32(); final byte[] bytes4Line = line.getBytes(); crcLine.update(bytes4Line, 0, bytes4Line.length); final long lineCRC = crcLine.getValue(); final int lineNumber = reader.getLineNumber(); System.out.println("for " + lineNumber + " line CRC is " + lineCRC); if (count2 == 3) { newFAS.currentLineCrc = lineCRC; newFAS.currentLineNumber = lineNumber; newFAS.lastReadTime = System.currentTimeMillis(); lineLastRead = lineNumber; } line = reader.readLine(); } fileWritten = AbstractFileStreamStateHandler.writeState(newFAS, testFilesDir, "TestStream"); Utils.close(reader); } final File findLastProcessed = rwd.findStreamingFile(newFAS, testFiles); assertEquals(fileToSearchFor, findLastProcessed); final int lineLastReadRecorded = rwd.checkLine(findLastProcessed, newFAS); assertEquals(lineLastRead, lineLastReadRecorded); fileWritten.delete(); }
/** @tests java.io.LineNumberReader#setLineNumber(int) */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "setLineNumber", args = {int.class}) public void test_setLineNumberI() { // Test for method void java.io.LineNumberReader.setLineNumber(int) lnr = new LineNumberReader(new StringReader(text)); lnr.setLineNumber(1001); assertEquals("set incorrect line number", 1001, lnr.getLineNumber()); }
public void setLineNumber(int lineNumber) { in.setLineNumber(lineNumber); }
/* (non-Javadoc) * @see java.io.LineNumberReader#setLineNumber(int) */ @Override public void setLineNumber(int lineNumber) { // TODO Auto-generated method stub super.setLineNumber(lineNumber); }
/** * Constructor PlacementPicker(java.lang.String) * * <p>Setus up all GUI components, initializes variables with default or needed values, and * prepares the map for user commands. * * @param java .lang.String mapName name of map file */ public PlacementPicker(final String mapName) { super("Placement Picker"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); if (!placeDimensionsSet) { try { File file = null; if (s_mapFolderLocation != null && s_mapFolderLocation.exists()) file = new File(s_mapFolderLocation, "map.properties"); if (file == null || !file.exists()) file = new File(new File(mapName).getParent() + File.separator + "map.properties"); if (file.exists()) { double scale = unit_zoom_percent; int width = unit_width; int height = unit_height; boolean found = false; final String scaleProperty = MapData.PROPERTY_UNITS_SCALE + "="; final String widthProperty = MapData.PROPERTY_UNITS_WIDTH + "="; final String heightProperty = MapData.PROPERTY_UNITS_HEIGHT + "="; final FileReader reader = new FileReader(file); final LineNumberReader reader2 = new LineNumberReader(reader); int i = 0; while (true) { reader2.setLineNumber(i); final String line = reader2.readLine(); if (line == null) break; if (line.contains(scaleProperty)) { try { scale = Double.parseDouble( line.substring(line.indexOf(scaleProperty) + scaleProperty.length()) .trim()); found = true; } catch (final NumberFormatException ex) { } } if (line.contains(widthProperty)) { try { width = Integer.parseInt( line.substring(line.indexOf(widthProperty) + widthProperty.length()) .trim()); found = true; } catch (final NumberFormatException ex) { } } if (line.contains(heightProperty)) { try { height = Integer.parseInt( line.substring(line.indexOf(heightProperty) + heightProperty.length()) .trim()); found = true; } catch (final NumberFormatException ex) { } } } reader2.close(); i++; if (found) { final int result = JOptionPane.showConfirmDialog( new JPanel(), "A map.properties file was found in the map's folder, " + "\r\n do you want to use the file to supply the info for the placement box size? " + "\r\n Zoom = " + scale + ", Width = " + width + ", Height = " + height + ", Result = (" + ((int) (scale * width)) + "x" + ((int) (scale * height)) + ")", "File Suggestion", 1); // if (result == 2) // return; if (result == 0) { unit_zoom_percent = scale; PLACEWIDTH = (int) (unit_zoom_percent * width); PLACEHEIGHT = (int) (unit_zoom_percent * height); placeDimensionsSet = true; } } } } catch (final Exception ex) { } } if (!placeDimensionsSet || JOptionPane.showConfirmDialog( new JPanel(), "Placement Box Size already set (" + PLACEWIDTH + "x" + PLACEHEIGHT + "), " + "do you wish to continue with this?\r\nSelect Yes to continue, Select No to override and change the size.", "Placement Box Size", JOptionPane.YES_NO_OPTION) == 1) { try { final String result = getUnitsScale(); try { unit_zoom_percent = Double.parseDouble(result.toLowerCase()); } catch (final NumberFormatException ex) { } final String width = JOptionPane.showInputDialog( null, "Enter the unit's image width in pixels (unscaled / without zoom).\r\n(e.g. 48)"); if (width != null) { try { PLACEWIDTH = (int) (unit_zoom_percent * Integer.parseInt(width)); } catch (final NumberFormatException ex) { } } final String height = JOptionPane.showInputDialog( null, "Enter the unit's image height in pixels (unscaled / without zoom).\r\n(e.g. 48)"); if (height != null) { try { PLACEHEIGHT = (int) (unit_zoom_percent * Integer.parseInt(height)); } catch (final NumberFormatException ex) { } } placeDimensionsSet = true; } catch (final Exception ex) { } } File file = null; if (s_mapFolderLocation != null && s_mapFolderLocation.exists()) file = new File(s_mapFolderLocation, "polygons.txt"); if (file == null || !file.exists()) file = new File(new File(mapName).getParent() + File.separator + "polygons.txt"); if (file.exists() && JOptionPane.showConfirmDialog( new JPanel(), "A polygons.txt file was found in the map's folder, do you want to use the file to supply the territories?", "File Suggestion", 1) == 0) { try { System.out.println("Polygons : " + file.getPath()); m_polygons = PointFileReaderWriter.readOneToManyPolygons(new FileInputStream(file.getPath())); } catch (final IOException ex1) { ex1.printStackTrace(); } } else { try { System.out.println("Select the Polygons file"); final String polyPath = new FileOpen("Select A Polygon File", s_mapFolderLocation, ".txt").getPathString(); if (polyPath != null) { System.out.println("Polygons : " + polyPath); m_polygons = PointFileReaderWriter.readOneToManyPolygons(new FileInputStream(polyPath)); } else { System.out.println("Polygons file not given. Will run regardless"); } } catch (final IOException ex1) { ex1.printStackTrace(); } } createImage(mapName); final JPanel imagePanel = createMainPanel(); /* Add a mouse listener to show X : Y coordinates on the lower left corner of the screen. */ imagePanel.addMouseMotionListener( new MouseMotionAdapter() { @Override public void mouseMoved(final MouseEvent e) { m_location.setText("x:" + e.getX() + " y:" + e.getY()); m_currentSquare = new Point(e.getPoint()); repaint(); } }); /* Add a mouse listener to monitor for right mouse button being clicked. */ imagePanel.addMouseListener( new MouseAdapter() { @Override public void mouseClicked(final MouseEvent e) { mouseEvent( e.getPoint(), e.isControlDown() || e.isShiftDown(), SwingUtilities.isRightMouseButton(e)); } }); // set up the image panel size dimensions ...etc imagePanel.setMinimumSize(new Dimension(m_image.getWidth(this), m_image.getHeight(this))); imagePanel.setPreferredSize(new Dimension(m_image.getWidth(this), m_image.getHeight(this))); imagePanel.setMaximumSize(new Dimension(m_image.getWidth(this), m_image.getHeight(this))); // set up the layout manager this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(new JScrollPane(imagePanel), BorderLayout.CENTER); this.getContentPane().add(m_location, BorderLayout.SOUTH); // set up the actions final Action openAction = new AbstractAction("Load Placements") { private static final long serialVersionUID = -2894085191455411106L; public void actionPerformed(final ActionEvent event) { loadPlacements(); } }; openAction.putValue(Action.SHORT_DESCRIPTION, "Load An Existing Placement File"); final Action saveAction = new AbstractAction("Save Placements") { private static final long serialVersionUID = -3341738809601318716L; public void actionPerformed(final ActionEvent event) { savePlacements(); } }; saveAction.putValue(Action.SHORT_DESCRIPTION, "Save The Placements To File"); final Action exitAction = new AbstractAction("Exit") { private static final long serialVersionUID = -9093426903644867897L; public void actionPerformed(final ActionEvent event) { System.exit(0); } }; exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit The Program"); // set up the menu items final JMenuItem openItem = new JMenuItem(openAction); openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK)); final JMenuItem saveItem = new JMenuItem(saveAction); saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK)); final JMenuItem exitItem = new JMenuItem(exitAction); // set up the menu bar final JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); final JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic('F'); fileMenu.add(openItem); fileMenu.add(saveItem); fileMenu.addSeparator(); fileMenu.add(exitItem); s_showAllMode = false; s_showOverflowMode = false; s_showIncompleteMode = false; s_incompleteNum = 1; showAllModeItem = new JCheckBoxMenuItem("Show All Placements Mode", false); showAllModeItem.addActionListener( new ActionListener() { public void actionPerformed(final ActionEvent event) { s_showAllMode = showAllModeItem.getState(); repaint(); } }); showOverflowModeItem = new JCheckBoxMenuItem("Show Overflow Mode", false); showOverflowModeItem.addActionListener( new ActionListener() { public void actionPerformed(final ActionEvent event) { s_showOverflowMode = showOverflowModeItem.getState(); repaint(); } }); showIncompleteModeItem = new JCheckBoxMenuItem("Show Incomplete Placements Mode", false); showIncompleteModeItem.addActionListener( new ActionListener() { public void actionPerformed(final ActionEvent event) { if (showIncompleteModeItem.getState()) { final String num = JOptionPane.showInputDialog( null, "Enter the minimum number of placements each territory must have.\r\n(examples: 1, 4, etc.)"); try { s_incompleteNum = Math.max(1, Math.min(50, Integer.parseInt(num))); } catch (final Exception ex) { s_incompleteNum = 1; } } s_showIncompleteMode = showIncompleteModeItem.getState(); repaint(); } }); final JMenu editMenu = new JMenu("Edit"); editMenu.setMnemonic('E'); editMenu.add(showAllModeItem); editMenu.add(showOverflowModeItem); editMenu.add(showIncompleteModeItem); menuBar.add(fileMenu); menuBar.add(editMenu); } // end constructor